Mercurial > hg > octave-nkf
annotate src/ov-fcn-handle.cc @ 9601:a9b37bae1802
add a couple of missing copyright statements
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 02 Sep 2009 09:21:36 +0200 |
parents | c5330ef7aecd |
children | b3089dba88bf |
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 | |
842 octave_fcn_handle::load_hdf5 (hid_t loc_id, const char *name, | |
6625 | 843 bool have_h5giterate_bug) |
4988 | 844 { |
7336 | 845 bool success = true; |
846 | |
4988 | 847 hid_t group_hid, data_hid, space_hid, type_hid, type_class_hid, st_id; |
848 hsize_t rank; | |
849 int slen; | |
850 | |
851 group_hid = H5Gopen (loc_id, name); | |
7336 | 852 if (group_hid < 0) |
853 return false; | |
4988 | 854 |
855 data_hid = H5Dopen (group_hid, "nm"); | |
856 | |
857 if (data_hid < 0) | |
858 { | |
859 H5Gclose (group_hid); | |
860 return false; | |
861 } | |
862 | |
863 type_hid = H5Dget_type (data_hid); | |
864 type_class_hid = H5Tget_class (type_hid); | |
865 | |
866 if (type_class_hid != H5T_STRING) | |
867 { | |
868 H5Tclose (type_hid); | |
869 H5Dclose (data_hid); | |
870 H5Gclose (group_hid); | |
871 return false; | |
872 } | |
873 | |
874 space_hid = H5Dget_space (data_hid); | |
875 rank = H5Sget_simple_extent_ndims (space_hid); | |
876 | |
877 if (rank != 0) | |
878 { | |
879 H5Sclose (space_hid); | |
880 H5Tclose (type_hid); | |
881 H5Dclose (data_hid); | |
882 H5Gclose (group_hid); | |
883 return false; | |
884 } | |
885 | |
886 slen = H5Tget_size (type_hid); | |
887 if (slen < 0) | |
888 { | |
889 H5Sclose (space_hid); | |
890 H5Tclose (type_hid); | |
891 H5Dclose (data_hid); | |
892 H5Gclose (group_hid); | |
893 return false; | |
894 } | |
895 | |
896 OCTAVE_LOCAL_BUFFER (char, nm_tmp, slen); | |
897 | |
898 // create datatype for (null-terminated) string to read into: | |
899 st_id = H5Tcopy (H5T_C_S1); | |
900 H5Tset_size (st_id, slen); | |
901 | |
5760 | 902 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, nm_tmp) < 0) |
4988 | 903 { |
6695 | 904 H5Tclose (st_id); |
4988 | 905 H5Sclose (space_hid); |
906 H5Tclose (type_hid); | |
6695 | 907 H5Dclose (data_hid); |
4988 | 908 H5Gclose (group_hid); |
909 return false; | |
910 } | |
911 H5Tclose (st_id); | |
912 H5Dclose (data_hid); | |
913 nm = nm_tmp; | |
914 | |
915 if (nm == "@<anonymous>") | |
916 { | |
917 data_hid = H5Dopen (group_hid, "fcn"); | |
918 | |
919 if (data_hid < 0) | |
920 { | |
6695 | 921 H5Sclose (space_hid); |
922 H5Tclose (type_hid); | |
4988 | 923 H5Gclose (group_hid); |
924 return false; | |
925 } | |
926 | |
6695 | 927 H5Tclose (type_hid); |
4988 | 928 type_hid = H5Dget_type (data_hid); |
929 type_class_hid = H5Tget_class (type_hid); | |
930 | |
931 if (type_class_hid != H5T_STRING) | |
932 { | |
6695 | 933 H5Sclose (space_hid); |
4988 | 934 H5Tclose (type_hid); |
935 H5Dclose (data_hid); | |
936 H5Gclose (group_hid); | |
937 return false; | |
938 } | |
939 | |
6695 | 940 H5Sclose (space_hid); |
4988 | 941 space_hid = H5Dget_space (data_hid); |
942 rank = H5Sget_simple_extent_ndims (space_hid); | |
943 | |
944 if (rank != 0) | |
945 { | |
946 H5Sclose (space_hid); | |
947 H5Tclose (type_hid); | |
948 H5Dclose (data_hid); | |
949 H5Gclose (group_hid); | |
950 return false; | |
951 } | |
952 | |
953 slen = H5Tget_size (type_hid); | |
954 if (slen < 0) | |
955 { | |
956 H5Sclose (space_hid); | |
957 H5Tclose (type_hid); | |
958 H5Dclose (data_hid); | |
959 H5Gclose (group_hid); | |
960 return false; | |
961 } | |
962 | |
963 OCTAVE_LOCAL_BUFFER (char, fcn_tmp, slen); | |
964 | |
965 // create datatype for (null-terminated) string to read into: | |
966 st_id = H5Tcopy (H5T_C_S1); | |
967 H5Tset_size (st_id, slen); | |
968 | |
5760 | 969 if (H5Dread (data_hid, st_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, fcn_tmp) < 0) |
4988 | 970 { |
6695 | 971 H5Tclose (st_id); |
4988 | 972 H5Sclose (space_hid); |
973 H5Tclose (type_hid); | |
6695 | 974 H5Dclose (data_hid); |
4988 | 975 H5Gclose (group_hid); |
976 return false; | |
977 } | |
6695 | 978 H5Tclose (st_id); |
4988 | 979 H5Dclose (data_hid); |
6625 | 980 |
981 octave_idx_type len = 0; | |
982 | |
983 // we have to pull some shenanigans here to make sure | |
984 // HDF5 doesn't print out all sorts of error messages if we | |
985 // call H5Aopen for a non-existing attribute | |
986 | |
987 H5E_auto_t err_func; | |
988 void *err_func_data; | |
4988 | 989 |
6625 | 990 // turn off error reporting temporarily, but save the error |
991 // reporting function: | |
992 H5Eget_auto (&err_func, &err_func_data); | |
993 H5Eset_auto (0, 0); | |
994 | |
995 hid_t attr_id = H5Aopen_name (group_hid, "SYMBOL_TABLE"); | |
4988 | 996 |
6625 | 997 if (attr_id >= 0) |
998 { | |
999 if (H5Aread (attr_id, H5T_NATIVE_IDX, &len) < 0) | |
1000 success = false; | |
1001 | |
1002 H5Aclose (attr_id); | |
1003 } | |
1004 | |
1005 // restore error reporting: | |
1006 H5Eset_auto (err_func, err_func_data); | |
1007 | |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9343
diff
changeset
|
1008 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
|
1009 |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
1010 // 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
|
1011 // 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
|
1012 |
7336 | 1013 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
|
1014 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
|
1015 |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
1016 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
|
1017 |
c6463412aebb
eliminate symbol_table::scope_stack; fix scoping issue with evalin
John W. Eaton <jwe@octave.org>
parents:
8946
diff
changeset
|
1018 octave_call_stack::push (local_scope, 0); |
9396
17af7cce7d1b
yet more unwind_protect improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9395
diff
changeset
|
1019 unwind_protect::add_fcn (octave_call_stack::pop); |
7336 | 1020 |
6625 | 1021 if (len > 0 && success) |
4989 | 1022 { |
6625 | 1023 #ifdef HAVE_H5GGET_NUM_OBJS |
1024 hsize_t num_obj = 0; | |
1025 data_hid = H5Gopen (group_hid, "symbol table"); | |
1026 H5Gget_num_objs (data_hid, &num_obj); | |
1027 H5Gclose (data_hid); | |
1028 | |
1029 if (num_obj != static_cast<hsize_t>(len)) | |
1030 { | |
1031 error ("load: failed to load anonymous function handle"); | |
1032 success = false; | |
1033 } | |
1034 #endif | |
1035 | |
1036 if (! error_state) | |
1037 { | |
1038 hdf5_callback_data dsub; | |
1039 int current_item = 0; | |
1040 for (octave_idx_type i = 0; i < len; i++) | |
1041 { | |
1042 if (H5Giterate (group_hid, "symbol table", ¤t_item, | |
1043 hdf5_read_next_data, &dsub) <= 0) | |
1044 { | |
1045 error ("load: failed to load anonymous function handle"); | |
1046 success = false; | |
1047 break; | |
1048 } | |
1049 | |
1050 if (have_h5giterate_bug) | |
1051 current_item++; // H5Giterate returns last index processed | |
1052 | |
7336 | 1053 symbol_table::varref (dsub.name, local_scope) = dsub.tc; |
6625 | 1054 } |
1055 } | |
1056 } | |
1057 | |
1058 if (success) | |
1059 { | |
1060 int parse_status; | |
1061 octave_value anon_fcn_handle = | |
1062 eval_string (fcn_tmp, true, parse_status); | |
1063 | |
1064 if (parse_status == 0) | |
1065 { | |
1066 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
|
1067 |
6625 | 1068 if (fh) |
7761
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
1069 { |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
1070 fcn = fh->fcn; |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
1071 |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
1072 octave_user_function *uf = fcn.user_function_value (true); |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
1073 |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
1074 if (uf) |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
1075 symbol_table::cache_name (uf->scope (), nm); |
5adeea5de26c
symbol table reporting functions
John W. Eaton <jwe@octave.org>
parents:
7756
diff
changeset
|
1076 } |
6625 | 1077 else |
1078 success = false; | |
1079 } | |
4989 | 1080 else |
6625 | 1081 success = false; |
4989 | 1082 } |
6625 | 1083 |
9377
610bf90fce2a
update unwind_protect usage everywhere
Jaroslav Hajek <highegg@gmail.com>
parents:
9343
diff
changeset
|
1084 unwind_protect::run_frame (uwp_frame); |
4988 | 1085 } |
1086 else | |
1087 { | |
6625 | 1088 std::string octaveroot; |
1089 std::string fpath; | |
1090 | |
1091 // we have to pull some shenanigans here to make sure | |
1092 // HDF5 doesn't print out all sorts of error messages if we | |
1093 // call H5Aopen for a non-existing attribute | |
1094 | |
1095 H5E_auto_t err_func; | |
1096 void *err_func_data; | |
1097 | |
1098 // turn off error reporting temporarily, but save the error | |
1099 // reporting function: | |
1100 H5Eget_auto (&err_func, &err_func_data); | |
1101 H5Eset_auto (0, 0); | |
1102 | |
1103 hid_t attr_id = H5Aopen_name (group_hid, "OCTAVEROOT"); | |
1104 if (attr_id >= 0) | |
6225 | 1105 { |
6695 | 1106 H5Tclose (type_hid); |
6625 | 1107 type_hid = H5Aget_type (attr_id); |
1108 type_class_hid = H5Tget_class (type_hid); | |
1109 | |
1110 if (type_class_hid != H5T_STRING) | |
1111 success = false; | |
1112 else | |
1113 { | |
1114 slen = H5Tget_size (type_hid); | |
1115 st_id = H5Tcopy (H5T_C_S1); | |
1116 H5Tset_size (st_id, slen); | |
1117 OCTAVE_LOCAL_BUFFER (char, root_tmp, slen); | |
1118 | |
1119 if (H5Aread (attr_id, st_id, root_tmp) < 0) | |
1120 success = false; | |
1121 else | |
1122 octaveroot = root_tmp; | |
6695 | 1123 |
1124 H5Tclose (st_id); | |
6625 | 1125 } |
1126 | |
1127 H5Aclose (attr_id); | |
6225 | 1128 } |
6625 | 1129 |
6695 | 1130 if (success) |
6625 | 1131 { |
6695 | 1132 attr_id = H5Aopen_name (group_hid, "FILE"); |
1133 if (attr_id >= 0) | |
1134 { | |
1135 H5Tclose (type_hid); | |
1136 type_hid = H5Aget_type (attr_id); | |
1137 type_class_hid = H5Tget_class (type_hid); | |
6625 | 1138 |
6695 | 1139 if (type_class_hid != H5T_STRING) |
6625 | 1140 success = false; |
1141 else | |
6695 | 1142 { |
1143 slen = H5Tget_size (type_hid); | |
1144 st_id = H5Tcopy (H5T_C_S1); | |
1145 H5Tset_size (st_id, slen); | |
1146 OCTAVE_LOCAL_BUFFER (char, path_tmp, slen); | |
1147 | |
1148 if (H5Aread (attr_id, st_id, path_tmp) < 0) | |
1149 success = false; | |
1150 else | |
1151 fpath = path_tmp; | |
1152 | |
1153 H5Tclose (st_id); | |
1154 } | |
1155 | |
1156 H5Aclose (attr_id); | |
6625 | 1157 } |
1158 } | |
1159 | |
1160 // restore error reporting: | |
1161 H5Eset_auto (err_func, err_func_data); | |
1162 | |
1163 success = (success ? set_fcn (octaveroot, fpath) : success); | |
4988 | 1164 } |
1165 | |
6695 | 1166 H5Tclose (type_hid); |
1167 H5Sclose (space_hid); | |
1168 H5Gclose (group_hid); | |
1169 | |
6625 | 1170 return success; |
4988 | 1171 } |
6625 | 1172 |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1173 #endif |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1174 |
6625 | 1175 /* |
1176 | |
1177 %!test | |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1178 %! a = 2; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1179 %! f = @(x) a + x; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1180 %! g = @(x) 2 * x; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1181 %! hm = @flops; |
7745
0ff0fc033f28
better handling of functions found by relative lookup
John W. Eaton <jwe@octave.org>
parents:
7744
diff
changeset
|
1182 %! hdld = @svd; |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1183 %! hbi = @log2; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1184 %! f2 = f; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1185 %! g2 = g; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1186 %! hm2 = hm; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1187 %! hdld2 = hdld; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1188 %! hbi2 = hbi; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1189 %! modes = {"-text", "-binary"}; |
6625 | 1190 %! 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
|
1191 %! modes(end+1) = "-hdf5"; |
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1192 %! endif |
7901 | 1193 %! for i = 1:numel (modes) |
1194 %! mode = modes{i}; | |
6625 | 1195 %! nm = tmpnam(); |
1196 %! unwind_protect | |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1197 %! 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
|
1198 %! clear f2 g2 hm2 hdld2 hbi2 |
6625 | 1199 %! load (nm); |
1200 %! assert (f(2),f2(2)); | |
1201 %! assert (g(2),g2(2)); | |
1202 %! assert (g(3),g2(3)); | |
1203 %! unlink (nm); | |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1204 %! save (mode, nm, "f2", "g2", "hm2", "hdld2", "hbi2"); |
6625 | 1205 %! unwind_protect_cleanup |
1206 %! unlink (nm); | |
1207 %! end_unwind_protect | |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1208 %! endfor |
6625 | 1209 |
1210 */ | |
4988 | 1211 |
4343 | 1212 void |
1213 octave_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax) const | |
1214 { | |
1215 print_raw (os, pr_as_read_syntax); | |
1216 newline (os); | |
1217 } | |
1218 | |
1219 void | |
1220 octave_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax) const | |
1221 { | |
4980 | 1222 bool printed = false; |
1223 | |
1224 if (nm == "@<anonymous>") | |
1225 { | |
1226 tree_print_code tpc (os); | |
1227 | |
4989 | 1228 // FCN is const because this member function is, so we can't |
4980 | 1229 // use it to call user_function_value, so we make a copy first. |
1230 | |
1231 octave_value ftmp = fcn; | |
1232 | |
1233 octave_user_function *f = ftmp.user_function_value (); | |
1234 | |
1235 if (f) | |
1236 { | |
1237 tree_parameter_list *p = f->parameter_list (); | |
1238 | |
1239 os << "@("; | |
1240 | |
1241 if (p) | |
1242 p->accept (tpc); | |
1243 | |
1244 os << ") "; | |
1245 | |
1246 tree_statement_list *b = f->body (); | |
1247 | |
1248 if (b) | |
1249 { | |
1250 assert (b->length () == 1); | |
1251 | |
1252 tree_statement *s = b->front (); | |
1253 | |
1254 if (s) | |
1255 { | |
1256 if (s->is_expression ()) | |
1257 { | |
1258 tree_expression *e = s->expression (); | |
1259 | |
1260 if (e) | |
6657 | 1261 e->accept (tpc); |
4980 | 1262 } |
1263 else | |
1264 { | |
1265 tree_command *c = s->command (); | |
1266 | |
1267 tpc.suspend_newline (); | |
1268 c->accept (tpc); | |
1269 tpc.resume_newline (); | |
1270 } | |
1271 } | |
1272 } | |
1273 | |
1274 printed = true; | |
1275 } | |
1276 } | |
1277 | |
1278 if (! printed) | |
1279 octave_print_internal (os, nm, pr_as_read_syntax, | |
1280 current_print_indent_level ()); | |
4343 | 1281 } |
1282 | |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1283 static string_vector |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1284 get_builtin_classes (void) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1285 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1286 // FIXME: this should really be read from somewhere else. |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1287 static const char *cnames[15] = { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1288 "double", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1289 "single", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1290 "int8", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1291 "int16", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1292 "int32", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1293 "int64", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1294 "uint8", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1295 "uint16", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1296 "uint32", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1297 "uint64", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1298 "logical", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1299 "char", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1300 "cell", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1301 "struct", |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1302 "function_handle" |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1303 }; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1304 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1305 static string_vector retval; |
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 if (retval.is_empty ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1308 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1309 retval = string_vector (15); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1310 for (int i = 0; i < 15; i++) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1311 retval(i) = cnames[i]; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1312 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1313 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1314 return retval; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1315 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1316 |
4343 | 1317 octave_value |
9463
d34baf412786
support non-local function lookups in str2func
Jaroslav Hajek <highegg@gmail.com>
parents:
9458
diff
changeset
|
1318 make_fcn_handle (const std::string& nm, bool local_funcs) |
4343 | 1319 { |
1320 octave_value retval; | |
1321 | |
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
|
1322 // 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
|
1323 |
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 // 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
|
1325 // 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
|
1326 // 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
|
1327 // 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
|
1328 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1329 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
|
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 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
|
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 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
|
1334 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
|
1335 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
|
1336 { |
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 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
|
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 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
|
1340 { |
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 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
|
1342 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
|
1343 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
|
1344 |
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 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
|
1346 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
|
1347 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
|
1348 |
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 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
|
1350 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
|
1351 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
|
1352 |
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 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
|
1354 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
|
1355 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
|
1356 |
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 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
|
1358 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
|
1359 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
|
1360 |
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 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
|
1362 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
|
1363 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
|
1364 |
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 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
|
1366 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
|
1367 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
|
1368 } |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1369 } |
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 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
|
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 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
|
1373 { |
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 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
|
1375 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
|
1376 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
|
1377 |
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 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
|
1379 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
|
1380 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
|
1381 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1382 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
|
1383 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
|
1384 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
|
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 case '~': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1387 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
|
1388 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
|
1389 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
|
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 } |
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 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
|
1393 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
|
1394 } |
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 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
|
1396 { |
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 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
|
1398 { |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1399 case '~': |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1400 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
|
1401 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
|
1402 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
|
1403 |
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 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
|
1405 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
|
1406 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
|
1407 |
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 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
|
1409 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
|
1410 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
|
1411 |
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 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
|
1413 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
|
1414 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
|
1415 |
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 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
|
1417 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
|
1418 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
|
1419 |
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 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
|
1421 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
|
1422 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
|
1423 |
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 case '^': |
9343
70e0d3b1f26f
fix typos in previous change
John W. Eaton <jwe@octave.org>
parents:
9342
diff
changeset
|
1425 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
|
1426 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
|
1427 |
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 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
|
1429 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
|
1430 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
|
1431 |
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 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
|
1433 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
|
1434 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
|
1435 |
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 case '>': |
9343
70e0d3b1f26f
fix typos in previous change
John W. Eaton <jwe@octave.org>
parents:
9342
diff
changeset
|
1437 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
|
1438 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
|
1439 |
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 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
|
1441 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
|
1442 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
|
1443 |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1444 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
|
1445 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
|
1446 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
|
1447 } |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1448 } |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1449 |
9458
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1450 bool handle_ok = false; |
9463
d34baf412786
support non-local function lookups in str2func
Jaroslav Hajek <highegg@gmail.com>
parents:
9458
diff
changeset
|
1451 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
|
1452 local_funcs); |
9458
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 if (f.is_undefined ()) |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1455 { |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1456 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
|
1457 handle_ok = true; |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1458 else |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1459 { |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1460 load_path::update (); |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1461 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
|
1462 handle_ok = true; |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1463 } |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1464 } |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1465 else |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1466 handle_ok = true; |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1467 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1468 octave_function *fptr = f.is_defined () ? f.function_value () : 0; |
6481 | 1469 |
9458
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1470 |
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1471 if (handle_ok) |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1472 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1473 // If it's a subfunction, private function, or class constructor, |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1474 // we want no dispatch. |
9458
0c7d84a65386
allow taking handles of methods with no base overload
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
1475 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
|
1476 || fptr->is_class_constructor ())) |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1477 retval = octave_value (new octave_fcn_handle (f, tnm)); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1478 else |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1479 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1480 typedef octave_fcn_handle::str_ov_map str_ov_map; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1481 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
|
1482 const string_vector cnames = get_builtin_classes (); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1483 for (octave_idx_type i = 0; i < cnames.length (); i++) |
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 std::string cnam = cnames(i); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1486 octave_value method = symbol_table::find_method (tnm, cnam); |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1487 if (method.is_defined ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1488 (*disp)[cnam] = method; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1489 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1490 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1491 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
|
1492 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1493 } |
9419
0dd3c7a2ba19
undo escaped change in previous patch
Jaroslav Hajek <highegg@gmail.com>
parents:
9418
diff
changeset
|
1494 else |
0dd3c7a2ba19
undo escaped change in previous patch
Jaroslav Hajek <highegg@gmail.com>
parents:
9418
diff
changeset
|
1495 error ("error creating function handle \"@%s\"", nm.c_str ()); |
4343 | 1496 |
1497 return retval; | |
1498 } | |
1499 | |
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
|
1500 /* |
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 %!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
|
1502 %! 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
|
1503 %! ".'", "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
|
1504 %! ".+", "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
|
1505 %! ".-", "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
|
1506 %! ".*", "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
|
1507 %! "./", "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
|
1508 %! ".^", "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
|
1509 %! ".\\", "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
|
1510 %! "<=", "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
|
1511 %! "==", "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
|
1512 %! ">=", "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
|
1513 %! "~=", "ne"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1514 %! "!=", "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
|
1515 %! "**", "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
|
1516 %! "~", "not"; |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1517 %! "!", "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
|
1518 %! "\'", "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
|
1519 %! "+", "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
|
1520 %! "-", "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
|
1521 %! "*", "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
|
1522 %! "/", "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
|
1523 %! "^", "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
|
1524 %! "\\", "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
|
1525 %! "<", "lt"; |
9343
70e0d3b1f26f
fix typos in previous change
John W. Eaton <jwe@octave.org>
parents:
9342
diff
changeset
|
1526 %! ">", "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
|
1527 %! "&", "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
|
1528 %! "|", "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
|
1529 %! 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
|
1530 %! 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
|
1531 %! 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
|
1532 */ |
2ca8879a140c
allow function handles to be created from operators that correspond to named functions
John W. Eaton <jwe@octave.org>
parents:
9144
diff
changeset
|
1533 |
4933 | 1534 DEFUN (functions, args, , |
4343 | 1535 "-*- texinfo -*-\n\ |
4933 | 1536 @deftypefn {Built-in Function} {} functions (@var{fcn_handle})\n\ |
1537 Return a struct containing information about the function handle\n\ | |
1538 @var{fcn_handle}.\n\ | |
1539 @end deftypefn") | |
4343 | 1540 { |
1541 octave_value retval; | |
1542 | |
4933 | 1543 if (args.length () == 1) |
4343 | 1544 { |
4933 | 1545 octave_fcn_handle *fh = args(0).fcn_handle_value (); |
4343 | 1546 |
1547 if (! error_state) | |
1548 { | |
7744
14b841c47a5f
handle load/save for handles to built-in functions
John W. Eaton <jwe@octave.org>
parents:
7740
diff
changeset
|
1549 octave_function *fcn = fh ? fh->function_value () : 0; |
4343 | 1550 |
4933 | 1551 if (fcn) |
4930 | 1552 { |
4933 | 1553 Octave_map m; |
4649 | 1554 |
4933 | 1555 std::string fh_nm = fh->fcn_name (); |
1556 | |
6625 | 1557 if (fh_nm == "@<anonymous>") |
1558 { | |
1559 std::ostringstream buf; | |
1560 fh->print_raw (buf); | |
1561 m.assign ("function", buf.str ()); | |
1562 | |
1563 m.assign ("type", "anonymous"); | |
1564 } | |
1565 else | |
1566 { | |
1567 m.assign ("function", fh_nm); | |
4343 | 1568 |
6625 | 1569 if (fcn->is_nested_function ()) |
1570 { | |
1571 m.assign ("type", "subfunction"); | |
1572 Cell parentage (dim_vector (1, 2)); | |
1573 parentage.elem(0) = fh_nm; | |
1574 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
|
1575 m.assign ("parentage", octave_value (parentage)); |
6625 | 1576 } |
9509
c5330ef7aecd
fix handles to private functions, simplify out-of-date checks in symtab
Jaroslav Hajek <highegg@gmail.com>
parents:
9466
diff
changeset
|
1577 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
|
1578 m.assign ("type", "private"); |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1579 else if (fh->is_overloaded ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9419
diff
changeset
|
1580 m.assign ("type", "overloaded"); |
6625 | 1581 else |
1582 m.assign ("type", "simple"); | |
1583 } | |
4933 | 1584 |
1585 std::string nm = fcn->fcn_file_name (); | |
4343 | 1586 |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1587 if (fh_nm == "@<anonymous>") |
4935 | 1588 { |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1589 m.assign ("file", nm); |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1590 |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1591 octave_user_function *fu = fh->user_function_value (); |
6625 | 1592 |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1593 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
|
1594 = 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
|
1595 |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1596 size_t varlen = vars.size (); |
6625 | 1597 |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1598 if (varlen > 0) |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1599 { |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1600 Octave_map ws; |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1601 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
|
1602 p != vars.end (); p++) |
6625 | 1603 { |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1604 ws.assign (p->name (), p->varval (0)); |
6625 | 1605 } |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1606 |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1607 m.assign ("workspace", ws); |
6625 | 1608 } |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1609 } |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1610 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
|
1611 { |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1612 octave_function *fu = fh->function_value (); |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1613 m.assign ("file", fu->fcn_file_name ()); |
4935 | 1614 } |
4343 | 1615 else |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1616 m.assign ("file", ""); |
4933 | 1617 |
1618 retval = m; | |
4343 | 1619 } |
1620 else | |
4933 | 1621 error ("functions: invalid function handle object"); |
4343 | 1622 } |
1623 else | |
4933 | 1624 error ("functions: argument must be a function handle object"); |
4343 | 1625 } |
1626 else | |
5823 | 1627 print_usage (); |
4343 | 1628 |
1629 return retval; | |
1630 } | |
1631 | |
4933 | 1632 DEFUN (func2str, args, , |
4343 | 1633 "-*- texinfo -*-\n\ |
4933 | 1634 @deftypefn {Built-in Function} {} func2str (@var{fcn_handle})\n\ |
1635 Return a string containing the name of the function referenced by\n\ | |
1636 the function handle @var{fcn_handle}.\n\ | |
1637 @end deftypefn") | |
4343 | 1638 { |
1639 octave_value retval; | |
1640 | |
4933 | 1641 if (args.length () == 1) |
4930 | 1642 { |
4933 | 1643 octave_fcn_handle *fh = args(0).fcn_handle_value (); |
4930 | 1644 |
4933 | 1645 if (! error_state && fh) |
1646 { | |
1647 std::string fh_nm = fh->fcn_name (); | |
6416 | 1648 |
1649 if (fh_nm == "@<anonymous>") | |
1650 { | |
1651 std::ostringstream buf; | |
1652 | |
1653 fh->print_raw (buf); | |
1654 | |
1655 retval = buf.str (); | |
1656 } | |
1657 else | |
1658 retval = fh_nm; | |
4933 | 1659 } |
4343 | 1660 else |
4933 | 1661 error ("func2str: expecting valid function handle as first argument"); |
4343 | 1662 } |
1663 else | |
5823 | 1664 print_usage (); |
4343 | 1665 |
1666 return retval; | |
1667 } | |
1668 | |
4933 | 1669 DEFUN (str2func, args, , |
4343 | 1670 "-*- texinfo -*-\n\ |
4933 | 1671 @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
|
1672 @deftypefnx {Built-in Function} {} str2func (@var{fcn_name}, \"global\")\n\ |
4933 | 1673 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
|
1674 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
|
1675 are ignored in the lookup.\n\ |
4933 | 1676 @end deftypefn") |
4343 | 1677 { |
1678 octave_value retval; | |
9463
d34baf412786
support non-local function lookups in str2func
Jaroslav Hajek <highegg@gmail.com>
parents:
9458
diff
changeset
|
1679 int nargin = args.length (); |
4343 | 1680 |
9463
d34baf412786
support non-local function lookups in str2func
Jaroslav Hajek <highegg@gmail.com>
parents:
9458
diff
changeset
|
1681 if (nargin == 1 || nargin == 2) |
4343 | 1682 { |
4933 | 1683 std::string nm = args(0).string_value (); |
4343 | 1684 |
4933 | 1685 if (! error_state) |
9463
d34baf412786
support non-local function lookups in str2func
Jaroslav Hajek <highegg@gmail.com>
parents:
9458
diff
changeset
|
1686 retval = make_fcn_handle (nm, nargin != 2); |
4343 | 1687 else |
4933 | 1688 error ("str2func: expecting string as first argument"); |
4343 | 1689 } |
1690 else | |
5823 | 1691 print_usage (); |
4343 | 1692 |
1693 return retval; | |
1694 } | |
1695 | |
1696 /* | |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1697 %!function y = testrecursionfunc (f, x, n) |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1698 %! if (nargin < 3) |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1699 %! n = 0; |
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 %! if (n > 2) |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1702 %! y = f (x); |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1703 %! else |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1704 %! n++; |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1705 %! 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
|
1706 %! endif |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1707 %!test |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1708 %! assert (testrecursionfunc (@(x) x, 1), 8); |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1709 */ |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1710 |
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7761
diff
changeset
|
1711 /* |
4343 | 1712 ;;; Local Variables: *** |
1713 ;;; mode: C++ *** | |
1714 ;;; End: *** | |
1715 */ |