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 |
6109
|
101 OCTINTERP_API |
5759
|
102 octave_base_value |
2376
|
103 { |
|
104 public: |
|
105 |
5759
|
106 typedef octave_base_value * (*type_conv_fcn) (const octave_base_value&); |
|
107 |
|
108 friend class octave_value; |
2376
|
109 |
5759
|
110 octave_base_value (void) : count (1) { } |
|
111 |
|
112 octave_base_value (const octave_base_value&) { } |
2376
|
113 |
5759
|
114 virtual ~octave_base_value (void) { } |
|
115 |
|
116 virtual octave_base_value * |
|
117 clone (void) const { return new octave_base_value (*this); } |
2376
|
118 |
5759
|
119 virtual octave_base_value * |
|
120 empty_clone (void) const { return new octave_base_value (); } |
2376
|
121 |
5759
|
122 virtual type_conv_fcn |
|
123 numeric_conversion_function (void) const |
2800
|
124 { return static_cast<type_conv_fcn> (0); } |
2410
|
125 |
5759
|
126 virtual octave_value squeeze (void) const; |
|
127 |
|
128 virtual octave_base_value *try_narrowing_conversion (void) { return 0; } |
4532
|
129 |
5759
|
130 virtual octave_value |
|
131 subsref (const std::string& type, |
|
132 const std::list<octave_value_list>& idx); |
3933
|
133 |
5759
|
134 virtual octave_value_list |
|
135 subsref (const std::string& type, |
|
136 const std::list<octave_value_list>& idx, |
|
137 int nargout); |
3933
|
138 |
5759
|
139 virtual octave_value |
5885
|
140 do_index_op (const octave_value_list& idx, bool resize_ok = false); |
2974
|
141 |
5759
|
142 virtual octave_value_list |
3544
|
143 do_multi_index_op (int nargout, const octave_value_list& idx); |
2376
|
144 |
5759
|
145 virtual octave_value |
|
146 subsasgn (const std::string& type, |
|
147 const std::list<octave_value_list>& idx, |
|
148 const octave_value& rhs); |
2376
|
149 |
5759
|
150 virtual idx_vector index_vector (void) const; |
2376
|
151 |
5759
|
152 virtual dim_vector dims (void) const { return dim_vector (-1, -1); } |
4559
|
153 |
5759
|
154 octave_idx_type rows (void) const |
|
155 { |
|
156 dim_vector dv = dims (); |
5164
|
157 |
5759
|
158 return (dv.length () > 0) ? dv(0) : -1; |
|
159 } |
4791
|
160 |
5759
|
161 octave_idx_type columns (void) const |
|
162 { |
|
163 dim_vector dv = dims (); |
5602
|
164 |
5759
|
165 return (dv.length () > 1) ? dv(1) : -1; |
|
166 } |
5604
|
167 |
5759
|
168 virtual int ndims (void) const; |
4567
|
169 |
5759
|
170 virtual octave_idx_type numel (void) const { return dims ().numel (); } |
4593
|
171 |
5759
|
172 virtual octave_idx_type capacity (void) const { return numel (); } |
|
173 |
|
174 virtual size_t byte_size (void) const { return 0; } |
4915
|
175 |
5759
|
176 virtual octave_idx_type nnz (void) const; |
2376
|
177 |
5759
|
178 virtual octave_idx_type nzmax (void) const; |
|
179 |
5900
|
180 virtual octave_idx_type nfields (void) const; |
|
181 |
5759
|
182 virtual octave_value reshape (const dim_vector&) const; |
3351
|
183 |
5759
|
184 virtual octave_value permute (const Array<int>& vec, bool = false) const; |
2376
|
185 |
5759
|
186 virtual octave_value resize (const dim_vector&, bool fill = false) const; |
2376
|
187 |
5785
|
188 virtual MatrixType matrix_type (void) const; |
|
189 |
|
190 virtual MatrixType matrix_type (const MatrixType& typ) const; |
|
191 |
5759
|
192 virtual bool is_defined (void) const { return false; } |
4505
|
193 |
5759
|
194 bool is_empty (void) const { return numel () == 0; } |
2376
|
195 |
5759
|
196 virtual bool is_cell (void) const { return false; } |
2376
|
197 |
6116
|
198 virtual bool is_cellstr (void) const { return false; } |
|
199 |
5759
|
200 virtual bool is_real_scalar (void) const { return false; } |
4587
|
201 |
5759
|
202 virtual bool is_real_matrix (void) const { return false; } |
|
203 |
|
204 virtual bool is_real_nd_array (void) const { return false; } |
2376
|
205 |
5759
|
206 virtual bool is_complex_scalar (void) const { return false; } |
2376
|
207 |
5759
|
208 virtual bool is_complex_matrix (void) const { return false; } |
|
209 |
5881
|
210 virtual bool is_bool_scalar (void) const { return false; } |
|
211 |
5759
|
212 virtual bool is_bool_matrix (void) const { return false; } |
5279
|
213 |
5759
|
214 virtual bool is_char_matrix (void) const { return false; } |
2376
|
215 |
5759
|
216 virtual bool is_string (void) const { return false; } |
2376
|
217 |
5759
|
218 virtual bool is_sq_string (void) const { return false; } |
4643
|
219 |
5759
|
220 virtual bool is_range (void) const { return false; } |
3977
|
221 |
5759
|
222 virtual bool is_map (void) const { return false; } |
|
223 |
|
224 virtual bool is_streamoff (void) const { return false; } |
2882
|
225 |
5759
|
226 virtual bool is_cs_list (void) const { return false; } |
2376
|
227 |
5759
|
228 virtual bool is_list (void) const { return false; } |
|
229 |
|
230 virtual bool is_magic_colon (void) const { return false; } |
2376
|
231 |
5759
|
232 virtual bool is_all_va_args (void) const { return false; } |
2376
|
233 |
5759
|
234 virtual octave_value all (int = 0) const; |
|
235 |
|
236 virtual octave_value any (int = 0) const; |
2376
|
237 |
5895
|
238 virtual bool is_double_type (void) const { return false; } |
|
239 |
|
240 virtual bool is_single_type (void) const { return false; } |
|
241 |
|
242 virtual bool is_int8_type (void) const { return false; } |
|
243 |
|
244 virtual bool is_int16_type (void) const { return false; } |
|
245 |
|
246 virtual bool is_int32_type (void) const { return false; } |
|
247 |
|
248 virtual bool is_int64_type (void) const { return false; } |
|
249 |
|
250 virtual bool is_uint8_type (void) const { return false; } |
|
251 |
|
252 virtual bool is_uint16_type (void) const { return false; } |
|
253 |
|
254 virtual bool is_uint32_type (void) const { return false; } |
|
255 |
|
256 virtual bool is_uint64_type (void) const { return false; } |
|
257 |
5759
|
258 virtual bool is_bool_type (void) const { return false; } |
3209
|
259 |
5759
|
260 virtual bool is_real_type (void) const { return false; } |
2376
|
261 |
5759
|
262 virtual bool is_complex_type (void) const { return false; } |
2376
|
263 |
|
264 // Would be nice to get rid of the next four functions: |
|
265 |
5759
|
266 virtual bool is_scalar_type (void) const { return false; } |
2376
|
267 |
5759
|
268 virtual bool is_matrix_type (void) const { return false; } |
2376
|
269 |
5759
|
270 virtual bool is_numeric_type (void) const { return false; } |
2376
|
271 |
5759
|
272 virtual bool is_sparse_type (void) const { return false; } |
5631
|
273 |
5759
|
274 virtual bool valid_as_scalar_index (void) const { return false; } |
2376
|
275 |
5759
|
276 virtual bool valid_as_zero_index (void) const { return false; } |
2376
|
277 |
5759
|
278 virtual bool is_true (void) const { return false; } |
|
279 |
|
280 virtual bool is_constant (void) const { return false; } |
2376
|
281 |
5759
|
282 virtual bool is_function_handle (void) const { return false; } |
2376
|
283 |
5759
|
284 virtual bool is_inline_function (void) const { return false; } |
2974
|
285 |
5759
|
286 virtual bool is_function (void) const { return false; } |
4954
|
287 |
5759
|
288 virtual bool is_builtin_function (void) const { return false; } |
2974
|
289 |
5759
|
290 virtual bool is_dld_function (void) const { return false; } |
3325
|
291 |
5864
|
292 virtual bool is_mex_function (void) const { return false; } |
|
293 |
5759
|
294 virtual short int short_value (bool = false, bool = false) const; |
4254
|
295 |
5759
|
296 virtual unsigned short int ushort_value (bool = false, bool = false) const; |
4254
|
297 |
5759
|
298 virtual int int_value (bool = false, bool = false) const; |
3202
|
299 |
5759
|
300 virtual unsigned int uint_value (bool = false, bool = false) const; |
4254
|
301 |
5759
|
302 virtual int nint_value (bool = false) const; |
3202
|
303 |
5759
|
304 virtual long int long_value (bool = false, bool = false) const; |
4254
|
305 |
5759
|
306 virtual unsigned long int ulong_value (bool = false, bool = false) const; |
4254
|
307 |
5759
|
308 virtual double double_value (bool = false) const; |
2376
|
309 |
5759
|
310 virtual double scalar_value (bool frc_str_conv = false) const |
3145
|
311 { return double_value (frc_str_conv); } |
2376
|
312 |
5759
|
313 virtual Cell cell_value (void) const; |
3351
|
314 |
5759
|
315 virtual Matrix matrix_value (bool = false) const; |
3145
|
316 |
5759
|
317 virtual NDArray array_value (bool = false) const; |
4505
|
318 |
5759
|
319 virtual Complex complex_value (bool = false) const; |
2376
|
320 |
5759
|
321 virtual ComplexMatrix complex_matrix_value (bool = false) const; |
2376
|
322 |
5759
|
323 virtual ComplexNDArray complex_array_value (bool = false) const; |
4550
|
324 |
5943
|
325 virtual bool bool_value (bool = false) const; |
4550
|
326 |
5943
|
327 virtual boolMatrix bool_matrix_value (bool = false) const; |
4550
|
328 |
5943
|
329 virtual boolNDArray bool_array_value (bool = false) const; |
4550
|
330 |
5759
|
331 virtual charMatrix char_matrix_value (bool force = false) const; |
|
332 |
|
333 virtual charNDArray char_array_value (bool = false) const; |
2376
|
334 |
5759
|
335 virtual SparseMatrix sparse_matrix_value (bool = false) const; |
4550
|
336 |
5759
|
337 virtual SparseComplexMatrix sparse_complex_matrix_value (bool = false) const; |
5164
|
338 |
5759
|
339 virtual SparseBoolMatrix sparse_bool_matrix_value (bool = false) const; |
5164
|
340 |
5759
|
341 virtual octave_int8 int8_scalar_value (void) const; |
5164
|
342 |
5759
|
343 virtual octave_int16 int16_scalar_value (void) const; |
4910
|
344 |
5759
|
345 virtual octave_int32 int32_scalar_value (void) const; |
|
346 |
|
347 virtual octave_int64 int64_scalar_value (void) const; |
4910
|
348 |
5759
|
349 virtual octave_uint8 uint8_scalar_value (void) const; |
4910
|
350 |
5759
|
351 virtual octave_uint16 uint16_scalar_value (void) const; |
4910
|
352 |
5759
|
353 virtual octave_uint32 uint32_scalar_value (void) const; |
4910
|
354 |
5759
|
355 virtual octave_uint64 uint64_scalar_value (void) const; |
4910
|
356 |
5759
|
357 virtual int8NDArray int8_array_value (void) const; |
4910
|
358 |
5759
|
359 virtual int16NDArray int16_array_value (void) const; |
4910
|
360 |
5759
|
361 virtual int32NDArray int32_array_value (void) const; |
4906
|
362 |
5759
|
363 virtual int64NDArray int64_array_value (void) const; |
4906
|
364 |
5759
|
365 virtual uint8NDArray uint8_array_value (void) const; |
4906
|
366 |
5759
|
367 virtual uint16NDArray uint16_array_value (void) const; |
4906
|
368 |
5759
|
369 virtual uint32NDArray uint32_array_value (void) const; |
4906
|
370 |
5759
|
371 virtual uint64NDArray uint64_array_value (void) const; |
4906
|
372 |
5759
|
373 virtual string_vector all_strings (bool pad = false) const; |
4906
|
374 |
5759
|
375 virtual std::string string_value (bool force = false) const; |
4906
|
376 |
5759
|
377 virtual Range range_value (void) const; |
2376
|
378 |
5759
|
379 virtual Octave_map map_value (void) const; |
2376
|
380 |
5759
|
381 virtual string_vector map_keys (void) const; |
2376
|
382 |
5759
|
383 virtual std::streamoff streamoff_value (void) const; |
2376
|
384 |
5759
|
385 virtual streamoff_array streamoff_array_value (void) const; |
|
386 |
|
387 virtual octave_function *function_value (bool silent = false); |
3933
|
388 |
5759
|
389 virtual octave_user_function *user_function_value (bool silent = false); |
4645
|
390 |
5759
|
391 virtual octave_fcn_handle *fcn_handle_value (bool silent = false); |
4643
|
392 |
5759
|
393 virtual octave_fcn_inline *fcn_inline_value (bool silent = false); |
2974
|
394 |
5759
|
395 virtual octave_value_list list_value (void) const; |
4343
|
396 |
5759
|
397 virtual octave_value convert_to_str (bool pad = false, bool force = false, |
|
398 char type = '"') const; |
|
399 virtual octave_value |
|
400 convert_to_str_internal (bool pad, bool force, char type) const; |
4933
|
401 |
5759
|
402 virtual void convert_to_row_or_column_vector (void); |
2882
|
403 |
5759
|
404 virtual bool print_as_scalar (void) const { return false; } |
2376
|
405 |
5759
|
406 virtual void print (std::ostream& os, bool pr_as_read_syntax = false) const; |
2376
|
407 |
5759
|
408 virtual void |
|
409 print_raw (std::ostream& os, bool pr_as_read_syntax = false) const; |
4604
|
410 |
5759
|
411 virtual bool |
|
412 print_name_tag (std::ostream& os, const std::string& name) const; |
2901
|
413 |
5759
|
414 virtual void |
|
415 print_with_name (std::ostream& output_buf, const std::string& name, |
|
416 bool print_padding = true) const; |
2376
|
417 |
5759
|
418 virtual void print_info (std::ostream& os, const std::string& prefix) const; |
3933
|
419 |
5958
|
420 virtual bool save_ascii (std::ostream& os, bool& infnan_warned); |
4687
|
421 |
5759
|
422 virtual bool load_ascii (std::istream& is); |
4687
|
423 |
5759
|
424 virtual bool save_binary (std::ostream& os, bool& save_as_floats); |
4687
|
425 |
5759
|
426 virtual bool load_binary (std::istream& is, bool swap, |
|
427 oct_mach_info::float_format fmt); |
4687
|
428 |
|
429 #if defined (HAVE_HDF5) |
5759
|
430 virtual bool |
|
431 save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats); |
4687
|
432 |
5759
|
433 virtual bool |
|
434 load_hdf5 (hid_t loc_id, const char *name, bool have_h5giterate_bug); |
4687
|
435 #endif |
|
436 |
5759
|
437 virtual int |
|
438 write (octave_stream& os, int block_size, |
|
439 oct_data_conv::data_type output_type, int skip, |
|
440 oct_mach_info::float_format flt_fmt) const; |
|
441 |
5900
|
442 virtual void *mex_get_data (void) const { return 0; } |
|
443 |
|
444 virtual octave_idx_type *mex_get_ir (void) const { return 0; } |
|
445 |
|
446 virtual octave_idx_type *mex_get_jc (void) const { return 0; } |
|
447 |
|
448 virtual mxArray *as_mxArray (void) const; |
|
449 |
5759
|
450 protected: |
|
451 |
|
452 // This should only be called for derived types. |
|
453 |
|
454 octave_value numeric_assign (const std::string& type, |
|
455 const std::list<octave_value_list>& idx, |
|
456 const octave_value& rhs); |
|
457 |
|
458 void reset_indent_level (void) const |
|
459 { curr_print_indent_level = 0; } |
|
460 |
|
461 void increment_indent_level (void) const |
|
462 { curr_print_indent_level += 2; } |
|
463 |
|
464 void decrement_indent_level (void) const |
|
465 { curr_print_indent_level -= 2; } |
|
466 |
|
467 int current_print_indent_level (void) const |
|
468 { return curr_print_indent_level; } |
|
469 |
|
470 void indent (std::ostream& os) const; |
|
471 |
|
472 void newline (std::ostream& os) const; |
|
473 |
|
474 void reset (void) const; |
|
475 |
|
476 // A reference count. |
|
477 int count; |
4944
|
478 |
2376
|
479 private: |
|
480 |
5759
|
481 static int curr_print_indent_level; |
|
482 static bool beginning_of_line; |
|
483 |
|
484 DECLARE_OV_BASE_TYPEID_FUNCTIONS_AND_DATA |
2376
|
485 }; |
|
486 |
|
487 #endif |
|
488 |
|
489 /* |
|
490 ;;; Local Variables: *** |
|
491 ;;; mode: C++ *** |
|
492 ;;; End: *** |
|
493 */ |