Mercurial > octave-nkf
annotate src/ov-fcn-handle.cc @ 9463:d34baf412786
support non-local function lookups in str2func
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 27 Jul 2009 10:39:09 +0200 |
parents | 0c7d84a65386 |
children | 2ebd0717c12d |
rev | line source |
---|---|
4343 | 1 /* |
2 | |
8920 | 3 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 John W. Eaton |
4343 | 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 | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
4343 | 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 | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
4343 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
26 | |
27 #include <iostream> | |
5765 | 28 #include <sstream> |
5164 | 29 #include <vector> |
4343 | 30 |
7336 | 31 #include "file-ops.h" |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
32 #include "oct-locbuf.h" |
7336 | 33 |
4343 | 34 #include "defun.h" |
4654 | 35 #include "error.h" |
36 #include "gripes.h" | |
5663 | 37 #include "input.h" |
4343 | 38 #include "oct-map.h" |
39 #include "ov-base.h" | |
40 #include "ov-fcn-handle.h" | |
4980 | 41 #include "ov-usr-fcn.h" |
4343 | 42 #include "pr-output.h" |
4980 | 43 #include "pt-pr-code.h" |
44 #include "pt-misc.h" | |
45 #include "pt-stmt.h" | |
46 #include "pt-cmd.h" | |
47 #include "pt-exp.h" | |
48 #include "pt-assign.h" | |
4343 | 49 #include "variables.h" |
4988 | 50 #include "parse.h" |
6625 | 51 #include "unwind-prot.h" |
52 #include "defaults.h" | |
53 #include "file-stat.h" | |
54 #include "load-path.h" | |
55 #include "oct-env.h" | |
4988 | 56 |
57 #include "byte-swap.h" | |
8946
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
58 #include "ls-ascii-helper.h" |
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
59 #include "ls-hdf5.h" |
4988 | 60 #include "ls-oct-ascii.h" |
6625 | 61 #include "ls-oct-binary.h" |
4988 | 62 #include "ls-utils.h" |
4343 | 63 |
64 DEFINE_OCTAVE_ALLOCATOR (octave_fcn_handle); | |
65 | |
4612 | 66 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_handle, |
67 "function handle", | |
5946 | 68 "function_handle"); |
4343 | 69 |
7761
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
70 octave_fcn_handle::octave_fcn_handle (const octave_value& f, |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
71 const std::string& n) |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
72 : fcn (f), nm (n) |
7761
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
73 { |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
74 octave_user_function *uf = fcn.user_function_value (true); |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
75 |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
76 if (uf) |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
77 symbol_table::cache_name (uf->scope (), nm); |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
78 } |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
79 |
4924 | 80 octave_value_list |
81 octave_fcn_handle::subsref (const std::string& type, | |
82 const std::list<octave_value_list>& idx, | |
83 int nargout) | |
84 { | |
85 octave_value_list retval; | |
86 | |
87 switch (type[0]) | |
88 { | |
89 case '(': | |
90 { | |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
91 int tmp_nargout = (type.length () > 1 && nargout == 0) ? 1 : nargout; |
5663 | 92 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
93 retval = do_multi_index_op (tmp_nargout, idx.front ()); |
4924 | 94 } |
95 break; | |
96 | |
97 case '{': | |
98 case '.': | |
99 { | |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
100 std::string tnm = type_name (); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
101 error ("%s cannot be indexed with %c", tnm.c_str (), type[0]); |
4924 | 102 } |
103 break; | |
104 | |
105 default: | |
106 panic_impossible (); | |
107 } | |
108 | |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
109 // FIXME -- perhaps there should be an |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
110 // octave_value_list::next_subsref member function? See also |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
111 // octave_builtin::subsref. |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
112 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
113 if (idx.size () > 1) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
114 retval = retval(0).next_subsref (nargout, type, idx); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
115 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
116 return retval; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
117 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
118 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
119 octave_value_list |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
120 octave_fcn_handle::do_multi_index_op (int nargout, |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
121 const octave_value_list& args) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
122 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
123 octave_value_list retval; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
124 |
9458
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
125 if (fcn.is_defined ()) |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
126 out_of_date_check (fcn); |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
127 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
128 if (disp.get () && ! args.empty ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
129 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
130 // Possibly overloaded function. |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
131 octave_value ovfcn = fcn; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
132 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
133 // Get dynamic (class) dispatch type. |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
134 std::string ddt = get_dispatch_type (args); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
135 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
136 if (ddt.empty ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
137 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
138 // Static dispatch (class of 1st arg)? |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
139 if (! disp->empty ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
140 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
141 std::string sdt = args(0).class_name (); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
142 str_ov_map::iterator pos = disp->find (sdt); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
143 if (pos != disp->end ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
144 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
145 out_of_date_check (pos->second, sdt); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
146 ovfcn = pos->second; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
147 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
148 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
149 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
150 else |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
151 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
152 octave_value method = symbol_table::find_method (nm, ddt); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
153 if (method.is_defined ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
154 ovfcn = method; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
155 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
156 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
157 if (ovfcn.is_defined ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
158 retval = ovfcn.do_multi_index_op (nargout, args); |
9458
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
159 else if (fcn.is_undefined ()) |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
160 { |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
161 if (ddt.empty ()) |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
162 ddt = args(0).class_name (); |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
163 |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
164 error ("no %s method to handle class %s", nm.c_str (), ddt.c_str ()); |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
165 } |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
166 else |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
167 error ("invalid function handle"); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
168 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
169 else |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
170 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
171 // Non-overloaded function (anonymous, subfunction, private function). |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
172 if (fcn.is_defined ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
173 retval = fcn.do_multi_index_op (nargout, args); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
174 else |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
175 error ("invalid function handle"); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
176 } |
4924 | 177 |
178 return retval; | |
179 } | |
180 | |
4988 | 181 bool |
6625 | 182 octave_fcn_handle::set_fcn (const std::string &octaveroot, |
183 const std::string& fpath) | |
4988 | 184 { |
6625 | 185 bool success = true; |
186 | |
7745
0ff0fc033f28
better handling of functions found by relative lookup
John W. Eaton <jwe@octave.org>
parents:
7744
diff
changeset
|
187 if (octaveroot.length () != 0 |
0ff0fc033f28
better handling of functions found by relative lookup
John W. Eaton <jwe@octave.org>
parents:
7744
diff
changeset
|
188 && fpath.length () >= octaveroot.length () |
0ff0fc033f28
better handling of functions found by relative lookup
John W. Eaton <jwe@octave.org>
parents:
7744
diff
changeset
|
189 && fpath.substr (0, octaveroot.length ()) == octaveroot |
0ff0fc033f28
better handling of functions found by relative lookup
John W. Eaton <jwe@octave.org>
parents:
7744
diff
changeset
|
190 && OCTAVE_EXEC_PREFIX != octaveroot) |
6625 | 191 { |
192 // First check if just replacing matlabroot is enough | |
193 std::string str = OCTAVE_EXEC_PREFIX + | |
194 fpath.substr (octaveroot.length ()); | |
195 file_stat fs (str); | |
196 | |
197 if (fs.exists ()) | |
198 { | |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7901
diff
changeset
|
199 size_t xpos = str.find_last_of (file_ops::dir_sep_chars ()); |
6625 | 200 |
7336 | 201 std::string dir_name = str.substr (0, xpos); |
6625 | 202 |
7336 | 203 octave_function *xfcn |
204 = load_fcn_from_file (str, dir_name, "", nm); | |
6625 | 205 |
7336 | 206 if (xfcn) |
207 { | |
208 octave_value tmp (xfcn); | |
6625 | 209 |
7336 | 210 fcn = octave_value (new octave_fcn_handle (tmp, nm)); |
6625 | 211 } |
212 else | |
213 { | |
214 error ("function handle points to non-existent function"); | |
215 success = false; | |
216 } | |
217 } | |
218 else | |
219 { | |
220 // Next just search for it anywhere in the system path | |
221 string_vector names(3); | |
222 names(0) = nm + ".oct"; | |
223 names(1) = nm + ".mex"; | |
224 names(2) = nm + ".m"; | |
225 | |
6626 | 226 dir_path p (load_path::system_path ()); |
6625 | 227 |
228 str = octave_env::make_absolute | |
229 (p.find_first_of (names), octave_env::getcwd ()); | |
230 | |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7901
diff
changeset
|
231 size_t xpos = str.find_last_of (file_ops::dir_sep_chars ()); |
6625 | 232 |
7336 | 233 std::string dir_name = str.substr (0, xpos); |
234 | |
235 octave_function *xfcn = load_fcn_from_file (str, dir_name, "", nm); | |
4989 | 236 |
7336 | 237 if (xfcn) |
238 { | |
239 octave_value tmp (xfcn); | |
6625 | 240 |
7336 | 241 fcn = octave_value (new octave_fcn_handle (tmp, nm)); |
6625 | 242 } |
243 else | |
244 { | |
245 error ("function handle points to non-existent function"); | |
246 success = false; | |
247 } | |
248 } | |
249 } | |
250 else | |
251 { | |
252 if (fpath.length () > 0) | |
253 { | |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7901
diff
changeset
|
254 size_t xpos = fpath.find_last_of (file_ops::dir_sep_chars ()); |
6625 | 255 |
7336 | 256 std::string dir_name = fpath.substr (0, xpos); |
257 | |
258 octave_function *xfcn = load_fcn_from_file (fpath, dir_name, "", nm); | |
6625 | 259 |
7336 | 260 if (xfcn) |
261 { | |
262 octave_value tmp (xfcn); | |
6625 | 263 |
7336 | 264 fcn = octave_value (new octave_fcn_handle (tmp, nm)); |
6625 | 265 } |
266 else | |
267 { | |
268 error ("function handle points to non-existent function"); | |
269 success = false; | |
270 } | |
271 } | |
272 else | |
273 { | |
7336 | 274 fcn = symbol_table::find_function (nm); |
275 | |
6625 | 276 if (! fcn.is_function ()) |
277 { | |
278 error ("function handle points to non-existent function"); | |
279 success = false; | |
280 } | |
281 } | |
282 } | |
283 | |
284 return success; | |
285 } | |
286 | |
287 bool | |
6974 | 288 octave_fcn_handle::save_ascii (std::ostream& os) |
6625 | 289 { |
4988 | 290 if (nm == "@<anonymous>") |
291 { | |
6625 | 292 os << nm << "\n"; |
293 | |
4989 | 294 print_raw (os, true); |
295 os << "\n"; | |
6625 | 296 |
7336 | 297 if (fcn.is_undefined ()) |
6625 | 298 return false; |
299 | |
300 octave_user_function *f = fcn.user_function_value (); | |
301 | |
7336 | 302 std::list<symbol_table::symbol_record> vars |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
303 = symbol_table::all_variables (f->scope (), 0); |
6625 | 304 |
7336 | 305 size_t varlen = vars.size (); |
6625 | 306 |
307 if (varlen > 0) | |
308 { | |
309 os << "# length: " << varlen << "\n"; | |
310 | |
7336 | 311 for (std::list<symbol_table::symbol_record>::const_iterator p = vars.begin (); |
312 p != vars.end (); p++) | |
6625 | 313 { |
7336 | 314 if (! save_ascii_data (os, p->varval (), p->name (), false, 0)) |
6625 | 315 return os; |
316 } | |
317 } | |
318 } | |
319 else | |
320 { | |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
321 octave_function *f = function_value (); |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
322 std::string fnm = f ? f->fcn_file_name () : std::string (); |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
323 |
6625 | 324 os << "# octaveroot: " << OCTAVE_EXEC_PREFIX << "\n"; |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
325 if (! fnm.empty ()) |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
326 os << "# path: " << fnm << "\n"; |
6625 | 327 os << nm << "\n"; |
4988 | 328 } |
329 | |
330 return true; | |
331 } | |
332 | |
333 bool | |
334 octave_fcn_handle::load_ascii (std::istream& is) | |
335 { | |
6625 | 336 bool success = true; |
337 | |
338 std::streampos pos = is.tellg (); | |
339 std::string octaveroot = extract_keyword (is, "octaveroot", true); | |
340 if (octaveroot.length() == 0) | |
341 { | |
342 is.seekg (pos); | |
343 is.clear (); | |
344 } | |
345 pos = is.tellg (); | |
346 std::string fpath = extract_keyword (is, "path", true); | |
347 if (fpath.length() == 0) | |
348 { | |
349 is.seekg (pos); | |
350 is.clear (); | |
351 } | |
352 | |
4988 | 353 is >> nm; |
4989 | 354 |
4988 | 355 if (nm == "@<anonymous>") |
356 { | |
8946
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
357 skip_preceeding_newline (is); |
4988 | 358 |
8946
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
359 std::string buf; |
4988 | 360 |
361 if (is) | |
362 { | |
363 | |
364 // Get a line of text whitespace characters included, leaving | |
4989 | 365 // newline in the stream. |
8946
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
366 buf = read_until_newline (is, true); |
4989 | 367 |
4988 | 368 } |
369 | |
6625 | 370 pos = is.tellg (); |
7336 | 371 |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9343
diff
changeset
|
372 unwind_protect::frame_id_t uwp_frame = unwind_protect::begin_frame (); |
9144
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
373 |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
374 // Set up temporary scope to use for evaluating the text that |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
375 // defines the anonymous function. |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
376 |
7336 | 377 symbol_table::scope_id local_scope = symbol_table::alloc_scope (); |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9395
diff
changeset
|
378 unwind_protect::add_fcn (symbol_table::erase_scope, local_scope); |
9144
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
379 |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
380 symbol_table::set_scope (local_scope); |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
381 |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
382 octave_call_stack::push (local_scope, 0); |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9395
diff
changeset
|
383 unwind_protect::add_fcn (octave_call_stack::pop); |
4988 | 384 |
8946
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
385 octave_idx_type len = 0; |
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
386 |
6625 | 387 if (extract_keyword (is, "length", len, true) && len >= 0) |
4989 | 388 { |
6625 | 389 if (len > 0) |
390 { | |
391 for (octave_idx_type i = 0; i < len; i++) | |
392 { | |
393 octave_value t2; | |
394 bool dummy; | |
395 | |
396 std::string name | |
397 = read_ascii_data (is, std::string (), dummy, t2, i); | |
398 | |
399 if (!is) | |
400 { | |
401 error ("load: failed to load anonymous function handle"); | |
402 break; | |
403 } | |
404 | |
7901 | 405 symbol_table::varref (name, local_scope, 0) = t2; |
6625 | 406 } |
407 } | |
4989 | 408 } |
409 else | |
6625 | 410 { |
411 is.seekg (pos); | |
412 is.clear (); | |
413 } | |
414 | |
415 if (is && success) | |
416 { | |
417 int parse_status; | |
418 octave_value anon_fcn_handle = | |
8946
e7e928088e90
fix CRLF issues with text-mode reading in windows when loading ascii data
Benjamin Lindner <lindnerb@users.sourceforge.net>
parents:
8920
diff
changeset
|
419 eval_string (buf, true, parse_status); |
6625 | 420 |
421 if (parse_status == 0) | |
422 { | |
423 octave_fcn_handle *fh = | |
424 anon_fcn_handle.fcn_handle_value (); | |
7761
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
425 |
6625 | 426 if (fh) |
7761
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
427 { |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
428 fcn = fh->fcn; |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
429 |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
430 octave_user_function *uf = fcn.user_function_value (true); |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
431 |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
432 if (uf) |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
433 symbol_table::cache_name (uf->scope (), nm); |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
434 } |
6625 | 435 else |
436 success = false; | |
437 } | |
438 else | |
439 success = false; | |
440 } | |
441 else | |
442 success = false; | |
443 | |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9343
diff
changeset
|
444 unwind_protect::run_frame (uwp_frame); |
4988 | 445 } |
446 else | |
6625 | 447 success = set_fcn (octaveroot, fpath); |
4988 | 448 |
6625 | 449 return success; |
4988 | 450 } |
451 | |
452 bool | |
6625 | 453 octave_fcn_handle::save_binary (std::ostream& os, bool& save_as_floats) |
4988 | 454 { |
455 if (nm == "@<anonymous>") | |
456 { | |
6625 | 457 std::ostringstream nmbuf; |
458 | |
7336 | 459 if (fcn.is_undefined ()) |
6625 | 460 return false; |
461 | |
462 octave_user_function *f = fcn.user_function_value (); | |
463 | |
7336 | 464 std::list<symbol_table::symbol_record> vars |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
465 = symbol_table::all_variables (f->scope (), 0); |
6625 | 466 |
7336 | 467 size_t varlen = vars.size (); |
6625 | 468 |
469 if (varlen > 0) | |
470 nmbuf << nm << " " << varlen; | |
471 else | |
472 nmbuf << nm; | |
473 | |
474 std::string buf_str = nmbuf.str(); | |
475 int32_t tmp = buf_str.length (); | |
476 os.write (reinterpret_cast<char *> (&tmp), 4); | |
477 os.write (buf_str.c_str (), buf_str.length ()); | |
478 | |
5765 | 479 std::ostringstream buf; |
4988 | 480 print_raw (buf, true); |
5765 | 481 std::string stmp = buf.str (); |
4988 | 482 tmp = stmp.length (); |
5760 | 483 os.write (reinterpret_cast<char *> (&tmp), 4); |
4988 | 484 os.write (stmp.c_str (), stmp.length ()); |
6625 | 485 |
486 if (varlen > 0) | |
487 { | |
7336 | 488 for (std::list<symbol_table::symbol_record>::const_iterator p = vars.begin (); |
489 p != vars.end (); p++) | |
6625 | 490 { |
7336 | 491 if (! save_binary_data (os, p->varval (), p->name (), |
6625 | 492 "", 0, save_as_floats)) |
493 return os; | |
494 } | |
495 } | |
496 } | |
497 else | |
498 { | |
499 std::ostringstream nmbuf; | |
500 | |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
501 octave_function *f = function_value (); |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
502 std::string fnm = f ? f->fcn_file_name () : std::string (); |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
503 |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
504 nmbuf << nm << "\n" << OCTAVE_EXEC_PREFIX << "\n" << fnm; |
6625 | 505 |
506 std::string buf_str = nmbuf.str (); | |
507 int32_t tmp = buf_str.length (); | |
508 os.write (reinterpret_cast<char *> (&tmp), 4); | |
509 os.write (buf_str.c_str (), buf_str.length ()); | |
4988 | 510 } |
7336 | 511 |
4988 | 512 return true; |
513 } | |
514 | |
515 bool | |
516 octave_fcn_handle::load_binary (std::istream& is, bool swap, | |
6625 | 517 oct_mach_info::float_format fmt) |
4988 | 518 { |
6625 | 519 bool success = true; |
7336 | 520 |
5828 | 521 int32_t tmp; |
5760 | 522 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
4988 | 523 return false; |
524 if (swap) | |
525 swap_bytes<4> (&tmp); | |
526 | |
527 OCTAVE_LOCAL_BUFFER (char, ctmp1, tmp+1); | |
8378
7d0492aa522d
fix use of uninitialized buffers
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
528 is.get (ctmp1, tmp+1, 0); |
4988 | 529 nm = std::string (ctmp1); |
530 | |
531 if (! is) | |
532 return false; | |
533 | |
6625 | 534 if (nm.length() >= 12 && nm.substr (0, 12) == "@<anonymous>") |
4988 | 535 { |
6625 | 536 octave_idx_type len = 0; |
537 | |
538 if (nm.length() > 12) | |
539 { | |
540 std::istringstream nm_is (nm.substr(12)); | |
541 nm_is >> len; | |
542 nm = nm.substr(0,12); | |
543 } | |
544 | |
5760 | 545 if (! is.read (reinterpret_cast<char *> (&tmp), 4)) |
4988 | 546 return false; |
547 if (swap) | |
548 swap_bytes<4> (&tmp); | |
549 | |
550 OCTAVE_LOCAL_BUFFER (char, ctmp2, tmp+1); | |
8378
7d0492aa522d
fix use of uninitialized buffers
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
551 is.get (ctmp2, tmp+1, 0); |
4988 | 552 |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9343
diff
changeset
|
553 unwind_protect::frame_id_t uwp_frame = unwind_protect::begin_frame (); |
9144
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
554 |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
555 // Set up temporary scope to use for evaluating the text that |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
556 // defines the anonymous function. |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
557 |
7336 | 558 symbol_table::scope_id local_scope = symbol_table::alloc_scope (); |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9395
diff
changeset
|
559 unwind_protect::add_fcn (symbol_table::erase_scope, local_scope); |
9144
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
560 |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
561 symbol_table::set_scope (local_scope); |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
562 |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
563 octave_call_stack::push (local_scope, 0); |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9395
diff
changeset
|
564 unwind_protect::add_fcn (octave_call_stack::pop); |
9144
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
565 |
6625 | 566 if (len > 0) |
4989 | 567 { |
6625 | 568 for (octave_idx_type i = 0; i < len; i++) |
569 { | |
570 octave_value t2; | |
571 bool dummy; | |
572 std::string doc; | |
573 | |
574 std::string name = | |
575 read_binary_data (is, swap, fmt, std::string (), | |
576 dummy, t2, doc); | |
577 | |
578 if (!is) | |
579 { | |
580 error ("load: failed to load anonymous function handle"); | |
581 break; | |
582 } | |
583 | |
7336 | 584 symbol_table::varref (name, local_scope) = t2; |
6625 | 585 } |
586 } | |
587 | |
588 if (is && success) | |
589 { | |
590 int parse_status; | |
591 octave_value anon_fcn_handle = | |
592 eval_string (ctmp2, true, parse_status); | |
593 | |
594 if (parse_status == 0) | |
595 { | |
596 octave_fcn_handle *fh = anon_fcn_handle.fcn_handle_value (); | |
7761
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
597 |
6625 | 598 if (fh) |
7761
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
599 { |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
600 fcn = fh->fcn; |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
601 |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
602 octave_user_function *uf = fcn.user_function_value (true); |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
603 |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
604 if (uf) |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
605 symbol_table::cache_name (uf->scope (), nm); |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
606 } |
6625 | 607 else |
608 success = false; | |
609 } | |
4989 | 610 else |
6625 | 611 success = false; |
4989 | 612 } |
6625 | 613 |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9343
diff
changeset
|
614 unwind_protect::run_frame (uwp_frame); |
4988 | 615 } |
616 else | |
617 { | |
6625 | 618 std::string octaveroot; |
619 std::string fpath; | |
620 | |
8021 | 621 if (nm.find_first_of ("\n") != std::string::npos) |
6225 | 622 { |
6625 | 623 size_t pos1 = nm.find_first_of ("\n"); |
624 size_t pos2 = nm.find_first_of ("\n", pos1 + 1); | |
625 octaveroot = nm.substr (pos1 + 1, pos2 - pos1 - 1); | |
626 fpath = nm.substr (pos2 + 1); | |
627 nm = nm.substr (0, pos1); | |
6225 | 628 } |
6625 | 629 |
630 success = set_fcn (octaveroot, fpath); | |
631 } | |
632 | |
633 return success; | |
4988 | 634 } |
635 | |
636 #if defined (HAVE_HDF5) | |
637 bool | |
638 octave_fcn_handle::save_hdf5 (hid_t loc_id, const char *name, | |
6625 | 639 bool save_as_floats) |
4988 | 640 { |
7336 | 641 bool retval = true; |
642 | |
4988 | 643 hid_t group_hid = -1; |
644 group_hid = H5Gcreate (loc_id, name, 0); | |
7336 | 645 if (group_hid < 0) |
646 return false; | |
4988 | 647 |
648 hid_t space_hid = -1, data_hid = -1, type_hid = -1;; | |
649 | |
650 // attach the type of the variable | |
651 type_hid = H5Tcopy (H5T_C_S1); | |
652 H5Tset_size (type_hid, nm.length () + 1); | |
653 if (type_hid < 0) | |
654 { | |
655 H5Gclose (group_hid); | |
656 return false; | |
657 } | |
658 | |
659 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, 2); | |
660 hdims[0] = 0; | |
661 hdims[1] = 0; | |
5760 | 662 space_hid = H5Screate_simple (0 , hdims, 0); |
4988 | 663 if (space_hid < 0) |
664 { | |
665 H5Tclose (type_hid); | |
666 H5Gclose (group_hid); | |
667 return false; | |
668 } | |
669 | |
670 data_hid = H5Dcreate (group_hid, "nm", type_hid, space_hid, H5P_DEFAULT); | |
671 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, | |
5760 | 672 H5P_DEFAULT, nm.c_str ()) < 0) |
4988 | 673 { |
674 H5Sclose (space_hid); | |
675 H5Tclose (type_hid); | |
676 H5Gclose (group_hid); | |
677 return false; | |
678 } | |
679 H5Dclose (data_hid); | |
680 | |
681 if (nm == "@<anonymous>") | |
682 { | |
5765 | 683 std::ostringstream buf; |
4988 | 684 print_raw (buf, true); |
5765 | 685 std::string stmp = buf.str (); |
4988 | 686 |
687 // attach the type of the variable | |
688 H5Tset_size (type_hid, stmp.length () + 1); | |
689 if (type_hid < 0) | |
690 { | |
6695 | 691 H5Sclose (space_hid); |
4988 | 692 H5Gclose (group_hid); |
693 return false; | |
694 } | |
695 | |
696 data_hid = H5Dcreate (group_hid, "fcn", type_hid, space_hid, | |
697 H5P_DEFAULT); | |
698 if (data_hid < 0 || H5Dwrite (data_hid, type_hid, H5S_ALL, H5S_ALL, | |
5760 | 699 H5P_DEFAULT, stmp.c_str ()) < 0) |
4988 | 700 { |
701 H5Sclose (space_hid); | |
702 H5Tclose (type_hid); | |
703 H5Gclose (group_hid); | |
704 return false; | |
705 } | |
706 | |
707 H5Dclose (data_hid); | |
6625 | 708 |
709 octave_user_function *f = fcn.user_function_value (); | |
710 | |
7336 | 711 std::list<symbol_table::symbol_record> vars |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
712 = symbol_table::all_variables (f->scope (), 0); |
7336 | 713 |
714 size_t varlen = vars.size (); | |
6625 | 715 |
716 if (varlen > 0) | |
717 { | |
718 hid_t as_id = H5Screate (H5S_SCALAR); | |
719 | |
720 if (as_id >= 0) | |
721 { | |
722 hid_t a_id = H5Acreate (group_hid, "SYMBOL_TABLE", | |
723 H5T_NATIVE_IDX, as_id, H5P_DEFAULT); | |
724 | |
725 if (a_id >= 0) | |
726 { | |
727 retval = (H5Awrite (a_id, H5T_NATIVE_IDX, &varlen) >= 0); | |
728 | |
729 H5Aclose (a_id); | |
730 } | |
731 else | |
732 retval = false; | |
733 | |
734 H5Sclose (as_id); | |
735 } | |
736 else | |
737 retval = false; | |
738 | |
739 data_hid = H5Gcreate (group_hid, "symbol table", 0); | |
740 if (data_hid < 0) | |
741 { | |
742 H5Sclose (space_hid); | |
743 H5Tclose (type_hid); | |
744 H5Gclose (group_hid); | |
745 return false; | |
746 } | |
747 | |
7336 | 748 for (std::list<symbol_table::symbol_record>::const_iterator p = vars.begin (); |
749 p != vars.end (); p++) | |
6625 | 750 { |
7336 | 751 if (! add_hdf5_data (data_hid, p->varval (), p->name (), |
6625 | 752 "", false, save_as_floats)) |
753 break; | |
754 } | |
755 H5Gclose (data_hid); | |
756 } | |
757 } | |
758 else | |
759 { | |
760 std::string octaveroot = OCTAVE_EXEC_PREFIX; | |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
761 |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
762 octave_function *f = function_value (); |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
763 std::string fpath = f ? f->fcn_file_name () : std::string (); |
6625 | 764 |
765 H5Sclose (space_hid); | |
766 hdims[0] = 1; | |
767 hdims[1] = octaveroot.length (); | |
768 space_hid = H5Screate_simple (0 , hdims, 0); | |
769 if (space_hid < 0) | |
770 { | |
771 H5Tclose (type_hid); | |
772 H5Gclose (group_hid); | |
773 return false; | |
774 } | |
775 | |
776 H5Tclose (type_hid); | |
777 type_hid = H5Tcopy (H5T_C_S1); | |
778 H5Tset_size (type_hid, octaveroot.length () + 1); | |
779 | |
780 hid_t a_id = H5Acreate (group_hid, "OCTAVEROOT", | |
781 type_hid, space_hid, H5P_DEFAULT); | |
782 | |
783 if (a_id >= 0) | |
784 { | |
785 retval = (H5Awrite (a_id, type_hid, octaveroot.c_str ()) >= 0); | |
786 | |
787 H5Aclose (a_id); | |
788 } | |
789 else | |
6695 | 790 { |
791 H5Sclose (space_hid); | |
792 H5Tclose (type_hid); | |
793 H5Gclose (group_hid); | |
794 return false; | |
795 } | |
6625 | 796 |
797 H5Sclose (space_hid); | |
798 hdims[0] = 1; | |
799 hdims[1] = fpath.length (); | |
800 space_hid = H5Screate_simple (0 , hdims, 0); | |
801 if (space_hid < 0) | |
802 { | |
803 H5Tclose (type_hid); | |
804 H5Gclose (group_hid); | |
805 return false; | |
806 } | |
807 | |
808 H5Tclose (type_hid); | |
809 type_hid = H5Tcopy (H5T_C_S1); | |
810 H5Tset_size (type_hid, fpath.length () + 1); | |
811 | |
812 a_id = H5Acreate (group_hid, "FILE", type_hid, space_hid, H5P_DEFAULT); | |
813 | |
814 if (a_id >= 0) | |
815 { | |
816 retval = (H5Awrite (a_id, type_hid, fpath.c_str ()) >= 0); | |
817 | |
818 H5Aclose (a_id); | |
819 } | |
820 else | |
821 retval = false; | |
4988 | 822 } |
823 | |
824 H5Sclose (space_hid); | |
825 H5Tclose (type_hid); | |
826 H5Gclose (group_hid); | |
827 | |
828 return retval; | |
829 } | |
830 | |
831 bool | |
832 octave_fcn_handle::load_hdf5 (hid_t loc_id, const char *name, | |
6625 | 833 bool have_h5giterate_bug) |
4988 | 834 { |
7336 | 835 bool success = true; |
836 | |
4988 | 837 hid_t group_hid, data_hid, space_hid, type_hid, type_class_hid, st_id; |
838 hsize_t rank; | |
839 int slen; | |
840 | |
841 group_hid = H5Gopen (loc_id, name); | |
7336 | 842 if (group_hid < 0) |
843 return false; | |
4988 | 844 |
845 data_hid = H5Dopen (group_hid, "nm"); | |
846 | |
847 if (data_hid < 0) | |
848 { | |
849 H5Gclose (group_hid); | |
850 return false; | |
851 } | |
852 | |
853 type_hid = H5Dget_type (data_hid); | |
854 type_class_hid = H5Tget_class (type_hid); | |
855 | |
856 if (type_class_hid != H5T_STRING) | |
857 { | |
858 H5Tclose (type_hid); | |
859 H5Dclose (data_hid); | |
860 H5Gclose (group_hid); | |
861 return false; | |
862 } | |
863 | |
864 space_hid = H5Dget_space (data_hid); | |
865 rank = H5Sget_simple_extent_ndims (space_hid); | |
866 | |
867 if (rank != 0) | |
868 { | |
869 H5Sclose (space_hid); | |
870 H5Tclose (type_hid); | |
871 H5Dclose (data_hid); | |
872 H5Gclose (group_hid); | |
873 return false; | |
874 } | |
875 | |
876 slen = H5Tget_size (type_hid); | |
877 if (slen < 0) | |
878 { | |
879 H5Sclose (space_hid); | |
880 H5Tclose (type_hid); | |
881 H5Dclose (data_hid); | |
882 H5Gclose (group_hid); | |
883 return false; | |
884 } | |
885 | |
886 OCTAVE_LOCAL_BUFFER (char, nm_tmp, slen); | |
887 | |
888 // create datatype for (null-terminated) string to read into: | |
889 st_id = H5Tcopy (H5T_C_S1); | |
890 H5Tset_size (st_id, slen); | |
891 | |
5760 | 892 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, nm_tmp) < 0) |
4988 | 893 { |
6695 | 894 H5Tclose (st_id); |
4988 | 895 H5Sclose (space_hid); |
896 H5Tclose (type_hid); | |
6695 | 897 H5Dclose (data_hid); |
4988 | 898 H5Gclose (group_hid); |
899 return false; | |
900 } | |
901 H5Tclose (st_id); | |
902 H5Dclose (data_hid); | |
903 nm = nm_tmp; | |
904 | |
905 if (nm == "@<anonymous>") | |
906 { | |
907 data_hid = H5Dopen (group_hid, "fcn"); | |
908 | |
909 if (data_hid < 0) | |
910 { | |
6695 | 911 H5Sclose (space_hid); |
912 H5Tclose (type_hid); | |
4988 | 913 H5Gclose (group_hid); |
914 return false; | |
915 } | |
916 | |
6695 | 917 H5Tclose (type_hid); |
4988 | 918 type_hid = H5Dget_type (data_hid); |
919 type_class_hid = H5Tget_class (type_hid); | |
920 | |
921 if (type_class_hid != H5T_STRING) | |
922 { | |
6695 | 923 H5Sclose (space_hid); |
4988 | 924 H5Tclose (type_hid); |
925 H5Dclose (data_hid); | |
926 H5Gclose (group_hid); | |
927 return false; | |
928 } | |
929 | |
6695 | 930 H5Sclose (space_hid); |
4988 | 931 space_hid = H5Dget_space (data_hid); |
932 rank = H5Sget_simple_extent_ndims (space_hid); | |
933 | |
934 if (rank != 0) | |
935 { | |
936 H5Sclose (space_hid); | |
937 H5Tclose (type_hid); | |
938 H5Dclose (data_hid); | |
939 H5Gclose (group_hid); | |
940 return false; | |
941 } | |
942 | |
943 slen = H5Tget_size (type_hid); | |
944 if (slen < 0) | |
945 { | |
946 H5Sclose (space_hid); | |
947 H5Tclose (type_hid); | |
948 H5Dclose (data_hid); | |
949 H5Gclose (group_hid); | |
950 return false; | |
951 } | |
952 | |
953 OCTAVE_LOCAL_BUFFER (char, fcn_tmp, slen); | |
954 | |
955 // create datatype for (null-terminated) string to read into: | |
956 st_id = H5Tcopy (H5T_C_S1); | |
957 H5Tset_size (st_id, slen); | |
958 | |
5760 | 959 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, fcn_tmp) < 0) |
4988 | 960 { |
6695 | 961 H5Tclose (st_id); |
4988 | 962 H5Sclose (space_hid); |
963 H5Tclose (type_hid); | |
6695 | 964 H5Dclose (data_hid); |
4988 | 965 H5Gclose (group_hid); |
966 return false; | |
967 } | |
6695 | 968 H5Tclose (st_id); |
4988 | 969 H5Dclose (data_hid); |
6625 | 970 |
971 octave_idx_type len = 0; | |
972 | |
973 // we have to pull some shenanigans here to make sure | |
974 // HDF5 doesn't print out all sorts of error messages if we | |
975 // call H5Aopen for a non-existing attribute | |
976 | |
977 H5E_auto_t err_func; | |
978 void *err_func_data; | |
4988 | 979 |
6625 | 980 // turn off error reporting temporarily, but save the error |
981 // reporting function: | |
982 H5Eget_auto (&err_func, &err_func_data); | |
983 H5Eset_auto (0, 0); | |
984 | |
985 hid_t attr_id = H5Aopen_name (group_hid, "SYMBOL_TABLE"); | |
4988 | 986 |
6625 | 987 if (attr_id >= 0) |
988 { | |
989 if (H5Aread (attr_id, H5T_NATIVE_IDX, &len) < 0) | |
990 success = false; | |
991 | |
992 H5Aclose (attr_id); | |
993 } | |
994 | |
995 // restore error reporting: | |
996 H5Eset_auto (err_func, err_func_data); | |
997 | |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9343
diff
changeset
|
998 unwind_protect::frame_id_t uwp_frame = unwind_protect::begin_frame (); |
9144
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
999 |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
1000 // Set up temporary scope to use for evaluating the text that |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
1001 // defines the anonymous function. |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
1002 |
7336 | 1003 symbol_table::scope_id local_scope = symbol_table::alloc_scope (); |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9395
diff
changeset
|
1004 unwind_protect::add_fcn (symbol_table::erase_scope, local_scope); |
9144
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
1005 |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
1006 symbol_table::set_scope (local_scope); |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
1007 |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
1008 octave_call_stack::push (local_scope, 0); |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9395
diff
changeset
|
1009 unwind_protect::add_fcn (octave_call_stack::pop); |
7336 | 1010 |
6625 | 1011 if (len > 0 && success) |
4989 | 1012 { |
6625 | 1013 #ifdef HAVE_H5GGET_NUM_OBJS |
1014 hsize_t num_obj = 0; | |
1015 data_hid = H5Gopen (group_hid, "symbol table"); | |
1016 H5Gget_num_objs (data_hid, &num_obj); | |
1017 H5Gclose (data_hid); | |
1018 | |
1019 if (num_obj != static_cast<hsize_t>(len)) | |
1020 { | |
1021 error ("load: failed to load anonymous function handle"); | |
1022 success = false; | |
1023 } | |
1024 #endif | |
1025 | |
1026 if (! error_state) | |
1027 { | |
1028 hdf5_callback_data dsub; | |
1029 int current_item = 0; | |
1030 for (octave_idx_type i = 0; i < len; i++) | |
1031 { | |
1032 if (H5Giterate (group_hid, "symbol table", ¤t_item, | |
1033 hdf5_read_next_data, &dsub) <= 0) | |
1034 { | |
1035 error ("load: failed to load anonymous function handle"); | |
1036 success = false; | |
1037 break; | |
1038 } | |
1039 | |
1040 if (have_h5giterate_bug) | |
1041 current_item++; // H5Giterate returns last index processed | |
1042 | |
7336 | 1043 symbol_table::varref (dsub.name, local_scope) = dsub.tc; |
6625 | 1044 } |
1045 } | |
1046 } | |
1047 | |
1048 if (success) | |
1049 { | |
1050 int parse_status; | |
1051 octave_value anon_fcn_handle = | |
1052 eval_string (fcn_tmp, true, parse_status); | |
1053 | |
1054 if (parse_status == 0) | |
1055 { | |
1056 octave_fcn_handle *fh = anon_fcn_handle.fcn_handle_value (); | |
7761
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
1057 |
6625 | 1058 if (fh) |
7761
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
1059 { |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
1060 fcn = fh->fcn; |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
1061 |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
1062 octave_user_function *uf = fcn.user_function_value (true); |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
1063 |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
1064 if (uf) |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
1065 symbol_table::cache_name (uf->scope (), nm); |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
1066 } |
6625 | 1067 else |
1068 success = false; | |
1069 } | |
4989 | 1070 else |
6625 | 1071 success = false; |
4989 | 1072 } |
6625 | 1073 |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9343
diff
changeset
|
1074 unwind_protect::run_frame (uwp_frame); |
4988 | 1075 } |
1076 else | |
1077 { | |
6625 | 1078 std::string octaveroot; |
1079 std::string fpath; | |
1080 | |
1081 // we have to pull some shenanigans here to make sure | |
1082 // HDF5 doesn't print out all sorts of error messages if we | |
1083 // call H5Aopen for a non-existing attribute | |
1084 | |
1085 H5E_auto_t err_func; | |
1086 void *err_func_data; | |
1087 | |
1088 // turn off error reporting temporarily, but save the error | |
1089 // reporting function: | |
1090 H5Eget_auto (&err_func, &err_func_data); | |
1091 H5Eset_auto (0, 0); | |
1092 | |
1093 hid_t attr_id = H5Aopen_name (group_hid, "OCTAVEROOT"); | |
1094 if (attr_id >= 0) | |
6225 | 1095 { |
6695 | 1096 H5Tclose (type_hid); |
6625 | 1097 type_hid = H5Aget_type (attr_id); |
1098 type_class_hid = H5Tget_class (type_hid); | |
1099 | |
1100 if (type_class_hid != H5T_STRING) | |
1101 success = false; | |
1102 else | |
1103 { | |
1104 slen = H5Tget_size (type_hid); | |
1105 st_id = H5Tcopy (H5T_C_S1); | |
1106 H5Tset_size (st_id, slen); | |
1107 OCTAVE_LOCAL_BUFFER (char, root_tmp, slen); | |
1108 | |
1109 if (H5Aread (attr_id, st_id, root_tmp) < 0) | |
1110 success = false; | |
1111 else | |
1112 octaveroot = root_tmp; | |
6695 | 1113 |
1114 H5Tclose (st_id); | |
6625 | 1115 } |
1116 | |
1117 H5Aclose (attr_id); | |
6225 | 1118 } |
6625 | 1119 |
6695 | 1120 if (success) |
6625 | 1121 { |
6695 | 1122 attr_id = H5Aopen_name (group_hid, "FILE"); |
1123 if (attr_id >= 0) | |
1124 { | |
1125 H5Tclose (type_hid); | |
1126 type_hid = H5Aget_type (attr_id); | |
1127 type_class_hid = H5Tget_class (type_hid); | |
6625 | 1128 |
6695 | 1129 if (type_class_hid != H5T_STRING) |
6625 | 1130 success = false; |
1131 else | |
6695 | 1132 { |
1133 slen = H5Tget_size (type_hid); | |
1134 st_id = H5Tcopy (H5T_C_S1); | |
1135 H5Tset_size (st_id, slen); | |
1136 OCTAVE_LOCAL_BUFFER (char, path_tmp, slen); | |
1137 | |
1138 if (H5Aread (attr_id, st_id, path_tmp) < 0) | |
1139 success = false; | |
1140 else | |
1141 fpath = path_tmp; | |
1142 | |
1143 H5Tclose (st_id); | |
1144 } | |
1145 | |
1146 H5Aclose (attr_id); | |
6625 | 1147 } |
1148 } | |
1149 | |
1150 // restore error reporting: | |
1151 H5Eset_auto (err_func, err_func_data); | |
1152 | |
1153 success = (success ? set_fcn (octaveroot, fpath) : success); | |
4988 | 1154 } |
1155 | |
6695 | 1156 H5Tclose (type_hid); |
1157 H5Sclose (space_hid); | |
1158 H5Gclose (group_hid); | |
1159 | |
6625 | 1160 return success; |
4988 | 1161 } |
6625 | 1162 |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1163 #endif |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1164 |
6625 | 1165 /* |
1166 | |
1167 %!test | |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1168 %! a = 2; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1169 %! f = @(x) a + x; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1170 %! g = @(x) 2 * x; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1171 %! hm = @flops; |
7745
0ff0fc033f28
better handling of functions found by relative lookup
John W. Eaton <jwe@octave.org>
parents:
7744
diff
changeset
|
1172 %! hdld = @svd; |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1173 %! hbi = @log2; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1174 %! f2 = f; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1175 %! g2 = g; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1176 %! hm2 = hm; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1177 %! hdld2 = hdld; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1178 %! hbi2 = hbi; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1179 %! modes = {"-text", "-binary"}; |
6625 | 1180 %! if (!isempty(findstr(octave_config_info ("DEFS"),"HAVE_HDF5"))) |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1181 %! modes(end+1) = "-hdf5"; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1182 %! endif |
7901 | 1183 %! for i = 1:numel (modes) |
1184 %! mode = modes{i}; | |
6625 | 1185 %! nm = tmpnam(); |
1186 %! unwind_protect | |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1187 %! save (mode, nm, "f2", "g2", "hm2", "hdld2", "hbi2"); |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1188 %! clear f2 g2 hm2 hdld2 hbi2 |
6625 | 1189 %! load (nm); |
1190 %! assert (f(2),f2(2)); | |
1191 %! assert (g(2),g2(2)); | |
1192 %! assert (g(3),g2(3)); | |
1193 %! unlink (nm); | |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1194 %! save (mode, nm, "f2", "g2", "hm2", "hdld2", "hbi2"); |
6625 | 1195 %! unwind_protect_cleanup |
1196 %! unlink (nm); | |
1197 %! end_unwind_protect | |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1198 %! endfor |
6625 | 1199 |
1200 */ | |
4988 | 1201 |
4343 | 1202 void |
1203 octave_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax) const | |
1204 { | |
1205 print_raw (os, pr_as_read_syntax); | |
1206 newline (os); | |
1207 } | |
1208 | |
1209 void | |
1210 octave_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax) const | |
1211 { | |
4980 | 1212 bool printed = false; |
1213 | |
1214 if (nm == "@<anonymous>") | |
1215 { | |
1216 tree_print_code tpc (os); | |
1217 | |
4989 | 1218 // FCN is const because this member function is, so we can't |
4980 | 1219 // use it to call user_function_value, so we make a copy first. |
1220 | |
1221 octave_value ftmp = fcn; | |
1222 | |
1223 octave_user_function *f = ftmp.user_function_value (); | |
1224 | |
1225 if (f) | |
1226 { | |
1227 tree_parameter_list *p = f->parameter_list (); | |
1228 | |
1229 os << "@("; | |
1230 | |
1231 if (p) | |
1232 p->accept (tpc); | |
1233 | |
1234 os << ") "; | |
1235 | |
1236 tree_statement_list *b = f->body (); | |
1237 | |
1238 if (b) | |
1239 { | |
1240 assert (b->length () == 1); | |
1241 | |
1242 tree_statement *s = b->front (); | |
1243 | |
1244 if (s) | |
1245 { | |
1246 if (s->is_expression ()) | |
1247 { | |
1248 tree_expression *e = s->expression (); | |
1249 | |
1250 if (e) | |
6657 | 1251 e->accept (tpc); |
4980 | 1252 } |
1253 else | |
1254 { | |
1255 tree_command *c = s->command (); | |
1256 | |
1257 tpc.suspend_newline (); | |
1258 c->accept (tpc); | |
1259 tpc.resume_newline (); | |
1260 } | |
1261 } | |
1262 } | |
1263 | |
1264 printed = true; | |
1265 } | |
1266 } | |
1267 | |
1268 if (! printed) | |
1269 octave_print_internal (os, nm, pr_as_read_syntax, | |
1270 current_print_indent_level ()); | |
4343 | 1271 } |
1272 | |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1273 static string_vector |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1274 get_builtin_classes (void) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1275 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1276 // FIXME: this should really be read from somewhere else. |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1277 static const char *cnames[15] = { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1278 "double", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1279 "single", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1280 "int8", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1281 "int16", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1282 "int32", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1283 "int64", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1284 "uint8", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1285 "uint16", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1286 "uint32", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1287 "uint64", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1288 "logical", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1289 "char", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1290 "cell", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1291 "struct", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1292 "function_handle" |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1293 }; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1294 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1295 static string_vector retval; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1296 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1297 if (retval.is_empty ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1298 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1299 retval = string_vector (15); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1300 for (int i = 0; i < 15; i++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1301 retval(i) = cnames[i]; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1302 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1303 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1304 return retval; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1305 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1306 |
4343 | 1307 octave_value |
9463
d34baf412786
support non-local function lookups in str2func
Jaroslav Hajek <highegg@gmail.com>
parents:
9458
diff
changeset
|
1308 make_fcn_handle (const std::string& nm, bool local_funcs) |
4343 | 1309 { |
1310 octave_value retval; | |
1311 | |
9342
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1312 // Bow to the god of compatibility. |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1313 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1314 // FIXME -- it seems ugly to put this here, but there is no single |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1315 // function in the parser that converts from the operator name to |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1316 // the corresponding function name. At least try to do it without N |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1317 // string compares. |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1318 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1319 std::string tnm = nm; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1320 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1321 size_t len = nm.length (); |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1322 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1323 if (len == 3 && nm == ".**") |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1324 tnm = "power"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1325 else if (len == 2) |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1326 { |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1327 if (nm[0] == '.') |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1328 { |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1329 switch (nm[1]) |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1330 { |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1331 case '\'': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1332 tnm = "transpose"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1333 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1334 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1335 case '+': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1336 tnm = "plus"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1337 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1338 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1339 case '-': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1340 tnm = "minus"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1341 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1342 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1343 case '*': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1344 tnm = "times"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1345 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1346 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1347 case '/': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1348 tnm = "rdivide"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1349 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1350 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1351 case '^': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1352 tnm = "power"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1353 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1354 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1355 case '\\': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1356 tnm = "ldivide"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1357 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1358 } |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1359 } |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1360 else if (nm[1] == '=') |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1361 { |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1362 switch (nm[0]) |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1363 { |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1364 case '<': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1365 tnm = "le"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1366 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1367 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1368 case '=': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1369 tnm = "eq"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1370 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1371 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1372 case '>': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1373 tnm = "ge"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1374 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1375 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1376 case '~': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1377 case '!': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1378 tnm = "ne"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1379 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1380 } |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1381 } |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1382 else if (nm == "**") |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1383 tnm = "mpower"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1384 } |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1385 else if (len == 1) |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1386 { |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1387 switch (nm[0]) |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1388 { |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1389 case '~': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1390 case '!': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1391 tnm = "not"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1392 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1393 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1394 case '\'': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1395 tnm = "ctranspose"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1396 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1397 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1398 case '+': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1399 tnm = "plus"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1400 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1401 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1402 case '-': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1403 tnm = "minus"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1404 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1405 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1406 case '*': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1407 tnm = "mtimes"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1408 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1409 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1410 case '/': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1411 tnm = "mrdivide"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1412 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1413 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1414 case '^': |
9343
70e0d3b1f26f
fix typos in previous change
John W. Eaton <jwe@octave.org>
parents:
9342
diff
changeset
|
1415 tnm = "mpower"; |
9342
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1416 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1417 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1418 case '\\': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1419 tnm = "mldivide"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1420 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1421 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1422 case '<': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1423 tnm = "lt"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1424 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1425 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1426 case '>': |
9343
70e0d3b1f26f
fix typos in previous change
John W. Eaton <jwe@octave.org>
parents:
9342
diff
changeset
|
1427 tnm = "gt"; |
9342
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1428 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1429 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1430 case '&': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1431 tnm = "and"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1432 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1433 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1434 case '|': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1435 tnm = "or"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1436 break; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1437 } |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1438 } |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1439 |
9458
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1440 bool handle_ok = false; |
9463
d34baf412786
support non-local function lookups in str2func
Jaroslav Hajek <highegg@gmail.com>
parents:
9458
diff
changeset
|
1441 octave_value f = symbol_table::find_function (tnm, octave_value_list (), |
d34baf412786
support non-local function lookups in str2func
Jaroslav Hajek <highegg@gmail.com>
parents:
9458
diff
changeset
|
1442 local_funcs); |
9458
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1443 |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1444 if (f.is_undefined ()) |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1445 { |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1446 if (load_path::any_class_method (tnm)) |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1447 handle_ok = true; |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1448 else |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1449 { |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1450 load_path::update (); |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1451 if (load_path::any_class_method (tnm)) |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1452 handle_ok = true; |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1453 } |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1454 } |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1455 else |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1456 handle_ok = true; |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1457 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1458 octave_function *fptr = f.is_defined () ? f.function_value () : 0; |
6481 | 1459 |
9458
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1460 |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1461 if (handle_ok) |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1462 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1463 // If it's a subfunction, private function, or class constructor, |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1464 // we want no dispatch. |
9458
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1465 if (fptr && (fptr->is_nested_function () || fptr->is_private_function () |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1466 || fptr->is_class_constructor ())) |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1467 retval = octave_value (new octave_fcn_handle (f, tnm)); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1468 else |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1469 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1470 typedef octave_fcn_handle::str_ov_map str_ov_map; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1471 std::auto_ptr<str_ov_map> disp (new str_ov_map); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1472 const string_vector cnames = get_builtin_classes (); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1473 for (octave_idx_type i = 0; i < cnames.length (); i++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1474 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1475 std::string cnam = cnames(i); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1476 octave_value method = symbol_table::find_method (tnm, cnam); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1477 if (method.is_defined ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1478 (*disp)[cnam] = method; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1479 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1480 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1481 retval = octave_value (new octave_fcn_handle (f, tnm, disp.release ())); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1482 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1483 } |
9419
0dd3c7a2ba19
undo escaped change in previous patch
Jaroslav Hajek <highegg@gmail.com>
parents:
9418
diff
changeset
|
1484 else |
0dd3c7a2ba19
undo escaped change in previous patch
Jaroslav Hajek <highegg@gmail.com>
parents:
9418
diff
changeset
|
1485 error ("error creating function handle \"@%s\"", nm.c_str ()); |
4343 | 1486 |
1487 return retval; | |
1488 } | |
1489 | |
9342
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1490 /* |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1491 %!test |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1492 %! x = {".**", "power"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1493 %! ".'", "transpose"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1494 %! ".+", "plus"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1495 %! ".-", "minus"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1496 %! ".*", "times"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1497 %! "./", "rdivide"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1498 %! ".^", "power"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1499 %! ".\\", "ldivide"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1500 %! "<=", "le"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1501 %! "==", "eq"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1502 %! ">=", "ge"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1503 %! "~=", "ne"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1504 %! "!=", "ne"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1505 %! "**", "mpower"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1506 %! "~", "not"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1507 %! "!", "not"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1508 %! "\'", "ctranspose"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1509 %! "+", "plus"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1510 %! "-", "minus"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1511 %! "*", "mtimes"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1512 %! "/", "mrdivide"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1513 %! "^", "mpower"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1514 %! "\\", "mldivide"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1515 %! "<", "lt"; |
9343
70e0d3b1f26f
fix typos in previous change
John W. Eaton <jwe@octave.org>
parents:
9342
diff
changeset
|
1516 %! ">", "gt"; |
9342
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1517 %! "&", "and"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1518 %! "|", "or"}; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1519 %! for i = 1:rows (x) |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1520 %! assert (functions (str2func (x{i,1})).function, x{i,2}) |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1521 %! endfor |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1522 */ |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1523 |
4933 | 1524 DEFUN (functions, args, , |
4343 | 1525 "-*- texinfo -*-\n\ |
4933 | 1526 @deftypefn {Built-in Function} {} functions (@var{fcn_handle})\n\ |
1527 Return a struct containing information about the function handle\n\ | |
1528 @var{fcn_handle}.\n\ | |
1529 @end deftypefn") | |
4343 | 1530 { |
1531 octave_value retval; | |
1532 | |
4933 | 1533 if (args.length () == 1) |
4343 | 1534 { |
4933 | 1535 octave_fcn_handle *fh = args(0).fcn_handle_value (); |
4343 | 1536 |
1537 if (! error_state) | |
1538 { | |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1539 octave_function *fcn = fh ? fh->function_value () : 0; |
4343 | 1540 |
4933 | 1541 if (fcn) |
4930 | 1542 { |
4933 | 1543 Octave_map m; |
4649 | 1544 |
4933 | 1545 std::string fh_nm = fh->fcn_name (); |
1546 | |
6625 | 1547 if (fh_nm == "@<anonymous>") |
1548 { | |
1549 std::ostringstream buf; | |
1550 fh->print_raw (buf); | |
1551 m.assign ("function", buf.str ()); | |
1552 | |
1553 m.assign ("type", "anonymous"); | |
1554 } | |
1555 else | |
1556 { | |
1557 m.assign ("function", fh_nm); | |
4343 | 1558 |
6625 | 1559 if (fcn->is_nested_function ()) |
1560 { | |
1561 m.assign ("type", "subfunction"); | |
1562 Cell parentage (dim_vector (1, 2)); | |
1563 parentage.elem(0) = fh_nm; | |
1564 parentage.elem(1) = fcn->parent_fcn_name (); | |
7756
45de7d8dac72
ov-fcn-handle.cc (Ffunctions): fix structure assignment
John W. Eaton <jwe@octave.org>
parents:
7745
diff
changeset
|
1565 m.assign ("parentage", octave_value (parentage)); |
6625 | 1566 } |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1567 else if (fh->is_overloaded ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1568 m.assign ("type", "overloaded"); |
6625 | 1569 else |
1570 m.assign ("type", "simple"); | |
1571 } | |
4933 | 1572 |
1573 std::string nm = fcn->fcn_file_name (); | |
4343 | 1574 |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1575 if (fh_nm == "@<anonymous>") |
4935 | 1576 { |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1577 m.assign ("file", nm); |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1578 |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1579 octave_user_function *fu = fh->user_function_value (); |
6625 | 1580 |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1581 std::list<symbol_table::symbol_record> vars |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1582 = symbol_table::all_variables (fu->scope (), 0); |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1583 |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1584 size_t varlen = vars.size (); |
6625 | 1585 |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1586 if (varlen > 0) |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1587 { |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1588 Octave_map ws; |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1589 for (std::list<symbol_table::symbol_record>::const_iterator p = vars.begin (); |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1590 p != vars.end (); p++) |
6625 | 1591 { |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1592 ws.assign (p->name (), p->varval (0)); |
6625 | 1593 } |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1594 |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1595 m.assign ("workspace", ws); |
6625 | 1596 } |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1597 } |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1598 else if (fcn->is_user_function () || fcn->is_user_script ()) |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1599 { |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1600 octave_function *fu = fh->function_value (); |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1601 m.assign ("file", fu->fcn_file_name ()); |
4935 | 1602 } |
4343 | 1603 else |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1604 m.assign ("file", ""); |
4933 | 1605 |
1606 retval = m; | |
4343 | 1607 } |
1608 else | |
4933 | 1609 error ("functions: invalid function handle object"); |
4343 | 1610 } |
1611 else | |
4933 | 1612 error ("functions: argument must be a function handle object"); |
4343 | 1613 } |
1614 else | |
5823 | 1615 print_usage (); |
4343 | 1616 |
1617 return retval; | |
1618 } | |
1619 | |
4933 | 1620 DEFUN (func2str, args, , |
4343 | 1621 "-*- texinfo -*-\n\ |
4933 | 1622 @deftypefn {Built-in Function} {} func2str (@var{fcn_handle})\n\ |
1623 Return a string containing the name of the function referenced by\n\ | |
1624 the function handle @var{fcn_handle}.\n\ | |
1625 @end deftypefn") | |
4343 | 1626 { |
1627 octave_value retval; | |
1628 | |
4933 | 1629 if (args.length () == 1) |
4930 | 1630 { |
4933 | 1631 octave_fcn_handle *fh = args(0).fcn_handle_value (); |
4930 | 1632 |
4933 | 1633 if (! error_state && fh) |
1634 { | |
1635 std::string fh_nm = fh->fcn_name (); | |
6416 | 1636 |
1637 if (fh_nm == "@<anonymous>") | |
1638 { | |
1639 std::ostringstream buf; | |
1640 | |
1641 fh->print_raw (buf); | |
1642 | |
1643 retval = buf.str (); | |
1644 } | |
1645 else | |
1646 retval = fh_nm; | |
4933 | 1647 } |
4343 | 1648 else |
4933 | 1649 error ("func2str: expecting valid function handle as first argument"); |
4343 | 1650 } |
1651 else | |
5823 | 1652 print_usage (); |
4343 | 1653 |
1654 return retval; | |
1655 } | |
1656 | |
4933 | 1657 DEFUN (str2func, args, , |
4343 | 1658 "-*- texinfo -*-\n\ |
4933 | 1659 @deftypefn {Built-in Function} {} str2func (@var{fcn_name})\n\ |
9463
d34baf412786
support non-local function lookups in str2func
Jaroslav Hajek <highegg@gmail.com>
parents:
9458
diff
changeset
|
1660 @deftypefnx {Built-in Function} {} str2func (@var{fcn_name}, \"global\")\n\ |
4933 | 1661 Return a function handle constructed from the string @var{fcn_name}.\n\ |
9463
d34baf412786
support non-local function lookups in str2func
Jaroslav Hajek <highegg@gmail.com>
parents:
9458
diff
changeset
|
1662 If the optional \"global\" argument is passed, locally visible functions\n\ |
d34baf412786
support non-local function lookups in str2func
Jaroslav Hajek <highegg@gmail.com>
parents:
9458
diff
changeset
|
1663 are ignored in the lookup.\n\ |
4933 | 1664 @end deftypefn") |
4343 | 1665 { |
1666 octave_value retval; | |
9463
d34baf412786
support non-local function lookups in str2func
Jaroslav Hajek <highegg@gmail.com>
parents:
9458
diff
changeset
|
1667 int nargin = args.length (); |
4343 | 1668 |
9463
d34baf412786
support non-local function lookups in str2func
Jaroslav Hajek <highegg@gmail.com>
parents:
9458
diff
changeset
|
1669 if (nargin == 1 || nargin == 2) |
4343 | 1670 { |
4933 | 1671 std::string nm = args(0).string_value (); |
4343 | 1672 |
4933 | 1673 if (! error_state) |
9463
d34baf412786
support non-local function lookups in str2func
Jaroslav Hajek <highegg@gmail.com>
parents:
9458
diff
changeset
|
1674 retval = make_fcn_handle (nm, nargin != 2); |
4343 | 1675 else |
4933 | 1676 error ("str2func: expecting string as first argument"); |
4343 | 1677 } |
1678 else | |
5823 | 1679 print_usage (); |
4343 | 1680 |
1681 return retval; | |
1682 } | |
1683 | |
1684 /* | |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1685 %!function y = testrecursionfunc (f, x, n) |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1686 %! if (nargin < 3) |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1687 %! n = 0; |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1688 %! endif |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1689 %! if (n > 2) |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1690 %! y = f (x); |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1691 %! else |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1692 %! n++; |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1693 %! y = testrecursionfunc (@(x) f(2*x), x, n); |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1694 %! endif |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1695 %!test |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1696 %! assert (testrecursionfunc (@(x) x, 1), 8); |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1697 */ |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1698 |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1699 /* |
4343 | 1700 ;;; Local Variables: *** |
1701 ;;; mode: C++ *** | |
1702 ;;; End: *** | |
1703 */ |