2376
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, |
|
4 2006, 2007 John W. Eaton |
2376
|
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 |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
2376
|
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 |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
2376
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (octave_base_value_h) |
|
25 #define octave_base_value_h 1 |
|
26 |
|
27 #include <cstdlib> |
|
28 |
3503
|
29 #include <iostream> |
5759
|
30 #include <list> |
2376
|
31 #include <string> |
|
32 |
5759
|
33 #if defined (HAVE_HDF5) |
|
34 #include <hdf5.h> |
|
35 #endif |
|
36 |
|
37 #include "Range.h" |
5828
|
38 #include "data-conv.h" |
5900
|
39 #include "mxarray.h" |
2376
|
40 #include "mx-base.h" |
|
41 #include "str-vec.h" |
|
42 |
|
43 #include "error.h" |
|
44 |
3351
|
45 class Cell; |
5759
|
46 class streamoff_array; |
2376
|
47 class Octave_map; |
5759
|
48 class octave_value; |
2376
|
49 class octave_value_list; |
5759
|
50 class octave_stream; |
|
51 class octave_streamoff; |
|
52 class octave_function; |
|
53 class octave_user_function; |
|
54 class octave_fcn_handle; |
|
55 class octave_fcn_inline; |
|
56 class octave_value_list; |
|
57 class octave_lvalue; |
2376
|
58 |
|
59 class tree_walker; |
|
60 |
5759
|
61 // T_ID is the type id of struct objects, set by register_type(). |
|
62 // T_NAME is the type name of struct objects. |
7066
|
63 #define OCTAVE_EMPTY_CPP_ARG |
5759
|
64 #define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA \ |
7066
|
65 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA2 (OCTAVE_EMPTY_CPP_ARG) |
|
66 #undef OCTAVE_EMPTY_CPP_ARG |
5759
|
67 |
|
68 #define DECLARE_OV_BASE_TYPEID_FUNCTIONS_AND_DATA \ |
|
69 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA2(virtual) |
|
70 |
|
71 #define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA2(VIRTUAL) \ |
|
72 public: \ |
|
73 VIRTUAL int type_id (void) const { return t_id; } \ |
|
74 VIRTUAL std::string type_name (void) const { return t_name; } \ |
|
75 VIRTUAL std::string class_name (void) const { return c_name; } \ |
|
76 static int static_type_id (void) { return t_id; } \ |
|
77 static std::string static_type_name (void) { return t_name; } \ |
|
78 static std::string static_class_name (void) { return c_name; } \ |
|
79 static void register_type (void); \ |
|
80 \ |
|
81 private: \ |
|
82 static int t_id; \ |
|
83 static const std::string t_name; \ |
|
84 static const std::string c_name; |
|
85 |
|
86 |
|
87 #define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c) \ |
|
88 int t::t_id (-1); \ |
|
89 const std::string t::t_name (n); \ |
|
90 const std::string t::c_name (c); \ |
|
91 void t::register_type (void) \ |
|
92 { \ |
|
93 t_id = octave_value_typeinfo::register_type (t::t_name, \ |
|
94 t::c_name, \ |
|
95 octave_value (new t ())); \ |
|
96 } |
|
97 |
2477
|
98 // A base value type, so that derived types only have to redefine what |
|
99 // they need (if they are derived from octave_base_value instead of |
|
100 // octave_value). |
2376
|
101 |
|
102 class |
6109
|
103 OCTINTERP_API |
5759
|
104 octave_base_value |
2376
|
105 { |
|
106 public: |
|
107 |
5759
|
108 typedef octave_base_value * (*type_conv_fcn) (const octave_base_value&); |
|
109 |
|
110 friend class octave_value; |
2376
|
111 |
5759
|
112 octave_base_value (void) : count (1) { } |
|
113 |
|
114 octave_base_value (const octave_base_value&) { } |
2376
|
115 |
5759
|
116 virtual ~octave_base_value (void) { } |
|
117 |
|
118 virtual octave_base_value * |
|
119 clone (void) const { return new octave_base_value (*this); } |
2376
|
120 |
5759
|
121 virtual octave_base_value * |
|
122 empty_clone (void) const { return new octave_base_value (); } |
2376
|
123 |
5759
|
124 virtual type_conv_fcn |
|
125 numeric_conversion_function (void) const |
2800
|
126 { return static_cast<type_conv_fcn> (0); } |
2410
|
127 |
5759
|
128 virtual octave_value squeeze (void) const; |
|
129 |
|
130 virtual octave_base_value *try_narrowing_conversion (void) { return 0; } |
4532
|
131 |
5759
|
132 virtual octave_value |
|
133 subsref (const std::string& type, |
|
134 const std::list<octave_value_list>& idx); |
3933
|
135 |
5759
|
136 virtual octave_value_list |
|
137 subsref (const std::string& type, |
|
138 const std::list<octave_value_list>& idx, |
|
139 int nargout); |
3933
|
140 |
5759
|
141 virtual octave_value |
5885
|
142 do_index_op (const octave_value_list& idx, bool resize_ok = false); |
2974
|
143 |
5759
|
144 virtual octave_value_list |
3544
|
145 do_multi_index_op (int nargout, const octave_value_list& idx); |
2376
|
146 |
5759
|
147 virtual octave_value |
|
148 subsasgn (const std::string& type, |
|
149 const std::list<octave_value_list>& idx, |
|
150 const octave_value& rhs); |
2376
|
151 |
5759
|
152 virtual idx_vector index_vector (void) const; |
2376
|
153 |
5759
|
154 virtual dim_vector dims (void) const { return dim_vector (-1, -1); } |
4559
|
155 |
5759
|
156 octave_idx_type rows (void) const |
|
157 { |
|
158 dim_vector dv = dims (); |
5164
|
159 |
5759
|
160 return (dv.length () > 0) ? dv(0) : -1; |
|
161 } |
4791
|
162 |
5759
|
163 octave_idx_type columns (void) const |
|
164 { |
|
165 dim_vector dv = dims (); |
5602
|
166 |
5759
|
167 return (dv.length () > 1) ? dv(1) : -1; |
|
168 } |
5604
|
169 |
5759
|
170 virtual int ndims (void) const; |
4567
|
171 |
5759
|
172 virtual octave_idx_type numel (void) const { return dims ().numel (); } |
4593
|
173 |
5759
|
174 virtual octave_idx_type capacity (void) const { return numel (); } |
|
175 |
|
176 virtual size_t byte_size (void) const { return 0; } |
4915
|
177 |
5759
|
178 virtual octave_idx_type nnz (void) const; |
2376
|
179 |
5759
|
180 virtual octave_idx_type nzmax (void) const; |
|
181 |
5900
|
182 virtual octave_idx_type nfields (void) const; |
|
183 |
5759
|
184 virtual octave_value reshape (const dim_vector&) const; |
3351
|
185 |
5759
|
186 virtual octave_value permute (const Array<int>& vec, bool = false) const; |
2376
|
187 |
5759
|
188 virtual octave_value resize (const dim_vector&, bool fill = false) const; |
2376
|
189 |
5785
|
190 virtual MatrixType matrix_type (void) const; |
|
191 |
|
192 virtual MatrixType matrix_type (const MatrixType& typ) const; |
|
193 |
5759
|
194 virtual bool is_defined (void) const { return false; } |
4505
|
195 |
5759
|
196 bool is_empty (void) const { return numel () == 0; } |
2376
|
197 |
5759
|
198 virtual bool is_cell (void) const { return false; } |
2376
|
199 |
6116
|
200 virtual bool is_cellstr (void) const { return false; } |
|
201 |
5759
|
202 virtual bool is_real_scalar (void) const { return false; } |
4587
|
203 |
5759
|
204 virtual bool is_real_matrix (void) const { return false; } |
|
205 |
|
206 virtual bool is_real_nd_array (void) const { return false; } |
2376
|
207 |
5759
|
208 virtual bool is_complex_scalar (void) const { return false; } |
2376
|
209 |
5759
|
210 virtual bool is_complex_matrix (void) const { return false; } |
|
211 |
5881
|
212 virtual bool is_bool_scalar (void) const { return false; } |
|
213 |
5759
|
214 virtual bool is_bool_matrix (void) const { return false; } |
5279
|
215 |
5759
|
216 virtual bool is_char_matrix (void) const { return false; } |
2376
|
217 |
5759
|
218 virtual bool is_string (void) const { return false; } |
2376
|
219 |
5759
|
220 virtual bool is_sq_string (void) const { return false; } |
4643
|
221 |
5759
|
222 virtual bool is_range (void) const { return false; } |
3977
|
223 |
5759
|
224 virtual bool is_map (void) const { return false; } |
|
225 |
|
226 virtual bool is_streamoff (void) const { return false; } |
2882
|
227 |
5759
|
228 virtual bool is_cs_list (void) const { return false; } |
2376
|
229 |
5759
|
230 virtual bool is_list (void) const { return false; } |
|
231 |
|
232 virtual bool is_magic_colon (void) const { return false; } |
2376
|
233 |
5759
|
234 virtual bool is_all_va_args (void) const { return false; } |
2376
|
235 |
5759
|
236 virtual octave_value all (int = 0) const; |
|
237 |
|
238 virtual octave_value any (int = 0) const; |
2376
|
239 |
5895
|
240 virtual bool is_double_type (void) const { return false; } |
|
241 |
|
242 virtual bool is_single_type (void) const { return false; } |
|
243 |
|
244 virtual bool is_int8_type (void) const { return false; } |
|
245 |
|
246 virtual bool is_int16_type (void) const { return false; } |
|
247 |
|
248 virtual bool is_int32_type (void) const { return false; } |
|
249 |
|
250 virtual bool is_int64_type (void) const { return false; } |
|
251 |
|
252 virtual bool is_uint8_type (void) const { return false; } |
|
253 |
|
254 virtual bool is_uint16_type (void) const { return false; } |
|
255 |
|
256 virtual bool is_uint32_type (void) const { return false; } |
|
257 |
|
258 virtual bool is_uint64_type (void) const { return false; } |
|
259 |
5759
|
260 virtual bool is_bool_type (void) const { return false; } |
3209
|
261 |
6223
|
262 virtual bool is_integer_type (void) const { return false; } |
|
263 |
5759
|
264 virtual bool is_real_type (void) const { return false; } |
2376
|
265 |
5759
|
266 virtual bool is_complex_type (void) const { return false; } |
2376
|
267 |
|
268 // Would be nice to get rid of the next four functions: |
|
269 |
5759
|
270 virtual bool is_scalar_type (void) const { return false; } |
2376
|
271 |
5759
|
272 virtual bool is_matrix_type (void) const { return false; } |
2376
|
273 |
5759
|
274 virtual bool is_numeric_type (void) const { return false; } |
2376
|
275 |
5759
|
276 virtual bool is_sparse_type (void) const { return false; } |
5631
|
277 |
5759
|
278 virtual bool valid_as_scalar_index (void) const { return false; } |
2376
|
279 |
5759
|
280 virtual bool valid_as_zero_index (void) const { return false; } |
2376
|
281 |
5759
|
282 virtual bool is_true (void) const { return false; } |
|
283 |
|
284 virtual bool is_constant (void) const { return false; } |
2376
|
285 |
5759
|
286 virtual bool is_function_handle (void) const { return false; } |
2376
|
287 |
5759
|
288 virtual bool is_inline_function (void) const { return false; } |
2974
|
289 |
5759
|
290 virtual bool is_function (void) const { return false; } |
4954
|
291 |
5759
|
292 virtual bool is_builtin_function (void) const { return false; } |
2974
|
293 |
5759
|
294 virtual bool is_dld_function (void) const { return false; } |
3325
|
295 |
5864
|
296 virtual bool is_mex_function (void) const { return false; } |
|
297 |
5759
|
298 virtual short int short_value (bool = false, bool = false) const; |
4254
|
299 |
5759
|
300 virtual unsigned short int ushort_value (bool = false, bool = false) const; |
4254
|
301 |
5759
|
302 virtual int int_value (bool = false, bool = false) const; |
3202
|
303 |
5759
|
304 virtual unsigned int uint_value (bool = false, bool = false) const; |
4254
|
305 |
5759
|
306 virtual int nint_value (bool = false) const; |
3202
|
307 |
5759
|
308 virtual long int long_value (bool = false, bool = false) const; |
4254
|
309 |
5759
|
310 virtual unsigned long int ulong_value (bool = false, bool = false) const; |
4254
|
311 |
5759
|
312 virtual double double_value (bool = false) const; |
2376
|
313 |
5759
|
314 virtual double scalar_value (bool frc_str_conv = false) const |
3145
|
315 { return double_value (frc_str_conv); } |
2376
|
316 |
5759
|
317 virtual Cell cell_value (void) const; |
3351
|
318 |
5759
|
319 virtual Matrix matrix_value (bool = false) const; |
3145
|
320 |
5759
|
321 virtual NDArray array_value (bool = false) const; |
4505
|
322 |
5759
|
323 virtual Complex complex_value (bool = false) const; |
2376
|
324 |
5759
|
325 virtual ComplexMatrix complex_matrix_value (bool = false) const; |
2376
|
326 |
5759
|
327 virtual ComplexNDArray complex_array_value (bool = false) const; |
4550
|
328 |
5943
|
329 virtual bool bool_value (bool = false) const; |
4550
|
330 |
5943
|
331 virtual boolMatrix bool_matrix_value (bool = false) const; |
4550
|
332 |
5943
|
333 virtual boolNDArray bool_array_value (bool = false) const; |
4550
|
334 |
5759
|
335 virtual charMatrix char_matrix_value (bool force = false) const; |
|
336 |
|
337 virtual charNDArray char_array_value (bool = false) const; |
2376
|
338 |
5759
|
339 virtual SparseMatrix sparse_matrix_value (bool = false) const; |
4550
|
340 |
5759
|
341 virtual SparseComplexMatrix sparse_complex_matrix_value (bool = false) const; |
5164
|
342 |
5759
|
343 virtual SparseBoolMatrix sparse_bool_matrix_value (bool = false) const; |
5164
|
344 |
5759
|
345 virtual octave_int8 int8_scalar_value (void) const; |
5164
|
346 |
5759
|
347 virtual octave_int16 int16_scalar_value (void) const; |
4910
|
348 |
5759
|
349 virtual octave_int32 int32_scalar_value (void) const; |
|
350 |
|
351 virtual octave_int64 int64_scalar_value (void) const; |
4910
|
352 |
5759
|
353 virtual octave_uint8 uint8_scalar_value (void) const; |
4910
|
354 |
5759
|
355 virtual octave_uint16 uint16_scalar_value (void) const; |
4910
|
356 |
5759
|
357 virtual octave_uint32 uint32_scalar_value (void) const; |
4910
|
358 |
5759
|
359 virtual octave_uint64 uint64_scalar_value (void) const; |
4910
|
360 |
5759
|
361 virtual int8NDArray int8_array_value (void) const; |
4910
|
362 |
5759
|
363 virtual int16NDArray int16_array_value (void) const; |
4910
|
364 |
5759
|
365 virtual int32NDArray int32_array_value (void) const; |
4906
|
366 |
5759
|
367 virtual int64NDArray int64_array_value (void) const; |
4906
|
368 |
5759
|
369 virtual uint8NDArray uint8_array_value (void) const; |
4906
|
370 |
5759
|
371 virtual uint16NDArray uint16_array_value (void) const; |
4906
|
372 |
5759
|
373 virtual uint32NDArray uint32_array_value (void) const; |
4906
|
374 |
5759
|
375 virtual uint64NDArray uint64_array_value (void) const; |
4906
|
376 |
5759
|
377 virtual string_vector all_strings (bool pad = false) const; |
4906
|
378 |
5759
|
379 virtual std::string string_value (bool force = false) const; |
4906
|
380 |
5759
|
381 virtual Range range_value (void) const; |
2376
|
382 |
5759
|
383 virtual Octave_map map_value (void) const; |
2376
|
384 |
5759
|
385 virtual string_vector map_keys (void) const; |
2376
|
386 |
5759
|
387 virtual std::streamoff streamoff_value (void) const; |
2376
|
388 |
5759
|
389 virtual streamoff_array streamoff_array_value (void) const; |
|
390 |
|
391 virtual octave_function *function_value (bool silent = false); |
3933
|
392 |
5759
|
393 virtual octave_user_function *user_function_value (bool silent = false); |
4645
|
394 |
5759
|
395 virtual octave_fcn_handle *fcn_handle_value (bool silent = false); |
4643
|
396 |
5759
|
397 virtual octave_fcn_inline *fcn_inline_value (bool silent = false); |
2974
|
398 |
5759
|
399 virtual octave_value_list list_value (void) const; |
4343
|
400 |
5759
|
401 virtual octave_value convert_to_str (bool pad = false, bool force = false, |
|
402 char type = '"') const; |
|
403 virtual octave_value |
|
404 convert_to_str_internal (bool pad, bool force, char type) const; |
4933
|
405 |
5759
|
406 virtual void convert_to_row_or_column_vector (void); |
2882
|
407 |
5759
|
408 virtual bool print_as_scalar (void) const { return false; } |
2376
|
409 |
5759
|
410 virtual void print (std::ostream& os, bool pr_as_read_syntax = false) const; |
2376
|
411 |
5759
|
412 virtual void |
|
413 print_raw (std::ostream& os, bool pr_as_read_syntax = false) const; |
4604
|
414 |
5759
|
415 virtual bool |
|
416 print_name_tag (std::ostream& os, const std::string& name) const; |
2901
|
417 |
5759
|
418 virtual void |
|
419 print_with_name (std::ostream& output_buf, const std::string& name, |
|
420 bool print_padding = true) const; |
2376
|
421 |
5759
|
422 virtual void print_info (std::ostream& os, const std::string& prefix) const; |
3933
|
423 |
6974
|
424 virtual bool save_ascii (std::ostream& os); |
4687
|
425 |
5759
|
426 virtual bool load_ascii (std::istream& is); |
4687
|
427 |
5759
|
428 virtual bool save_binary (std::ostream& os, bool& save_as_floats); |
4687
|
429 |
5759
|
430 virtual bool load_binary (std::istream& is, bool swap, |
|
431 oct_mach_info::float_format fmt); |
4687
|
432 |
|
433 #if defined (HAVE_HDF5) |
5759
|
434 virtual bool |
|
435 save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats); |
4687
|
436 |
5759
|
437 virtual bool |
|
438 load_hdf5 (hid_t loc_id, const char *name, bool have_h5giterate_bug); |
4687
|
439 #endif |
|
440 |
5759
|
441 virtual int |
|
442 write (octave_stream& os, int block_size, |
|
443 oct_data_conv::data_type output_type, int skip, |
|
444 oct_mach_info::float_format flt_fmt) const; |
|
445 |
5900
|
446 virtual void *mex_get_data (void) const { return 0; } |
|
447 |
|
448 virtual octave_idx_type *mex_get_ir (void) const { return 0; } |
|
449 |
|
450 virtual octave_idx_type *mex_get_jc (void) const { return 0; } |
|
451 |
|
452 virtual mxArray *as_mxArray (void) const; |
|
453 |
5759
|
454 protected: |
|
455 |
|
456 // This should only be called for derived types. |
|
457 |
|
458 octave_value numeric_assign (const std::string& type, |
|
459 const std::list<octave_value_list>& idx, |
|
460 const octave_value& rhs); |
|
461 |
|
462 void reset_indent_level (void) const |
|
463 { curr_print_indent_level = 0; } |
|
464 |
|
465 void increment_indent_level (void) const |
|
466 { curr_print_indent_level += 2; } |
|
467 |
|
468 void decrement_indent_level (void) const |
|
469 { curr_print_indent_level -= 2; } |
|
470 |
|
471 int current_print_indent_level (void) const |
|
472 { return curr_print_indent_level; } |
|
473 |
|
474 void indent (std::ostream& os) const; |
|
475 |
|
476 void newline (std::ostream& os) const; |
|
477 |
|
478 void reset (void) const; |
|
479 |
|
480 // A reference count. |
|
481 int count; |
4944
|
482 |
2376
|
483 private: |
|
484 |
5759
|
485 static int curr_print_indent_level; |
|
486 static bool beginning_of_line; |
|
487 |
|
488 DECLARE_OV_BASE_TYPEID_FUNCTIONS_AND_DATA |
2376
|
489 }; |
|
490 |
|
491 #endif |
|
492 |
|
493 /* |
|
494 ;;; Local Variables: *** |
|
495 ;;; mode: C++ *** |
|
496 ;;; End: *** |
|
497 */ |