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