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 |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
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. |
|
63 #define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA \ |
|
64 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA2() |
|
65 |
|
66 #define DECLARE_OV_BASE_TYPEID_FUNCTIONS_AND_DATA \ |
|
67 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA2(virtual) |
|
68 |
|
69 #define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA2(VIRTUAL) \ |
|
70 public: \ |
|
71 VIRTUAL int type_id (void) const { return t_id; } \ |
|
72 VIRTUAL std::string type_name (void) const { return t_name; } \ |
|
73 VIRTUAL std::string class_name (void) const { return c_name; } \ |
|
74 static int static_type_id (void) { return t_id; } \ |
|
75 static std::string static_type_name (void) { return t_name; } \ |
|
76 static std::string static_class_name (void) { return c_name; } \ |
|
77 static void register_type (void); \ |
|
78 \ |
|
79 private: \ |
|
80 static int t_id; \ |
|
81 static const std::string t_name; \ |
|
82 static const std::string c_name; |
|
83 |
|
84 |
|
85 #define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c) \ |
|
86 int t::t_id (-1); \ |
|
87 const std::string t::t_name (n); \ |
|
88 const std::string t::c_name (c); \ |
|
89 void t::register_type (void) \ |
|
90 { \ |
|
91 t_id = octave_value_typeinfo::register_type (t::t_name, \ |
|
92 t::c_name, \ |
|
93 octave_value (new t ())); \ |
|
94 } |
|
95 |
2477
|
96 // A base value type, so that derived types only have to redefine what |
|
97 // they need (if they are derived from octave_base_value instead of |
|
98 // octave_value). |
2376
|
99 |
|
100 class |
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 |
5759
|
197 virtual bool is_real_scalar (void) const { return false; } |
4587
|
198 |
5759
|
199 virtual bool is_real_matrix (void) const { return false; } |
|
200 |
|
201 virtual bool is_real_nd_array (void) const { return false; } |
2376
|
202 |
5759
|
203 virtual bool is_complex_scalar (void) const { return false; } |
2376
|
204 |
5759
|
205 virtual bool is_complex_matrix (void) const { return false; } |
|
206 |
5881
|
207 virtual bool is_bool_scalar (void) const { return false; } |
|
208 |
5759
|
209 virtual bool is_bool_matrix (void) const { return false; } |
5279
|
210 |
5759
|
211 virtual bool is_char_matrix (void) const { return false; } |
2376
|
212 |
5759
|
213 virtual bool is_string (void) const { return false; } |
2376
|
214 |
5759
|
215 virtual bool is_sq_string (void) const { return false; } |
4643
|
216 |
5759
|
217 virtual bool is_range (void) const { return false; } |
3977
|
218 |
5759
|
219 virtual bool is_map (void) const { return false; } |
|
220 |
|
221 virtual bool is_streamoff (void) const { return false; } |
2882
|
222 |
5759
|
223 virtual bool is_cs_list (void) const { return false; } |
2376
|
224 |
5759
|
225 virtual bool is_list (void) const { return false; } |
|
226 |
|
227 virtual bool is_magic_colon (void) const { return false; } |
2376
|
228 |
5759
|
229 virtual bool is_all_va_args (void) const { return false; } |
2376
|
230 |
5759
|
231 virtual octave_value all (int = 0) const; |
|
232 |
|
233 virtual octave_value any (int = 0) const; |
2376
|
234 |
5895
|
235 virtual bool is_double_type (void) const { return false; } |
|
236 |
|
237 virtual bool is_single_type (void) const { return false; } |
|
238 |
|
239 virtual bool is_int8_type (void) const { return false; } |
|
240 |
|
241 virtual bool is_int16_type (void) const { return false; } |
|
242 |
|
243 virtual bool is_int32_type (void) const { return false; } |
|
244 |
|
245 virtual bool is_int64_type (void) const { return false; } |
|
246 |
|
247 virtual bool is_uint8_type (void) const { return false; } |
|
248 |
|
249 virtual bool is_uint16_type (void) const { return false; } |
|
250 |
|
251 virtual bool is_uint32_type (void) const { return false; } |
|
252 |
|
253 virtual bool is_uint64_type (void) const { return false; } |
|
254 |
5759
|
255 virtual bool is_bool_type (void) const { return false; } |
3209
|
256 |
5759
|
257 virtual bool is_real_type (void) const { return false; } |
2376
|
258 |
5759
|
259 virtual bool is_complex_type (void) const { return false; } |
2376
|
260 |
|
261 // Would be nice to get rid of the next four functions: |
|
262 |
5759
|
263 virtual bool is_scalar_type (void) const { return false; } |
2376
|
264 |
5759
|
265 virtual bool is_matrix_type (void) const { return false; } |
2376
|
266 |
5759
|
267 virtual bool is_numeric_type (void) const { return false; } |
2376
|
268 |
5759
|
269 virtual bool is_sparse_type (void) const { return false; } |
5631
|
270 |
5759
|
271 virtual bool valid_as_scalar_index (void) const { return false; } |
2376
|
272 |
5759
|
273 virtual bool valid_as_zero_index (void) const { return false; } |
2376
|
274 |
5759
|
275 virtual bool is_true (void) const { return false; } |
|
276 |
|
277 virtual bool is_constant (void) const { return false; } |
2376
|
278 |
5759
|
279 virtual bool is_function_handle (void) const { return false; } |
2376
|
280 |
5759
|
281 virtual bool is_inline_function (void) const { return false; } |
2974
|
282 |
5759
|
283 virtual bool is_function (void) const { return false; } |
4954
|
284 |
5759
|
285 virtual bool is_builtin_function (void) const { return false; } |
2974
|
286 |
5759
|
287 virtual bool is_dld_function (void) const { return false; } |
3325
|
288 |
5864
|
289 virtual bool is_mex_function (void) const { return false; } |
|
290 |
5759
|
291 virtual short int short_value (bool = false, bool = false) const; |
4254
|
292 |
5759
|
293 virtual unsigned short int ushort_value (bool = false, bool = false) const; |
4254
|
294 |
5759
|
295 virtual int int_value (bool = false, bool = false) const; |
3202
|
296 |
5759
|
297 virtual unsigned int uint_value (bool = false, bool = false) const; |
4254
|
298 |
5759
|
299 virtual int nint_value (bool = false) const; |
3202
|
300 |
5759
|
301 virtual long int long_value (bool = false, bool = false) const; |
4254
|
302 |
5759
|
303 virtual unsigned long int ulong_value (bool = false, bool = false) const; |
4254
|
304 |
5759
|
305 virtual double double_value (bool = false) const; |
2376
|
306 |
5759
|
307 virtual double scalar_value (bool frc_str_conv = false) const |
3145
|
308 { return double_value (frc_str_conv); } |
2376
|
309 |
5759
|
310 virtual Cell cell_value (void) const; |
3351
|
311 |
5759
|
312 virtual Matrix matrix_value (bool = false) const; |
3145
|
313 |
5759
|
314 virtual NDArray array_value (bool = false) const; |
4505
|
315 |
5759
|
316 virtual Complex complex_value (bool = false) const; |
2376
|
317 |
5759
|
318 virtual ComplexMatrix complex_matrix_value (bool = false) const; |
2376
|
319 |
5759
|
320 virtual ComplexNDArray complex_array_value (bool = false) const; |
4550
|
321 |
5943
|
322 virtual bool bool_value (bool = false) const; |
4550
|
323 |
5943
|
324 virtual boolMatrix bool_matrix_value (bool = false) const; |
4550
|
325 |
5943
|
326 virtual boolNDArray bool_array_value (bool = false) const; |
4550
|
327 |
5759
|
328 virtual charMatrix char_matrix_value (bool force = false) const; |
|
329 |
|
330 virtual charNDArray char_array_value (bool = false) const; |
2376
|
331 |
5759
|
332 virtual SparseMatrix sparse_matrix_value (bool = false) const; |
4550
|
333 |
5759
|
334 virtual SparseComplexMatrix sparse_complex_matrix_value (bool = false) const; |
5164
|
335 |
5759
|
336 virtual SparseBoolMatrix sparse_bool_matrix_value (bool = false) const; |
5164
|
337 |
5759
|
338 virtual octave_int8 int8_scalar_value (void) const; |
5164
|
339 |
5759
|
340 virtual octave_int16 int16_scalar_value (void) const; |
4910
|
341 |
5759
|
342 virtual octave_int32 int32_scalar_value (void) const; |
|
343 |
|
344 virtual octave_int64 int64_scalar_value (void) const; |
4910
|
345 |
5759
|
346 virtual octave_uint8 uint8_scalar_value (void) const; |
4910
|
347 |
5759
|
348 virtual octave_uint16 uint16_scalar_value (void) const; |
4910
|
349 |
5759
|
350 virtual octave_uint32 uint32_scalar_value (void) const; |
4910
|
351 |
5759
|
352 virtual octave_uint64 uint64_scalar_value (void) const; |
4910
|
353 |
5759
|
354 virtual int8NDArray int8_array_value (void) const; |
4910
|
355 |
5759
|
356 virtual int16NDArray int16_array_value (void) const; |
4910
|
357 |
5759
|
358 virtual int32NDArray int32_array_value (void) const; |
4906
|
359 |
5759
|
360 virtual int64NDArray int64_array_value (void) const; |
4906
|
361 |
5759
|
362 virtual uint8NDArray uint8_array_value (void) const; |
4906
|
363 |
5759
|
364 virtual uint16NDArray uint16_array_value (void) const; |
4906
|
365 |
5759
|
366 virtual uint32NDArray uint32_array_value (void) const; |
4906
|
367 |
5759
|
368 virtual uint64NDArray uint64_array_value (void) const; |
4906
|
369 |
5759
|
370 virtual string_vector all_strings (bool pad = false) const; |
4906
|
371 |
5759
|
372 virtual std::string string_value (bool force = false) const; |
4906
|
373 |
5759
|
374 virtual Range range_value (void) const; |
2376
|
375 |
5759
|
376 virtual Octave_map map_value (void) const; |
2376
|
377 |
5759
|
378 virtual string_vector map_keys (void) const; |
2376
|
379 |
5759
|
380 virtual std::streamoff streamoff_value (void) const; |
2376
|
381 |
5759
|
382 virtual streamoff_array streamoff_array_value (void) const; |
|
383 |
|
384 virtual octave_function *function_value (bool silent = false); |
3933
|
385 |
5759
|
386 virtual octave_user_function *user_function_value (bool silent = false); |
4645
|
387 |
5759
|
388 virtual octave_fcn_handle *fcn_handle_value (bool silent = false); |
4643
|
389 |
5759
|
390 virtual octave_fcn_inline *fcn_inline_value (bool silent = false); |
2974
|
391 |
5759
|
392 virtual octave_value_list list_value (void) const; |
4343
|
393 |
5759
|
394 virtual octave_value convert_to_str (bool pad = false, bool force = false, |
|
395 char type = '"') const; |
|
396 virtual octave_value |
|
397 convert_to_str_internal (bool pad, bool force, char type) const; |
4933
|
398 |
5759
|
399 virtual void convert_to_row_or_column_vector (void); |
2882
|
400 |
5759
|
401 virtual bool print_as_scalar (void) const { return false; } |
2376
|
402 |
5759
|
403 virtual void print (std::ostream& os, bool pr_as_read_syntax = false) const; |
2376
|
404 |
5759
|
405 virtual void |
|
406 print_raw (std::ostream& os, bool pr_as_read_syntax = false) const; |
4604
|
407 |
5759
|
408 virtual bool |
|
409 print_name_tag (std::ostream& os, const std::string& name) const; |
2901
|
410 |
5759
|
411 virtual void |
|
412 print_with_name (std::ostream& output_buf, const std::string& name, |
|
413 bool print_padding = true) const; |
2376
|
414 |
5759
|
415 virtual void print_info (std::ostream& os, const std::string& prefix) const; |
3933
|
416 |
5759
|
417 virtual bool save_ascii (std::ostream& os, bool& infnan_warned, |
5956
|
418 int strip_nan_and_inf); |
4687
|
419 |
5759
|
420 virtual bool load_ascii (std::istream& is); |
4687
|
421 |
5759
|
422 virtual bool save_binary (std::ostream& os, bool& save_as_floats); |
4687
|
423 |
5759
|
424 virtual bool load_binary (std::istream& is, bool swap, |
|
425 oct_mach_info::float_format fmt); |
4687
|
426 |
|
427 #if defined (HAVE_HDF5) |
5759
|
428 virtual bool |
|
429 save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats); |
4687
|
430 |
5759
|
431 virtual bool |
|
432 load_hdf5 (hid_t loc_id, const char *name, bool have_h5giterate_bug); |
4687
|
433 #endif |
|
434 |
5759
|
435 virtual int |
|
436 write (octave_stream& os, int block_size, |
|
437 oct_data_conv::data_type output_type, int skip, |
|
438 oct_mach_info::float_format flt_fmt) const; |
|
439 |
5900
|
440 virtual void *mex_get_data (void) const { return 0; } |
|
441 |
|
442 virtual octave_idx_type *mex_get_ir (void) const { return 0; } |
|
443 |
|
444 virtual octave_idx_type *mex_get_jc (void) const { return 0; } |
|
445 |
|
446 virtual mxArray *as_mxArray (void) const; |
|
447 |
5759
|
448 protected: |
|
449 |
|
450 // This should only be called for derived types. |
|
451 |
|
452 octave_value numeric_assign (const std::string& type, |
|
453 const std::list<octave_value_list>& idx, |
|
454 const octave_value& rhs); |
|
455 |
|
456 void reset_indent_level (void) const |
|
457 { curr_print_indent_level = 0; } |
|
458 |
|
459 void increment_indent_level (void) const |
|
460 { curr_print_indent_level += 2; } |
|
461 |
|
462 void decrement_indent_level (void) const |
|
463 { curr_print_indent_level -= 2; } |
|
464 |
|
465 int current_print_indent_level (void) const |
|
466 { return curr_print_indent_level; } |
|
467 |
|
468 void indent (std::ostream& os) const; |
|
469 |
|
470 void newline (std::ostream& os) const; |
|
471 |
|
472 void reset (void) const; |
|
473 |
|
474 // A reference count. |
|
475 int count; |
4944
|
476 |
2376
|
477 private: |
|
478 |
5759
|
479 static int curr_print_indent_level; |
|
480 static bool beginning_of_line; |
|
481 |
|
482 DECLARE_OV_BASE_TYPEID_FUNCTIONS_AND_DATA |
2376
|
483 }; |
|
484 |
|
485 #endif |
|
486 |
|
487 /* |
|
488 ;;; Local Variables: *** |
|
489 ;;; mode: C++ *** |
|
490 ;;; End: *** |
|
491 */ |