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