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" |
|
34 #include "oct-map.h" |
|
35 #include "ov-base.h" |
|
36 #include "ov-fcn-handle.h" |
|
37 #include "pr-output.h" |
|
38 #include "variables.h" |
|
39 |
|
40 DEFINE_OCTAVE_ALLOCATOR (octave_fcn_handle); |
|
41 |
4612
|
42 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_fcn_handle, |
|
43 "function handle", |
|
44 "function handle"); |
4343
|
45 |
|
46 void |
|
47 octave_fcn_handle::print (std::ostream& os, bool pr_as_read_syntax) const |
|
48 { |
|
49 print_raw (os, pr_as_read_syntax); |
|
50 newline (os); |
|
51 } |
|
52 |
|
53 void |
|
54 octave_fcn_handle::print_raw (std::ostream& os, bool pr_as_read_syntax) const |
|
55 { |
|
56 indent (os); |
4649
|
57 os << name (); |
4343
|
58 } |
|
59 |
|
60 octave_value |
|
61 make_fcn_handle (const std::string& nm) |
|
62 { |
|
63 octave_value retval; |
|
64 |
|
65 octave_function *f = lookup_function (nm); |
|
66 |
|
67 if (f) |
|
68 { |
|
69 octave_fcn_handle fh (f, nm); |
|
70 |
|
71 retval = octave_value (fh); |
|
72 } |
|
73 else |
|
74 error ("error creating function handle \"@%s\"", nm.c_str ()); |
|
75 |
|
76 return retval; |
|
77 } |
|
78 |
|
79 DEFUN (functions, args, , |
|
80 "-*- texinfo -*-\n\ |
|
81 @deftypefn {Built-in Function} {} functions (@var{fcn_handle})\n\ |
|
82 Return a struct containing information about the function handle\n\ |
|
83 @var{fcn_handle}.\n\ |
|
84 @end deftypefn") |
|
85 { |
|
86 octave_value retval; |
|
87 |
|
88 if (args.length () == 1) |
|
89 { |
4346
|
90 octave_fcn_handle *fh = args(0).fcn_handle_value (); |
4343
|
91 |
|
92 if (! error_state) |
|
93 { |
4346
|
94 octave_function *fcn = fh ? fh->function_value (true) : 0; |
4343
|
95 |
|
96 if (fcn) |
|
97 { |
|
98 Octave_map m; |
|
99 |
4649
|
100 std::string fh_nm = fh->name (); |
|
101 |
|
102 m ["function"](0) = fh_nm.substr (1); |
4343
|
103 |
|
104 if (fcn->is_nested_function ()) |
|
105 m ["type"](0) = "subfunction"; |
|
106 else |
|
107 m ["type"](0) = "simple"; |
|
108 |
|
109 std::string nm = fcn->fcn_file_name (); |
|
110 |
|
111 if (nm.empty ()) |
|
112 m ["file"](0) = "built-in function"; |
|
113 else |
|
114 m ["file"](0) = nm; |
|
115 |
|
116 retval = m; |
|
117 } |
|
118 else |
|
119 error ("functions: invalid function handle object"); |
|
120 } |
|
121 else |
|
122 error ("functions: argument must be a function handle object"); |
|
123 } |
|
124 else |
|
125 print_usage ("functions"); |
|
126 |
|
127 return retval; |
|
128 } |
|
129 |
|
130 DEFUN (func2str, args, , |
|
131 "-*- texinfo -*-\n\ |
|
132 @deftypefn {Built-in Function} {} func2str (@var{fcn_handle})\n\ |
|
133 Return a string containing the name of the function referenced by\n\ |
|
134 the function handle @var{fcn_handle}.\n\ |
|
135 @end deftypefn") |
|
136 { |
|
137 octave_value retval; |
|
138 |
|
139 if (args.length () == 1) |
|
140 { |
4346
|
141 octave_fcn_handle *fh = args(0).fcn_handle_value (); |
4343
|
142 |
4346
|
143 if (! error_state && fh) |
4649
|
144 { |
|
145 std::string fh_nm = fh->name (); |
|
146 retval = fh_nm.substr (1); |
|
147 } |
4343
|
148 else |
4346
|
149 error ("func2str: expecting valid function handle as first argument"); |
4343
|
150 } |
|
151 else |
|
152 print_usage ("func2str"); |
|
153 |
|
154 return retval; |
|
155 } |
|
156 |
|
157 DEFUN (str2func, args, , |
|
158 "-*- texinfo -*-\n\ |
|
159 @deftypefn {Built-in Function} {} str2func (@var{fcn_name})\n\ |
|
160 Return a function handle constructed from the string @var{fcn_name}.\n\ |
|
161 @end deftypefn") |
|
162 { |
|
163 octave_value retval; |
|
164 |
|
165 if (args.length () == 1) |
|
166 { |
|
167 std::string nm = args(0).string_value (); |
|
168 |
|
169 if (! error_state) |
|
170 retval = make_fcn_handle (nm); |
|
171 else |
|
172 error ("str2func: expecting string as first argument"); |
|
173 } |
|
174 else |
|
175 print_usage ("str2func"); |
|
176 |
|
177 return retval; |
|
178 } |
|
179 |
|
180 /* |
|
181 ;;; Local Variables: *** |
|
182 ;;; mode: C++ *** |
|
183 ;;; End: *** |
|
184 */ |