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