4343
|
1 /* |
|
2 |
|
3 Copyright (C) 2003 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include <iostream> |
|
32 |
|
33 #include "defun.h" |
4654
|
34 #include "error.h" |
|
35 #include "gripes.h" |
4343
|
36 #include "oct-map.h" |
|
37 #include "ov-base.h" |
|
38 #include "ov-fcn-handle.h" |
|
39 #include "pr-output.h" |
|
40 #include "variables.h" |
|
41 |
|
42 DEFINE_OCTAVE_ALLOCATOR (octave_fcn_handle); |
|
43 |
4612
|
44 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_handle, |
|
45 "function handle", |
|
46 "function handle"); |
4343
|
47 |
4924
|
48 octave_value_list |
|
49 octave_fcn_handle::subsref (const std::string& type, |
|
50 const std::list<octave_value_list>& idx, |
|
51 int nargout) |
|
52 { |
|
53 octave_value_list retval; |
|
54 |
|
55 switch (type[0]) |
|
56 { |
|
57 case '(': |
|
58 { |
|
59 octave_function *f = function_value (); |
|
60 retval = f->subsref (type, idx, nargout); |
|
61 } |
|
62 break; |
|
63 |
|
64 |
|
65 case '{': |
|
66 case '.': |
|
67 { |
4930
|
68 std::string typ_nm = type_name (); |
|
69 error ("%s cannot be indexed with %c", typ_nm.c_str (), type[0]); |
4924
|
70 } |
|
71 break; |
|
72 |
|
73 default: |
|
74 panic_impossible (); |
|
75 } |
|
76 |
|
77 // XXX FIXME XXX -- perhaps there should be an |
|
78 // octave_value_list::next_subsref member function? See also |
|
79 // octave_builtin::subsref. |
|
80 |
|
81 if (idx.size () > 1) |
|
82 retval = retval(0).next_subsref (type, idx); |
|
83 |
|
84 return retval; |
|
85 } |
|
86 |
4343
|
87 void |
|
88 octave_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax) const |
|
89 { |
|
90 print_raw (os, pr_as_read_syntax); |
|
91 newline (os); |
|
92 } |
|
93 |
|
94 void |
|
95 octave_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
|
96 { |
4933
|
97 octave_print_internal (os, nm, pr_as_read_syntax, |
|
98 current_print_indent_level ()); |
4343
|
99 } |
|
100 |
|
101 octave_value |
|
102 make_fcn_handle (const std::string& nm) |
|
103 { |
|
104 octave_value retval; |
|
105 |
4930
|
106 octave_value f = lookup_function (nm); |
4343
|
107 |
4930
|
108 if (f.is_function ()) |
|
109 retval = octave_value (new octave_fcn_handle (f, nm)); |
4343
|
110 else |
|
111 error ("error creating function handle \"@%s\"", nm.c_str ()); |
|
112 |
|
113 return retval; |
|
114 } |
|
115 |
4933
|
116 DEFUN (functions, args, , |
4343
|
117 "-*- texinfo -*-\n\ |
4933
|
118 @deftypefn {Built-in Function} {} functions (@var{fcn_handle})\n\ |
|
119 Return a struct containing information about the function handle\n\ |
|
120 @var{fcn_handle}.\n\ |
|
121 @end deftypefn") |
4343
|
122 { |
|
123 octave_value retval; |
|
124 |
4933
|
125 if (args.length () == 1) |
4343
|
126 { |
4933
|
127 octave_fcn_handle *fh = args(0).fcn_handle_value (); |
4343
|
128 |
|
129 if (! error_state) |
|
130 { |
4933
|
131 octave_function *fcn = fh ? fh->function_value (true) : 0; |
4343
|
132 |
4933
|
133 if (fcn) |
4930
|
134 { |
4933
|
135 Octave_map m; |
4649
|
136 |
4933
|
137 std::string fh_nm = fh->fcn_name (); |
|
138 |
|
139 m.assign ("function", fh_nm.substr (1)); |
4343
|
140 |
4933
|
141 if (fcn->is_nested_function ()) |
|
142 m.assign ("type", "subfunction"); |
|
143 else |
|
144 m.assign ("type", "simple"); |
|
145 |
|
146 std::string nm = fcn->fcn_file_name (); |
4343
|
147 |
4933
|
148 if (nm.empty ()) |
|
149 m.assign ("file", "built-in function"); |
4343
|
150 else |
4933
|
151 m.assign ("file", nm); |
|
152 |
|
153 retval = m; |
4343
|
154 } |
|
155 else |
4933
|
156 error ("functions: invalid function handle object"); |
4343
|
157 } |
|
158 else |
4933
|
159 error ("functions: argument must be a function handle object"); |
4343
|
160 } |
|
161 else |
4933
|
162 print_usage ("functions"); |
4343
|
163 |
|
164 return retval; |
|
165 } |
|
166 |
4933
|
167 DEFUN (func2str, args, , |
4343
|
168 "-*- texinfo -*-\n\ |
4933
|
169 @deftypefn {Built-in Function} {} func2str (@var{fcn_handle})\n\ |
|
170 Return a string containing the name of the function referenced by\n\ |
|
171 the function handle @var{fcn_handle}.\n\ |
|
172 @end deftypefn") |
4343
|
173 { |
|
174 octave_value retval; |
|
175 |
4933
|
176 if (args.length () == 1) |
4930
|
177 { |
4933
|
178 octave_fcn_handle *fh = args(0).fcn_handle_value (); |
4930
|
179 |
4933
|
180 if (! error_state && fh) |
|
181 { |
|
182 std::string fh_nm = fh->fcn_name (); |
|
183 retval = fh_nm.substr (1); |
|
184 } |
4343
|
185 else |
4933
|
186 error ("func2str: expecting valid function handle as first argument"); |
4343
|
187 } |
|
188 else |
4933
|
189 print_usage ("func2str"); |
4343
|
190 |
|
191 return retval; |
|
192 } |
|
193 |
4933
|
194 DEFUN (str2func, args, , |
4343
|
195 "-*- texinfo -*-\n\ |
4933
|
196 @deftypefn {Built-in Function} {} str2func (@var{fcn_name})\n\ |
|
197 Return a function handle constructed from the string @var{fcn_name}.\n\ |
|
198 @end deftypefn") |
4343
|
199 { |
|
200 octave_value retval; |
|
201 |
4933
|
202 if (args.length () == 1) |
4343
|
203 { |
4933
|
204 std::string nm = args(0).string_value (); |
4343
|
205 |
4933
|
206 if (! error_state) |
|
207 retval = make_fcn_handle (nm); |
4343
|
208 else |
4933
|
209 error ("str2func: expecting string as first argument"); |
4343
|
210 } |
|
211 else |
4933
|
212 print_usage ("str2func"); |
4343
|
213 |
|
214 return retval; |
|
215 } |
|
216 |
|
217 /* |
|
218 ;;; Local Variables: *** |
|
219 ;;; mode: C++ *** |
|
220 ;;; End: *** |
|
221 */ |