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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2376
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include "ov-typeinfo.h" |
|
29 |
|
30 #include "defun.h" |
|
31 #include "error.h" |
|
32 #include "oct-obj.h" |
|
33 |
2479
|
34 const int |
|
35 octave_value_typeinfo::init_tab_sz (16); |
|
36 |
|
37 octave_value_typeinfo * |
|
38 octave_value_typeinfo::instance (0); |
2376
|
39 |
4515
|
40 #include <Array.h> |
|
41 #include <Array2.h> |
|
42 #include <Array3.h> |
|
43 |
2376
|
44 #include <Array.cc> |
|
45 |
5759
|
46 INSTANTIATE_ARRAY (octave_value_typeinfo::unary_op_fcn); |
|
47 template class Array2<octave_value_typeinfo::unary_op_fcn>; |
3203
|
48 |
5759
|
49 INSTANTIATE_ARRAY (octave_value_typeinfo::non_const_unary_op_fcn); |
|
50 template class Array2<octave_value_typeinfo::non_const_unary_op_fcn>; |
3203
|
51 |
5759
|
52 INSTANTIATE_ARRAY (octave_value_typeinfo::binary_op_fcn); |
|
53 template class Array2<octave_value_typeinfo::binary_op_fcn>; |
|
54 template class Array3<octave_value_typeinfo::binary_op_fcn>; |
2376
|
55 |
5759
|
56 INSTANTIATE_ARRAY (octave_value_typeinfo::cat_op_fcn); |
|
57 template class Array2<octave_value_typeinfo::cat_op_fcn>; |
4915
|
58 |
5759
|
59 INSTANTIATE_ARRAY (octave_value_typeinfo::assign_op_fcn); |
|
60 template class Array2<octave_value_typeinfo::assign_op_fcn>; |
|
61 template class Array3<octave_value_typeinfo::assign_op_fcn>; |
2376
|
62 |
5759
|
63 INSTANTIATE_ARRAY (octave_base_value::type_conv_fcn); |
|
64 template class Array2<octave_base_value::type_conv_fcn>; |
2376
|
65 |
2926
|
66 bool |
|
67 octave_value_typeinfo::instance_ok (void) |
|
68 { |
|
69 bool retval = true; |
|
70 if (! instance) |
|
71 instance = new octave_value_typeinfo (); |
|
72 |
|
73 if (! instance) |
|
74 { |
|
75 ::error ("unable to create value type info object!"); |
|
76 |
|
77 retval = false; |
|
78 } |
|
79 |
|
80 return retval; |
|
81 } |
|
82 |
2376
|
83 int |
4612
|
84 octave_value_typeinfo::register_type (const std::string& t_name, |
4640
|
85 const std::string& c_name, |
|
86 const octave_value& val) |
2376
|
87 { |
2926
|
88 return (instance_ok ()) |
4640
|
89 ? instance->do_register_type (t_name, c_name, val) : -1; |
2376
|
90 } |
|
91 |
|
92 bool |
3203
|
93 octave_value_typeinfo::register_unary_op (octave_value::unary_op op, |
5759
|
94 int t, octave_value_typeinfo::unary_op_fcn f) |
3203
|
95 { |
|
96 return (instance_ok ()) |
|
97 ? instance->do_register_unary_op (op, t, f) : false; |
|
98 } |
|
99 |
|
100 bool |
|
101 octave_value_typeinfo::register_non_const_unary_op (octave_value::unary_op op, |
|
102 int t, |
5759
|
103 octave_value_typeinfo::non_const_unary_op_fcn f) |
3203
|
104 { |
|
105 return (instance_ok ()) |
|
106 ? instance->do_register_non_const_unary_op (op, t, f) : false; |
|
107 } |
|
108 |
|
109 bool |
2376
|
110 octave_value_typeinfo::register_binary_op (octave_value::binary_op op, |
|
111 int t1, int t2, |
5759
|
112 octave_value_typeinfo::binary_op_fcn f) |
2376
|
113 { |
2926
|
114 return (instance_ok ()) |
|
115 ? instance->do_register_binary_op (op, t1, t2, f) : false; |
2376
|
116 } |
|
117 |
|
118 bool |
5759
|
119 octave_value_typeinfo::register_cat_op (int t1, int t2, octave_value_typeinfo::cat_op_fcn f) |
4915
|
120 { |
|
121 return (instance_ok ()) |
|
122 ? instance->do_register_cat_op (t1, t2, f) : false; |
|
123 } |
|
124 |
|
125 bool |
2882
|
126 octave_value_typeinfo::register_assign_op (octave_value::assign_op op, |
|
127 int t_lhs, int t_rhs, |
5759
|
128 octave_value_typeinfo::assign_op_fcn f) |
2376
|
129 { |
2926
|
130 return (instance_ok ()) |
|
131 ? instance->do_register_assign_op (op, t_lhs, t_rhs, f) : -1; |
2376
|
132 } |
|
133 |
|
134 bool |
3196
|
135 octave_value_typeinfo::register_assignany_op (octave_value::assign_op op, |
5759
|
136 int t_lhs, octave_value_typeinfo::assignany_op_fcn f) |
3196
|
137 { |
|
138 return (instance_ok ()) |
|
139 ? instance->do_register_assignany_op (op, t_lhs, f) : -1; |
|
140 } |
|
141 |
|
142 bool |
2376
|
143 octave_value_typeinfo::register_pref_assign_conv (int t_lhs, int t_rhs, |
|
144 int t_result) |
|
145 { |
2926
|
146 return (instance_ok ()) |
|
147 ? instance->do_register_pref_assign_conv (t_lhs, t_rhs, t_result) : false; |
2376
|
148 } |
|
149 |
|
150 bool |
4901
|
151 octave_value_typeinfo::register_type_conv_op (int t, int t_result, |
5759
|
152 octave_base_value::type_conv_fcn f) |
4901
|
153 { |
|
154 return (instance_ok ()) |
|
155 ? instance->do_register_type_conv_op (t, t_result, f) : false; |
|
156 } |
|
157 |
|
158 bool |
2376
|
159 octave_value_typeinfo::register_widening_op (int t, int t_result, |
5759
|
160 octave_base_value::type_conv_fcn f) |
2376
|
161 { |
2926
|
162 return (instance_ok ()) |
|
163 ? instance->do_register_widening_op (t, t_result, f) : false; |
2376
|
164 } |
|
165 |
5775
|
166 // FIXME -- we should also store all class names and provide a |
4687
|
167 // way to list them (calling class with nargin == 0?). |
|
168 |
2376
|
169 int |
4612
|
170 octave_value_typeinfo::do_register_type (const std::string& t_name, |
4640
|
171 const std::string& c_name, |
|
172 const octave_value& val) |
2376
|
173 { |
|
174 int i = 0; |
|
175 |
|
176 for (i = 0; i < num_types; i++) |
4612
|
177 if (t_name == types (i)) |
2376
|
178 return i; |
|
179 |
|
180 int len = types.length (); |
|
181 |
|
182 if (i == len) |
|
183 { |
|
184 len *= 2; |
|
185 |
4625
|
186 types.resize (len, std::string ()); |
2376
|
187 |
4640
|
188 vals.resize (len, octave_value ()); |
|
189 |
3203
|
190 unary_ops.resize (static_cast<int> (octave_value::num_unary_ops), |
5759
|
191 len, static_cast<octave_value_typeinfo::unary_op_fcn> (0)); |
3203
|
192 |
|
193 non_const_unary_ops.resize |
|
194 (static_cast<int> (octave_value::num_unary_ops), |
5759
|
195 len, static_cast<octave_value_typeinfo::non_const_unary_op_fcn> (0)); |
3203
|
196 |
2800
|
197 binary_ops.resize (static_cast<int> (octave_value::num_binary_ops), |
5759
|
198 len, len, static_cast<octave_value_typeinfo::binary_op_fcn> (0)); |
2376
|
199 |
5759
|
200 cat_ops.resize (len, len, static_cast<octave_value_typeinfo::cat_op_fcn> (0)); |
4915
|
201 |
2882
|
202 assign_ops.resize (static_cast<int> (octave_value::num_assign_ops), |
5759
|
203 len, len, static_cast<octave_value_typeinfo::assign_op_fcn> (0)); |
2376
|
204 |
3196
|
205 assignany_ops.resize (static_cast<int> (octave_value::num_assign_ops), |
5759
|
206 len, static_cast<octave_value_typeinfo::assignany_op_fcn> (0)); |
3196
|
207 |
2376
|
208 pref_assign_conv.resize (len, len, -1); |
|
209 |
5759
|
210 type_conv_ops.resize (len, len, static_cast<octave_base_value::type_conv_fcn> (0)); |
4901
|
211 |
5759
|
212 widening_ops.resize (len, len, static_cast<octave_base_value::type_conv_fcn> (0)); |
2376
|
213 } |
|
214 |
4612
|
215 types (i) = t_name; |
2376
|
216 |
4640
|
217 vals (i) = val; |
|
218 |
2376
|
219 num_types++; |
|
220 |
|
221 return i; |
|
222 } |
|
223 |
|
224 bool |
3203
|
225 octave_value_typeinfo::do_register_unary_op (octave_value::unary_op op, |
5759
|
226 int t, octave_value_typeinfo::unary_op_fcn f) |
3203
|
227 { |
4508
|
228 if (lookup_unary_op (op, t)) |
|
229 { |
|
230 std::string op_name = octave_value::unary_op_as_string (op); |
|
231 std::string type_name = types(t); |
|
232 |
|
233 warning ("duplicate unary operator `%s' for type `%s'", |
|
234 op_name.c_str (), type_name.c_str ()); |
|
235 } |
|
236 |
3203
|
237 unary_ops.checkelem (static_cast<int> (op), t) = f; |
|
238 |
|
239 return false; |
|
240 } |
|
241 |
|
242 bool |
|
243 octave_value_typeinfo::do_register_non_const_unary_op |
5759
|
244 (octave_value::unary_op op, int t, octave_value_typeinfo::non_const_unary_op_fcn f) |
3203
|
245 { |
4508
|
246 if (lookup_non_const_unary_op (op, t)) |
|
247 { |
|
248 std::string op_name = octave_value::unary_op_as_string (op); |
|
249 std::string type_name = types(t); |
|
250 |
|
251 warning ("duplicate unary operator `%s' for type `%s'", |
|
252 op_name.c_str (), type_name.c_str ()); |
|
253 } |
|
254 |
3203
|
255 non_const_unary_ops.checkelem (static_cast<int> (op), t) = f; |
|
256 |
|
257 return false; |
|
258 } |
|
259 |
|
260 bool |
2376
|
261 octave_value_typeinfo::do_register_binary_op (octave_value::binary_op op, |
|
262 int t1, int t2, |
5759
|
263 octave_value_typeinfo::binary_op_fcn f) |
2376
|
264 { |
4508
|
265 if (lookup_binary_op (op, t1, t2)) |
|
266 { |
|
267 std::string op_name = octave_value::binary_op_as_string (op); |
|
268 std::string t1_name = types(t1); |
|
269 std::string t2_name = types(t2); |
|
270 |
|
271 warning ("duplicate binary operator `%s' for types `%s' and `%s'", |
|
272 op_name.c_str (), t1_name.c_str (), t1_name.c_str ()); |
|
273 } |
|
274 |
2800
|
275 binary_ops.checkelem (static_cast<int> (op), t1, t2) = f; |
2376
|
276 |
|
277 return false; |
|
278 } |
|
279 |
|
280 bool |
5759
|
281 octave_value_typeinfo::do_register_cat_op (int t1, int t2, octave_value_typeinfo::cat_op_fcn f) |
4915
|
282 { |
|
283 if (lookup_cat_op (t1, t2)) |
|
284 { |
|
285 std::string t1_name = types(t1); |
|
286 std::string t2_name = types(t2); |
|
287 |
|
288 warning ("duplicate concatenation operator for types `%s' and `%s'", |
|
289 t1_name.c_str (), t1_name.c_str ()); |
|
290 } |
|
291 |
|
292 cat_ops.checkelem (t1, t2) = f; |
|
293 |
|
294 return false; |
|
295 } |
|
296 |
|
297 bool |
2882
|
298 octave_value_typeinfo::do_register_assign_op (octave_value::assign_op op, |
|
299 int t_lhs, int t_rhs, |
5759
|
300 octave_value_typeinfo::assign_op_fcn f) |
2376
|
301 { |
4508
|
302 if (lookup_assign_op (op, t_lhs, t_rhs)) |
|
303 { |
|
304 std::string op_name = octave_value::assign_op_as_string (op); |
|
305 std::string t_lhs_name = types(t_lhs); |
|
306 std::string t_rhs_name = types(t_rhs); |
|
307 |
|
308 warning ("duplicate assignment operator `%s' for types `%s' and `%s'", |
|
309 op_name.c_str (), t_lhs_name.c_str (), t_rhs_name.c_str ()); |
|
310 } |
|
311 |
2882
|
312 assign_ops.checkelem (static_cast<int> (op), t_lhs, t_rhs) = f; |
2376
|
313 |
|
314 return false; |
|
315 } |
|
316 |
|
317 bool |
3196
|
318 octave_value_typeinfo::do_register_assignany_op (octave_value::assign_op op, |
5759
|
319 int t_lhs, octave_value_typeinfo::assignany_op_fcn f) |
3196
|
320 { |
4508
|
321 if (lookup_assignany_op (op, t_lhs)) |
|
322 { |
|
323 std::string op_name = octave_value::assign_op_as_string (op); |
|
324 std::string t_lhs_name = types(t_lhs); |
|
325 |
|
326 warning ("duplicate assignment operator `%s' for types `%s'", |
|
327 op_name.c_str (), t_lhs_name.c_str ()); |
|
328 } |
|
329 |
3196
|
330 assignany_ops.checkelem (static_cast<int> (op), t_lhs) = f; |
|
331 |
|
332 return false; |
|
333 } |
|
334 |
|
335 bool |
2376
|
336 octave_value_typeinfo::do_register_pref_assign_conv (int t_lhs, int t_rhs, |
|
337 int t_result) |
|
338 { |
4508
|
339 if (lookup_pref_assign_conv (t_lhs, t_rhs) >= 0) |
|
340 { |
|
341 std::string t_lhs_name = types(t_lhs); |
|
342 std::string t_rhs_name = types(t_rhs); |
|
343 |
|
344 warning ("overriding assignment conversion for types `%s' and `%s'", |
|
345 t_lhs_name.c_str (), t_rhs_name.c_str ()); |
|
346 } |
|
347 |
2376
|
348 pref_assign_conv.checkelem (t_lhs, t_rhs) = t_result; |
|
349 |
|
350 return false; |
|
351 } |
|
352 |
|
353 bool |
4901
|
354 octave_value_typeinfo::do_register_type_conv_op |
5759
|
355 (int t, int t_result, octave_base_value::type_conv_fcn f) |
4901
|
356 { |
|
357 if (lookup_type_conv_op (t, t_result)) |
|
358 { |
|
359 std::string t_name = types(t); |
|
360 std::string t_result_name = types(t_result); |
|
361 |
|
362 warning ("overriding type conversion op for `%s' to `%s'", |
|
363 t_name.c_str (), t_result_name.c_str ()); |
|
364 } |
|
365 |
|
366 type_conv_ops.checkelem (t, t_result) = f; |
|
367 |
|
368 return false; |
|
369 } |
|
370 |
|
371 bool |
2376
|
372 octave_value_typeinfo::do_register_widening_op |
5759
|
373 (int t, int t_result, octave_base_value::type_conv_fcn f) |
2376
|
374 { |
4508
|
375 if (lookup_widening_op (t, t_result)) |
|
376 { |
|
377 std::string t_name = types(t); |
|
378 std::string t_result_name = types(t_result); |
|
379 |
|
380 warning ("overriding widening op for `%s' to `%s'", |
|
381 t_name.c_str (), t_result_name.c_str ()); |
|
382 } |
|
383 |
2376
|
384 widening_ops.checkelem (t, t_result) = f; |
|
385 |
|
386 return false; |
|
387 } |
|
388 |
4640
|
389 octave_value |
|
390 octave_value_typeinfo::do_lookup_type (const std::string& nm) |
|
391 { |
|
392 octave_value retval; |
|
393 |
|
394 for (int i = 0; i < num_types; i++) |
|
395 { |
|
396 if (nm == types(i)) |
|
397 { |
|
398 retval = vals(i); |
4687
|
399 retval.make_unique (); |
4640
|
400 break; |
|
401 } |
|
402 } |
|
403 |
|
404 return retval; |
|
405 } |
|
406 |
5759
|
407 octave_value_typeinfo::unary_op_fcn |
3203
|
408 octave_value_typeinfo::do_lookup_unary_op (octave_value::unary_op op, int t) |
|
409 { |
|
410 return unary_ops.checkelem (static_cast<int> (op), t); |
|
411 } |
|
412 |
5759
|
413 octave_value_typeinfo::non_const_unary_op_fcn |
3203
|
414 octave_value_typeinfo::do_lookup_non_const_unary_op |
|
415 (octave_value::unary_op op, int t) |
|
416 { |
|
417 return non_const_unary_ops.checkelem (static_cast<int> (op), t); |
|
418 } |
|
419 |
5759
|
420 octave_value_typeinfo::binary_op_fcn |
2376
|
421 octave_value_typeinfo::do_lookup_binary_op (octave_value::binary_op op, |
|
422 int t1, int t2) |
|
423 { |
2800
|
424 return binary_ops.checkelem (static_cast<int> (op), t1, t2); |
2376
|
425 } |
|
426 |
5759
|
427 octave_value_typeinfo::cat_op_fcn |
4915
|
428 octave_value_typeinfo::do_lookup_cat_op (int t1, int t2) |
|
429 { |
|
430 return cat_ops.checkelem (t1, t2); |
|
431 } |
|
432 |
5759
|
433 octave_value_typeinfo::assign_op_fcn |
2882
|
434 octave_value_typeinfo::do_lookup_assign_op (octave_value::assign_op op, |
|
435 int t_lhs, int t_rhs) |
2376
|
436 { |
2882
|
437 return assign_ops.checkelem (static_cast<int> (op), t_lhs, t_rhs); |
2376
|
438 } |
|
439 |
5759
|
440 octave_value_typeinfo::assignany_op_fcn |
3196
|
441 octave_value_typeinfo::do_lookup_assignany_op (octave_value::assign_op op, |
|
442 int t_lhs) |
|
443 { |
|
444 return assignany_ops.checkelem (static_cast<int> (op), t_lhs); |
|
445 } |
|
446 |
2376
|
447 int |
|
448 octave_value_typeinfo::do_lookup_pref_assign_conv (int t_lhs, int t_rhs) |
|
449 { |
|
450 return pref_assign_conv.checkelem (t_lhs, t_rhs); |
|
451 } |
|
452 |
5759
|
453 octave_base_value::type_conv_fcn |
4901
|
454 octave_value_typeinfo::do_lookup_type_conv_op (int t, int t_result) |
|
455 { |
|
456 return type_conv_ops.checkelem (t, t_result); |
|
457 } |
|
458 |
5759
|
459 octave_base_value::type_conv_fcn |
2376
|
460 octave_value_typeinfo::do_lookup_widening_op (int t, int t_result) |
|
461 { |
|
462 return widening_ops.checkelem (t, t_result); |
|
463 } |
|
464 |
|
465 string_vector |
|
466 octave_value_typeinfo::do_installed_type_names (void) |
|
467 { |
|
468 string_vector retval (num_types); |
|
469 |
|
470 for (int i = 0;i < num_types; i++) |
|
471 retval (i) = types (i); |
|
472 |
|
473 return retval; |
|
474 } |
|
475 |
2457
|
476 DEFUN (typeinfo, args, , |
3445
|
477 "-*- texinfo -*-\n\ |
|
478 @deftypefn {Built-in Function} {} typeinfo (@var{expr})\n\ |
2822
|
479 \n\ |
3445
|
480 Return the type of the expression @var{expr}, as a string. If\n\ |
|
481 @var{EXPR} is omitted, return an array of strings containing all the\n\ |
|
482 currently installed data types.\n\ |
|
483 @end deftypefn") |
2376
|
484 { |
|
485 octave_value retval; |
|
486 |
|
487 int nargin = args.length (); |
|
488 |
|
489 if (nargin == 0) |
|
490 retval = octave_value_typeinfo::installed_type_names (); |
2822
|
491 else if (nargin == 1) |
|
492 retval = args(0).type_name (); |
2376
|
493 else |
|
494 print_usage ("typeinfo"); |
|
495 |
|
496 return retval; |
|
497 } |
|
498 |
4612
|
499 DEFUN (class, args, , |
|
500 "-*- texinfo -*-\n\ |
|
501 @deftypefn {Built-in Function} {} class (@var{expr})\n\ |
|
502 \n\ |
|
503 Return the class of the expression @var{expr}, as a string.\n\ |
|
504 @end deftypefn") |
|
505 { |
|
506 octave_value retval; |
|
507 |
|
508 int nargin = args.length (); |
|
509 |
|
510 if (nargin == 1) |
|
511 retval = args(0).class_name (); |
|
512 else |
|
513 print_usage ("class"); |
|
514 |
|
515 return retval; |
|
516 } |
|
517 |
2376
|
518 /* |
|
519 ;;; Local Variables: *** |
|
520 ;;; mode: C++ *** |
|
521 ;;; End: *** |
|
522 */ |