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