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