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 { |
|
221 indent (os); |
|
222 os << name << " ="; |
|
223 newline (os); |
|
224 newline (os); |
|
225 return true; |
|
226 } |
|
227 |
3933
|
228 void |
|
229 octave_base_value::print_info (std::ostream& os, |
|
230 const std::string& prefix) const |
|
231 { |
|
232 os << "no info for type: " << type_name () << "\n"; |
|
233 } |
|
234 |
4254
|
235 #define INT_CONV_METHOD(T, F, MIN_LIMIT, MAX_LIMIT) \ |
|
236 T \ |
|
237 octave_base_value::F ## _value (bool require_int, bool frc_str_conv) const \ |
|
238 { \ |
|
239 T retval = 0; \ |
|
240 \ |
|
241 double d = double_value (frc_str_conv); \ |
|
242 \ |
|
243 if (! error_state) \ |
|
244 { \ |
|
245 if (require_int && D_NINT (d) != d) \ |
|
246 error ("conversion of %g to " #T " value failed", d); \ |
|
247 else if (d < MIN_LIMIT || d > MAX_LIMIT) \ |
|
248 error ("conversion of %g to short int out of range (%d, %d)", \ |
|
249 d, MIN_LIMIT, MAX_LIMIT); \ |
|
250 else \ |
|
251 retval = static_cast<T> (d); \ |
|
252 } \ |
|
253 else \ |
|
254 gripe_wrong_type_arg ("octave_base_value::" #F "_value ()", \ |
|
255 type_name ()); \ |
|
256 \ |
|
257 return retval; \ |
|
258 } |
3202
|
259 |
4254
|
260 INT_CONV_METHOD (short int, short, SHRT_MIN, SHRT_MAX) |
|
261 INT_CONV_METHOD (unsigned short int, ushort, 0, USHRT_MAX) |
3202
|
262 |
4254
|
263 INT_CONV_METHOD (int, int, INT_MIN, INT_MAX) |
|
264 INT_CONV_METHOD (unsigned int, uint, 0, UINT_MAX) |
3202
|
265 |
4254
|
266 INT_CONV_METHOD (long int, long, LONG_MIN, LONG_MAX) |
|
267 INT_CONV_METHOD (unsigned long int, ulong, 0, ULONG_MAX) |
3202
|
268 |
|
269 int |
|
270 octave_base_value::nint_value (bool frc_str_conv) const |
|
271 { |
|
272 int retval = 0; |
|
273 |
|
274 double d = double_value (frc_str_conv); |
|
275 |
|
276 if (! error_state) |
|
277 { |
|
278 if (xisnan (d)) |
|
279 { |
|
280 error ("conversion of NaN to integer value failed"); |
|
281 return retval; |
|
282 } |
|
283 |
|
284 retval = NINT (d); |
|
285 } |
|
286 else |
|
287 gripe_wrong_type_arg ("octave_base_value::nint_value ()", type_name ()); |
|
288 |
|
289 return retval; |
|
290 } |
|
291 |
2376
|
292 double |
|
293 octave_base_value::double_value (bool) const |
|
294 { |
4102
|
295 double retval = lo_ieee_nan_value (); |
2376
|
296 gripe_wrong_type_arg ("octave_base_value::double_value ()", type_name ()); |
|
297 return retval; |
|
298 } |
|
299 |
3351
|
300 Cell |
3539
|
301 octave_base_value::cell_value () const |
3351
|
302 { |
|
303 Cell retval; |
|
304 gripe_wrong_type_arg ("octave_base_value::cell_value()", type_name ()); |
|
305 return retval; |
|
306 } |
|
307 |
2376
|
308 Matrix |
|
309 octave_base_value::matrix_value (bool) const |
|
310 { |
|
311 Matrix retval; |
|
312 gripe_wrong_type_arg ("octave_base_value::matrix_value()", type_name ()); |
|
313 return retval; |
|
314 } |
|
315 |
4507
|
316 NDArray |
4550
|
317 octave_base_value::array_value (bool) const |
4505
|
318 { |
4507
|
319 NDArray retval; |
4550
|
320 gripe_wrong_type_arg ("octave_base_value::array_value()", type_name ()); |
4505
|
321 return retval; |
|
322 } |
|
323 |
2376
|
324 Complex |
|
325 octave_base_value::complex_value (bool) const |
|
326 { |
4102
|
327 double tmp = lo_ieee_nan_value (); |
|
328 Complex retval (tmp, tmp); |
2376
|
329 gripe_wrong_type_arg ("octave_base_value::complex_value()", type_name ()); |
|
330 return retval; |
|
331 } |
|
332 |
|
333 ComplexMatrix |
|
334 octave_base_value::complex_matrix_value (bool) const |
|
335 { |
|
336 ComplexMatrix retval; |
|
337 gripe_wrong_type_arg ("octave_base_value::complex_matrix_value()", |
|
338 type_name ()); |
|
339 return retval; |
|
340 } |
|
341 |
4550
|
342 ComplexNDArray |
|
343 octave_base_value::complex_array_value (bool) const |
|
344 { |
|
345 ComplexNDArray retval; |
|
346 gripe_wrong_type_arg ("octave_base_value::complex_array_value()", |
|
347 type_name ()); |
|
348 return retval; |
|
349 } |
|
350 |
|
351 bool |
|
352 octave_base_value::bool_value (void) const |
|
353 { |
|
354 bool retval = false; |
|
355 gripe_wrong_type_arg ("octave_base_value::bool_value()", type_name ()); |
|
356 return retval; |
|
357 } |
|
358 |
|
359 boolMatrix |
|
360 octave_base_value::bool_matrix_value (void) const |
|
361 { |
|
362 boolMatrix retval; |
|
363 gripe_wrong_type_arg ("octave_base_value::bool_matrix_value()", |
|
364 type_name ()); |
|
365 return retval; |
|
366 } |
|
367 |
|
368 boolNDArray |
4580
|
369 octave_base_value::bool_array_value (void) const |
4550
|
370 { |
|
371 boolNDArray retval; |
|
372 gripe_wrong_type_arg ("octave_base_value::bool_array_value()", |
|
373 type_name ()); |
|
374 return retval; |
|
375 } |
|
376 |
2376
|
377 charMatrix |
|
378 octave_base_value::char_matrix_value (bool) const |
|
379 { |
|
380 charMatrix retval; |
4257
|
381 |
4452
|
382 octave_value tmp = convert_to_str (); |
4257
|
383 |
4452
|
384 if (! error_state) |
|
385 retval = tmp.char_matrix_value (); |
|
386 |
2376
|
387 return retval; |
|
388 } |
|
389 |
4550
|
390 charNDArray |
|
391 octave_base_value::char_array_value (bool) const |
|
392 { |
|
393 charNDArray retval; |
|
394 gripe_wrong_type_arg ("octave_base_value::char_array_value()", |
|
395 type_name ()); |
|
396 return retval; |
|
397 } |
|
398 |
2493
|
399 string_vector |
4457
|
400 octave_base_value::all_strings (bool pad, bool force) const |
2376
|
401 { |
2493
|
402 string_vector retval; |
4257
|
403 |
4457
|
404 octave_value tmp = convert_to_str (pad, force); |
4257
|
405 |
4452
|
406 if (! error_state) |
|
407 retval = tmp.all_strings (); |
4257
|
408 |
2376
|
409 return retval; |
|
410 } |
|
411 |
3536
|
412 std::string |
4457
|
413 octave_base_value::string_value (bool force) const |
2376
|
414 { |
3523
|
415 std::string retval; |
4257
|
416 |
4457
|
417 octave_value tmp = convert_to_str (force); |
4257
|
418 |
4452
|
419 if (! error_state) |
|
420 retval = tmp.string_value (); |
4257
|
421 |
2376
|
422 return retval; |
|
423 } |
|
424 |
|
425 Range |
|
426 octave_base_value::range_value (void) const |
|
427 { |
|
428 Range retval; |
|
429 gripe_wrong_type_arg ("octave_base_value::range_value()", type_name ()); |
|
430 return retval; |
|
431 } |
|
432 |
|
433 Octave_map |
|
434 octave_base_value::map_value (void) const |
|
435 { |
|
436 Octave_map retval; |
|
437 gripe_wrong_type_arg ("octave_base_value::map_value()", type_name ()); |
|
438 return retval; |
|
439 } |
|
440 |
3933
|
441 string_vector |
|
442 octave_base_value::map_keys (void) const |
|
443 { |
|
444 string_vector retval; |
|
445 gripe_wrong_type_arg ("octave_base_value::map_keys()", type_name ()); |
|
446 return retval; |
|
447 } |
|
448 |
3340
|
449 octave_stream |
2901
|
450 octave_base_value::stream_value (void) const |
|
451 { |
3340
|
452 octave_stream retval; |
2901
|
453 gripe_wrong_type_arg ("octave_base_value::stream_value()", type_name ()); |
|
454 return retval; |
|
455 } |
|
456 |
|
457 int |
|
458 octave_base_value::stream_number (void) const |
|
459 { |
|
460 int retval = -1; |
|
461 gripe_wrong_type_arg ("octave_base_value::stream_number()", type_name ()); |
|
462 return retval; |
|
463 } |
|
464 |
2974
|
465 octave_function * |
|
466 octave_base_value::function_value (bool silent) |
|
467 { |
|
468 octave_function *retval = 0; |
|
469 |
|
470 if (! silent) |
|
471 gripe_wrong_type_arg ("octave_base_value::function_value()", |
|
472 type_name ()); |
|
473 return retval; |
|
474 } |
|
475 |
4346
|
476 octave_fcn_handle * |
4343
|
477 octave_base_value::fcn_handle_value (bool silent) |
|
478 { |
4346
|
479 octave_fcn_handle *retval = 0; |
4343
|
480 |
|
481 if (! silent) |
|
482 gripe_wrong_type_arg ("octave_base_value::fcn_handle_value()", |
|
483 type_name ()); |
|
484 return retval; |
|
485 } |
|
486 |
2882
|
487 octave_value_list |
|
488 octave_base_value::list_value (void) const |
|
489 { |
|
490 octave_value_list retval; |
|
491 gripe_wrong_type_arg ("octave_base_value::list_value()", type_name ()); |
|
492 return retval; |
|
493 } |
|
494 |
3203
|
495 CONVDECLX (matrix_conv) |
2376
|
496 { |
|
497 return new octave_matrix (); |
|
498 } |
|
499 |
3203
|
500 CONVDECLX (complex_matrix_conv) |
2376
|
501 { |
|
502 return new octave_complex_matrix (); |
|
503 } |
|
504 |
3203
|
505 CONVDECLX (string_conv) |
2376
|
506 { |
|
507 return new octave_char_matrix_str (); |
|
508 } |
|
509 |
3928
|
510 CONVDECLX (cell_conv) |
|
511 { |
|
512 return new octave_cell (); |
|
513 } |
|
514 |
2376
|
515 void |
|
516 install_base_type_conversions (void) |
|
517 { |
|
518 INSTALL_ASSIGNCONV (octave_base_value, octave_scalar, octave_matrix); |
|
519 INSTALL_ASSIGNCONV (octave_base_value, octave_matrix, octave_matrix); |
|
520 INSTALL_ASSIGNCONV (octave_base_value, octave_complex, octave_complex_matrix); |
|
521 INSTALL_ASSIGNCONV (octave_base_value, octave_complex_matrix, octave_complex_matrix); |
|
522 INSTALL_ASSIGNCONV (octave_base_value, octave_range, octave_matrix); |
|
523 INSTALL_ASSIGNCONV (octave_base_value, octave_char_matrix_str, octave_char_matrix_str); |
3928
|
524 INSTALL_ASSIGNCONV (octave_base_value, octave_cell, octave_cell); |
2376
|
525 |
|
526 INSTALL_WIDENOP (octave_base_value, octave_matrix, matrix_conv); |
|
527 INSTALL_WIDENOP (octave_base_value, octave_complex_matrix, complex_matrix_conv); |
|
528 INSTALL_WIDENOP (octave_base_value, octave_char_matrix_str, string_conv); |
3928
|
529 INSTALL_WIDENOP (octave_base_value, octave_cell, cell_conv); |
2376
|
530 } |
|
531 |
|
532 /* |
|
533 ;;; Local Variables: *** |
|
534 ;;; mode: C++ *** |
|
535 ;;; End: *** |
|
536 */ |