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 |
|
144 octave_value_typeinfo::register_widening_op (int t, int t_result, |
2427
|
145 type_conv_fcn f) |
2376
|
146 { |
2926
|
147 return (instance_ok ()) |
|
148 ? instance->do_register_widening_op (t, t_result, f) : false; |
2376
|
149 } |
|
150 |
4687
|
151 // XXX FIXME XXX -- we should also store all class names and provide a |
|
152 // way to list them (calling class with nargin == 0?). |
|
153 |
2376
|
154 int |
4612
|
155 octave_value_typeinfo::do_register_type (const std::string& t_name, |
4640
|
156 const std::string& c_name, |
|
157 const octave_value& val) |
2376
|
158 { |
|
159 int i = 0; |
|
160 |
|
161 for (i = 0; i < num_types; i++) |
4612
|
162 if (t_name == types (i)) |
2376
|
163 return i; |
|
164 |
|
165 int len = types.length (); |
|
166 |
|
167 if (i == len) |
|
168 { |
|
169 len *= 2; |
|
170 |
4625
|
171 types.resize (len, std::string ()); |
2376
|
172 |
4640
|
173 vals.resize (len, octave_value ()); |
|
174 |
3203
|
175 unary_ops.resize (static_cast<int> (octave_value::num_unary_ops), |
|
176 len, static_cast<unary_op_fcn> (0)); |
|
177 |
|
178 non_const_unary_ops.resize |
|
179 (static_cast<int> (octave_value::num_unary_ops), |
|
180 len, static_cast<non_const_unary_op_fcn> (0)); |
|
181 |
2800
|
182 binary_ops.resize (static_cast<int> (octave_value::num_binary_ops), |
|
183 len, len, static_cast<binary_op_fcn> (0)); |
2376
|
184 |
2882
|
185 assign_ops.resize (static_cast<int> (octave_value::num_assign_ops), |
|
186 len, len, static_cast<assign_op_fcn> (0)); |
2376
|
187 |
3196
|
188 assignany_ops.resize (static_cast<int> (octave_value::num_assign_ops), |
|
189 len, static_cast<assign_op_fcn> (0)); |
|
190 |
2376
|
191 pref_assign_conv.resize (len, len, -1); |
|
192 |
2800
|
193 widening_ops.resize (len, len, static_cast<type_conv_fcn> (0)); |
2376
|
194 } |
|
195 |
4612
|
196 types (i) = t_name; |
2376
|
197 |
4640
|
198 vals (i) = val; |
|
199 |
2376
|
200 num_types++; |
|
201 |
|
202 return i; |
|
203 } |
|
204 |
|
205 bool |
3203
|
206 octave_value_typeinfo::do_register_unary_op (octave_value::unary_op op, |
|
207 int t, unary_op_fcn f) |
|
208 { |
4508
|
209 if (lookup_unary_op (op, t)) |
|
210 { |
|
211 std::string op_name = octave_value::unary_op_as_string (op); |
|
212 std::string type_name = types(t); |
|
213 |
|
214 warning ("duplicate unary operator `%s' for type `%s'", |
|
215 op_name.c_str (), type_name.c_str ()); |
|
216 } |
|
217 |
3203
|
218 unary_ops.checkelem (static_cast<int> (op), t) = f; |
|
219 |
|
220 return false; |
|
221 } |
|
222 |
|
223 bool |
|
224 octave_value_typeinfo::do_register_non_const_unary_op |
|
225 (octave_value::unary_op op, int t, non_const_unary_op_fcn f) |
|
226 { |
4508
|
227 if (lookup_non_const_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 non_const_unary_ops.checkelem (static_cast<int> (op), t) = f; |
|
237 |
|
238 return false; |
|
239 } |
|
240 |
|
241 bool |
2376
|
242 octave_value_typeinfo::do_register_binary_op (octave_value::binary_op op, |
|
243 int t1, int t2, |
2427
|
244 binary_op_fcn f) |
2376
|
245 { |
4508
|
246 if (lookup_binary_op (op, t1, t2)) |
|
247 { |
|
248 std::string op_name = octave_value::binary_op_as_string (op); |
|
249 std::string t1_name = types(t1); |
|
250 std::string t2_name = types(t2); |
|
251 |
|
252 warning ("duplicate binary operator `%s' for types `%s' and `%s'", |
|
253 op_name.c_str (), t1_name.c_str (), t1_name.c_str ()); |
|
254 } |
|
255 |
2800
|
256 binary_ops.checkelem (static_cast<int> (op), t1, t2) = f; |
2376
|
257 |
|
258 return false; |
|
259 } |
|
260 |
|
261 bool |
2882
|
262 octave_value_typeinfo::do_register_assign_op (octave_value::assign_op op, |
|
263 int t_lhs, int t_rhs, |
2427
|
264 assign_op_fcn f) |
2376
|
265 { |
4508
|
266 if (lookup_assign_op (op, t_lhs, t_rhs)) |
|
267 { |
|
268 std::string op_name = octave_value::assign_op_as_string (op); |
|
269 std::string t_lhs_name = types(t_lhs); |
|
270 std::string t_rhs_name = types(t_rhs); |
|
271 |
|
272 warning ("duplicate assignment operator `%s' for types `%s' and `%s'", |
|
273 op_name.c_str (), t_lhs_name.c_str (), t_rhs_name.c_str ()); |
|
274 } |
|
275 |
2882
|
276 assign_ops.checkelem (static_cast<int> (op), t_lhs, t_rhs) = f; |
2376
|
277 |
|
278 return false; |
|
279 } |
|
280 |
|
281 bool |
3196
|
282 octave_value_typeinfo::do_register_assignany_op (octave_value::assign_op op, |
4508
|
283 int t_lhs, assign_op_fcn f) |
3196
|
284 { |
4508
|
285 if (lookup_assignany_op (op, t_lhs)) |
|
286 { |
|
287 std::string op_name = octave_value::assign_op_as_string (op); |
|
288 std::string t_lhs_name = types(t_lhs); |
|
289 |
|
290 warning ("duplicate assignment operator `%s' for types `%s'", |
|
291 op_name.c_str (), t_lhs_name.c_str ()); |
|
292 } |
|
293 |
3196
|
294 assignany_ops.checkelem (static_cast<int> (op), t_lhs) = f; |
|
295 |
|
296 return false; |
|
297 } |
|
298 |
|
299 bool |
2376
|
300 octave_value_typeinfo::do_register_pref_assign_conv (int t_lhs, int t_rhs, |
|
301 int t_result) |
|
302 { |
4508
|
303 if (lookup_pref_assign_conv (t_lhs, t_rhs) >= 0) |
|
304 { |
|
305 std::string t_lhs_name = types(t_lhs); |
|
306 std::string t_rhs_name = types(t_rhs); |
|
307 |
|
308 warning ("overriding assignment conversion for types `%s' and `%s'", |
|
309 t_lhs_name.c_str (), t_rhs_name.c_str ()); |
|
310 } |
|
311 |
2376
|
312 pref_assign_conv.checkelem (t_lhs, t_rhs) = t_result; |
|
313 |
|
314 return false; |
|
315 } |
|
316 |
|
317 bool |
|
318 octave_value_typeinfo::do_register_widening_op |
2427
|
319 (int t, int t_result, type_conv_fcn f) |
2376
|
320 { |
4508
|
321 if (lookup_widening_op (t, t_result)) |
|
322 { |
|
323 std::string t_name = types(t); |
|
324 std::string t_result_name = types(t_result); |
|
325 |
|
326 warning ("overriding widening op for `%s' to `%s'", |
|
327 t_name.c_str (), t_result_name.c_str ()); |
|
328 } |
|
329 |
2376
|
330 widening_ops.checkelem (t, t_result) = f; |
|
331 |
|
332 return false; |
|
333 } |
|
334 |
4640
|
335 octave_value |
|
336 octave_value_typeinfo::do_lookup_type (const std::string& nm) |
|
337 { |
|
338 octave_value retval; |
|
339 |
|
340 for (int i = 0; i < num_types; i++) |
|
341 { |
|
342 if (nm == types(i)) |
|
343 { |
|
344 retval = vals(i); |
4687
|
345 retval.make_unique (); |
4640
|
346 break; |
|
347 } |
|
348 } |
|
349 |
|
350 return retval; |
|
351 } |
|
352 |
3203
|
353 unary_op_fcn |
|
354 octave_value_typeinfo::do_lookup_unary_op (octave_value::unary_op op, int t) |
|
355 { |
|
356 return unary_ops.checkelem (static_cast<int> (op), t); |
|
357 } |
|
358 |
|
359 non_const_unary_op_fcn |
|
360 octave_value_typeinfo::do_lookup_non_const_unary_op |
|
361 (octave_value::unary_op op, int t) |
|
362 { |
|
363 return non_const_unary_ops.checkelem (static_cast<int> (op), t); |
|
364 } |
|
365 |
2427
|
366 binary_op_fcn |
2376
|
367 octave_value_typeinfo::do_lookup_binary_op (octave_value::binary_op op, |
|
368 int t1, int t2) |
|
369 { |
2800
|
370 return binary_ops.checkelem (static_cast<int> (op), t1, t2); |
2376
|
371 } |
|
372 |
2427
|
373 assign_op_fcn |
2882
|
374 octave_value_typeinfo::do_lookup_assign_op (octave_value::assign_op op, |
|
375 int t_lhs, int t_rhs) |
2376
|
376 { |
2882
|
377 return assign_ops.checkelem (static_cast<int> (op), t_lhs, t_rhs); |
2376
|
378 } |
|
379 |
3196
|
380 assign_op_fcn |
|
381 octave_value_typeinfo::do_lookup_assignany_op (octave_value::assign_op op, |
|
382 int t_lhs) |
|
383 { |
|
384 return assignany_ops.checkelem (static_cast<int> (op), t_lhs); |
|
385 } |
|
386 |
2376
|
387 int |
|
388 octave_value_typeinfo::do_lookup_pref_assign_conv (int t_lhs, int t_rhs) |
|
389 { |
|
390 return pref_assign_conv.checkelem (t_lhs, t_rhs); |
|
391 } |
|
392 |
2427
|
393 type_conv_fcn |
2376
|
394 octave_value_typeinfo::do_lookup_widening_op (int t, int t_result) |
|
395 { |
|
396 return widening_ops.checkelem (t, t_result); |
|
397 } |
|
398 |
|
399 string_vector |
|
400 octave_value_typeinfo::do_installed_type_names (void) |
|
401 { |
|
402 string_vector retval (num_types); |
|
403 |
|
404 for (int i = 0;i < num_types; i++) |
|
405 retval (i) = types (i); |
|
406 |
|
407 return retval; |
|
408 } |
|
409 |
2457
|
410 DEFUN (typeinfo, args, , |
3445
|
411 "-*- texinfo -*-\n\ |
|
412 @deftypefn {Built-in Function} {} typeinfo (@var{expr})\n\ |
2822
|
413 \n\ |
3445
|
414 Return the type of the expression @var{expr}, as a string. If\n\ |
|
415 @var{EXPR} is omitted, return an array of strings containing all the\n\ |
|
416 currently installed data types.\n\ |
|
417 @end deftypefn") |
2376
|
418 { |
|
419 octave_value retval; |
|
420 |
|
421 int nargin = args.length (); |
|
422 |
|
423 if (nargin == 0) |
|
424 retval = octave_value_typeinfo::installed_type_names (); |
2822
|
425 else if (nargin == 1) |
|
426 retval = args(0).type_name (); |
2376
|
427 else |
|
428 print_usage ("typeinfo"); |
|
429 |
|
430 return retval; |
|
431 } |
|
432 |
4612
|
433 DEFUN (class, args, , |
|
434 "-*- texinfo -*-\n\ |
|
435 @deftypefn {Built-in Function} {} class (@var{expr})\n\ |
|
436 \n\ |
|
437 Return the class of the expression @var{expr}, as a string.\n\ |
|
438 @end deftypefn") |
|
439 { |
|
440 octave_value retval; |
|
441 |
|
442 int nargin = args.length (); |
|
443 |
|
444 if (nargin == 1) |
|
445 retval = args(0).class_name (); |
|
446 else |
|
447 print_usage ("class"); |
|
448 |
|
449 return retval; |
|
450 } |
|
451 |
2376
|
452 /* |
|
453 ;;; Local Variables: *** |
|
454 ;;; mode: C++ *** |
|
455 ;;; End: *** |
|
456 */ |