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