GNU Octave  4.4.1
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-2018 John W. Eaton
4 Copyright (C) 2009 VZLU Prague
5 
6 This file is part of Octave.
7 
8 Octave is free software: you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <https://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if ! defined (octave_ov_class_h)
25 #define octave_ov_class_h 1
26 
27 #include "octave-config.h"
28 
29 #include <cstdlib>
30 
31 #include <iosfwd>
32 #include <string>
33 
34 #include "mx-base.h"
35 #include "str-vec.h"
36 
37 #include "error.h"
38 #include "oct-map.h"
39 #include "ov-base.h"
40 
41 namespace octave
42 {
43  class type_info;
44 }
45 
46 class octave_value_list;
47 
48 // Data structures.
49 
50 class
52 {
53 public:
54 
55  octave_class (void)
56  : octave_base_value (), map (), c_name (),
57  parent_list (), obsolete_copies (0)
58  { }
59 
60  octave_class (const octave_map& m, const std::string& id,
61  const std::list<std::string>& plist)
62  : octave_base_value (), map (m), c_name (id),
63  parent_list (plist), obsolete_copies (0)
64  { }
65 
66  octave_class (const octave_map& m, const std::string& id,
67  const octave_value_list& parents);
68 
70  : octave_base_value (s), map (s.map), c_name (s.c_name),
71  parent_list (s.parent_list), obsolete_copies (0) { }
72 
73  ~octave_class (void) = default;
74 
75  octave_base_value * clone (void) const { return new octave_class (*this); }
76 
77  octave_base_value * unique_clone (void);
78 
80  {
81  return new octave_class (octave_map (map.keys ()), c_name, parent_list);
82  }
83 
84  Cell dotref (const octave_value_list& idx);
85 
86  Matrix size (void);
87 
89 
91  const std::list<octave_value_list>& idx)
92  {
93  octave_value_list tmp = subsref (type, idx, 1);
94  return tmp.length () > 0 ? tmp(0) : octave_value ();
95  }
96 
98  const std::list<octave_value_list>& idx,
99  int nargout);
100 
101  static octave_value numeric_conv (const Cell& val,
102  const std::string& type);
103 
104  void assign(const std::string& k, const octave_value& rhs)
105  { map.assign (k, rhs); };
106 
108  const std::list<octave_value_list>& idx,
109  const octave_value& rhs);
110 
111  octave_value undef_subsasgn (const std::string& type,
112  const std::list<octave_value_list>& idx,
113  const octave_value& rhs);
114 
115  idx_vector index_vector (bool require_integers = false) const;
116 
117  dim_vector dims (void) const { return map.dims (); }
118 
119  size_t byte_size (void) const;
120 
121  // This is the number of elements in each field. The total number
122  // of elements is numel () * nfields ().
123  octave_idx_type numel (void) const
124  {
125  dim_vector dv = dims ();
126  return dv.numel ();
127  }
128 
129  octave_idx_type nfields (void) const { return map.nfields (); }
130 
131  size_t nparents (void) const { return parent_list.size (); }
132 
133  octave_value reshape (const dim_vector& new_dims) const
134  {
135  octave_class retval = octave_class (*this);
136  retval.map = retval.map_value ().reshape (new_dims);
137  return octave_value (new octave_class (retval));
138  }
139 
140  octave_value resize (const dim_vector& dv, bool = false) const
141  {
142  octave_class retval = octave_class (*this);
143  retval.map.resize (dv);
144  return octave_value (new octave_class (retval));
145  }
146 
147  bool is_defined (void) const { return true; }
148 
149  bool isstruct (void) const { return false; }
150 
151  bool isobject (void) const { return true; }
152 
153  bool is_true (void) const;
154 
155  octave_map map_value (void) const { return map; }
156 
157  string_vector map_keys (void) const;
158 
159  std::list<std::string> parent_class_name_list (void) const
160  { return parent_list; }
161 
163  { return string_vector (parent_list); }
164 
165  octave_base_value * find_parent_class (const std::string&);
166 
167  octave_base_value * unique_parent_class (const std::string&);
168 
169  bool is_instance_of (const std::string&) const;
170 
171  string_vector string_vector_value (bool pad) const;
172 
173  void print (std::ostream& os, bool pr_as_read_syntax = false);
174 
175  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
176 
177  bool print_name_tag (std::ostream& os, const std::string& name) const;
178 
179  void print_with_name (std::ostream& os, const std::string& name,
180  bool print_padding = true);
181 
182  bool reconstruct_exemplar (void);
183 
184  static void clear_exemplar_map (void);
185 
186  bool reconstruct_parents (void);
187 
188  bool save_ascii (std::ostream& os);
189 
190  bool load_ascii (std::istream& is);
191 
192  bool save_binary (std::ostream& os, bool& save_as_floats);
193 
194  bool load_binary (std::istream& is, bool swap,
196 
197  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
198 
199  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
200 
201  mxArray * as_mxArray (void) const;
202 
203 private:
205 
206 public:
207  int type_id (void) const { return t_id; }
208  std::string type_name (void) const { return t_name; }
209  std::string class_name (void) const { return c_name; }
210 
211  static int static_type_id (void) { return t_id; }
212  static std::string static_type_name (void) { return t_name; }
213  static std::string static_class_name (void) { return "<unknown>"; }
214  static void register_type (octave::type_info&);
215 
216 private:
217  static int t_id;
218 
219  static const std::string t_name;
221  std::list<std::string> parent_list;
222 
223  bool in_class_method (void);
224  std::string get_current_method_class (void);
225 
226  octave_value subsasgn_common (const octave_value& obj,
227  const std::string& type,
228  const std::list<octave_value_list>& idx,
229  const octave_value& rhs);
230 
232 
233 public:
234  // The list of field names and parent classes defines a class. We
235  // keep track of each class that has been created so that we know
237  {
238  public:
239 
240  exemplar_info (void) : field_names (), parent_class_names () { }
241 
242  exemplar_info (const octave_value& obj);
243 
245  : field_names (x.field_names),
246  parent_class_names (x.parent_class_names) { }
247 
248  exemplar_info& operator = (const exemplar_info& x)
249  {
250  if (&x != this)
251  {
252  field_names = x.field_names;
253  parent_class_names = x.parent_class_names;
254  }
255  return *this;
256  }
257 
258  octave_idx_type nfields (void) const { return field_names.numel (); }
259 
260  size_t nparents (void) const { return parent_class_names.size (); }
261 
262  string_vector fields (void) const { return field_names; }
263 
264  std::list<std::string> parents (void) const { return parent_class_names; }
265 
266  bool compare (const octave_value& obj) const;
267 
268  private:
269 
271  std::list<std::string> parent_class_names;
272  };
273 
274  // A map from class names to lists of fields.
275  static std::map<std::string, exemplar_info> exemplar_map;
276 
277  typedef std::map<std::string, exemplar_info>::iterator
279  typedef std::map<std::string, exemplar_info>::const_iterator
281 };
282 
283 #endif
uint32_t id
Definition: graphics.cc:12193
dim_vector dims(void) const
Definition: ov-class.h:117
octave_map map_value(void) const
Definition: ov-class.h:155
static std::string static_class_name(void)
Definition: ov-class.h:213
octave_value map(octave_base_value::unary_mapper_t umap) const
Definition: ov.h:1473
octave_idx_type numel(void) const
Definition: ov-class.h:123
Definition: Cell.h:37
std::string class_name(void) const
Definition: ov-class.h:209
static int t_id
Definition: ov-class.h:217
string_vector keys(void) const
Definition: oct-map.h:342
void assign(const std::string &k, const Cell &val)
Definition: oct-map.h:351
octave_map map_value(void) const
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
idx subsref(val, idx) esult
Definition: ov.cc:3065
octave_idx_type nfields(void) const
Definition: ov-class.h:258
for large enough k
Definition: lu.cc:617
static bool in_class_method(const cdef_class &cls)
Definition: ov-classdef.cc:292
static const std::string t_name
Definition: ov-class.h:219
void assign(const std::string &k, const octave_value &rhs)
Definition: ov-class.h:104
octave_class(const octave_map &m, const std::string &id, const std::list< std::string > &plist)
Definition: ov-class.h:60
bool isstruct(void) const
Definition: ov-class.h:149
std::list< std::string > parents(void) const
Definition: ov-class.h:264
s
Definition: file-io.cc:2729
string_vector fields(void) const
Definition: ov-class.h:262
octave_class(void)
Definition: ov-class.h:55
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov.h:511
string_vector field_names
Definition: ov-class.h:270
bool swap
Definition: load-save.cc:738
std::map< std::string, exemplar_info >::const_iterator exemplar_const_iterator
Definition: ov-class.h:280
nd deftypefn *std::string name
Definition: sysdep.cc:647
static std::map< std::string, exemplar_info > exemplar_map
Definition: ov-class.h:275
OCTAVE_EXPORT octave_value_list return the number of command line arguments passed to Octave If called with the optional argument the function xample nargout(@histc)
Definition: ov-usr-fcn.cc:997
octave_map reshape(const dim_vector &dv) const
Definition: oct-map.cc:527
int obsolete_copies
Definition: ov-class.h:231
octave_base_value * empty_clone(void) const
Definition: ov-class.h:79
bool save_as_floats
Definition: load-save.cc:1617
string_vector parent_class_names(void) const
Definition: ov-class.h:162
double tmp
Definition: data.cc:6252
octave_value retval
Definition: data.cc:6246
bool is_true(const std::string &s)
int64_t octave_hdf5_id
idx type
Definition: ov.cc:3114
Definition: dMatrix.h:36
octave_base_value * clone(void) const
Definition: ov-class.h:75
std::list< std::string > parent_class_names
Definition: ov-class.h:271
the exceeded dimensions are set to if fewer subscripts than dimensions are the exceeding dimensions are merged into the final requested dimension For consider the following dims
Definition: sub2ind.cc:255
octave_idx_type nfields(void) const
Definition: ov-class.h:129
std::list< std::string > parent_class_name_list(void) const
Definition: ov-class.h:159
static std::string static_type_name(void)
Definition: ov-class.h:212
octave_idx_type nfields(void) const
Definition: oct-map.h:330
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
T::size_type numel(const T &str)
Definition: oct-string.cc:61
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition: dim-vector.h:362
octave_value resize(const dim_vector &dv, bool=false) const
Definition: ov-class.h:140
octave_map map
Definition: ov-class.h:204
octave_class(const octave_class &s)
Definition: ov-class.h:69
int register_type(const std::string &t_name, const std::string &c_name, const octave_value &val)
Definition: ov-typeinfo.cc:762
std::list< std::string > parent_list
Definition: ov-class.h:221
octave_map map(dims)
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-class.h:133
idx subsasgn(val, idx, 0) esult
Definition: ov.cc:3114
int type_id(void) const
Definition: ov-class.h:207
bool is_defined(void) const
Definition: ov-class.h:147
bool isobject(void) const
Definition: ov-class.h:151
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-class.h:90
dim_vector dims(void) const
Definition: oct-map.h:416
exemplar_info(const exemplar_info &x)
Definition: ov-class.h:244
write the output to stdout if nargout is
Definition: load-save.cc:1612
std::string c_name
Definition: ov-class.h:220
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
std::map< std::string, exemplar_info >::iterator exemplar_iterator
Definition: ov-class.h:278
size_t nparents(void) const
Definition: ov-class.h:260
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
static int static_type_id(void)
Definition: ov-class.h:211
dim_vector dv
Definition: sub2ind.cc:263
octave::stream os
Definition: file-io.cc:627
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE * x
std::string type_name(void) const
Definition: ov-class.h:208
size_t nparents(void) const
Definition: ov-class.h:131