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