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