comparison src/ov-base.h @ 5759:ce79d238f142

[project @ 2006-04-13 13:04:32 by jwe]
author jwe
date Thu, 13 Apr 2006 13:04:33 +0000
parents c7d5a534afa5
children faafc2d98b8d
comparison
equal deleted inserted replaced
5758:65208d959bf1 5759:ce79d238f142
25 #define octave_base_value_h 1 25 #define octave_base_value_h 1
26 26
27 #include <cstdlib> 27 #include <cstdlib>
28 28
29 #include <iostream> 29 #include <iostream>
30 #include <list>
30 #include <string> 31 #include <string>
31 32
33 #if defined (HAVE_HDF5)
34 #include <hdf5.h>
35 #endif
36
37 #include "Range.h"
32 #include "mx-base.h" 38 #include "mx-base.h"
33 #include "str-vec.h" 39 #include "str-vec.h"
34 40
35 #include "error.h" 41 #include "error.h"
36 #include "ov.h"
37 #include "ov-typeinfo.h"
38 42
39 class Cell; 43 class Cell;
44 class streamoff_array;
40 class Octave_map; 45 class Octave_map;
46 class octave_value;
41 class octave_value_list; 47 class octave_value_list;
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;
42 56
43 class tree_walker; 57 class tree_walker;
58
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;
44 101
45 // A base value type, so that derived types only have to redefine what 102 // A base value type, so that derived types only have to redefine what
46 // they need (if they are derived from octave_base_value instead of 103 // they need (if they are derived from octave_base_value instead of
47 // octave_value). 104 // octave_value).
48 105
49 class 106 class
50 octave_base_value : public octave_value 107 octave_base_value
51 { 108 {
52 public: 109 public:
53 110
54 octave_base_value (void) 111 typedef octave_base_value * (*type_conv_fcn) (const octave_base_value&);
55 : octave_value (octave_xvalue ()) { } 112
56 113 friend class octave_value;
57 octave_base_value (const octave_base_value&) 114
58 : octave_value (octave_xvalue ()) { } 115 octave_base_value (void) : count (1) { }
59 116
60 ~octave_base_value (void) { } 117 octave_base_value (const octave_base_value&) { }
61 118
62 octave_value *clone (void) const { return new octave_base_value (*this); } 119 virtual ~octave_base_value (void) { }
63 octave_value *empty_clone (void) const { return new octave_base_value (); } 120
64 121 virtual octave_base_value *
65 type_conv_fcn numeric_conversion_function (void) const 122 clone (void) const { return new octave_base_value (*this); }
123
124 virtual octave_base_value *
125 empty_clone (void) const { return new octave_base_value (); }
126
127 virtual type_conv_fcn
128 numeric_conversion_function (void) const
66 { return static_cast<type_conv_fcn> (0); } 129 { return static_cast<type_conv_fcn> (0); }
67 130
68 octave_value squeeze (void) const; 131 virtual octave_value squeeze (void) const;
69 132
70 octave_value *try_narrowing_conversion (void) 133 virtual octave_base_value *try_narrowing_conversion (void) { return 0; }
71 { return static_cast<octave_value *> (0); } 134
72 135 virtual octave_value
73 octave_value subsref (const std::string& type, 136 subsref (const std::string& type,
74 const std::list<octave_value_list>& idx); 137 const std::list<octave_value_list>& idx);
75 138
76 octave_value_list subsref (const std::string& type, 139 virtual octave_value_list
77 const std::list<octave_value_list>& idx, 140 subsref (const std::string& type,
78 int nargout); 141 const std::list<octave_value_list>& idx,
79 142 int nargout);
80 octave_value do_index_op (const octave_value_list& idx, int resize_ok); 143
81 144 virtual octave_value
82 octave_value do_index_op (const octave_value_list& idx) 145 do_index_op (const octave_value_list& idx, int resize_ok);
83 { return do_index_op (idx, 0); } 146
84 147 virtual octave_value
85 octave_value_list 148 do_index_op (const octave_value_list& idx);
149
150 virtual octave_value_list
86 do_multi_index_op (int nargout, const octave_value_list& idx); 151 do_multi_index_op (int nargout, const octave_value_list& idx);
87 152
88 idx_vector index_vector (void) const; 153 virtual octave_value
89 154 subsasgn (const std::string& type,
90 octave_value subsasgn (const std::string& type, 155 const std::list<octave_value_list>& idx,
91 const std::list<octave_value_list>& idx, 156 const octave_value& rhs);
92 const octave_value& rhs); 157
93 158 virtual idx_vector index_vector (void) const;
94 dim_vector dims (void) const { return dim_vector (-1, -1); } 159
95 160 virtual dim_vector dims (void) const { return dim_vector (-1, -1); }
96 octave_idx_type numel (void) const { return dims ().numel (); } 161
97 162 octave_idx_type rows (void) const
98 octave_idx_type capacity (void) const { return numel (); } 163 {
99 164 dim_vector dv = dims ();
100 size_t byte_size (void) const { return 0; } 165
101 166 return (dv.length () > 0) ? dv(0) : -1;
102 octave_idx_type nnz (void) const; 167 }
103 168
104 octave_idx_type nzmax (void) const; 169 octave_idx_type columns (void) const
105 170 {
106 octave_value reshape (const dim_vector&) const; 171 dim_vector dv = dims ();
107 172
108 octave_value permute (const Array<int>& vec, bool = false) const; 173 return (dv.length () > 1) ? dv(1) : -1;
109 174 }
110 octave_value resize (const dim_vector&, bool fill = false) const; 175
111 176 virtual int ndims (void) const;
112 bool is_defined (void) const { return false; } 177
113 178 virtual octave_idx_type numel (void) const { return dims ().numel (); }
114 bool is_cell (void) const { return false; } 179
115 180 virtual octave_idx_type capacity (void) const { return numel (); }
116 bool is_real_scalar (void) const { return false; } 181
117 182 virtual size_t byte_size (void) const { return 0; }
118 bool is_real_matrix (void) const { return false; } 183
119 184 virtual octave_idx_type nnz (void) const;
120 bool is_real_nd_array (void) const { return false; } 185
121 186 virtual octave_idx_type nzmax (void) const;
122 bool is_complex_scalar (void) const { return false; } 187
123 188 virtual octave_value reshape (const dim_vector&) const;
124 bool is_complex_matrix (void) const { return false; } 189
125 190 virtual octave_value permute (const Array<int>& vec, bool = false) const;
126 bool is_bool_matrix (void) const { return false; } 191
127 192 virtual octave_value resize (const dim_vector&, bool fill = false) const;
128 bool is_char_matrix (void) const { return false; } 193
129 194 virtual bool is_defined (void) const { return false; }
130 bool is_string (void) const { return false; } 195
131 196 bool is_empty (void) const { return numel () == 0; }
132 bool is_sq_string (void) const { return false; } 197
133 198 virtual bool is_cell (void) const { return false; }
134 bool is_range (void) const { return false; } 199
135 200 virtual bool is_real_scalar (void) const { return false; }
136 bool is_map (void) const { return false; } 201
137 202 virtual bool is_real_matrix (void) const { return false; }
138 bool is_streamoff (void) const { return false; } 203
139 204 virtual bool is_real_nd_array (void) const { return false; }
140 bool is_cs_list (void) const { return false; } 205
141 206 virtual bool is_complex_scalar (void) const { return false; }
142 bool is_list (void) const { return false; } 207
143 208 virtual bool is_complex_matrix (void) const { return false; }
144 bool is_magic_colon (void) const { return false; } 209
145 210 virtual bool is_bool_matrix (void) const { return false; }
146 bool is_all_va_args (void) const { return false; } 211
147 212 virtual bool is_char_matrix (void) const { return false; }
148 octave_value all (int = 0) const { return 0.0; } 213
149 214 virtual bool is_string (void) const { return false; }
150 octave_value any (int = 0) const { return 0.0; } 215
151 216 virtual bool is_sq_string (void) const { return false; }
152 bool is_bool_type (void) const { return false; } 217
153 218 virtual bool is_range (void) const { return false; }
154 bool is_real_type (void) const { return false; } 219
155 220 virtual bool is_map (void) const { return false; }
156 bool is_complex_type (void) const { return false; } 221
222 virtual bool is_streamoff (void) const { return false; }
223
224 virtual bool is_cs_list (void) const { return false; }
225
226 virtual bool is_list (void) const { return false; }
227
228 virtual bool is_magic_colon (void) const { return false; }
229
230 virtual bool is_all_va_args (void) const { return false; }
231
232 virtual octave_value all (int = 0) const;
233
234 virtual octave_value any (int = 0) const;
235
236 virtual bool is_bool_type (void) const { return false; }
237
238 virtual bool is_real_type (void) const { return false; }
239
240 virtual bool is_complex_type (void) const { return false; }
157 241
158 // Would be nice to get rid of the next four functions: 242 // Would be nice to get rid of the next four functions:
159 243
160 bool is_scalar_type (void) const { return false; } 244 virtual bool is_scalar_type (void) const { return false; }
161 245
162 bool is_matrix_type (void) const { return false; } 246 virtual bool is_matrix_type (void) const { return false; }
163 247
164 bool is_numeric_type (void) const { return false; } 248 virtual bool is_numeric_type (void) const { return false; }
165 249
166 bool is_sparse_type (void) const { return false; } 250 virtual bool is_sparse_type (void) const { return false; }
167 251
168 bool valid_as_scalar_index (void) const { return false; } 252 virtual bool valid_as_scalar_index (void) const { return false; }
169 253
170 bool valid_as_zero_index (void) const { return false; } 254 virtual bool valid_as_zero_index (void) const { return false; }
171 255
172 bool is_true (void) const { return false; } 256 virtual bool is_true (void) const { return false; }
173 257
174 bool is_zero_by_zero (void) const 258 virtual bool is_constant (void) const { return false; }
175 { return (rows () == 0 && columns () == 0); } 259
176 260 virtual bool is_function_handle (void) const { return false; }
177 bool is_constant (void) const { return false; } 261
178 262 virtual bool is_inline_function (void) const { return false; }
179 bool is_function_handle (void) const { return false; } 263
180 264 virtual bool is_function (void) const { return false; }
181 bool is_inline_function (void) const { return false; } 265
182 266 virtual bool is_builtin_function (void) const { return false; }
183 bool is_function (void) const { return false; } 267
184 268 virtual bool is_dld_function (void) const { return false; }
185 bool is_builtin_function (void) const { return false; } 269
186 270 virtual short int short_value (bool = false, bool = false) const;
187 bool is_dld_function (void) const { return false; } 271
188 272 virtual unsigned short int ushort_value (bool = false, bool = false) const;
189 short int short_value (bool = false, bool = false) const; 273
190 274 virtual int int_value (bool = false, bool = false) const;
191 unsigned short int ushort_value (bool = false, bool = false) const; 275
192 276 virtual unsigned int uint_value (bool = false, bool = false) const;
193 int int_value (bool = false, bool = false) const; 277
194 278 virtual int nint_value (bool = false) const;
195 unsigned int uint_value (bool = false, bool = false) const; 279
196 280 virtual long int long_value (bool = false, bool = false) const;
197 int nint_value (bool = false) const; 281
198 282 virtual unsigned long int ulong_value (bool = false, bool = false) const;
199 long int long_value (bool = false, bool = false) const; 283
200 284 virtual double double_value (bool = false) const;
201 unsigned long int ulong_value (bool = false, bool = false) const; 285
202 286 virtual double scalar_value (bool frc_str_conv = false) const
203 double double_value (bool = false) const;
204
205 double scalar_value (bool frc_str_conv = false) const
206 { return double_value (frc_str_conv); } 287 { return double_value (frc_str_conv); }
207 288
208 Cell cell_value (void) const; 289 virtual Cell cell_value (void) const;
209 290
210 Matrix matrix_value (bool = false) const; 291 virtual Matrix matrix_value (bool = false) const;
211 292
212 NDArray array_value (bool = false) const; 293 virtual NDArray array_value (bool = false) const;
213 294
214 Complex complex_value (bool = false) const; 295 virtual Complex complex_value (bool = false) const;
215 296
216 ComplexMatrix complex_matrix_value (bool = false) const; 297 virtual ComplexMatrix complex_matrix_value (bool = false) const;
217 298
218 ComplexNDArray complex_array_value (bool = false) const; 299 virtual ComplexNDArray complex_array_value (bool = false) const;
219 300
220 bool bool_value (void) const; 301 virtual bool bool_value (void) const;
221 302
222 boolMatrix bool_matrix_value (void) const; 303 virtual boolMatrix bool_matrix_value (void) const;
223 304
224 boolNDArray bool_array_value (void) const; 305 virtual boolNDArray bool_array_value (void) const;
225 306
226 charMatrix char_matrix_value (bool force = false) const; 307 virtual charMatrix char_matrix_value (bool force = false) const;
227 308
228 charNDArray char_array_value (bool = false) const; 309 virtual charNDArray char_array_value (bool = false) const;
229 310
230 SparseMatrix sparse_matrix_value (bool = false) const; 311 virtual SparseMatrix sparse_matrix_value (bool = false) const;
231 312
232 SparseComplexMatrix sparse_complex_matrix_value (bool = false) const; 313 virtual SparseComplexMatrix sparse_complex_matrix_value (bool = false) const;
233 314
234 SparseBoolMatrix sparse_bool_matrix_value (bool = false) const; 315 virtual SparseBoolMatrix sparse_bool_matrix_value (bool = false) const;
235 316
236 octave_int8 int8_scalar_value (void) const; 317 virtual octave_int8 int8_scalar_value (void) const;
237 318
238 octave_int16 int16_scalar_value (void) const; 319 virtual octave_int16 int16_scalar_value (void) const;
239 320
240 octave_int32 int32_scalar_value (void) const; 321 virtual octave_int32 int32_scalar_value (void) const;
241 322
242 octave_int64 int64_scalar_value (void) const; 323 virtual octave_int64 int64_scalar_value (void) const;
243 324
244 octave_uint8 uint8_scalar_value (void) const; 325 virtual octave_uint8 uint8_scalar_value (void) const;
245 326
246 octave_uint16 uint16_scalar_value (void) const; 327 virtual octave_uint16 uint16_scalar_value (void) const;
247 328
248 octave_uint32 uint32_scalar_value (void) const; 329 virtual octave_uint32 uint32_scalar_value (void) const;
249 330
250 octave_uint64 uint64_scalar_value (void) const; 331 virtual octave_uint64 uint64_scalar_value (void) const;
251 332
252 int8NDArray int8_array_value (void) const; 333 virtual int8NDArray int8_array_value (void) const;
253 334
254 int16NDArray int16_array_value (void) const; 335 virtual int16NDArray int16_array_value (void) const;
255 336
256 int32NDArray int32_array_value (void) const; 337 virtual int32NDArray int32_array_value (void) const;
257 338
258 int64NDArray int64_array_value (void) const; 339 virtual int64NDArray int64_array_value (void) const;
259 340
260 uint8NDArray uint8_array_value (void) const; 341 virtual uint8NDArray uint8_array_value (void) const;
261 342
262 uint16NDArray uint16_array_value (void) const; 343 virtual uint16NDArray uint16_array_value (void) const;
263 344
264 uint32NDArray uint32_array_value (void) const; 345 virtual uint32NDArray uint32_array_value (void) const;
265 346
266 uint64NDArray uint64_array_value (void) const; 347 virtual uint64NDArray uint64_array_value (void) const;
267 348
268 string_vector all_strings (bool pad = false) const; 349 virtual string_vector all_strings (bool pad = false) const;
269 350
270 std::string string_value (bool force = false) const; 351 virtual std::string string_value (bool force = false) const;
271 352
272 Range range_value (void) const; 353 virtual Range range_value (void) const;
273 354
274 Octave_map map_value (void) const; 355 virtual Octave_map map_value (void) const;
275 356
276 string_vector map_keys (void) const; 357 virtual string_vector map_keys (void) const;
277 358
278 std::streamoff streamoff_value (void) const; 359 virtual std::streamoff streamoff_value (void) const;
279 360
280 streamoff_array streamoff_array_value (void) const; 361 virtual streamoff_array streamoff_array_value (void) const;
281 362
282 octave_function *function_value (bool silent = false); 363 virtual octave_function *function_value (bool silent = false);
283 364
284 octave_user_function *user_function_value (bool silent = false); 365 virtual octave_user_function *user_function_value (bool silent = false);
285 366
286 octave_fcn_handle *fcn_handle_value (bool silent = false); 367 virtual octave_fcn_handle *fcn_handle_value (bool silent = false);
287 368
288 octave_fcn_inline *fcn_inline_value (bool silent = false); 369 virtual octave_fcn_inline *fcn_inline_value (bool silent = false);
289 370
290 octave_value_list list_value (void) const; 371 virtual octave_value_list list_value (void) const;
291 372
292 octave_value convert_to_str_internal (bool pad, bool force, char type) const; 373 virtual octave_value convert_to_str (bool pad = false, bool force = false,
293 374 char type = '"') const;
294 void convert_to_row_or_column_vector (void); 375 virtual octave_value
295 376 convert_to_str_internal (bool pad, bool force, char type) const;
296 bool print_as_scalar (void) const { return false; } 377
297 378 virtual void convert_to_row_or_column_vector (void);
298 void print (std::ostream& os, bool pr_as_read_syntax = false) const; 379
299 380 virtual bool print_as_scalar (void) const { return false; }
300 void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const; 381
301 382 virtual void print (std::ostream& os, bool pr_as_read_syntax = false) const;
302 bool print_name_tag (std::ostream& os, const std::string& name) const; 383
303 384 virtual void
304 void print_info (std::ostream& os, const std::string& prefix) const; 385 print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
305 386
306 bool save_ascii (std::ostream& os, bool& infnan_warned, 387 virtual bool
388 print_name_tag (std::ostream& os, const std::string& name) const;
389
390 virtual void
391 print_with_name (std::ostream& output_buf, const std::string& name,
392 bool print_padding = true) const;
393
394 virtual void print_info (std::ostream& os, const std::string& prefix) const;
395
396 virtual bool save_ascii (std::ostream& os, bool& infnan_warned,
307 bool strip_nan_and_inf); 397 bool strip_nan_and_inf);
308 398
309 bool load_ascii (std::istream& is); 399 virtual bool load_ascii (std::istream& is);
310 400
311 bool save_binary (std::ostream& os, bool& save_as_floats); 401 virtual bool save_binary (std::ostream& os, bool& save_as_floats);
312 402
313 bool load_binary (std::istream& is, bool swap, 403 virtual bool load_binary (std::istream& is, bool swap,
314 oct_mach_info::float_format fmt); 404 oct_mach_info::float_format fmt);
315 405
316 #if defined (HAVE_HDF5) 406 #if defined (HAVE_HDF5)
317 bool save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats); 407 virtual bool
318 408 save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats);
319 bool load_hdf5 (hid_t loc_id, const char *name, bool have_h5giterate_bug); 409
410 virtual bool
411 load_hdf5 (hid_t loc_id, const char *name, bool have_h5giterate_bug);
320 #endif 412 #endif
321 413
322 int write (octave_stream& os, int block_size, 414 virtual int
323 oct_data_conv::data_type output_type, int skip, 415 write (octave_stream& os, int block_size,
324 oct_mach_info::float_format flt_fmt) const; 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;
325 447
326 private: 448 private:
327 449
328 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA 450 static int curr_print_indent_level;
451 static bool beginning_of_line;
452
453 DECLARE_OV_BASE_TYPEID_FUNCTIONS_AND_DATA
329 }; 454 };
330 455
331 #endif 456 #endif
332 457
333 /* 458 /*