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 octave_print_internal (os, name_array (), pr_as_read_syntax, |
4655
|
156 current_print_indent_level ()); |
4343
|
157 } |
|
158 |
|
159 octave_value |
|
160 make_fcn_handle (const std::string& nm) |
|
161 { |
|
162 octave_value retval; |
|
163 |
|
164 octave_function *f = lookup_function (nm); |
|
165 |
|
166 if (f) |
4654
|
167 return fcn_handle_array (f, nm); |
4343
|
168 else |
|
169 error ("error creating function handle \"@%s\"", nm.c_str ()); |
|
170 |
|
171 return retval; |
|
172 } |
|
173 |
|
174 DEFUN (functions, args, , |
|
175 "-*- texinfo -*-\n\ |
|
176 @deftypefn {Built-in Function} {} functions (@var{fcn_handle})\n\ |
|
177 Return a struct containing information about the function handle\n\ |
|
178 @var{fcn_handle}.\n\ |
|
179 @end deftypefn") |
|
180 { |
|
181 octave_value retval; |
|
182 |
|
183 if (args.length () == 1) |
|
184 { |
4346
|
185 octave_fcn_handle *fh = args(0).fcn_handle_value (); |
4343
|
186 |
|
187 if (! error_state) |
|
188 { |
4346
|
189 octave_function *fcn = fh ? fh->function_value (true) : 0; |
4343
|
190 |
|
191 if (fcn) |
|
192 { |
|
193 Octave_map m; |
|
194 |
4649
|
195 std::string fh_nm = fh->name (); |
|
196 |
4675
|
197 m.assign ("function", fh_nm.substr (1)); |
4343
|
198 |
|
199 if (fcn->is_nested_function ()) |
4675
|
200 m.assign ("type", "subfunction"); |
4343
|
201 else |
4675
|
202 m.assign ("type", "simple"); |
4343
|
203 |
|
204 std::string nm = fcn->fcn_file_name (); |
|
205 |
|
206 if (nm.empty ()) |
4675
|
207 m.assign ("file", "built-in function"); |
4343
|
208 else |
4675
|
209 m.assign ("file", nm); |
4343
|
210 |
|
211 retval = m; |
|
212 } |
|
213 else |
|
214 error ("functions: invalid function handle object"); |
|
215 } |
|
216 else |
|
217 error ("functions: argument must be a function handle object"); |
|
218 } |
|
219 else |
|
220 print_usage ("functions"); |
|
221 |
|
222 return retval; |
|
223 } |
|
224 |
|
225 DEFUN (func2str, args, , |
|
226 "-*- texinfo -*-\n\ |
|
227 @deftypefn {Built-in Function} {} func2str (@var{fcn_handle})\n\ |
|
228 Return a string containing the name of the function referenced by\n\ |
|
229 the function handle @var{fcn_handle}.\n\ |
|
230 @end deftypefn") |
|
231 { |
|
232 octave_value retval; |
|
233 |
|
234 if (args.length () == 1) |
|
235 { |
4346
|
236 octave_fcn_handle *fh = args(0).fcn_handle_value (); |
4343
|
237 |
4346
|
238 if (! error_state && fh) |
4649
|
239 { |
|
240 std::string fh_nm = fh->name (); |
|
241 retval = fh_nm.substr (1); |
|
242 } |
4343
|
243 else |
4346
|
244 error ("func2str: expecting valid function handle as first argument"); |
4343
|
245 } |
|
246 else |
|
247 print_usage ("func2str"); |
|
248 |
|
249 return retval; |
|
250 } |
|
251 |
|
252 DEFUN (str2func, args, , |
|
253 "-*- texinfo -*-\n\ |
|
254 @deftypefn {Built-in Function} {} str2func (@var{fcn_name})\n\ |
|
255 Return a function handle constructed from the string @var{fcn_name}.\n\ |
|
256 @end deftypefn") |
|
257 { |
|
258 octave_value retval; |
|
259 |
|
260 if (args.length () == 1) |
|
261 { |
|
262 std::string nm = args(0).string_value (); |
|
263 |
|
264 if (! error_state) |
|
265 retval = make_fcn_handle (nm); |
|
266 else |
|
267 error ("str2func: expecting string as first argument"); |
|
268 } |
|
269 else |
|
270 print_usage ("str2func"); |
|
271 |
|
272 return retval; |
|
273 } |
|
274 |
|
275 /* |
|
276 ;;; Local Variables: *** |
|
277 ;;; mode: C++ *** |
|
278 ;;; End: *** |
|
279 */ |