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