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