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