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 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
2376
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
4254
|
31 #include <climits> |
|
32 |
3503
|
33 #include <iostream> |
2901
|
34 |
2376
|
35 #include "lo-ieee.h" |
4649
|
36 #include "so-array.h" |
2376
|
37 |
|
38 #include "gripes.h" |
|
39 #include "oct-map.h" |
2974
|
40 #include "oct-obj.h" |
2979
|
41 #include "oct-lvalue.h" |
3340
|
42 #include "oct-stream.h" |
2376
|
43 #include "ops.h" |
|
44 #include "ov-base.h" |
3928
|
45 #include "ov-cell.h" |
|
46 #include "ov-ch-mat.h" |
2376
|
47 #include "ov-complex.h" |
|
48 #include "ov-cx-mat.h" |
3928
|
49 #include "ov-list.h" |
|
50 #include "ov-range.h" |
|
51 #include "ov-re-mat.h" |
|
52 #include "ov-scalar.h" |
2376
|
53 #include "ov-str-mat.h" |
4343
|
54 #include "ov-fcn-handle.h" |
2948
|
55 #include "variables.h" |
2376
|
56 |
4612
|
57 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_base_value, |
|
58 "<unknown type>", "unknown"); |
2376
|
59 |
|
60 octave_value |
4532
|
61 octave_base_value::squeeze (void) const |
|
62 { |
|
63 std::string nm = type_name (); |
|
64 error ("squeeze: invalid operation for %s type", nm.c_str ()); |
|
65 return octave_value (); |
|
66 } |
|
67 |
|
68 octave_value |
4247
|
69 octave_base_value::subsref (const std::string&, |
4219
|
70 const std::list<octave_value_list>&) |
3933
|
71 { |
|
72 std::string nm = type_name (); |
|
73 error ("can't perform indexing operations for %s type", nm.c_str ()); |
|
74 return octave_value (); |
|
75 } |
|
76 |
|
77 octave_value_list |
4247
|
78 octave_base_value::subsref (const std::string&, |
4219
|
79 const std::list<octave_value_list>&, int) |
3933
|
80 { |
|
81 std::string nm = type_name (); |
|
82 error ("can't perform indexing operations for %s type", nm.c_str ()); |
|
83 return octave_value (); |
|
84 } |
|
85 |
|
86 octave_value |
|
87 octave_base_value::do_index_op (const octave_value_list&, int) |
2974
|
88 { |
3523
|
89 std::string nm = type_name (); |
2974
|
90 error ("can't perform indexing operations for %s type", nm.c_str ()); |
|
91 return octave_value (); |
|
92 } |
|
93 |
|
94 octave_value_list |
3544
|
95 octave_base_value::do_multi_index_op (int, const octave_value_list&) |
2376
|
96 { |
3523
|
97 std::string nm = type_name (); |
2376
|
98 error ("can't perform indexing operations for %s type", nm.c_str ()); |
|
99 return octave_value (); |
|
100 } |
|
101 |
|
102 idx_vector |
|
103 octave_base_value::index_vector (void) const |
|
104 { |
3523
|
105 std::string nm = type_name (); |
2376
|
106 error ("%s type invalid as index value", nm.c_str ()); |
|
107 return idx_vector (); |
|
108 } |
|
109 |
|
110 octave_value |
4247
|
111 octave_base_value::subsasgn (const std::string& type, |
4219
|
112 const std::list<octave_value_list>& idx, |
3933
|
113 const octave_value& rhs) |
2376
|
114 { |
3933
|
115 octave_value retval; |
2376
|
116 |
3933
|
117 if (is_defined ()) |
|
118 { |
4139
|
119 if (is_numeric_type ()) |
|
120 { |
|
121 switch (type[0]) |
|
122 { |
|
123 case '(': |
|
124 { |
|
125 if (type.length () == 1) |
|
126 retval = numeric_assign (type, idx, rhs); |
4436
|
127 else if (is_empty ()) |
|
128 { |
|
129 // Allow conversion of empty matrix to some other |
|
130 // type in cases like |
|
131 // |
|
132 // x = []; x(i).f = rhs |
|
133 |
|
134 octave_value tmp = octave_value::empty_conv (type, rhs); |
|
135 |
|
136 retval = tmp.subsasgn (type, idx, rhs); |
|
137 } |
4139
|
138 else |
|
139 { |
|
140 std::string nm = type_name (); |
|
141 error ("in indexed assignment of %s, last rhs index must be ()", |
|
142 nm.c_str ()); |
|
143 } |
|
144 } |
|
145 break; |
|
146 |
|
147 case '{': |
|
148 case '.': |
|
149 { |
|
150 std::string nm = type_name (); |
|
151 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
152 } |
|
153 break; |
|
154 |
|
155 default: |
|
156 panic_impossible (); |
|
157 } |
|
158 } |
|
159 else |
|
160 { |
|
161 std::string nm = type_name (); |
|
162 error ("can't perform indexed assignment for %s type", nm.c_str ()); |
|
163 } |
3933
|
164 } |
|
165 else |
|
166 { |
|
167 // Create new object of appropriate type for given index and rhs |
|
168 // types and then call subsasgn again for that object. |
|
169 |
|
170 octave_value tmp = octave_value::empty_conv (type, rhs); |
|
171 |
|
172 retval = tmp.subsasgn (type, idx, rhs); |
|
173 } |
|
174 |
|
175 return retval; |
2376
|
176 } |
|
177 |
|
178 octave_value |
4567
|
179 octave_base_value::reshape (const dim_vector&) const |
|
180 { |
|
181 gripe_wrong_type_arg ("octave_base_value::reshape ()", type_name ()); |
|
182 return octave_value (); |
|
183 } |
|
184 |
|
185 octave_value |
4593
|
186 octave_base_value::permute (const Array<int>&, bool) const |
|
187 { |
|
188 gripe_wrong_type_arg ("octave_base_value::permute ()", type_name ()); |
|
189 return octave_value (); |
|
190 } |
|
191 |
|
192 octave_value |
4457
|
193 octave_base_value::convert_to_str_internal (bool, bool) const |
2376
|
194 { |
4452
|
195 gripe_wrong_type_arg ("octave_base_value::convert_to_str_internal ()", |
2376
|
196 type_name ()); |
|
197 return octave_value (); |
|
198 } |
|
199 |
|
200 void |
|
201 octave_base_value::convert_to_row_or_column_vector (void) |
|
202 { |
|
203 gripe_wrong_type_arg |
|
204 ("octave_base_value::convert_to_row_or_column_vector ()", |
|
205 type_name ()); |
|
206 } |
|
207 |
|
208 void |
3523
|
209 octave_base_value::print (std::ostream&, bool) const |
2376
|
210 { |
3195
|
211 gripe_wrong_type_arg ("octave_base_value::print ()", type_name ()); |
2376
|
212 } |
|
213 |
2901
|
214 void |
3523
|
215 octave_base_value::print_raw (std::ostream&, bool) const |
2901
|
216 { |
3195
|
217 gripe_wrong_type_arg ("octave_base_value::print_raw ()", type_name ()); |
2901
|
218 } |
|
219 |
|
220 bool |
3523
|
221 octave_base_value::print_name_tag (std::ostream& os, const std::string& name) const |
2901
|
222 { |
4604
|
223 bool retval = false; |
|
224 |
2901
|
225 indent (os); |
4604
|
226 |
|
227 if (print_as_scalar ()) |
|
228 os << name << " = "; |
|
229 else |
|
230 { |
|
231 os << name << " ="; |
|
232 newline (os); |
|
233 newline (os); |
|
234 retval = true; |
|
235 } |
|
236 |
|
237 return retval; |
2901
|
238 } |
|
239 |
3933
|
240 void |
|
241 octave_base_value::print_info (std::ostream& os, |
4661
|
242 const std::string& /* prefix */) const |
3933
|
243 { |
|
244 os << "no info for type: " << type_name () << "\n"; |
|
245 } |
|
246 |
4254
|
247 #define INT_CONV_METHOD(T, F, MIN_LIMIT, MAX_LIMIT) \ |
|
248 T \ |
|
249 octave_base_value::F ## _value (bool require_int, bool frc_str_conv) const \ |
|
250 { \ |
|
251 T retval = 0; \ |
|
252 \ |
|
253 double d = double_value (frc_str_conv); \ |
|
254 \ |
|
255 if (! error_state) \ |
|
256 { \ |
|
257 if (require_int && D_NINT (d) != d) \ |
|
258 error ("conversion of %g to " #T " value failed", d); \ |
|
259 else if (d < MIN_LIMIT || d > MAX_LIMIT) \ |
|
260 error ("conversion of %g to short int out of range (%d, %d)", \ |
|
261 d, MIN_LIMIT, MAX_LIMIT); \ |
|
262 else \ |
|
263 retval = static_cast<T> (d); \ |
|
264 } \ |
|
265 else \ |
|
266 gripe_wrong_type_arg ("octave_base_value::" #F "_value ()", \ |
|
267 type_name ()); \ |
|
268 \ |
|
269 return retval; \ |
|
270 } |
3202
|
271 |
4254
|
272 INT_CONV_METHOD (short int, short, SHRT_MIN, SHRT_MAX) |
|
273 INT_CONV_METHOD (unsigned short int, ushort, 0, USHRT_MAX) |
3202
|
274 |
4254
|
275 INT_CONV_METHOD (int, int, INT_MIN, INT_MAX) |
|
276 INT_CONV_METHOD (unsigned int, uint, 0, UINT_MAX) |
3202
|
277 |
4254
|
278 INT_CONV_METHOD (long int, long, LONG_MIN, LONG_MAX) |
|
279 INT_CONV_METHOD (unsigned long int, ulong, 0, ULONG_MAX) |
3202
|
280 |
|
281 int |
|
282 octave_base_value::nint_value (bool frc_str_conv) const |
|
283 { |
|
284 int retval = 0; |
|
285 |
|
286 double d = double_value (frc_str_conv); |
|
287 |
|
288 if (! error_state) |
|
289 { |
|
290 if (xisnan (d)) |
|
291 { |
|
292 error ("conversion of NaN to integer value failed"); |
|
293 return retval; |
|
294 } |
|
295 |
|
296 retval = NINT (d); |
|
297 } |
|
298 else |
|
299 gripe_wrong_type_arg ("octave_base_value::nint_value ()", type_name ()); |
|
300 |
|
301 return retval; |
|
302 } |
|
303 |
2376
|
304 double |
|
305 octave_base_value::double_value (bool) const |
|
306 { |
4102
|
307 double retval = lo_ieee_nan_value (); |
2376
|
308 gripe_wrong_type_arg ("octave_base_value::double_value ()", type_name ()); |
|
309 return retval; |
|
310 } |
|
311 |
3351
|
312 Cell |
3539
|
313 octave_base_value::cell_value () const |
3351
|
314 { |
|
315 Cell retval; |
|
316 gripe_wrong_type_arg ("octave_base_value::cell_value()", type_name ()); |
|
317 return retval; |
|
318 } |
|
319 |
2376
|
320 Matrix |
|
321 octave_base_value::matrix_value (bool) const |
|
322 { |
|
323 Matrix retval; |
|
324 gripe_wrong_type_arg ("octave_base_value::matrix_value()", type_name ()); |
|
325 return retval; |
|
326 } |
|
327 |
4507
|
328 NDArray |
4550
|
329 octave_base_value::array_value (bool) const |
4505
|
330 { |
4507
|
331 NDArray retval; |
4550
|
332 gripe_wrong_type_arg ("octave_base_value::array_value()", type_name ()); |
4505
|
333 return retval; |
|
334 } |
|
335 |
2376
|
336 Complex |
|
337 octave_base_value::complex_value (bool) const |
|
338 { |
4102
|
339 double tmp = lo_ieee_nan_value (); |
|
340 Complex retval (tmp, tmp); |
2376
|
341 gripe_wrong_type_arg ("octave_base_value::complex_value()", type_name ()); |
|
342 return retval; |
|
343 } |
|
344 |
|
345 ComplexMatrix |
|
346 octave_base_value::complex_matrix_value (bool) const |
|
347 { |
|
348 ComplexMatrix retval; |
|
349 gripe_wrong_type_arg ("octave_base_value::complex_matrix_value()", |
|
350 type_name ()); |
|
351 return retval; |
|
352 } |
|
353 |
4550
|
354 ComplexNDArray |
|
355 octave_base_value::complex_array_value (bool) const |
|
356 { |
|
357 ComplexNDArray retval; |
|
358 gripe_wrong_type_arg ("octave_base_value::complex_array_value()", |
|
359 type_name ()); |
|
360 return retval; |
|
361 } |
|
362 |
|
363 bool |
|
364 octave_base_value::bool_value (void) const |
|
365 { |
|
366 bool retval = false; |
|
367 gripe_wrong_type_arg ("octave_base_value::bool_value()", type_name ()); |
|
368 return retval; |
|
369 } |
|
370 |
|
371 boolMatrix |
|
372 octave_base_value::bool_matrix_value (void) const |
|
373 { |
|
374 boolMatrix retval; |
|
375 gripe_wrong_type_arg ("octave_base_value::bool_matrix_value()", |
|
376 type_name ()); |
|
377 return retval; |
|
378 } |
|
379 |
|
380 boolNDArray |
4580
|
381 octave_base_value::bool_array_value (void) const |
4550
|
382 { |
|
383 boolNDArray retval; |
|
384 gripe_wrong_type_arg ("octave_base_value::bool_array_value()", |
|
385 type_name ()); |
|
386 return retval; |
|
387 } |
|
388 |
2376
|
389 charMatrix |
|
390 octave_base_value::char_matrix_value (bool) const |
|
391 { |
|
392 charMatrix retval; |
4257
|
393 |
4452
|
394 octave_value tmp = convert_to_str (); |
4257
|
395 |
4452
|
396 if (! error_state) |
|
397 retval = tmp.char_matrix_value (); |
|
398 |
2376
|
399 return retval; |
|
400 } |
|
401 |
4550
|
402 charNDArray |
|
403 octave_base_value::char_array_value (bool) const |
|
404 { |
|
405 charNDArray retval; |
|
406 gripe_wrong_type_arg ("octave_base_value::char_array_value()", |
|
407 type_name ()); |
|
408 return retval; |
|
409 } |
|
410 |
2493
|
411 string_vector |
4457
|
412 octave_base_value::all_strings (bool pad, bool force) const |
2376
|
413 { |
2493
|
414 string_vector retval; |
4257
|
415 |
4457
|
416 octave_value tmp = convert_to_str (pad, force); |
4257
|
417 |
4452
|
418 if (! error_state) |
|
419 retval = tmp.all_strings (); |
4257
|
420 |
2376
|
421 return retval; |
|
422 } |
|
423 |
3536
|
424 std::string |
4457
|
425 octave_base_value::string_value (bool force) const |
2376
|
426 { |
3523
|
427 std::string retval; |
4257
|
428 |
4457
|
429 octave_value tmp = convert_to_str (force); |
4257
|
430 |
4452
|
431 if (! error_state) |
|
432 retval = tmp.string_value (); |
4257
|
433 |
2376
|
434 return retval; |
|
435 } |
|
436 |
|
437 Range |
|
438 octave_base_value::range_value (void) const |
|
439 { |
|
440 Range retval; |
|
441 gripe_wrong_type_arg ("octave_base_value::range_value()", type_name ()); |
|
442 return retval; |
|
443 } |
|
444 |
|
445 Octave_map |
|
446 octave_base_value::map_value (void) const |
|
447 { |
|
448 Octave_map retval; |
|
449 gripe_wrong_type_arg ("octave_base_value::map_value()", type_name ()); |
|
450 return retval; |
|
451 } |
|
452 |
3933
|
453 string_vector |
|
454 octave_base_value::map_keys (void) const |
|
455 { |
|
456 string_vector retval; |
|
457 gripe_wrong_type_arg ("octave_base_value::map_keys()", type_name ()); |
|
458 return retval; |
|
459 } |
|
460 |
3340
|
461 octave_stream |
2901
|
462 octave_base_value::stream_value (void) const |
|
463 { |
3340
|
464 octave_stream retval; |
2901
|
465 gripe_wrong_type_arg ("octave_base_value::stream_value()", type_name ()); |
|
466 return retval; |
|
467 } |
|
468 |
4645
|
469 std::streamoff |
4643
|
470 octave_base_value::streamoff_value (void) const |
|
471 { |
4649
|
472 std::streamoff retval (-1); |
4645
|
473 gripe_wrong_type_arg ("octave_base_value::streamoff_value()", type_name ()); |
|
474 return retval; |
|
475 } |
|
476 |
|
477 streamoff_array |
|
478 octave_base_value::streamoff_array_value (void) const |
|
479 { |
4643
|
480 streamoff_array retval; |
4645
|
481 gripe_wrong_type_arg ("octave_base_value::streamoff_array_value()", |
|
482 type_name ()); |
4643
|
483 return retval; |
|
484 } |
|
485 |
2901
|
486 int |
|
487 octave_base_value::stream_number (void) const |
|
488 { |
|
489 int retval = -1; |
|
490 gripe_wrong_type_arg ("octave_base_value::stream_number()", type_name ()); |
|
491 return retval; |
|
492 } |
|
493 |
2974
|
494 octave_function * |
|
495 octave_base_value::function_value (bool silent) |
|
496 { |
|
497 octave_function *retval = 0; |
|
498 |
|
499 if (! silent) |
|
500 gripe_wrong_type_arg ("octave_base_value::function_value()", |
|
501 type_name ()); |
|
502 return retval; |
|
503 } |
|
504 |
4346
|
505 octave_fcn_handle * |
4343
|
506 octave_base_value::fcn_handle_value (bool silent) |
|
507 { |
4346
|
508 octave_fcn_handle *retval = 0; |
4343
|
509 |
|
510 if (! silent) |
|
511 gripe_wrong_type_arg ("octave_base_value::fcn_handle_value()", |
|
512 type_name ()); |
|
513 return retval; |
|
514 } |
|
515 |
2882
|
516 octave_value_list |
|
517 octave_base_value::list_value (void) const |
|
518 { |
|
519 octave_value_list retval; |
|
520 gripe_wrong_type_arg ("octave_base_value::list_value()", type_name ()); |
|
521 return retval; |
|
522 } |
|
523 |
3203
|
524 CONVDECLX (matrix_conv) |
2376
|
525 { |
|
526 return new octave_matrix (); |
|
527 } |
|
528 |
3203
|
529 CONVDECLX (complex_matrix_conv) |
2376
|
530 { |
|
531 return new octave_complex_matrix (); |
|
532 } |
|
533 |
3203
|
534 CONVDECLX (string_conv) |
2376
|
535 { |
|
536 return new octave_char_matrix_str (); |
|
537 } |
|
538 |
3928
|
539 CONVDECLX (cell_conv) |
|
540 { |
|
541 return new octave_cell (); |
|
542 } |
|
543 |
2376
|
544 void |
|
545 install_base_type_conversions (void) |
|
546 { |
|
547 INSTALL_ASSIGNCONV (octave_base_value, octave_scalar, octave_matrix); |
|
548 INSTALL_ASSIGNCONV (octave_base_value, octave_matrix, octave_matrix); |
|
549 INSTALL_ASSIGNCONV (octave_base_value, octave_complex, octave_complex_matrix); |
|
550 INSTALL_ASSIGNCONV (octave_base_value, octave_complex_matrix, octave_complex_matrix); |
|
551 INSTALL_ASSIGNCONV (octave_base_value, octave_range, octave_matrix); |
|
552 INSTALL_ASSIGNCONV (octave_base_value, octave_char_matrix_str, octave_char_matrix_str); |
3928
|
553 INSTALL_ASSIGNCONV (octave_base_value, octave_cell, octave_cell); |
2376
|
554 |
|
555 INSTALL_WIDENOP (octave_base_value, octave_matrix, matrix_conv); |
|
556 INSTALL_WIDENOP (octave_base_value, octave_complex_matrix, complex_matrix_conv); |
|
557 INSTALL_WIDENOP (octave_base_value, octave_char_matrix_str, string_conv); |
3928
|
558 INSTALL_WIDENOP (octave_base_value, octave_cell, cell_conv); |
2376
|
559 } |
|
560 |
|
561 /* |
|
562 ;;; Local Variables: *** |
|
563 ;;; mode: C++ *** |
|
564 ;;; End: *** |
|
565 */ |