GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
ov-class.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2007-2025 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if ! defined (octave_ov_class_h)
27#define octave_ov_class_h 1
28
29#include "octave-config.h"
30
31#include <cstdlib>
32
33#include <iosfwd>
34#include <string>
35
36#include "mx-base.h"
37#include "str-vec.h"
38
39#include "error.h"
40#include "oct-map.h"
41#include "ov-base.h"
42
44
45class type_info;
46
47OCTAVE_END_NAMESPACE(octave)
48
50
51// Data structures.
52
53class OCTINTERP_API octave_class : public octave_base_value
54{
55public:
56
58 : octave_base_value (), m_map (), m_c_name (),
59 m_parent_list (), m_obsolete_copies (0)
60 { }
61
62 octave_class (const octave_map& m, const std::string& id)
63 : octave_base_value (), m_map (m), m_c_name (id),
64 m_parent_list (), m_obsolete_copies (0)
65 { }
66
67 octave_class (const octave_map& m, const std::string& id,
68 const std::list<std::string>& plist)
69 : octave_base_value (), m_map (m), m_c_name (id),
70 m_parent_list (plist), m_obsolete_copies (0)
71 { }
72
73 OCTINTERP_API
74 octave_class (const octave_map& m, const std::string& id,
75 const octave_value_list& parents);
76
78 : octave_base_value (s), m_map (s.m_map), m_c_name (s.m_c_name),
79 m_parent_list (s.m_parent_list), m_obsolete_copies (0) { }
80
81 ~octave_class () = default;
82
83 octave_base_value * clone () const { return new octave_class (*this); }
84
85 OCTINTERP_API octave_base_value * unique_clone ();
86
88 {
89 return new octave_class (octave_map (m_map.keys ()), m_c_name, m_parent_list);
90 }
91
92 OCTINTERP_API void
93 break_closure_cycles (const std::shared_ptr<octave::stack_frame>& frame);
94
95 OCTINTERP_API Cell dotref (const octave_value_list& idx);
96
97 OCTINTERP_API Matrix size ();
98
99 OCTINTERP_API octave_idx_type xnumel (const octave_value_list&);
100
101 // We don't need to override all three forms of subsref. The using
102 // declaration will avoid warnings about partially-overloaded virtual
103 // functions.
105
106 octave_value subsref (const std::string& type,
107 const std::list<octave_value_list>& idx)
108 {
109 octave_value_list tmp = subsref (type, idx, 1);
110 return tmp.length () > 0 ? tmp(0) : octave_value ();
111 }
112
113 OCTINTERP_API octave_value_list
114 subsref (const std::string& type, const std::list<octave_value_list>& idx,
115 int nargout);
116
117 static OCTINTERP_API octave_value
118 numeric_conv (const Cell& val, const std::string& type);
119
120 void assign(const std::string& k, const octave_value& rhs)
121 { m_map.assign (k, rhs); }
122
123 OCTINTERP_API octave_value
124 subsasgn (const std::string& type, const std::list<octave_value_list>& idx,
125 const octave_value& rhs);
126
127 OCTINTERP_API octave_value
128 undef_subsasgn (const std::string& type,
129 const std::list<octave_value_list>& idx,
130 const octave_value& rhs);
131
132 OCTINTERP_API octave::idx_vector
133 index_vector (bool require_integers = false) const;
134
135 dim_vector dims () const { return m_map.dims (); }
136
137 OCTINTERP_API std::size_t byte_size () const;
138
139 // This is the number of elements in each field. The total number
140 // of elements is numel () * nfields ().
142 {
143 const dim_vector& dv = dims ();
144 return dv.numel ();
145 }
146
147 octave_idx_type nfields () const { return m_map.nfields (); }
148
149 std::size_t nparents () const { return m_parent_list.size (); }
150
151 octave_value reshape (const dim_vector& new_dims) const
152 {
153 octave_class retval = octave_class (*this);
154 retval.m_map = retval.map_value ().reshape (new_dims);
155 return octave_value (new octave_class (retval));
156 }
157
158 octave_value resize (const dim_vector& dv, bool = false) const
159 {
160 octave_class retval = octave_class (*this);
161 retval.m_map.resize (dv);
162 return octave_value (new octave_class (retval));
163 }
164
165 bool is_defined () const { return true; }
166
167 bool isstruct () const { return false; }
168
169 bool isobject () const { return true; }
170
171 OCTINTERP_API bool is_true () const;
172
173 octave_map map_value () const { return m_map; }
174
175 OCTINTERP_API string_vector map_keys () const;
176
177 std::list<std::string> parent_class_name_list () const
178 { return m_parent_list; }
179
181 { return string_vector (m_parent_list); }
182
183 OCTINTERP_API octave_base_value * find_parent_class (const std::string&);
184
185 OCTINTERP_API octave_base_value * unique_parent_class (const std::string&);
186
187 OCTINTERP_API bool is_instance_of (const std::string&) const;
188
189 OCTINTERP_API string_vector string_vector_value (bool pad) const;
190
191 OCTINTERP_API void print (std::ostream& os, bool pr_as_read_syntax = false);
192
193 OCTINTERP_API void print_raw (std::ostream& os,
194 bool pr_as_read_syntax = false) const;
195
196 OCTINTERP_API bool reconstruct_exemplar ();
197
198 OCTINTERP_API static void clear_exemplar_map ();
199
200 OCTINTERP_API bool reconstruct_parents ();
201
202 OCTINTERP_API bool save_ascii (std::ostream& os);
203
204 OCTINTERP_API bool load_ascii (std::istream& is);
205
206 OCTINTERP_API bool save_binary (std::ostream& os, bool save_as_floats);
207
208 OCTINTERP_API bool
209 load_binary (std::istream& is, bool swap,
210 octave::mach_info::float_format fmt);
211
212 OCTINTERP_API bool
213 save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
214
215 OCTINTERP_API bool
216 load_hdf5 (octave_hdf5_id loc_id, const char *name);
217
218 OCTINTERP_API mxArray * as_mxArray (bool interleaved) const;
219
220private:
221 octave_map m_map;
222
223public:
224 int type_id () const { return s_t_id; }
225 std::string type_name () const { return s_t_name; }
226 std::string class_name () const { return m_c_name; }
227
228 static int static_type_id () { return s_t_id; }
229 static std::string static_type_name () { return s_t_name; }
230 static std::string static_class_name () { return "<unknown>"; }
231 static OCTINTERP_API void register_type (octave::type_info&);
232
233private:
234 static int s_t_id;
235
236 static const std::string s_t_name;
237 std::string m_c_name;
238 std::list<std::string> m_parent_list;
239
240 OCTINTERP_API bool in_class_method ();
241 OCTINTERP_API std::string get_current_method_class ();
242
243 OCTINTERP_API octave_value
244 subsasgn_common (const octave_value& obj, const std::string& type,
245 const std::list<octave_value_list>& idx,
246 const octave_value& rhs);
247
248 int m_obsolete_copies;
249
250public:
251 // The list of field names and parent classes defines a class. We
252 // keep track of each class that has been created so that we know
254 {
255 public:
256
257 exemplar_info () : m_field_names (), m_parent_class_names () { }
258
259 OCTINTERP_API exemplar_info (const octave_value& obj);
260
262 : m_field_names (x.m_field_names),
263 m_parent_class_names (x.m_parent_class_names) { }
264
265 exemplar_info& operator = (const exemplar_info& x)
266 {
267 if (&x != this)
268 {
269 m_field_names = x.m_field_names;
270 m_parent_class_names = x.m_parent_class_names;
271 }
272 return *this;
273 }
274
275 octave_idx_type nfields () const { return m_field_names.numel (); }
276
277 std::size_t nparents () const { return m_parent_class_names.size (); }
278
279 string_vector fields () const { return m_field_names; }
280
281 std::list<std::string> parents () const { return m_parent_class_names; }
282
283 OCTINTERP_API bool compare (const octave_value& obj) const;
284
285 private:
286
287 string_vector m_field_names;
288 std::list<std::string> m_parent_class_names;
289 };
290
291 // A map from class names to lists of fields.
292 static std::map<std::string, exemplar_info> exemplar_map;
293
294 typedef std::map<std::string, exemplar_info>::iterator
296 typedef std::map<std::string, exemplar_info>::const_iterator
298};
299
300#endif
Definition Cell.h:41
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition dim-vector.h:331
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition ov-base.cc:245
string_vector fields() const
Definition ov-class.h:279
octave_idx_type nfields() const
Definition ov-class.h:275
std::size_t nparents() const
Definition ov-class.h:277
exemplar_info(const exemplar_info &x)
Definition ov-class.h:261
std::list< std::string > parents() const
Definition ov-class.h:281
octave_base_value * clone() const
Definition ov-class.h:83
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition ov-class.h:106
bool isstruct() const
Definition ov-class.h:167
octave_base_value * empty_clone() const
Definition ov-class.h:87
octave_idx_type nfields() const
Definition ov-class.h:147
std::map< std::string, exemplar_info >::iterator exemplar_iterator
Definition ov-class.h:295
string_vector parent_class_names() const
Definition ov-class.h:180
std::string type_name() const
Definition ov-class.h:225
std::map< std::string, exemplar_info >::const_iterator exemplar_const_iterator
Definition ov-class.h:297
static std::string static_type_name()
Definition ov-class.h:229
octave_idx_type numel() const
Definition ov-class.h:141
octave_value resize(const dim_vector &dv, bool=false) const
Definition ov-class.h:158
std::string class_name() const
Definition ov-class.h:226
static std::map< std::string, exemplar_info > exemplar_map
Definition ov-class.h:292
static std::string static_class_name()
Definition ov-class.h:230
octave_class(const octave_map &m, const std::string &id, const std::list< std::string > &plist)
Definition ov-class.h:67
std::size_t nparents() const
Definition ov-class.h:149
octave_map map_value() const
Definition ov-class.h:173
dim_vector dims() const
Definition ov-class.h:135
bool is_defined() const
Definition ov-class.h:165
static int static_type_id()
Definition ov-class.h:228
octave_value reshape(const dim_vector &new_dims) const
Definition ov-class.h:151
std::list< std::string > parent_class_name_list() const
Definition ov-class.h:177
int type_id() const
Definition ov-class.h:224
octave_class(const octave_class &s)
Definition ov-class.h:77
void assign(const std::string &k, const octave_value &rhs)
Definition ov-class.h:120
~octave_class()=default
octave_class(const octave_map &m, const std::string &id)
Definition ov-class.h:62
bool isobject() const
Definition ov-class.h:169
void resize(const dim_vector &dv, bool fill=false)
Definition oct-map.cc:574
octave_map reshape(const dim_vector &dv) const
Definition oct-map.cc:524
octave_idx_type length() const
Definition ovl.h:111
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
F77_RET_T const F77_DBLE * x
bool is_true(const std::string &s)
int64_t octave_hdf5_id
int register_type(const std::string &t_name, const std::string &c_name, const octave_value &val)