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