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