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