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