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