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