GNU Octave 7.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-class.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2007-2022 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
43namespace octave
44{
45 class type_info;
46}
47
49
50// Data structures.
51
52class
54{
55public:
56
58 : octave_base_value (), m_map (), 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), 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), c_name (id),
70 m_parent_list (plist), m_obsolete_copies (0)
71 { }
72
73 octave_class (const octave_map& m, const std::string& id,
74 const octave_value_list& parents);
75
77 : octave_base_value (s), m_map (s.m_map), c_name (s.c_name),
78 m_parent_list (s.m_parent_list), m_obsolete_copies (0) { }
79
80 ~octave_class (void) = default;
81
82 octave_base_value * clone (void) const { return new octave_class (*this); }
83
84 OCTINTERP_API octave_base_value * unique_clone (void);
85
87 {
88 return new octave_class (octave_map (m_map.keys ()), c_name, m_parent_list);
89 }
90
91 void break_closure_cycles (const std::shared_ptr<octave::stack_frame>& frame);
92
93 OCTINTERP_API Cell dotref (const octave_value_list& idx);
94
95 OCTINTERP_API Matrix size (void);
96
97 OCTINTERP_API octave_idx_type xnumel (const octave_value_list&);
98
99 // We don't need to override all three forms of subsref. The using
100 // declaration will avoid warnings about partially-overloaded virtual
101 // functions.
103
104 octave_value subsref (const std::string& type,
105 const std::list<octave_value_list>& idx)
106 {
107 octave_value_list tmp = subsref (type, idx, 1);
108 return tmp.length () > 0 ? tmp(0) : octave_value ();
109 }
110
111 OCTINTERP_API octave_value_list
112 subsref (const std::string& type, const std::list<octave_value_list>& idx,
113 int nargout);
114
115 static OCTINTERP_API octave_value
116 numeric_conv (const Cell& val, const std::string& type);
117
118 void assign(const std::string& k, const octave_value& rhs)
119 { m_map.assign (k, rhs); };
120
121 OCTINTERP_API octave_value
122 subsasgn (const std::string& type, const std::list<octave_value_list>& idx,
123 const octave_value& rhs);
124
125 OCTINTERP_API octave_value
126 undef_subsasgn (const std::string& type,
127 const std::list<octave_value_list>& idx,
128 const octave_value& rhs);
129
130 OCTINTERP_API octave::idx_vector
131 index_vector (bool require_integers = false) const;
132
133 dim_vector dims (void) const { return m_map.dims (); }
134
135 OCTINTERP_API std::size_t byte_size (void) const;
136
137 // This is the number of elements in each field. The total number
138 // of elements is numel () * nfields ().
140 {
141 dim_vector dv = dims ();
142 return dv.numel ();
143 }
144
145 octave_idx_type nfields (void) const { return m_map.nfields (); }
146
147 std::size_t nparents (void) const { return m_parent_list.size (); }
148
149 octave_value reshape (const dim_vector& new_dims) const
150 {
151 octave_class retval = octave_class (*this);
152 retval.m_map = retval.map_value ().reshape (new_dims);
153 return octave_value (new octave_class (retval));
154 }
155
156 octave_value resize (const dim_vector& dv, bool = false) const
157 {
158 octave_class retval = octave_class (*this);
159 retval.m_map.resize (dv);
160 return octave_value (new octave_class (retval));
161 }
162
163 bool is_defined (void) const { return true; }
164
165 bool isstruct (void) const { return false; }
166
167 bool isobject (void) const { return true; }
168
169 OCTINTERP_API bool is_true (void) const;
170
171 octave_map map_value (void) const { return m_map; }
172
173 OCTINTERP_API string_vector map_keys (void) const;
174
175 std::list<std::string> parent_class_name_list (void) const
176 { return m_parent_list; }
177
179 { return string_vector (m_parent_list); }
180
181 OCTINTERP_API octave_base_value * find_parent_class (const std::string&);
182
183 OCTINTERP_API octave_base_value * unique_parent_class (const std::string&);
184
185 OCTINTERP_API bool is_instance_of (const std::string&) const;
186
187 OCTINTERP_API string_vector string_vector_value (bool pad) const;
188
189 OCTINTERP_API void print (std::ostream& os, bool pr_as_read_syntax = false);
190
191 OCTINTERP_API void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
192
193 OCTINTERP_API bool reconstruct_exemplar (void);
194
195 OCTINTERP_API static void clear_exemplar_map (void);
196
197 OCTINTERP_API bool reconstruct_parents (void);
198
199 OCTINTERP_API bool save_ascii (std::ostream& os);
200
201 OCTINTERP_API bool load_ascii (std::istream& is);
202
203 OCTINTERP_API bool save_binary (std::ostream& os, bool save_as_floats);
204
205 OCTINTERP_API bool
206 load_binary (std::istream& is, bool swap,
208
209 OCTINTERP_API bool
210 save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
211
212 OCTINTERP_API bool
213 load_hdf5 (octave_hdf5_id loc_id, const char *name);
214
215 OCTINTERP_API mxArray * as_mxArray (bool interleaved) const;
216
217private:
219
220public:
221 int type_id (void) const { return t_id; }
222 std::string type_name (void) const { return t_name; }
223 std::string class_name (void) const { return c_name; }
224
225 static int static_type_id (void) { return t_id; }
226 static std::string static_type_name (void) { return t_name; }
227 static std::string static_class_name (void) { return "<unknown>"; }
228 static OCTINTERP_API void register_type (octave::type_info&);
229
230private:
231 static int t_id;
232
233 static const std::string t_name;
234 std::string c_name;
235 std::list<std::string> m_parent_list;
236
237 OCTINTERP_API bool in_class_method (void);
238 OCTINTERP_API std::string get_current_method_class (void);
239
240 OCTINTERP_API octave_value
241 subsasgn_common (const octave_value& obj, const std::string& type,
242 const std::list<octave_value_list>& idx,
243 const octave_value& rhs);
244
246
247public:
248 // The list of field names and parent classes defines a class. We
249 // keep track of each class that has been created so that we know
251 {
252 public:
253
254 exemplar_info (void) : m_field_names (), m_parent_class_names () { }
255
256 OCTINTERP_API exemplar_info (const octave_value& obj);
257
259 : m_field_names (x.m_field_names),
260 m_parent_class_names (x.m_parent_class_names) { }
261
262 exemplar_info& operator = (const exemplar_info& x)
263 {
264 if (&x != this)
265 {
266 m_field_names = x.m_field_names;
267 m_parent_class_names = x.m_parent_class_names;
268 }
269 return *this;
270 }
271
272 octave_idx_type nfields (void) const { return m_field_names.numel (); }
273
274 std::size_t nparents (void) const { return m_parent_class_names.size (); }
275
276 string_vector fields (void) const { return m_field_names; }
277
278 std::list<std::string> parents (void) const { return m_parent_class_names; }
279
280 OCTINTERP_API bool compare (const octave_value& obj) const;
281
282 private:
283
285 std::list<std::string> m_parent_class_names;
286 };
287
288 // A map from class names to lists of fields.
289 static std::map<std::string, exemplar_info> exemplar_map;
290
291 typedef std::map<std::string, exemplar_info>::iterator
293 typedef std::map<std::string, exemplar_info>::const_iterator
295};
296
297#endif
Definition: Cell.h:43
Definition: dMatrix.h:42
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition: dim-vector.h:335
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-base.cc:201
octave_idx_type nfields(void) const
Definition: ov-class.h:272
std::list< std::string > parents(void) const
Definition: ov-class.h:278
string_vector m_field_names
Definition: ov-class.h:284
std::list< std::string > m_parent_class_names
Definition: ov-class.h:285
string_vector fields(void) const
Definition: ov-class.h:276
std::size_t nparents(void) const
Definition: ov-class.h:274
exemplar_info(const exemplar_info &x)
Definition: ov-class.h:258
string_vector parent_class_names(void) const
Definition: ov-class.h:178
static std::string static_class_name(void)
Definition: ov-class.h:227
std::string type_name(void) const
Definition: ov-class.h:222
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-class.h:104
octave_base_value * empty_clone(void) const
Definition: ov-class.h:86
bool isstruct(void) const
Definition: ov-class.h:165
std::map< std::string, exemplar_info >::iterator exemplar_iterator
Definition: ov-class.h:292
std::map< std::string, exemplar_info >::const_iterator exemplar_const_iterator
Definition: ov-class.h:294
std::string c_name
Definition: ov-class.h:234
std::list< std::string > m_parent_list
Definition: ov-class.h:235
octave_value resize(const dim_vector &dv, bool=false) const
Definition: ov-class.h:156
int type_id(void) const
Definition: ov-class.h:221
octave_map m_map
Definition: ov-class.h:218
static std::map< std::string, exemplar_info > exemplar_map
Definition: ov-class.h:289
octave_idx_type numel(void) const
Definition: ov-class.h:139
octave_class(const octave_map &m, const std::string &id, const std::list< std::string > &plist)
Definition: ov-class.h:67
std::string class_name(void) const
Definition: ov-class.h:223
static const std::string t_name
Definition: ov-class.h:233
bool is_defined(void) const
Definition: ov-class.h:163
octave_map map_value(void) const
Definition: ov-class.h:171
~octave_class(void)=default
std::list< std::string > parent_class_name_list(void) const
Definition: ov-class.h:175
static int static_type_id(void)
Definition: ov-class.h:225
std::size_t nparents(void) const
Definition: ov-class.h:147
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-class.h:149
static int t_id
Definition: ov-class.h:231
octave_class(void)
Definition: ov-class.h:57
static std::string static_type_name(void)
Definition: ov-class.h:226
int m_obsolete_copies
Definition: ov-class.h:245
octave_idx_type nfields(void) const
Definition: ov-class.h:145
dim_vector dims(void) const
Definition: ov-class.h:133
octave_base_value * clone(void) const
Definition: ov-class.h:82
bool isobject(void) const
Definition: ov-class.h:167
octave_class(const octave_class &s)
Definition: ov-class.h:76
void assign(const std::string &k, const octave_value &rhs)
Definition: ov-class.h:118
octave_class(const octave_map &m, const std::string &id)
Definition: ov-class.h:62
void resize(const dim_vector &dv, bool fill=false)
Definition: oct-map.cc:576
octave_map reshape(const dim_vector &dv) const
Definition: oct-map.cc:526
octave_idx_type length(void) const
Definition: ovl.h:113
QString name
F77_RET_T const F77_DBLE * x
bool is_true(const std::string &s)
int register_type(const std::string &t_name, const std::string &c_name, const octave_value &val)
Definition: ov-typeinfo.cc:767
int64_t octave_hdf5_id
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
static bool in_class_method(const octave::cdef_class &cls)
Definition: ov-classdef.cc:53