4343
|
1 /* |
|
2 |
|
3 Copyright (C) 2003 John W. Eaton |
|
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__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include <iostream> |
|
32 |
|
33 #include "defun.h" |
4654
|
34 #include "error.h" |
|
35 #include "gripes.h" |
4343
|
36 #include "oct-map.h" |
|
37 #include "ov-base.h" |
4654
|
38 #include "ov-base-mat.h" |
|
39 #include "ov-base-mat.cc" |
4343
|
40 #include "ov-fcn-handle.h" |
|
41 #include "pr-output.h" |
|
42 #include "variables.h" |
|
43 |
4654
|
44 // Instantiate Arrays of fcn_handle_elt values. |
|
45 |
|
46 #include "Array.h" |
|
47 #include "Array.cc" |
|
48 |
|
49 INSTANTIATE_ARRAY_AND_ASSIGN (fcn_handle_elt); |
|
50 |
|
51 #include "Array2.h" |
|
52 |
|
53 template class Array2<fcn_handle_elt>; |
|
54 |
|
55 #include "ArrayN.h" |
|
56 #include "ArrayN.cc" |
|
57 |
|
58 template class ArrayN<fcn_handle_elt>; |
|
59 |
|
60 template class octave_base_matrix<fcn_handle_array>; |
|
61 |
|
62 boolNDArray |
|
63 fcn_handle_array::all (int) const |
|
64 { |
|
65 error ("all: invalid call for function handle object"); |
|
66 return boolNDArray (); |
|
67 } |
|
68 |
|
69 boolNDArray |
|
70 fcn_handle_array::any (int) const |
|
71 { |
|
72 error ("any: invalid call for function handle object"); |
|
73 return boolNDArray (); |
|
74 } |
|
75 |
4343
|
76 DEFINE_OCTAVE_ALLOCATOR (octave_fcn_handle); |
|
77 |
4612
|
78 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_handle, |
|
79 "function handle", |
|
80 "function handle"); |
4343
|
81 |
4924
|
82 octave_value_list |
|
83 octave_fcn_handle::subsref (const std::string& type, |
|
84 const std::list<octave_value_list>& idx, |
|
85 int nargout) |
|
86 { |
|
87 octave_value_list retval; |
|
88 |
|
89 switch (type[0]) |
|
90 { |
|
91 case '(': |
|
92 { |
|
93 octave_function *f = function_value (); |
|
94 retval = f->subsref (type, idx, nargout); |
|
95 } |
|
96 break; |
|
97 |
|
98 |
|
99 case '{': |
|
100 case '.': |
|
101 { |
|
102 std::string nm = type_name (); |
|
103 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
|
104 } |
|
105 break; |
|
106 |
|
107 default: |
|
108 panic_impossible (); |
|
109 } |
|
110 |
|
111 // XXX FIXME XXX -- perhaps there should be an |
|
112 // octave_value_list::next_subsref member function? See also |
|
113 // octave_builtin::subsref. |
|
114 |
|
115 if (idx.size () > 1) |
|
116 retval = retval(0).next_subsref (type, idx); |
|
117 |
|
118 return retval; |
|
119 } |
|
120 |
4654
|
121 octave_function * |
|
122 octave_fcn_handle::function_value (bool) |
|
123 { |
|
124 octave_function *retval = 0; |
|
125 |
|
126 if (numel () > 0) |
|
127 { |
|
128 // XXX FIXME XXX -- is warn_fortran_indexing the right variable here? |
|
129 if (Vwarn_fortran_indexing) |
|
130 gripe_implicit_conversion ("function handle array", |
|
131 "scalar function handle"); |
|
132 |
|
133 fcn_handle_elt elt = matrix(0); |
|
134 |
|
135 retval = elt.function_value (); |
|
136 } |
|
137 else |
|
138 gripe_invalid_conversion ("function handle array", |
|
139 "scalar function handle"); |
|
140 |
|
141 return retval; |
|
142 } |
|
143 |
|
144 std::string |
|
145 octave_fcn_handle::name (void) const |
|
146 { |
|
147 std::string retval; |
|
148 |
|
149 if (numel () > 0) |
|
150 { |
|
151 // XXX FIXME XXX -- is warn_fortran_indexing the right variable here? |
|
152 if (Vwarn_fortran_indexing) |
|
153 gripe_implicit_conversion ("function handle array", |
|
154 "scalar function handle"); |
|
155 |
|
156 fcn_handle_elt elt = matrix(0); |
|
157 |
|
158 retval = elt.name (); |
|
159 } |
|
160 else |
|
161 gripe_invalid_conversion ("function handle array", |
|
162 "scalar function handle"); |
|
163 |
|
164 return retval; |
|
165 } |
|
166 |
|
167 ArrayN<std::string> |
|
168 fcn_handle_array::names (void) const |
|
169 { |
|
170 ArrayN<std::string> retval (dims ()); |
|
171 |
|
172 int nel = length (); |
|
173 |
|
174 for (int i = 0; i < nel; i++) |
|
175 { |
|
176 fcn_handle_elt elt = elem (i); |
|
177 |
|
178 retval(i) = elt.name (); |
|
179 } |
|
180 |
|
181 return retval; |
|
182 } |
|
183 |
4343
|
184 void |
|
185 octave_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax) const |
|
186 { |
|
187 print_raw (os, pr_as_read_syntax); |
|
188 newline (os); |
|
189 } |
|
190 |
|
191 void |
|
192 octave_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
|
193 { |
4654
|
194 octave_print_internal (os, name_array (), pr_as_read_syntax, |
4655
|
195 current_print_indent_level ()); |
4343
|
196 } |
|
197 |
|
198 octave_value |
|
199 make_fcn_handle (const std::string& nm) |
|
200 { |
|
201 octave_value retval; |
|
202 |
|
203 octave_function *f = lookup_function (nm); |
|
204 |
|
205 if (f) |
4654
|
206 return fcn_handle_array (f, nm); |
4343
|
207 else |
|
208 error ("error creating function handle \"@%s\"", nm.c_str ()); |
|
209 |
|
210 return retval; |
|
211 } |
|
212 |
|
213 DEFUN (functions, args, , |
|
214 "-*- texinfo -*-\n\ |
|
215 @deftypefn {Built-in Function} {} functions (@var{fcn_handle})\n\ |
|
216 Return a struct containing information about the function handle\n\ |
|
217 @var{fcn_handle}.\n\ |
|
218 @end deftypefn") |
|
219 { |
|
220 octave_value retval; |
|
221 |
|
222 if (args.length () == 1) |
|
223 { |
4346
|
224 octave_fcn_handle *fh = args(0).fcn_handle_value (); |
4343
|
225 |
|
226 if (! error_state) |
|
227 { |
4346
|
228 octave_function *fcn = fh ? fh->function_value (true) : 0; |
4343
|
229 |
|
230 if (fcn) |
|
231 { |
|
232 Octave_map m; |
|
233 |
4649
|
234 std::string fh_nm = fh->name (); |
|
235 |
4675
|
236 m.assign ("function", fh_nm.substr (1)); |
4343
|
237 |
|
238 if (fcn->is_nested_function ()) |
4675
|
239 m.assign ("type", "subfunction"); |
4343
|
240 else |
4675
|
241 m.assign ("type", "simple"); |
4343
|
242 |
|
243 std::string nm = fcn->fcn_file_name (); |
|
244 |
|
245 if (nm.empty ()) |
4675
|
246 m.assign ("file", "built-in function"); |
4343
|
247 else |
4675
|
248 m.assign ("file", nm); |
4343
|
249 |
|
250 retval = m; |
|
251 } |
|
252 else |
|
253 error ("functions: invalid function handle object"); |
|
254 } |
|
255 else |
|
256 error ("functions: argument must be a function handle object"); |
|
257 } |
|
258 else |
|
259 print_usage ("functions"); |
|
260 |
|
261 return retval; |
|
262 } |
|
263 |
|
264 DEFUN (func2str, args, , |
|
265 "-*- texinfo -*-\n\ |
|
266 @deftypefn {Built-in Function} {} func2str (@var{fcn_handle})\n\ |
|
267 Return a string containing the name of the function referenced by\n\ |
|
268 the function handle @var{fcn_handle}.\n\ |
|
269 @end deftypefn") |
|
270 { |
|
271 octave_value retval; |
|
272 |
|
273 if (args.length () == 1) |
|
274 { |
4346
|
275 octave_fcn_handle *fh = args(0).fcn_handle_value (); |
4343
|
276 |
4346
|
277 if (! error_state && fh) |
4649
|
278 { |
|
279 std::string fh_nm = fh->name (); |
|
280 retval = fh_nm.substr (1); |
|
281 } |
4343
|
282 else |
4346
|
283 error ("func2str: expecting valid function handle as first argument"); |
4343
|
284 } |
|
285 else |
|
286 print_usage ("func2str"); |
|
287 |
|
288 return retval; |
|
289 } |
|
290 |
|
291 DEFUN (str2func, args, , |
|
292 "-*- texinfo -*-\n\ |
|
293 @deftypefn {Built-in Function} {} str2func (@var{fcn_name})\n\ |
|
294 Return a function handle constructed from the string @var{fcn_name}.\n\ |
|
295 @end deftypefn") |
|
296 { |
|
297 octave_value retval; |
|
298 |
|
299 if (args.length () == 1) |
|
300 { |
|
301 std::string nm = args(0).string_value (); |
|
302 |
|
303 if (! error_state) |
|
304 retval = make_fcn_handle (nm); |
|
305 else |
|
306 error ("str2func: expecting string as first argument"); |
|
307 } |
|
308 else |
|
309 print_usage ("str2func"); |
|
310 |
|
311 return retval; |
|
312 } |
|
313 |
|
314 /* |
|
315 ;;; Local Variables: *** |
|
316 ;;; mode: C++ *** |
|
317 ;;; End: *** |
|
318 */ |