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 |
|
23 #if defined (__GNUG__) |
|
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 { |
|
196 unary_ops.checkelem (static_cast<int> (op), t) = f; |
|
197 |
|
198 return false; |
|
199 } |
|
200 |
|
201 bool |
|
202 octave_value_typeinfo::do_register_non_const_unary_op |
|
203 (octave_value::unary_op op, int t, non_const_unary_op_fcn f) |
|
204 { |
|
205 non_const_unary_ops.checkelem (static_cast<int> (op), t) = f; |
|
206 |
|
207 return false; |
|
208 } |
|
209 |
|
210 bool |
2376
|
211 octave_value_typeinfo::do_register_binary_op (octave_value::binary_op op, |
|
212 int t1, int t2, |
2427
|
213 binary_op_fcn f) |
2376
|
214 { |
2800
|
215 binary_ops.checkelem (static_cast<int> (op), t1, t2) = f; |
2376
|
216 |
|
217 return false; |
|
218 } |
|
219 |
|
220 bool |
2882
|
221 octave_value_typeinfo::do_register_assign_op (octave_value::assign_op op, |
|
222 int t_lhs, int t_rhs, |
2427
|
223 assign_op_fcn f) |
2376
|
224 { |
2882
|
225 assign_ops.checkelem (static_cast<int> (op), t_lhs, t_rhs) = f; |
2376
|
226 |
|
227 return false; |
|
228 } |
|
229 |
|
230 bool |
3196
|
231 octave_value_typeinfo::do_register_assignany_op (octave_value::assign_op op, |
|
232 int t_lhs, assign_op_fcn f) |
|
233 { |
|
234 assignany_ops.checkelem (static_cast<int> (op), t_lhs) = f; |
|
235 |
|
236 return false; |
|
237 } |
|
238 |
|
239 bool |
2376
|
240 octave_value_typeinfo::do_register_pref_assign_conv (int t_lhs, int t_rhs, |
|
241 int t_result) |
|
242 { |
|
243 pref_assign_conv.checkelem (t_lhs, t_rhs) = t_result; |
|
244 |
|
245 return false; |
|
246 } |
|
247 |
|
248 bool |
|
249 octave_value_typeinfo::do_register_widening_op |
2427
|
250 (int t, int t_result, type_conv_fcn f) |
2376
|
251 { |
|
252 widening_ops.checkelem (t, t_result) = f; |
|
253 |
|
254 return false; |
|
255 } |
|
256 |
3203
|
257 unary_op_fcn |
|
258 octave_value_typeinfo::do_lookup_unary_op (octave_value::unary_op op, int t) |
|
259 { |
|
260 return unary_ops.checkelem (static_cast<int> (op), t); |
|
261 } |
|
262 |
|
263 non_const_unary_op_fcn |
|
264 octave_value_typeinfo::do_lookup_non_const_unary_op |
|
265 (octave_value::unary_op op, int t) |
|
266 { |
|
267 return non_const_unary_ops.checkelem (static_cast<int> (op), t); |
|
268 } |
|
269 |
2427
|
270 binary_op_fcn |
2376
|
271 octave_value_typeinfo::do_lookup_binary_op (octave_value::binary_op op, |
|
272 int t1, int t2) |
|
273 { |
2800
|
274 return binary_ops.checkelem (static_cast<int> (op), t1, t2); |
2376
|
275 } |
|
276 |
2427
|
277 assign_op_fcn |
2882
|
278 octave_value_typeinfo::do_lookup_assign_op (octave_value::assign_op op, |
|
279 int t_lhs, int t_rhs) |
2376
|
280 { |
2882
|
281 return assign_ops.checkelem (static_cast<int> (op), t_lhs, t_rhs); |
2376
|
282 } |
|
283 |
3196
|
284 assign_op_fcn |
|
285 octave_value_typeinfo::do_lookup_assignany_op (octave_value::assign_op op, |
|
286 int t_lhs) |
|
287 { |
|
288 return assignany_ops.checkelem (static_cast<int> (op), t_lhs); |
|
289 } |
|
290 |
2376
|
291 int |
|
292 octave_value_typeinfo::do_lookup_pref_assign_conv (int t_lhs, int t_rhs) |
|
293 { |
|
294 return pref_assign_conv.checkelem (t_lhs, t_rhs); |
|
295 } |
|
296 |
2427
|
297 type_conv_fcn |
2376
|
298 octave_value_typeinfo::do_lookup_widening_op (int t, int t_result) |
|
299 { |
|
300 return widening_ops.checkelem (t, t_result); |
|
301 } |
|
302 |
|
303 string_vector |
|
304 octave_value_typeinfo::do_installed_type_names (void) |
|
305 { |
|
306 string_vector retval (num_types); |
|
307 |
|
308 for (int i = 0;i < num_types; i++) |
|
309 retval (i) = types (i); |
|
310 |
|
311 return retval; |
|
312 } |
|
313 |
2457
|
314 DEFUN (typeinfo, args, , |
3445
|
315 "-*- texinfo -*-\n\ |
|
316 @deftypefn {Built-in Function} {} typeinfo (@var{expr})\n\ |
2822
|
317 \n\ |
3445
|
318 Return the type of the expression @var{expr}, as a string. If\n\ |
|
319 @var{EXPR} is omitted, return an array of strings containing all the\n\ |
|
320 currently installed data types.\n\ |
|
321 @end deftypefn") |
2376
|
322 { |
|
323 octave_value retval; |
|
324 |
|
325 int nargin = args.length (); |
|
326 |
|
327 if (nargin == 0) |
|
328 retval = octave_value_typeinfo::installed_type_names (); |
2822
|
329 else if (nargin == 1) |
|
330 retval = args(0).type_name (); |
2376
|
331 else |
|
332 print_usage ("typeinfo"); |
|
333 |
|
334 return retval; |
|
335 } |
|
336 |
|
337 /* |
|
338 ;;; Local Variables: *** |
|
339 ;;; mode: C++ *** |
|
340 ;;; End: *** |
|
341 */ |