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