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