GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ov-class.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2007-2015 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 the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 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 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if !defined (octave_ov_class_h)
25 #define octave_ov_class_h 1
26 
27 #include <cstdlib>
28 
29 #include <iosfwd>
30 #include <string>
31 
32 #include "mx-base.h"
33 #include "str-vec.h"
34 
35 #include "error.h"
36 #include "oct-map.h"
37 #include "ov-base.h"
38 #include "ov-typeinfo.h"
39 
40 class octave_value_list;
41 
42 class tree_walker;
43 
44 // Data structures.
45 
46 class
48 {
49 public:
50 
51  octave_class (void)
52  : octave_base_value (), map (), c_name (),
53  parent_list (), obsolete_copies (0)
54  { }
55 
56  octave_class (const octave_map& m, const std::string& id,
57  const std::list<std::string>& plist)
58  : octave_base_value (), map (m), c_name (id),
59  parent_list (plist), obsolete_copies (0)
60  { }
61 
62  octave_class (const octave_map& m, const std::string& id,
63  const octave_value_list& parents);
64 
66  : octave_base_value (s), map (s.map), c_name (s.c_name),
67  parent_list (s.parent_list), obsolete_copies (0) { }
68 
69  ~octave_class (void) { }
70 
71  octave_base_value *clone (void) const { return new octave_class (*this); }
72 
73  octave_base_value *unique_clone (void);
74 
76  {
77  return new octave_class (octave_map (map.keys ()), c_name, parent_list);
78  }
79 
80  Cell dotref (const octave_value_list& idx);
81 
82  Matrix size (void);
83 
84  octave_idx_type numel (const octave_value_list&);
85 
86  octave_value subsref (const std::string& type,
87  const std::list<octave_value_list>& idx)
88  {
89  octave_value_list tmp = subsref (type, idx, 1);
90  return tmp.length () > 0 ? tmp(0) : octave_value ();
91  }
92 
93  octave_value_list subsref (const std::string& type,
94  const std::list<octave_value_list>& idx,
95  int nargout);
96 
98  do_multi_index_op (int nargout, const octave_value_list& idx)
99  {
100  return subsref ("(", std::list<octave_value_list> (1, idx), nargout);
101  }
102 
103  static octave_value numeric_conv (const Cell& val,
104  const std::string& type);
105 
106  void assign(const std::string& k, const octave_value& rhs)
107  { map.assign (k, rhs); };
108 
109  octave_value subsasgn (const std::string& type,
110  const std::list<octave_value_list>& idx,
111  const octave_value& rhs);
112 
113  octave_value undef_subsasgn (const std::string& type,
114  const std::list<octave_value_list>& idx,
115  const octave_value& rhs);
116 
117  idx_vector index_vector (bool require_integers = false) const;
118 
119  dim_vector dims (void) const { return map.dims (); }
120 
121  size_t byte_size (void) const;
122 
123  // This is the number of elements in each field. The total number
124  // of elements is numel () * nfields ().
125  octave_idx_type numel (void) const
126  {
127  dim_vector dv = dims ();
128  return dv.numel ();
129  }
130 
131  octave_idx_type nfields (void) const { return map.nfields (); }
132 
133  size_t nparents (void) const { return parent_list.size (); }
134 
135  octave_value reshape (const dim_vector& new_dims) const
136  {
137  octave_class retval = octave_class (*this);
138  retval.map = retval.map_value ().reshape (new_dims);
139  return octave_value (new octave_class (retval));
140  }
141 
142  octave_value resize (const dim_vector& dv, bool = false) const
143  {
144  octave_class retval = octave_class (*this);
145  retval.map.resize (dv);
146  return octave_value (new octave_class (retval));
147  }
148 
149  bool is_defined (void) const { return true; }
150 
151  bool is_map (void) const { return false; }
152 
153  bool is_object (void) const { return true; }
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 all_strings (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:
204 
206 
207 
208 public:
209  int type_id (void) const { return t_id; }
210  std::string type_name (void) const { return t_name; }
211  std::string class_name (void) const { return c_name; }
212 
213  static int static_type_id (void) { return t_id; }
214  static std::string static_type_name (void) { return t_name; }
215  static std::string static_class_name (void) { return "<unknown>"; }
216  static void register_type (void);
217 
218 private:
219  static int t_id;
220 
221  static const std::string t_name;
222  std::string c_name;
223  std::list<std::string> parent_list;
224 
225  bool in_class_method (void);
226  std::string get_current_method_class (void);
227 
228  octave_value subsasgn_common (const octave_value& obj,
229  const std::string& type,
230  const std::list<octave_value_list>& idx,
231  const octave_value& rhs);
232 
234 
235 public:
236  // The list of field names and parent classes defines a class. We
237  // keep track of each class that has been created so that we know
239  {
240  public:
241 
242  exemplar_info (void) : field_names (), parent_class_names () { }
243 
244  exemplar_info (const octave_value& obj);
245 
247  : field_names (x.field_names),
248  parent_class_names (x.parent_class_names) { }
249 
250  exemplar_info& operator = (const exemplar_info& x)
251  {
252  if (&x != this)
253  {
254  field_names = x.field_names;
255  parent_class_names = x.parent_class_names;
256  }
257  return *this;
258  }
259 
260  octave_idx_type nfields (void) const { return field_names.length (); }
261 
262  size_t nparents (void) const { return parent_class_names.size (); }
263 
264  string_vector fields (void) const { return field_names; }
265 
266  std::list<std::string> parents (void) const { return parent_class_names; }
267 
268  bool compare (const octave_value& obj) const;
269 
270  private:
271 
273  std::list<std::string> parent_class_names;
274  };
275 
276  // A map from class names to lists of fields.
277  static std::map<std::string, exemplar_info> exemplar_map;
278 
279  typedef std::map<std::string, exemplar_info>::iterator
281  typedef std::map<std::string, exemplar_info>::const_iterator
283 };
284 
285 #endif
int type_id(void) const
Definition: ov-class.h:209
std::list< std::string > parent_class_name_list(void) const
Definition: ov-class.h:159
static std::string static_class_name(void)
Definition: ov-class.h:215
bool is_map(void) const
Definition: ov-class.h:151
Definition: Cell.h:35
std::list< std::string > parents(void) const
Definition: ov-class.h:266
static int t_id
Definition: ov-class.h:219
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-class.h:135
octave_base_value * clone(void) const
Definition: ov-class.h:71
octave_idx_type length(void) const
Definition: oct-obj.h:89
dim_vector dims(void) const
Definition: ov-class.h:119
octave_idx_type nfields(void) const
Definition: ov-class.h:260
std::string type_name(void) const
Definition: ov-class.h:210
static bool in_class_method(const cdef_class &cls)
Definition: ov-classdef.cc:310
static const std::string t_name
Definition: ov-class.h:221
void assign(const std::string &k, const octave_value &rhs)
Definition: ov-class.h:106
octave_class(const octave_map &m, const std::string &id, const std::list< std::string > &plist)
Definition: ov-class.h:56
octave_base_value * empty_clone(void) const
Definition: ov-class.h:75
string_vector parent_class_names(void) const
Definition: ov-class.h:162
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition: dim-vector.h:361
~octave_class(void)
Definition: ov-class.h:69
octave_class(void)
Definition: ov-class.h:51
string_vector field_names
Definition: ov-class.h:272
octave_map map_value(void) const
Definition: ov-class.h:155
std::map< std::string, exemplar_info >::const_iterator exemplar_const_iterator
Definition: ov-class.h:282
static std::map< std::string, exemplar_info > exemplar_map
Definition: ov-class.h:277
size_t nparents(void) const
Definition: ov-class.h:262
int obsolete_copies
Definition: ov-class.h:233
Definition: dMatrix.h:35
size_t size(T const (&)[z])
Definition: help.cc:103
std::list< std::string > parent_class_names
Definition: ov-class.h:273
bool is_defined(void) const
Definition: ov-class.h:149
static std::string static_type_name(void)
Definition: ov-class.h:214
size_t nparents(void) const
Definition: ov-class.h:133
octave_idx_type nfields(void) const
Definition: ov-class.h:131
octave_map map
Definition: ov-class.h:205
octave_class(const octave_class &s)
Definition: ov-class.h:65
std::list< std::string > parent_list
Definition: ov-class.h:223
octave_idx_type numel(void) const
Definition: ov-class.h:125
bool is_object(void) const
Definition: ov-class.h:153
octave_map reshape(const dim_vector &dv) const
Definition: oct-map.cc:513
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-class.h:86
octave_value_list do_multi_index_op(int nargout, const octave_value_list &idx)
Definition: ov-class.h:98
void resize(const dim_vector &dv, bool fill=false)
Definition: oct-map.cc:546
exemplar_info(const exemplar_info &x)
Definition: ov-class.h:246
octave_value resize(const dim_vector &dv, bool=false) const
Definition: ov-class.h:142
std::string c_name
Definition: ov-class.h:222
std::map< std::string, exemplar_info >::iterator exemplar_iterator
Definition: ov-class.h:280
string_vector fields(void) const
Definition: ov-class.h:264
static int static_type_id(void)
Definition: ov-class.h:213
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))
F77_RET_T const double * x
std::string class_name(void) const
Definition: ov-class.h:211