11226
|
1 ## Copyright (C) 2010 John W. Eaton |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Loadable Function} {} dispatch (@var{f}, @var{r}, @var{type}) |
|
21 ## |
|
22 ## Replace the function @var{f} with a dispatch so that function @var{r} |
|
23 ## is called when @var{f} is called with the first argument of the named |
|
24 ## @var{type}. If the type is @var{any} then call @var{r} if no other type |
|
25 ## matches. The original function @var{f} is accessible using |
|
26 ## @code{builtin (@var{f}, @dots{})}. |
|
27 ## |
|
28 ## If @var{r} is omitted, clear dispatch function associated with @var{type}. |
|
29 ## |
|
30 ## If both @var{r} and @var{type} are omitted, list dispatch functions |
|
31 ## for @var{f}. |
|
32 ## @seealso{builtin} |
|
33 ## @end deftypefn |
|
34 |
|
35 ## Deprecated in version 3.4 |
|
36 |
|
37 function varargout = dispatch (varargin) |
|
38 |
|
39 persistent warned = false; |
|
40 if (! warned) |
|
41 warned = true; |
|
42 warning ("Octave:deprecated-function", |
|
43 "dispatch is obsolete and will be removed from a future version of Octave; please use classes instead"); |
|
44 endif |
|
45 |
|
46 varargout = cell (nargout, 1); |
|
47 [ varargout{:} ] = __dispatch__ (varargin{:}); |
|
48 |
|
49 endfunction |
|
50 |
|
51 |
|
52 %!test # builtin function replacement |
|
53 %! warning off Octave:deprecated-function |
|
54 %! dispatch('sin','length','string') |
|
55 %! assert(sin("abc"),3) |
|
56 %! assert(sin(0),0,10*eps); |
|
57 %!test # 'any' function |
|
58 %! warning off Octave:deprecated-function |
|
59 %! dispatch('sin','exp','any') |
|
60 %! assert(sin(0),1,eps); |
|
61 %! assert(sin("abc"),3); |
|
62 %!test # 'builtin' function |
|
63 %! warning off Octave:deprecated-function |
|
64 %! assert(builtin('sin',0),0,eps); |
|
65 %! builtin('eval','x=1;'); |
|
66 %! assert(x,1); |
|
67 %!test # clear function mapping |
|
68 %! warning off Octave:deprecated-function |
|
69 %! dispatch('sin','string') |
|
70 %! dispatch('sin','any') |
|
71 %! assert(sin(0),0,10*eps); |
|
72 %!test # oct-file replacement |
|
73 %! warning off Octave:deprecated-function |
|
74 %! dispatch('fft','length','string') |
|
75 %! assert(fft([1,1]),[2,0]); |
|
76 %! assert(fft("abc"),3) |
|
77 %! dispatch('fft','string'); |
|
78 %!test # m-file replacement |
|
79 %! warning off Octave:deprecated-function |
|
80 %! dispatch('hamming','length','string') |
|
81 %! assert(hamming(1),1) |
|
82 %! assert(hamming("abc"),3) |
|
83 %! dispatch('hamming','string') |
|
84 |
|
85 %!test # override preloaded builtin |
|
86 %! warning off Octave:deprecated-function |
|
87 %! evalin('base','cos(1);'); |
|
88 %! dispatch('cos','length','string') |
|
89 %! evalin('base','assert(cos("abc"),3)'); |
|
90 %! evalin('base','assert(cos(0),1,eps)'); |
|
91 %! dispatch('cos','string') |
|
92 %!test # override pre-loaded oct-file |
|
93 %! warning off Octave:deprecated-function |
|
94 %! evalin('base','qr(1);'); |
|
95 %! dispatch('qr','length','string') |
|
96 %! evalin('base','assert(qr("abc"),3)'); |
|
97 %! evalin('base','assert(qr(1),1)'); |
|
98 %! dispatch('qr','string'); |
|
99 %!test # override pre-loaded m-file |
|
100 %! warning off Octave:deprecated-function |
|
101 %! evalin('base','hanning(1);'); |
|
102 %! dispatch('hanning','length','string') |
|
103 %! evalin('base','assert(hanning("abc"),3)'); |
|
104 %! evalin('base','assert(hanning(1),1)'); |
|
105 %! dispatch('hanning','string'); |