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