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