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