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