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