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