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 |
4654
|
82 octave_function * |
|
83 octave_fcn_handle::function_value (bool) |
|
84 { |
|
85 octave_function *retval = 0; |
|
86 |
|
87 if (numel () > 0) |
|
88 { |
|
89 // XXX FIXME XXX -- is warn_fortran_indexing the right variable here? |
|
90 if (Vwarn_fortran_indexing) |
|
91 gripe_implicit_conversion ("function handle array", |
|
92 "scalar function handle"); |
|
93 |
|
94 fcn_handle_elt elt = matrix(0); |
|
95 |
|
96 retval = elt.function_value (); |
|
97 } |
|
98 else |
|
99 gripe_invalid_conversion ("function handle array", |
|
100 "scalar function handle"); |
|
101 |
|
102 return retval; |
|
103 } |
|
104 |
|
105 std::string |
|
106 octave_fcn_handle::name (void) const |
|
107 { |
|
108 std::string retval; |
|
109 |
|
110 if (numel () > 0) |
|
111 { |
|
112 // XXX FIXME XXX -- is warn_fortran_indexing the right variable here? |
|
113 if (Vwarn_fortran_indexing) |
|
114 gripe_implicit_conversion ("function handle array", |
|
115 "scalar function handle"); |
|
116 |
|
117 fcn_handle_elt elt = matrix(0); |
|
118 |
|
119 retval = elt.name (); |
|
120 } |
|
121 else |
|
122 gripe_invalid_conversion ("function handle array", |
|
123 "scalar function handle"); |
|
124 |
|
125 return retval; |
|
126 } |
|
127 |
|
128 ArrayN<std::string> |
|
129 fcn_handle_array::names (void) const |
|
130 { |
|
131 ArrayN<std::string> retval (dims ()); |
|
132 |
|
133 int nel = length (); |
|
134 |
|
135 for (int i = 0; i < nel; i++) |
|
136 { |
|
137 fcn_handle_elt elt = elem (i); |
|
138 |
|
139 retval(i) = elt.name (); |
|
140 } |
|
141 |
|
142 return retval; |
|
143 } |
|
144 |
4343
|
145 void |
|
146 octave_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax) const |
|
147 { |
|
148 print_raw (os, pr_as_read_syntax); |
|
149 newline (os); |
|
150 } |
|
151 |
|
152 void |
|
153 octave_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
|
154 { |
4654
|
155 dim_vector dv = matrix.dims (); |
|
156 os << "<" << dv.str () << " function handle object>"; |
|
157 |
|
158 #if 0 |
4343
|
159 indent (os); |
4654
|
160 octave_print_internal (os, name_array (), pr_as_read_syntax, |
|
161 current_print_indent_level (), true); |
|
162 #endif |
4343
|
163 } |
|
164 |
|
165 octave_value |
|
166 make_fcn_handle (const std::string& nm) |
|
167 { |
|
168 octave_value retval; |
|
169 |
|
170 octave_function *f = lookup_function (nm); |
|
171 |
|
172 if (f) |
4654
|
173 return fcn_handle_array (f, nm); |
4343
|
174 else |
|
175 error ("error creating function handle \"@%s\"", nm.c_str ()); |
|
176 |
|
177 return retval; |
|
178 } |
|
179 |
|
180 DEFUN (functions, args, , |
|
181 "-*- texinfo -*-\n\ |
|
182 @deftypefn {Built-in Function} {} functions (@var{fcn_handle})\n\ |
|
183 Return a struct containing information about the function handle\n\ |
|
184 @var{fcn_handle}.\n\ |
|
185 @end deftypefn") |
|
186 { |
|
187 octave_value retval; |
|
188 |
|
189 if (args.length () == 1) |
|
190 { |
4346
|
191 octave_fcn_handle *fh = args(0).fcn_handle_value (); |
4343
|
192 |
|
193 if (! error_state) |
|
194 { |
4346
|
195 octave_function *fcn = fh ? fh->function_value (true) : 0; |
4343
|
196 |
|
197 if (fcn) |
|
198 { |
|
199 Octave_map m; |
|
200 |
4649
|
201 std::string fh_nm = fh->name (); |
|
202 |
|
203 m ["function"](0) = fh_nm.substr (1); |
4343
|
204 |
|
205 if (fcn->is_nested_function ()) |
|
206 m ["type"](0) = "subfunction"; |
|
207 else |
|
208 m ["type"](0) = "simple"; |
|
209 |
|
210 std::string nm = fcn->fcn_file_name (); |
|
211 |
|
212 if (nm.empty ()) |
|
213 m ["file"](0) = "built-in function"; |
|
214 else |
|
215 m ["file"](0) = nm; |
|
216 |
|
217 retval = m; |
|
218 } |
|
219 else |
|
220 error ("functions: invalid function handle object"); |
|
221 } |
|
222 else |
|
223 error ("functions: argument must be a function handle object"); |
|
224 } |
|
225 else |
|
226 print_usage ("functions"); |
|
227 |
|
228 return retval; |
|
229 } |
|
230 |
|
231 DEFUN (func2str, args, , |
|
232 "-*- texinfo -*-\n\ |
|
233 @deftypefn {Built-in Function} {} func2str (@var{fcn_handle})\n\ |
|
234 Return a string containing the name of the function referenced by\n\ |
|
235 the function handle @var{fcn_handle}.\n\ |
|
236 @end deftypefn") |
|
237 { |
|
238 octave_value retval; |
|
239 |
|
240 if (args.length () == 1) |
|
241 { |
4346
|
242 octave_fcn_handle *fh = args(0).fcn_handle_value (); |
4343
|
243 |
4346
|
244 if (! error_state && fh) |
4649
|
245 { |
|
246 std::string fh_nm = fh->name (); |
|
247 retval = fh_nm.substr (1); |
|
248 } |
4343
|
249 else |
4346
|
250 error ("func2str: expecting valid function handle as first argument"); |
4343
|
251 } |
|
252 else |
|
253 print_usage ("func2str"); |
|
254 |
|
255 return retval; |
|
256 } |
|
257 |
|
258 DEFUN (str2func, args, , |
|
259 "-*- texinfo -*-\n\ |
|
260 @deftypefn {Built-in Function} {} str2func (@var{fcn_name})\n\ |
|
261 Return a function handle constructed from the string @var{fcn_name}.\n\ |
|
262 @end deftypefn") |
|
263 { |
|
264 octave_value retval; |
|
265 |
|
266 if (args.length () == 1) |
|
267 { |
|
268 std::string nm = args(0).string_value (); |
|
269 |
|
270 if (! error_state) |
|
271 retval = make_fcn_handle (nm); |
|
272 else |
|
273 error ("str2func: expecting string as first argument"); |
|
274 } |
|
275 else |
|
276 print_usage ("str2func"); |
|
277 |
|
278 return retval; |
|
279 } |
|
280 |
|
281 /* |
|
282 ;;; Local Variables: *** |
|
283 ;;; mode: C++ *** |
|
284 ;;; End: *** |
|
285 */ |