GNU Octave  9.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-2024 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 
45 class type_info;
46 
47 OCTAVE_END_NAMESPACE(octave)
48 
49 class octave_value_list;
50 
51 // Data structures.
52 
53 class
55 {
56 public:
57 
59  : octave_base_value (), m_map (), m_c_name (),
60  m_parent_list (), m_obsolete_copies (0)
61  { }
62 
63  octave_class (const octave_map& m, const std::string& id)
64  : octave_base_value (), m_map (m), m_c_name (id),
65  m_parent_list (), m_obsolete_copies (0)
66  { }
67 
68  octave_class (const octave_map& m, const std::string& id,
69  const std::list<std::string>& plist)
70  : octave_base_value (), m_map (m), m_c_name (id),
71  m_parent_list (plist), m_obsolete_copies (0)
72  { }
73 
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  void break_closure_cycles (const std::shared_ptr<octave::stack_frame>& frame);
93 
94  OCTINTERP_API Cell dotref (const octave_value_list& idx);
95 
96  OCTINTERP_API Matrix size ();
97 
98  OCTINTERP_API octave_idx_type xnumel (const octave_value_list&);
99 
100  // We don't need to override all three forms of subsref. The using
101  // declaration will avoid warnings about partially-overloaded virtual
102  // functions.
104 
105  octave_value subsref (const std::string& type,
106  const std::list<octave_value_list>& idx)
107  {
108  octave_value_list tmp = subsref (type, idx, 1);
109  return tmp.length () > 0 ? tmp(0) : octave_value ();
110  }
111 
112  OCTINTERP_API octave_value_list
113  subsref (const std::string& type, const std::list<octave_value_list>& idx,
114  int nargout);
115 
116  static OCTINTERP_API octave_value
117  numeric_conv (const Cell& val, const std::string& type);
118 
119  void assign(const std::string& k, const octave_value& rhs)
120  { m_map.assign (k, rhs); };
121 
122  OCTINTERP_API octave_value
123  subsasgn (const std::string& type, const std::list<octave_value_list>& idx,
124  const octave_value& rhs);
125 
126  OCTINTERP_API octave_value
127  undef_subsasgn (const std::string& type,
128  const std::list<octave_value_list>& idx,
129  const octave_value& rhs);
130 
131  OCTINTERP_API octave::idx_vector
132  index_vector (bool require_integers = false) const;
133 
134  dim_vector dims () const { return m_map.dims (); }
135 
136  OCTINTERP_API std::size_t byte_size () const;
137 
138  // This is the number of elements in each field. The total number
139  // of elements is numel () * nfields ().
141  {
142  dim_vector dv = dims ();
143  return dv.numel ();
144  }
145 
146  octave_idx_type nfields () const { return m_map.nfields (); }
147 
148  std::size_t nparents () const { return m_parent_list.size (); }
149 
150  octave_value reshape (const dim_vector& new_dims) const
151  {
152  octave_class retval = octave_class (*this);
153  retval.m_map = retval.map_value ().reshape (new_dims);
154  return octave_value (new octave_class (retval));
155  }
156 
157  octave_value resize (const dim_vector& dv, bool = false) const
158  {
159  octave_class retval = octave_class (*this);
160  retval.m_map.resize (dv);
161  return octave_value (new octave_class (retval));
162  }
163 
164  bool is_defined () const { return true; }
165 
166  bool isstruct () const { return false; }
167 
168  bool isobject () const { return true; }
169 
170  OCTINTERP_API bool is_true () const;
171 
172  octave_map map_value () const { return m_map; }
173 
174  OCTINTERP_API string_vector map_keys () const;
175 
176  std::list<std::string> parent_class_name_list () const
177  { return m_parent_list; }
178 
180  { return string_vector (m_parent_list); }
181 
182  OCTINTERP_API octave_base_value * find_parent_class (const std::string&);
183 
184  OCTINTERP_API octave_base_value * unique_parent_class (const std::string&);
185 
186  OCTINTERP_API bool is_instance_of (const std::string&) const;
187 
188  OCTINTERP_API string_vector string_vector_value (bool pad) const;
189 
190  OCTINTERP_API void print (std::ostream& os, bool pr_as_read_syntax = false);
191 
192  OCTINTERP_API void print_raw (std::ostream& os,
193  bool pr_as_read_syntax = false) const;
194 
195  OCTINTERP_API bool reconstruct_exemplar ();
196 
197  OCTINTERP_API static void clear_exemplar_map ();
198 
199  OCTINTERP_API bool reconstruct_parents ();
200 
201  OCTINTERP_API bool save_ascii (std::ostream& os);
202 
203  OCTINTERP_API bool load_ascii (std::istream& is);
204 
205  OCTINTERP_API bool save_binary (std::ostream& os, bool save_as_floats);
206 
207  OCTINTERP_API bool
208  load_binary (std::istream& is, bool swap,
210 
211  OCTINTERP_API bool
212  save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
213 
214  OCTINTERP_API bool
215  load_hdf5 (octave_hdf5_id loc_id, const char *name);
216 
217  OCTINTERP_API mxArray * as_mxArray (bool interleaved) const;
218 
219 private:
220  octave_map m_map;
221 
222 public:
223  int type_id () const { return s_t_id; }
224  std::string type_name () const { return s_t_name; }
225  std::string class_name () const { return m_c_name; }
226 
227  static int static_type_id () { return s_t_id; }
228  static std::string static_type_name () { return s_t_name; }
229  static std::string static_class_name () { return "<unknown>"; }
230  static OCTINTERP_API void register_type (octave::type_info&);
231 
232 private:
233  static int s_t_id;
234 
235  static const std::string s_t_name;
236  std::string m_c_name;
237  std::list<std::string> m_parent_list;
238 
239  OCTINTERP_API bool in_class_method ();
240  OCTINTERP_API std::string get_current_method_class ();
241 
242  OCTINTERP_API octave_value
243  subsasgn_common (const octave_value& obj, const std::string& type,
244  const std::list<octave_value_list>& idx,
245  const octave_value& rhs);
246 
247  int m_obsolete_copies;
248 
249 public:
250  // The list of field names and parent classes defines a class. We
251  // keep track of each class that has been created so that we know
253  {
254  public:
255 
256  exemplar_info () : m_field_names (), m_parent_class_names () { }
257 
258  OCTINTERP_API exemplar_info (const octave_value& obj);
259 
261  : m_field_names (x.m_field_names),
262  m_parent_class_names (x.m_parent_class_names) { }
263 
264  exemplar_info& operator = (const exemplar_info& x)
265  {
266  if (&x != this)
267  {
268  m_field_names = x.m_field_names;
269  m_parent_class_names = x.m_parent_class_names;
270  }
271  return *this;
272  }
273 
274  octave_idx_type nfields () const { return m_field_names.numel (); }
275 
276  std::size_t nparents () const { return m_parent_class_names.size (); }
277 
278  string_vector fields () const { return m_field_names; }
279 
280  std::list<std::string> parents () const { return m_parent_class_names; }
281 
282  OCTINTERP_API bool compare (const octave_value& obj) const;
283 
284  private:
285 
286  string_vector m_field_names;
287  std::list<std::string> m_parent_class_names;
288  };
289 
290  // A map from class names to lists of fields.
291  static std::map<std::string, exemplar_info> exemplar_map;
292 
293  typedef std::map<std::string, exemplar_info>::iterator
295  typedef std::map<std::string, exemplar_info>::const_iterator
297 };
298 
299 #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:248
string_vector fields() const
Definition: ov-class.h:278
octave_idx_type nfields() const
Definition: ov-class.h:274
std::size_t nparents() const
Definition: ov-class.h:276
exemplar_info(const exemplar_info &x)
Definition: ov-class.h:260
std::list< std::string > parents() const
Definition: ov-class.h:280
octave_class()
Definition: ov-class.h:58
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-class.h:105
bool isstruct() const
Definition: ov-class.h:166
std::map< std::string, exemplar_info >::iterator exemplar_iterator
Definition: ov-class.h:294
octave_idx_type nfields() const
Definition: ov-class.h:146
string_vector parent_class_names() const
Definition: ov-class.h:179
std::string type_name() const
Definition: ov-class.h:224
static std::string static_type_name()
Definition: ov-class.h:228
octave_idx_type numel() const
Definition: ov-class.h:140
octave_value resize(const dim_vector &dv, bool=false) const
Definition: ov-class.h:157
std::string class_name() const
Definition: ov-class.h:225
octave_base_value * empty_clone() const
Definition: ov-class.h:87
static std::map< std::string, exemplar_info > exemplar_map
Definition: ov-class.h:291
static std::string static_class_name()
Definition: ov-class.h:229
octave_class(const octave_map &m, const std::string &id, const std::list< std::string > &plist)
Definition: ov-class.h:68
std::size_t nparents() const
Definition: ov-class.h:148
octave_map map_value() const
Definition: ov-class.h:172
dim_vector dims() const
Definition: ov-class.h:134
bool is_defined() const
Definition: ov-class.h:164
static int static_type_id()
Definition: ov-class.h:227
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-class.h:150
std::map< std::string, exemplar_info >::const_iterator exemplar_const_iterator
Definition: ov-class.h:296
int type_id() const
Definition: ov-class.h:223
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:119
~octave_class()=default
std::list< std::string > parent_class_name_list() const
Definition: ov-class.h:176
octave_class(const octave_map &m, const std::string &id)
Definition: ov-class.h:63
bool isobject() const
Definition: ov-class.h:168
octave_base_value * clone() const
Definition: ov-class.h:83
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:113
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
octave::idx_vector idx_vector
Definition: idx-vector.h:1022
F77_RET_T const F77_DBLE * x
float_format
Definition: mach-info.h:38
bool is_true(const std::string &s)
Definition: mkoctfile.cc:604
T octave_idx_type m
Definition: mx-inlines.cc:781
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()) ? '\'' :'"'))
int register_type(const std::string &t_name, const std::string &c_name, const octave_value &val)
Definition: ov-typeinfo.cc:784