GNU Octave  3.8.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-2013 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-alloc.h"
37 #include "oct-map.h"
38 #include "ov-base.h"
39 #include "ov-typeinfo.h"
40 
41 class octave_value_list;
42 
43 class tree_walker;
44 
45 // Data structures.
46 
47 class
49 {
50 public:
51 
52  octave_class (void)
53  : octave_base_value (), map (), c_name (),
54  parent_list (), obsolete_copies (0)
55  { }
56 
57  octave_class (const octave_map& m, const std::string& id,
58  const std::list<std::string>& plist)
59  : octave_base_value (), map (m), c_name (id),
60  parent_list (plist), obsolete_copies (0)
61  { }
62 
63  octave_class (const octave_map& m, const std::string& id,
64  const octave_value_list& parents);
65 
67  : octave_base_value (s), map (s.map), c_name (s.c_name),
68  parent_list (s.parent_list), obsolete_copies (0) { }
69 
70  ~octave_class (void) { }
71 
72  octave_base_value *clone (void) const { return new octave_class (*this); }
73 
74  octave_base_value *unique_clone (void);
75 
76  octave_base_value *empty_clone (void) const
77  {
78  return new octave_class (octave_map (map.keys ()), c_name, parent_list);
79  }
80 
81  Cell dotref (const octave_value_list& idx);
82 
83  Matrix size (void);
84 
85  octave_idx_type numel (const octave_value_list&);
86 
87  octave_value subsref (const std::string& type,
88  const std::list<octave_value_list>& idx)
89  {
90  octave_value_list tmp = subsref (type, idx, 1);
91  return tmp.length () > 0 ? tmp(0) : octave_value ();
92  }
93 
94  octave_value_list subsref (const std::string& type,
95  const std::list<octave_value_list>& idx,
96  int nargout);
97 
99  do_multi_index_op (int nargout, const octave_value_list& idx)
100  {
101  return subsref ("(", std::list<octave_value_list> (1, idx), nargout);
102  }
103 
104  static octave_value numeric_conv (const Cell& val,
105  const std::string& type);
106 
107  void assign(const std::string& k, const octave_value& rhs)
108  { map.assign (k, rhs); };
109 
110  octave_value subsasgn (const std::string& type,
111  const std::list<octave_value_list>& idx,
112  const octave_value& rhs);
113 
114  octave_value undef_subsasgn (const std::string& type,
115  const std::list<octave_value_list>& idx,
116  const octave_value& rhs);
117 
118  idx_vector index_vector (void) const;
119 
120  dim_vector dims (void) const { return map.dims (); }
121 
122  size_t byte_size (void) const;
123 
124  // This is the number of elements in each field. The total number
125  // of elements is numel () * nfields ().
126  octave_idx_type numel (void) const
127  {
128  dim_vector dv = dims ();
129  return dv.numel ();
130  }
131 
132  octave_idx_type nfields (void) const { return map.nfields (); }
133 
134  size_t nparents (void) const { return parent_list.size (); }
135 
136  octave_value reshape (const dim_vector& new_dims) const
137  {
138  octave_class retval = octave_class (*this);
139  retval.map = retval.map_value ().reshape (new_dims);
140  return octave_value (new octave_class (retval));
141  }
142 
143  octave_value resize (const dim_vector& dv, bool = false) const
144  {
145  octave_class retval = octave_class (*this);
146  retval.map.resize (dv);
147  return octave_value (new octave_class (retval));
148  }
149 
150  bool is_defined (void) const { return true; }
151 
152  bool is_map (void) const { return false; }
153 
154  bool is_object (void) const { return true; }
155 
156  octave_map map_value (void) const { return map; }
157 
158  string_vector map_keys (void) const;
159 
160  std::list<std::string> parent_class_name_list (void) const
161  { return parent_list; }
162 
163  string_vector parent_class_names (void) const
164  { return string_vector (parent_list); }
165 
166  octave_base_value *find_parent_class (const std::string&);
167 
168  octave_base_value *unique_parent_class (const std::string&);
169 
170  string_vector all_strings (bool pad) const;
171 
172  void print (std::ostream& os, bool pr_as_read_syntax = false) const;
173 
174  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
175 
176  bool print_name_tag (std::ostream& os, const std::string& name) const;
177 
178  void print_with_name (std::ostream& os, const std::string& name,
179  bool print_padding = true);
180 
181  bool reconstruct_exemplar (void);
182 
183  static void clear_exemplar_map (void);
184 
185  bool reconstruct_parents (void);
186 
187  bool save_ascii (std::ostream& os);
188 
189  bool load_ascii (std::istream& is);
190 
191  bool save_binary (std::ostream& os, bool& save_as_floats);
192 
193  bool load_binary (std::istream& is, bool swap,
195 
196 #if defined (HAVE_HDF5)
197  bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
198 
199  bool load_hdf5 (hid_t loc_id, const char *name);
200 #endif
201 
202  mxArray *as_mxArray (void) const;
203 
204 private:
205 
207 
209 
210 public:
211  int type_id (void) const { return t_id; }
212  std::string type_name (void) const { return t_name; }
213  std::string class_name (void) const { return c_name; }
214 
215  static int static_type_id (void) { return t_id; }
216  static std::string static_type_name (void) { return t_name; }
217  static std::string static_class_name (void) { return "<unknown>"; }
218  static void register_type (void);
219 
220 private:
221  static int t_id;
222 
223  static const std::string t_name;
224  std::string c_name;
225  std::list<std::string> parent_list;
226 
227  bool in_class_method (void);
228  std::string get_current_method_class (void);
229 
230  octave_value subsasgn_common (const octave_value& obj,
231  const std::string& type,
232  const std::list<octave_value_list>& idx,
233  const octave_value& rhs);
234 
236 
237 public:
238  // The list of field names and parent classes defines a class. We
239  // keep track of each class that has been created so that we know
241  {
242  public:
243 
244  exemplar_info (void) : field_names (), parent_class_names () { }
245 
246  exemplar_info (const octave_value& obj);
247 
249  : field_names (x.field_names),
250  parent_class_names (x.parent_class_names) { }
251 
252  exemplar_info& operator = (const exemplar_info& x)
253  {
254  if (&x != this)
255  {
256  field_names = x.field_names;
257  parent_class_names = x.parent_class_names;
258  }
259  return *this;
260  }
261 
262  octave_idx_type nfields (void) const { return field_names.length (); }
263 
264  size_t nparents (void) const { return parent_class_names.size (); }
265 
266  string_vector fields (void) const { return field_names; }
267 
268  std::list<std::string> parents (void) const { return parent_class_names; }
269 
270  bool compare (const octave_value& obj) const;
271 
272  private:
273 
275  std::list<std::string> parent_class_names;
276  };
277 
278  // A map from class names to lists of fields.
279  static std::map<std::string, exemplar_info> exemplar_map;
280 
281  typedef std::map<std::string, exemplar_info>::iterator
283  typedef std::map<std::string, exemplar_info>::const_iterator
285 };
286 
287 #endif