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