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 |
2963
|
216 virtual octave_value do_index_op (const octave_value_list& idx) const |
|
217 { return rep->do_index_op (idx); } |
2376
|
218 |
2963
|
219 void assign (assign_op, const octave_value& rhs); |
2880
|
220 |
2963
|
221 void 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 |
2963
|
236 do_struct_elt_index_op (const string& nm, bool silent = false) |
|
237 { return rep->do_struct_elt_index_op (nm, silent); } |
|
238 |
|
239 virtual octave_value |
|
240 do_struct_elt_index_op (const string& nm, const octave_value_list& idx, |
|
241 bool silent = false) |
|
242 { return rep->do_struct_elt_index_op (nm, idx, silent); } |
2376
|
243 |
2948
|
244 octave_variable_reference struct_elt_ref (const string& nm); |
|
245 |
|
246 virtual octave_variable_reference |
|
247 struct_elt_ref (octave_value *parent, const string& nm); |
2376
|
248 |
|
249 // Size. |
|
250 |
|
251 virtual int rows (void) const |
|
252 { return rep->rows (); } |
|
253 |
|
254 virtual int columns (void) const |
|
255 { return rep->columns (); } |
|
256 |
|
257 // Does this constant have a type? Both of these are provided since |
|
258 // it is sometimes more natural to write is_undefined() instead of |
|
259 // ! is_defined(). |
|
260 |
|
261 virtual bool is_defined (void) const |
|
262 { return rep->is_defined (); } |
|
263 |
|
264 bool is_undefined (void) const |
|
265 { return ! is_defined (); } |
|
266 |
|
267 virtual bool is_real_scalar (void) const |
|
268 { return rep->is_real_scalar (); } |
|
269 |
|
270 virtual bool is_real_matrix (void) const |
|
271 { return rep->is_real_matrix (); } |
|
272 |
|
273 virtual bool is_complex_scalar (void) const |
|
274 { return rep->is_complex_scalar (); } |
|
275 |
|
276 virtual bool is_complex_matrix (void) const |
|
277 { return rep->is_complex_matrix (); } |
|
278 |
|
279 virtual bool is_char_matrix (void) const |
|
280 { return rep->is_char_matrix (); } |
|
281 |
|
282 virtual bool is_string (void) const |
|
283 { return rep->is_string (); } |
|
284 |
|
285 virtual bool is_range (void) const |
|
286 { return rep->is_range (); } |
|
287 |
|
288 virtual bool is_map (void) const |
|
289 { return rep->is_map (); } |
|
290 |
2880
|
291 virtual bool is_list (void) const |
|
292 { return rep->is_list (); } |
|
293 |
2376
|
294 virtual bool is_magic_colon (void) const |
|
295 { return rep->is_magic_colon (); } |
|
296 |
|
297 virtual bool is_all_va_args (void) const |
|
298 { return rep->is_all_va_args (); } |
|
299 |
|
300 // Are any or all of the elements in this constant nonzero? |
|
301 |
|
302 virtual octave_value all (void) const |
|
303 { return rep->all (); } |
|
304 |
|
305 virtual octave_value any (void) const |
|
306 { return rep->any (); } |
|
307 |
|
308 // Other type stuff. |
|
309 |
|
310 virtual bool is_real_type (void) const |
|
311 { return rep->is_real_type (); } |
|
312 |
|
313 virtual bool is_complex_type (void) const |
|
314 { return rep->is_complex_type (); } |
|
315 |
|
316 virtual bool is_scalar_type (void) const |
|
317 { return rep->is_scalar_type (); } |
|
318 |
|
319 virtual bool is_matrix_type (void) const |
|
320 { return rep->is_matrix_type (); } |
|
321 |
|
322 virtual bool is_numeric_type (void) const |
|
323 { return rep->is_numeric_type (); } |
|
324 |
|
325 virtual bool valid_as_scalar_index (void) const |
|
326 { return rep->valid_as_scalar_index (); } |
|
327 |
|
328 virtual bool valid_as_zero_index (void) const |
|
329 { return rep->valid_as_zero_index (); } |
|
330 |
|
331 // Does this constant correspond to a truth value? |
|
332 |
|
333 virtual bool is_true (void) const |
|
334 { return rep->is_true (); } |
|
335 |
|
336 // Is at least one of the dimensions of this constant zero? |
|
337 |
|
338 virtual bool is_empty (void) const |
|
339 { return rep->is_empty (); } |
|
340 |
|
341 // Are the dimensions of this constant zero by zero? |
|
342 |
|
343 virtual bool is_zero_by_zero (void) const |
|
344 { return rep->is_zero_by_zero (); } |
|
345 |
2891
|
346 bool is_constant (void) const |
|
347 { return true; } |
|
348 |
2376
|
349 // Values. |
|
350 |
2891
|
351 octave_value eval (void) { return *this; } |
|
352 |
|
353 octave_value_list eval (int, const octave_value_list& idx); |
|
354 |
2376
|
355 virtual double double_value (bool frc_str_conv = false) const |
|
356 { return rep->double_value (frc_str_conv); } |
|
357 |
2916
|
358 virtual double scalar_value (bool frc_str_conv = false) const |
|
359 { return rep->scalar_value (frc_str_conv); } |
|
360 |
2376
|
361 virtual Matrix matrix_value (bool frc_str_conv = false) const |
|
362 { return rep->matrix_value (frc_str_conv); } |
|
363 |
|
364 virtual Complex complex_value (bool frc_str_conv = false) const |
|
365 { return rep->complex_value (frc_str_conv); } |
|
366 |
|
367 virtual ComplexMatrix complex_matrix_value (bool frc_str_conv = false) const |
|
368 { return rep->complex_matrix_value (frc_str_conv); } |
|
369 |
|
370 virtual charMatrix char_matrix_value (bool frc_str_conv = false) const |
|
371 { return rep->char_matrix_value (frc_str_conv); } |
|
372 |
2493
|
373 virtual string_vector all_strings (void) const |
2376
|
374 { return rep->all_strings (); } |
|
375 |
|
376 virtual string string_value (void) const |
|
377 { return rep->string_value (); } |
|
378 |
|
379 virtual Range range_value (void) const |
|
380 { return rep->range_value (); } |
|
381 |
|
382 virtual Octave_map map_value (void) const; |
|
383 |
2903
|
384 virtual octave_stream *stream_value (void) const; |
|
385 |
|
386 virtual int stream_number (void) const; |
|
387 |
2880
|
388 virtual octave_value_list list_value (void) const; |
|
389 |
2825
|
390 virtual bool bool_value (void) const |
|
391 { return rep->bool_value (); } |
|
392 |
|
393 virtual boolMatrix bool_matrix_value (void) const |
|
394 { return rep->bool_matrix_value (); } |
|
395 |
2376
|
396 // Unary ops. |
|
397 |
|
398 virtual octave_value not (void) const { return rep->not (); } |
|
399 |
|
400 virtual octave_value uminus (void) const { return rep->uminus (); } |
|
401 |
|
402 virtual octave_value transpose (void) const { return rep->transpose (); } |
|
403 |
|
404 virtual octave_value hermitian (void) const { return rep->hermitian (); } |
|
405 |
|
406 virtual void increment (void) |
|
407 { |
|
408 make_unique (); |
|
409 rep->increment (); |
|
410 } |
|
411 |
|
412 virtual void decrement (void) |
|
413 { |
|
414 make_unique (); |
|
415 rep->decrement (); |
|
416 } |
|
417 |
|
418 ColumnVector vector_value (bool frc_str_conv = false, |
|
419 bool frc_vec_conv = false) const; |
|
420 |
|
421 ComplexColumnVector |
|
422 complex_vector_value (bool frc_str_conv = false, |
|
423 bool frc_vec_conv = false) const; |
|
424 |
|
425 // Conversions. These should probably be private. If a user of this |
|
426 // class wants a certain kind of constant, he should simply ask for |
|
427 // it, and we should convert it if possible. |
|
428 |
|
429 virtual octave_value convert_to_str (void) const |
|
430 { return rep->convert_to_str (); } |
|
431 |
|
432 virtual void convert_to_row_or_column_vector (void) |
|
433 { rep->convert_to_row_or_column_vector (); } |
|
434 |
2903
|
435 virtual void print (ostream& os, bool pr_as_read_syntax = false) const |
2466
|
436 { rep->print (os, pr_as_read_syntax); } |
2376
|
437 |
2903
|
438 virtual void print_raw (ostream& os, bool pr_as_read_syntax = false) const |
|
439 { rep->print_raw (os, pr_as_read_syntax); } |
|
440 |
|
441 virtual bool print_name_tag (ostream& os, const string& name) const |
|
442 { return rep->print_name_tag (os, name); } |
2376
|
443 |
|
444 void print_with_name (ostream& os, const string& name, |
2903
|
445 bool print_padding = true) const; |
2376
|
446 |
|
447 virtual int type_id (void) const { return rep->type_id (); } |
|
448 |
|
449 virtual string type_name (void) const { return rep->type_name (); } |
|
450 |
|
451 // Binary and unary operations. |
|
452 |
2575
|
453 friend octave_value do_binary_op (octave_value::binary_op, |
|
454 const octave_value&, |
|
455 const octave_value&); |
2376
|
456 |
|
457 protected: |
|
458 |
|
459 octave_value (const octave_xvalue&) : rep (0) { } |
|
460 |
2903
|
461 void reset_indent_level (void) const |
|
462 { curr_print_indent_level = 0; } |
|
463 |
|
464 void increment_indent_level (void) const |
|
465 { curr_print_indent_level += 2; } |
|
466 |
|
467 void decrement_indent_level (void) const |
|
468 { curr_print_indent_level -= 2; } |
|
469 |
|
470 int current_print_indent_level (void) const |
|
471 { return curr_print_indent_level; } |
|
472 |
|
473 void newline (ostream& os) const; |
|
474 |
|
475 void indent (ostream& os) const; |
|
476 |
|
477 void reset (void) const; |
|
478 |
2376
|
479 private: |
|
480 |
2477
|
481 static octave_allocator allocator; |
|
482 |
2376
|
483 union |
|
484 { |
|
485 octave_value *rep; // The real representation. |
|
486 int count; // A reference count. |
|
487 }; |
2413
|
488 |
2880
|
489 bool convert_and_assign (assign_op, const octave_value_list& idx, |
2413
|
490 const octave_value& rhs); |
|
491 |
2880
|
492 bool try_assignment_with_conversion (assign_op, |
|
493 const octave_value_list& idx, |
2413
|
494 const octave_value& rhs); |
|
495 |
2880
|
496 bool try_assignment (assign_op, const octave_value_list& idx, |
2413
|
497 const octave_value& rhs); |
2903
|
498 |
|
499 static int curr_print_indent_level; |
|
500 static bool beginning_of_line; |
2376
|
501 }; |
|
502 |
|
503 // If TRUE, allow assignments like |
|
504 // |
|
505 // octave> A(1) = 3; A(2) = 5 |
|
506 // |
|
507 // for A already defined and a matrix type. |
|
508 extern bool Vdo_fortran_indexing; |
|
509 |
|
510 // Should we allow things like: |
|
511 // |
|
512 // octave> 'abc' + 0 |
|
513 // 97 98 99 |
|
514 // |
|
515 // to happen? A positive value means yes. A negative value means |
|
516 // yes, but print a warning message. Zero means it should be |
|
517 // considered an error. |
|
518 extern int Vimplicit_str_to_num_ok; |
|
519 |
|
520 // Should we allow silent conversion of complex to real when a real |
|
521 // type is what we're really looking for? A positive value means yes. |
|
522 // A negative value means yes, but print a warning message. Zero |
|
523 // means it should be considered an error. |
|
524 extern int Vok_to_lose_imaginary_part; |
|
525 |
|
526 // If TRUE, create column vectors when doing assignments like: |
|
527 // |
|
528 // octave> A(1) = 3; A(2) = 5 |
|
529 // |
|
530 // (for A undefined). Only matters when resize_on_range_error is also |
|
531 // TRUE. |
|
532 extern bool Vprefer_column_vectors; |
|
533 |
|
534 // If TRUE, print the name along with the value. |
|
535 extern bool Vprint_answer_id_name; |
|
536 |
|
537 // Should operations on empty matrices return empty matrices or an |
|
538 // error? A positive value means yes. A negative value means yes, |
|
539 // but print a warning message. Zero means it should be considered an |
|
540 // error. |
|
541 extern int Vpropagate_empty_matrices; |
|
542 |
|
543 // How many levels of structure elements should we print? |
|
544 extern int Vstruct_levels_to_print; |
|
545 |
|
546 // Allow divide by zero errors to be suppressed. |
|
547 extern bool Vwarn_divide_by_zero; |
|
548 |
|
549 // Indentation level for structures. |
|
550 extern int struct_indent; |
|
551 |
|
552 extern void increment_struct_indent (void); |
|
553 extern void decrement_struct_indent (void); |
|
554 |
2880
|
555 // Indentation level for lists. |
|
556 extern int list_indent; |
|
557 |
|
558 extern void increment_list_indent (void); |
|
559 extern void decrement_list_indent (void); |
|
560 |
2376
|
561 extern void install_types (void); |
|
562 |
|
563 #endif |
|
564 |
|
565 /* |
|
566 ;; Local Variables: *** |
|
567 ;; mode: C++ *** |
|
568 ;; End: *** |
|
569 */ |