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