5164
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 2001, 2005, 2006, 2007 John W. Eaton and Paul Kienzle |
5164
|
4 |
7016
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
5164
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
5164
|
11 |
7016
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
5164
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
5164
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <list> |
|
28 #include <map> |
|
29 #include <string> |
|
30 |
7336
|
31 #include "Cell.h" |
|
32 #include "oct-map.h" |
5164
|
33 #include "defun-dld.h" |
|
34 #include "ov.h" |
|
35 #include "ov-fcn.h" |
|
36 #include "ov-typeinfo.h" |
|
37 #include "pager.h" |
|
38 #include "parse.h" |
|
39 #include "symtab.h" |
|
40 #include "variables.h" |
|
41 |
|
42 DEFUN_DLD (builtin, args, nargout, |
|
43 "-*- texinfo -*-\n\ |
|
44 @deftypefn {Loadable Function} {[@dots{}]} builtin (@var{f}, @dots{})\n\ |
|
45 Call the base function @var{f} even if @var{f} is overloaded to\n\ |
|
46 some other function for the given type signature.\n\ |
5646
|
47 @seealso{dispatch}\n\ |
|
48 @end deftypefn") |
5164
|
49 { |
|
50 octave_value_list retval; |
|
51 |
|
52 int nargin = args.length (); |
|
53 |
|
54 if (nargin > 0) |
|
55 { |
|
56 const std::string name (args(0).string_value ()); |
|
57 |
7336
|
58 if (! error_state) |
5164
|
59 { |
7336
|
60 octave_value fcn = symbol_table::find_function (name); |
5164
|
61 |
7336
|
62 if (fcn.is_defined ()) |
|
63 retval = feval (fcn.function_value (), args.splice (0, 1), |
|
64 nargout); |
5200
|
65 else |
7336
|
66 error ("builtin: lookup for symbol `%s' failed", name.c_str ()); |
5164
|
67 } |
|
68 else |
7336
|
69 error ("builtin: expecting function name as first argument"); |
5164
|
70 } |
|
71 else |
5823
|
72 print_usage (); |
5164
|
73 |
|
74 return retval; |
|
75 } |
|
76 |
7336
|
77 DEFUN_DLD (dispatch, args, nargout, |
5164
|
78 "-*- texinfo -*-\n\ |
|
79 @deftypefn {Loadable Function} {} dispatch (@var{f}, @var{r}, @var{type})\n\ |
|
80 \n\ |
|
81 Replace the function @var{f} with a dispatch so that function @var{r}\n\ |
|
82 is called when @var{f} is called with the first argument of the named\n\ |
|
83 @var{type}. If the type is @var{any} then call @var{r} if no other type\n\ |
|
84 matches. The original function @var{f} is accessible using\n\ |
6248
|
85 @code{builtin (@var{f}, @dots{})}.\n\ |
5164
|
86 \n\ |
|
87 If @var{r} is omitted, clear dispatch function associated with @var{type}.\n\ |
|
88 \n\ |
|
89 If both @var{r} and @var{type} are omitted, list dispatch functions\n\ |
5646
|
90 for @var{f}.\n\ |
|
91 @seealso{builtin}\n\ |
|
92 @end deftypefn") |
5164
|
93 { |
|
94 octave_value retval; |
7336
|
95 |
5164
|
96 int nargin = args.length (); |
|
97 |
7336
|
98 std::string f, r, t; |
5164
|
99 |
7336
|
100 if (nargin > 0 && nargin < 4) |
5164
|
101 { |
7336
|
102 if (nargin > 0) |
|
103 { |
|
104 f = args(0).string_value (); |
|
105 |
|
106 if (error_state) |
|
107 { |
|
108 error ("dispatch: expecting first argument to be function name"); |
|
109 return retval; |
|
110 } |
|
111 } |
|
112 |
|
113 if (nargin > 1) |
|
114 { |
|
115 r = args(1).string_value (); |
5164
|
116 |
7336
|
117 if (error_state) |
|
118 { |
|
119 error ("dispatch: expecting second argument to be function name"); |
|
120 return retval; |
|
121 } |
|
122 } |
|
123 |
|
124 if (nargin > 2) |
|
125 { |
|
126 t = args(2).string_value (); |
|
127 |
|
128 if (error_state) |
|
129 { |
|
130 error ("dispatch: expecting third argument to be type name"); |
|
131 return retval; |
|
132 } |
|
133 } |
5164
|
134 |
7336
|
135 if (nargin == 1) |
|
136 { |
|
137 if (nargout > 0) |
|
138 { |
|
139 symbol_table::fcn_info::dispatch_map_type dm |
|
140 = symbol_table::get_dispatch (f); |
|
141 |
|
142 size_t len = dm.size (); |
|
143 |
|
144 Cell type_field (len, 1); |
|
145 Cell name_field (len, 1); |
|
146 |
|
147 symbol_table::fcn_info::dispatch_map_type::const_iterator p |
|
148 = dm.begin (); |
|
149 |
|
150 for (size_t i = 0; i < len; i++) |
|
151 { |
|
152 type_field(i) = p->first; |
|
153 name_field(i) = p->second; |
|
154 |
|
155 p++; |
|
156 } |
|
157 |
|
158 Octave_map m; |
|
159 |
|
160 m.assign ("type", type_field); |
|
161 m.assign ("name", name_field); |
|
162 |
|
163 retval = m; |
|
164 } |
|
165 else |
|
166 symbol_table::print_dispatch (octave_stdout, f); |
|
167 } |
|
168 else if (nargin == 2) |
|
169 { |
|
170 t = r; |
|
171 symbol_table::clear_dispatch (f, t); |
|
172 } |
|
173 else |
|
174 symbol_table::add_dispatch (f, t, r); |
5164
|
175 } |
7336
|
176 else |
|
177 print_usage (); |
5164
|
178 |
|
179 return retval; |
|
180 } |
|
181 |
|
182 /* |
|
183 |
|
184 %!test # builtin function replacement |
|
185 %! dispatch('sin','length','string') |
5582
|
186 %! assert(sin("abc"),3) |
5164
|
187 %! assert(sin(0),0,10*eps); |
|
188 %!test # 'any' function |
|
189 %! dispatch('sin','exp','any') |
|
190 %! assert(sin(0),1,eps); |
5582
|
191 %! assert(sin("abc"),3); |
5164
|
192 %!test # 'builtin' function |
|
193 %! assert(builtin('sin',0),0,eps); |
|
194 %! builtin('eval','x=1;'); |
|
195 %! assert(x,1); |
|
196 %!test # clear function mapping |
|
197 %! dispatch('sin','string') |
|
198 %! dispatch('sin','any') |
|
199 %! assert(sin(0),0,10*eps); |
|
200 %!test # oct-file replacement |
|
201 %! dispatch('fft','length','string') |
|
202 %! assert(fft([1,1]),[2,0]); |
5582
|
203 %! assert(fft("abc"),3) |
5164
|
204 %! dispatch('fft','string'); |
|
205 %!test # m-file replacement |
|
206 %! dispatch('hamming','length','string') |
|
207 %! assert(hamming(1),1) |
5582
|
208 %! assert(hamming("abc"),3) |
5164
|
209 %! dispatch('hamming','string') |
|
210 |
|
211 %!test # override preloaded builtin |
|
212 %! evalin('base','cos(1);'); |
|
213 %! dispatch('cos','length','string') |
5582
|
214 %! evalin('base','assert(cos("abc"),3)'); |
|
215 %! evalin('base','assert(cos(0),1,eps)'); |
5164
|
216 %! dispatch('cos','string') |
|
217 %!test # override pre-loaded oct-file |
|
218 %! evalin('base','qr(1);'); |
|
219 %! dispatch('qr','length','string') |
5582
|
220 %! evalin('base','assert(qr("abc"),3)'); |
|
221 %! evalin('base','assert(qr(1),1)'); |
5164
|
222 %! dispatch('qr','string'); |
|
223 %!test # override pre-loaded m-file |
|
224 %! evalin('base','hanning(1);'); |
|
225 %! dispatch('hanning','length','string') |
|
226 %! evalin('base','assert(hanning("abc"),3)'); |
|
227 %! evalin('base','assert(hanning(1),1)'); |
|
228 %! dispatch('hanning','string'); |
|
229 |
5775
|
230 FIXME I would rather not create dispatch_x/dispatch_y |
5164
|
231 in the current directory! I don't want them installed accidentally. |
|
232 |
|
233 %!test # replace base m-file |
|
234 %! system("echo 'function a=dispatch_x(a)'>dispatch_x.m"); |
|
235 %! dispatch('dispatch_x','length','string') |
|
236 %! assert(dispatch_x(3),3) |
5582
|
237 %! assert(dispatch_x("a"),1) |
5832
|
238 %! sleep (2); |
5164
|
239 %! system("echo 'function a=dispatch_x(a),++a;'>dispatch_x.m"); |
5832
|
240 %! rehash(); |
5164
|
241 %! assert(dispatch_x(3),4) |
5582
|
242 %! assert(dispatch_x("a"),1) |
5164
|
243 %!test |
5832
|
244 %! unlink("dispatch_x.m"); |
5164
|
245 |
|
246 %!test # replace dispatch m-file |
|
247 %! system("echo 'function a=dispatch_y(a)'>dispatch_y.m"); |
|
248 %! dispatch('hello','dispatch_y','complex scalar') |
|
249 %! assert(hello(3i),3i) |
5832
|
250 %! sleep (2); |
5164
|
251 %! system("echo 'function a=dispatch_y(a),++a;'>dispatch_y.m"); |
5832
|
252 %! rehash(); |
5164
|
253 %! assert(hello(3i),1+3i) |
|
254 %!test |
5832
|
255 %! unlink("dispatch_y.m"); |
5164
|
256 |
5775
|
257 FIXME add tests for preservation of mark_as_command status. |
5164
|
258 |
|
259 */ |