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