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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if !defined (octave_value_h) |
|
24 #define octave_value_h 1 |
|
25 |
|
26 #if defined (__GNUG__) |
|
27 #pragma interface |
|
28 #endif |
|
29 |
|
30 #include <cstdlib> |
|
31 |
|
32 #include <string> |
|
33 |
|
34 class ostream; |
|
35 |
|
36 #include "Range.h" |
|
37 #include "idx-vector.h" |
|
38 #include "mx-base.h" |
2477
|
39 #include "oct-alloc.h" |
2942
|
40 #include "str-vec.h" |
|
41 |
2376
|
42 class Octave_map; |
2903
|
43 class octave_stream; |
2974
|
44 class octave_function; |
2376
|
45 class octave_value_list; |
2979
|
46 class octave_lvalue; |
2376
|
47 |
|
48 // Constants. |
|
49 |
|
50 // This just provides a way to avoid infinite recursion when building |
|
51 // octave_value objects. |
|
52 |
|
53 class |
|
54 octave_xvalue |
|
55 { |
|
56 public: |
|
57 |
|
58 octave_xvalue (void) { } |
|
59 }; |
|
60 |
2427
|
61 class octave_value; |
|
62 |
|
63 // XXX FIXME XXX -- these should probably really be inside the scope |
|
64 // of the octave_value class, but the cygwin32 beta16 version of g++ |
|
65 // can't handlt that. |
|
66 |
|
67 typedef octave_value (*binary_op_fcn) |
|
68 (const octave_value&, const octave_value&); |
|
69 |
|
70 typedef octave_value (*assign_op_fcn) |
|
71 (octave_value&, const octave_value_list&, const octave_value&); |
|
72 |
|
73 typedef octave_value * (*type_conv_fcn) (const octave_value&); |
|
74 |
2376
|
75 class |
2974
|
76 octave_value |
2376
|
77 { |
|
78 public: |
|
79 |
|
80 enum binary_op |
|
81 { |
|
82 add, |
|
83 sub, |
|
84 mul, |
|
85 div, |
|
86 pow, |
|
87 ldiv, |
2903
|
88 lshift, |
|
89 rshift, |
2376
|
90 lt, |
|
91 le, |
|
92 eq, |
|
93 ge, |
|
94 gt, |
|
95 ne, |
|
96 el_mul, |
|
97 el_div, |
|
98 el_pow, |
|
99 el_ldiv, |
|
100 el_and, |
|
101 el_or, |
|
102 struct_ref, |
|
103 num_binary_ops, |
|
104 unknown_binary_op |
|
105 }; |
|
106 |
2880
|
107 enum assign_op |
|
108 { |
|
109 asn_eq, |
|
110 add_eq, |
|
111 sub_eq, |
|
112 mul_eq, |
|
113 div_eq, |
2903
|
114 lshift_eq, |
|
115 rshift_eq, |
2880
|
116 el_mul_eq, |
|
117 el_div_eq, |
|
118 el_and_eq, |
|
119 el_or_eq, |
|
120 num_assign_ops, |
|
121 unknown_assign_op |
|
122 }; |
|
123 |
2376
|
124 static string binary_op_as_string (binary_op); |
|
125 |
2880
|
126 static string assign_op_as_string (assign_op); |
|
127 |
2376
|
128 enum magic_colon { magic_colon_t }; |
|
129 enum all_va_args { all_va_args_t }; |
|
130 |
|
131 octave_value (void); |
|
132 octave_value (double d); |
|
133 octave_value (const Matrix& m); |
|
134 octave_value (const DiagMatrix& d); |
|
135 octave_value (const RowVector& v, int pcv = -1); |
|
136 octave_value (const ColumnVector& v, int pcv = -1); |
|
137 octave_value (const Complex& C); |
|
138 octave_value (const ComplexMatrix& m); |
|
139 octave_value (const ComplexDiagMatrix& d); |
|
140 octave_value (const ComplexRowVector& v, int pcv = -1); |
|
141 octave_value (const ComplexColumnVector& v, int pcv = -1); |
2825
|
142 octave_value (bool b); |
|
143 octave_value (const boolMatrix& bm); |
3189
|
144 octave_value (char c); |
2376
|
145 octave_value (const char *s); |
|
146 octave_value (const string& s); |
|
147 octave_value (const string_vector& s); |
|
148 octave_value (const charMatrix& chm, bool is_string = false); |
|
149 octave_value (double base, double limit, double inc); |
|
150 octave_value (const Range& r); |
|
151 octave_value (const Octave_map& m); |
2903
|
152 octave_value (octave_stream *s, int n); |
2974
|
153 octave_value (octave_function *f); |
2880
|
154 octave_value (const octave_value_list& m); |
2376
|
155 octave_value (octave_value::magic_colon); |
|
156 octave_value (octave_value::all_va_args); |
|
157 |
|
158 octave_value (octave_value *new_rep); |
|
159 |
|
160 // Copy constructor. |
|
161 |
|
162 octave_value (const octave_value& a) |
|
163 { |
|
164 rep = a.rep; |
|
165 rep->count++; |
|
166 } |
|
167 |
|
168 // Delete the representation of this constant if the count drops to |
|
169 // zero. |
|
170 |
|
171 virtual ~octave_value (void); |
|
172 |
|
173 // This should only be called for derived types. |
|
174 |
2880
|
175 virtual octave_value *clone (void); |
2376
|
176 |
|
177 void make_unique (void) |
|
178 { |
|
179 if (rep->count > 1) |
|
180 { |
|
181 --rep->count; |
|
182 rep = rep->clone (); |
|
183 rep->count = 1; |
|
184 } |
|
185 } |
|
186 |
2477
|
187 void *operator new (size_t size) |
|
188 { return allocator.alloc (size); } |
|
189 |
|
190 void operator delete (void *p, size_t size) |
|
191 { allocator.free (p, size); } |
2376
|
192 |
|
193 // Simple assignment. |
|
194 |
|
195 octave_value& operator = (const octave_value& a) |
|
196 { |
|
197 if (rep != a.rep) |
|
198 { |
|
199 if (--rep->count == 0) |
|
200 delete rep; |
|
201 |
|
202 rep = a.rep; |
|
203 rep->count++; |
|
204 } |
|
205 |
|
206 return *this; |
|
207 } |
|
208 |
2409
|
209 virtual type_conv_fcn numeric_conversion_function (void) const |
2376
|
210 { return rep->numeric_conversion_function (); } |
|
211 |
2409
|
212 void maybe_mutate (void); |
|
213 |
2410
|
214 virtual octave_value *try_narrowing_conversion (void) |
|
215 { return rep->try_narrowing_conversion (); } |
2409
|
216 |
2974
|
217 virtual octave_value do_index_op (const octave_value_list& idx) |
2963
|
218 { return rep->do_index_op (idx); } |
2376
|
219 |
2974
|
220 virtual octave_value_list |
|
221 do_index_op (int nargout, const octave_value_list& idx); |
|
222 |
2963
|
223 void assign (assign_op, const octave_value& rhs); |
2880
|
224 |
2963
|
225 void assign (assign_op, const octave_value_list& idx, |
|
226 const octave_value& rhs); |
2376
|
227 |
2948
|
228 virtual void |
|
229 assign_struct_elt (assign_op, const string& elt_nm, |
|
230 const octave_value& rhs); |
|
231 |
|
232 virtual void |
|
233 assign_struct_elt (assign_op, const string& elt_nm, |
|
234 const octave_value_list& idx, const octave_value& rhs); |
|
235 |
2376
|
236 virtual idx_vector index_vector (void) const |
|
237 { return rep->index_vector (); } |
|
238 |
2420
|
239 virtual octave_value |
2963
|
240 do_struct_elt_index_op (const string& nm, bool silent = false) |
|
241 { return rep->do_struct_elt_index_op (nm, silent); } |
|
242 |
|
243 virtual octave_value |
|
244 do_struct_elt_index_op (const string& nm, const octave_value_list& idx, |
|
245 bool silent = false) |
|
246 { return rep->do_struct_elt_index_op (nm, idx, silent); } |
2376
|
247 |
2979
|
248 octave_lvalue struct_elt_ref (const string& nm); |
2948
|
249 |
2979
|
250 virtual octave_lvalue |
2948
|
251 struct_elt_ref (octave_value *parent, const string& nm); |
2376
|
252 |
|
253 // Size. |
|
254 |
|
255 virtual int rows (void) const |
|
256 { return rep->rows (); } |
|
257 |
|
258 virtual int columns (void) const |
|
259 { return rep->columns (); } |
|
260 |
3195
|
261 virtual int length (void) const |
|
262 { return rep->length (); } |
|
263 |
2376
|
264 // Does this constant have a type? Both of these are provided since |
|
265 // it is sometimes more natural to write is_undefined() instead of |
|
266 // ! is_defined(). |
|
267 |
|
268 virtual bool is_defined (void) const |
|
269 { return rep->is_defined (); } |
|
270 |
|
271 bool is_undefined (void) const |
|
272 { return ! is_defined (); } |
|
273 |
|
274 virtual bool is_real_scalar (void) const |
|
275 { return rep->is_real_scalar (); } |
|
276 |
|
277 virtual bool is_real_matrix (void) const |
|
278 { return rep->is_real_matrix (); } |
|
279 |
|
280 virtual bool is_complex_scalar (void) const |
|
281 { return rep->is_complex_scalar (); } |
|
282 |
|
283 virtual bool is_complex_matrix (void) const |
|
284 { return rep->is_complex_matrix (); } |
|
285 |
|
286 virtual bool is_char_matrix (void) const |
|
287 { return rep->is_char_matrix (); } |
|
288 |
|
289 virtual bool is_string (void) const |
|
290 { return rep->is_string (); } |
|
291 |
|
292 virtual bool is_range (void) const |
|
293 { return rep->is_range (); } |
|
294 |
|
295 virtual bool is_map (void) const |
|
296 { return rep->is_map (); } |
|
297 |
2880
|
298 virtual bool is_list (void) const |
|
299 { return rep->is_list (); } |
|
300 |
2376
|
301 virtual bool is_magic_colon (void) const |
|
302 { return rep->is_magic_colon (); } |
|
303 |
|
304 virtual bool is_all_va_args (void) const |
|
305 { return rep->is_all_va_args (); } |
|
306 |
|
307 // Are any or all of the elements in this constant nonzero? |
|
308 |
|
309 virtual octave_value all (void) const |
|
310 { return rep->all (); } |
|
311 |
|
312 virtual octave_value any (void) const |
|
313 { return rep->any (); } |
|
314 |
|
315 // Other type stuff. |
|
316 |
|
317 virtual bool is_real_type (void) const |
|
318 { return rep->is_real_type (); } |
|
319 |
|
320 virtual bool is_complex_type (void) const |
|
321 { return rep->is_complex_type (); } |
|
322 |
|
323 virtual bool is_scalar_type (void) const |
|
324 { return rep->is_scalar_type (); } |
|
325 |
|
326 virtual bool is_matrix_type (void) const |
|
327 { return rep->is_matrix_type (); } |
|
328 |
|
329 virtual bool is_numeric_type (void) const |
|
330 { return rep->is_numeric_type (); } |
|
331 |
|
332 virtual bool valid_as_scalar_index (void) const |
|
333 { return rep->valid_as_scalar_index (); } |
|
334 |
|
335 virtual bool valid_as_zero_index (void) const |
|
336 { return rep->valid_as_zero_index (); } |
|
337 |
|
338 // Does this constant correspond to a truth value? |
|
339 |
|
340 virtual bool is_true (void) const |
|
341 { return rep->is_true (); } |
|
342 |
|
343 // Is at least one of the dimensions of this constant zero? |
|
344 |
|
345 virtual bool is_empty (void) const |
|
346 { return rep->is_empty (); } |
|
347 |
|
348 // Are the dimensions of this constant zero by zero? |
|
349 |
|
350 virtual bool is_zero_by_zero (void) const |
|
351 { return rep->is_zero_by_zero (); } |
|
352 |
2974
|
353 virtual bool is_constant (void) const |
|
354 { return rep->is_constant (); } |
|
355 |
|
356 virtual bool is_function (void) const |
|
357 { return rep->is_function (); } |
2891
|
358 |
2376
|
359 // Values. |
|
360 |
2891
|
361 octave_value eval (void) { return *this; } |
|
362 |
2376
|
363 virtual double double_value (bool frc_str_conv = false) const |
|
364 { return rep->double_value (frc_str_conv); } |
|
365 |
2916
|
366 virtual double scalar_value (bool frc_str_conv = false) const |
|
367 { return rep->scalar_value (frc_str_conv); } |
|
368 |
2376
|
369 virtual Matrix matrix_value (bool frc_str_conv = false) const |
|
370 { return rep->matrix_value (frc_str_conv); } |
|
371 |
|
372 virtual Complex complex_value (bool frc_str_conv = false) const |
|
373 { return rep->complex_value (frc_str_conv); } |
|
374 |
|
375 virtual ComplexMatrix complex_matrix_value (bool frc_str_conv = false) const |
|
376 { return rep->complex_matrix_value (frc_str_conv); } |
|
377 |
|
378 virtual charMatrix char_matrix_value (bool frc_str_conv = false) const |
|
379 { return rep->char_matrix_value (frc_str_conv); } |
|
380 |
2493
|
381 virtual string_vector all_strings (void) const |
2376
|
382 { return rep->all_strings (); } |
|
383 |
|
384 virtual string string_value (void) const |
|
385 { return rep->string_value (); } |
|
386 |
|
387 virtual Range range_value (void) const |
|
388 { return rep->range_value (); } |
|
389 |
|
390 virtual Octave_map map_value (void) const; |
|
391 |
2903
|
392 virtual octave_stream *stream_value (void) const; |
|
393 |
|
394 virtual int stream_number (void) const; |
|
395 |
2974
|
396 virtual octave_function *function_value (bool silent = false); |
|
397 |
2880
|
398 virtual octave_value_list list_value (void) const; |
|
399 |
2825
|
400 virtual bool bool_value (void) const |
|
401 { return rep->bool_value (); } |
|
402 |
|
403 virtual boolMatrix bool_matrix_value (void) const |
|
404 { return rep->bool_matrix_value (); } |
|
405 |
2376
|
406 // Unary ops. |
|
407 |
|
408 virtual octave_value not (void) const { return rep->not (); } |
|
409 |
|
410 virtual octave_value uminus (void) const { return rep->uminus (); } |
|
411 |
|
412 virtual octave_value transpose (void) const { return rep->transpose (); } |
|
413 |
|
414 virtual octave_value hermitian (void) const { return rep->hermitian (); } |
|
415 |
|
416 virtual void increment (void) |
|
417 { |
|
418 make_unique (); |
|
419 rep->increment (); |
|
420 } |
|
421 |
|
422 virtual void decrement (void) |
|
423 { |
|
424 make_unique (); |
|
425 rep->decrement (); |
|
426 } |
|
427 |
|
428 ColumnVector vector_value (bool frc_str_conv = false, |
|
429 bool frc_vec_conv = false) const; |
|
430 |
|
431 ComplexColumnVector |
|
432 complex_vector_value (bool frc_str_conv = false, |
|
433 bool frc_vec_conv = false) const; |
|
434 |
|
435 // Conversions. These should probably be private. If a user of this |
|
436 // class wants a certain kind of constant, he should simply ask for |
|
437 // it, and we should convert it if possible. |
|
438 |
|
439 virtual octave_value convert_to_str (void) const |
|
440 { return rep->convert_to_str (); } |
|
441 |
|
442 virtual void convert_to_row_or_column_vector (void) |
|
443 { rep->convert_to_row_or_column_vector (); } |
|
444 |
2903
|
445 virtual void print (ostream& os, bool pr_as_read_syntax = false) const |
2466
|
446 { rep->print (os, pr_as_read_syntax); } |
2376
|
447 |
2903
|
448 virtual void print_raw (ostream& os, bool pr_as_read_syntax = false) const |
|
449 { rep->print_raw (os, pr_as_read_syntax); } |
|
450 |
|
451 virtual bool print_name_tag (ostream& os, const string& name) const |
|
452 { return rep->print_name_tag (os, name); } |
2376
|
453 |
|
454 void print_with_name (ostream& os, const string& name, |
2903
|
455 bool print_padding = true) const; |
2376
|
456 |
|
457 virtual int type_id (void) const { return rep->type_id (); } |
|
458 |
|
459 virtual string type_name (void) const { return rep->type_name (); } |
|
460 |
|
461 // Binary and unary operations. |
|
462 |
2575
|
463 friend octave_value do_binary_op (octave_value::binary_op, |
|
464 const octave_value&, |
|
465 const octave_value&); |
2376
|
466 |
|
467 protected: |
|
468 |
|
469 octave_value (const octave_xvalue&) : rep (0) { } |
|
470 |
2903
|
471 void reset_indent_level (void) const |
|
472 { curr_print_indent_level = 0; } |
|
473 |
|
474 void increment_indent_level (void) const |
|
475 { curr_print_indent_level += 2; } |
|
476 |
|
477 void decrement_indent_level (void) const |
|
478 { curr_print_indent_level -= 2; } |
|
479 |
|
480 int current_print_indent_level (void) const |
|
481 { return curr_print_indent_level; } |
|
482 |
|
483 void newline (ostream& os) const; |
|
484 |
|
485 void indent (ostream& os) const; |
|
486 |
|
487 void reset (void) const; |
|
488 |
2376
|
489 private: |
|
490 |
2477
|
491 static octave_allocator allocator; |
|
492 |
2376
|
493 union |
|
494 { |
|
495 octave_value *rep; // The real representation. |
|
496 int count; // A reference count. |
|
497 }; |
2413
|
498 |
2880
|
499 bool convert_and_assign (assign_op, const octave_value_list& idx, |
2413
|
500 const octave_value& rhs); |
|
501 |
2880
|
502 bool try_assignment_with_conversion (assign_op, |
|
503 const octave_value_list& idx, |
2413
|
504 const octave_value& rhs); |
|
505 |
2880
|
506 bool try_assignment (assign_op, const octave_value_list& idx, |
2413
|
507 const octave_value& rhs); |
2903
|
508 |
|
509 static int curr_print_indent_level; |
|
510 static bool beginning_of_line; |
2376
|
511 }; |
|
512 |
|
513 // If TRUE, allow assignments like |
|
514 // |
|
515 // octave> A(1) = 3; A(2) = 5 |
|
516 // |
|
517 // for A already defined and a matrix type. |
|
518 extern bool Vdo_fortran_indexing; |
|
519 |
|
520 // Should we allow things like: |
|
521 // |
|
522 // octave> 'abc' + 0 |
|
523 // 97 98 99 |
|
524 // |
|
525 // to happen? A positive value means yes. A negative value means |
|
526 // yes, but print a warning message. Zero means it should be |
|
527 // considered an error. |
|
528 extern int Vimplicit_str_to_num_ok; |
|
529 |
|
530 // Should we allow silent conversion of complex to real when a real |
|
531 // type is what we're really looking for? A positive value means yes. |
|
532 // A negative value means yes, but print a warning message. Zero |
|
533 // means it should be considered an error. |
|
534 extern int Vok_to_lose_imaginary_part; |
|
535 |
|
536 // If TRUE, create column vectors when doing assignments like: |
|
537 // |
|
538 // octave> A(1) = 3; A(2) = 5 |
|
539 // |
|
540 // (for A undefined). Only matters when resize_on_range_error is also |
|
541 // TRUE. |
|
542 extern bool Vprefer_column_vectors; |
|
543 |
|
544 // If TRUE, print the name along with the value. |
|
545 extern bool Vprint_answer_id_name; |
|
546 |
|
547 // Should operations on empty matrices return empty matrices or an |
|
548 // error? A positive value means yes. A negative value means yes, |
|
549 // but print a warning message. Zero means it should be considered an |
|
550 // error. |
|
551 extern int Vpropagate_empty_matrices; |
|
552 |
|
553 // How many levels of structure elements should we print? |
|
554 extern int Vstruct_levels_to_print; |
|
555 |
|
556 // Allow divide by zero errors to be suppressed. |
|
557 extern bool Vwarn_divide_by_zero; |
|
558 |
3195
|
559 // If TRUE, resize matrices when performing and indexed assignment and |
|
560 // the indices are outside the current bounds. |
|
561 extern bool Vresize_on_range_error; |
|
562 |
2376
|
563 // Indentation level for structures. |
|
564 extern int struct_indent; |
|
565 |
|
566 extern void increment_struct_indent (void); |
|
567 extern void decrement_struct_indent (void); |
|
568 |
2880
|
569 // Indentation level for lists. |
|
570 extern int list_indent; |
|
571 |
|
572 extern void increment_list_indent (void); |
|
573 extern void decrement_list_indent (void); |
|
574 |
2376
|
575 extern void install_types (void); |
|
576 |
|
577 #endif |
|
578 |
|
579 /* |
|
580 ;; Local Variables: *** |
|
581 ;; mode: C++ *** |
|
582 ;; End: *** |
|
583 */ |