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