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