]> Creatis software - CreaPhase.git/blob - octave_packages/m/deprecated/dispatch.m
update packages
[CreaPhase.git] / octave_packages / m / deprecated / dispatch.m
1 ## Copyright (C) 2010-2012 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 %! dispatch('sin','length','string')
54 %! assert(sin("abc"),3)
55 %! assert(sin(0),0,10*eps);
56
57 %!test # 'any' function
58 %! dispatch('sin','exp','any')
59 %! assert(sin(0),1,eps);
60 %! assert(sin("abc"),3);
61
62 %!test # 'builtin' function
63 %! assert(builtin('sin',0),0,eps);
64 %! builtin('eval','x=1;');
65 %! assert(x,1);
66
67 %!test # clear function mapping
68 %! dispatch('sin','string')
69 %! dispatch('sin','any')
70 %! assert(sin(0),0,10*eps);
71
72 %!test # oct-file replacement
73 %! dispatch('fft','length','string')
74 %! assert(fft([1,1]),[2,0]);
75 %! assert(fft("abc"),3)
76 %! dispatch('fft','string');
77
78 %!test # m-file replacement
79 %! dispatch('hamming','length','string')
80 %! assert(hamming(1),1)
81 %! assert(hamming("abc"),3)
82 %! dispatch('hamming','string')
83
84 %!test # override preloaded builtin
85 %! evalin('base','cos(1);');
86 %! dispatch('cos','length','string')
87 %! evalin('base','assert(cos("abc"),3)');
88 %! evalin('base','assert(cos(0),1,eps)');
89 %! dispatch('cos','string')
90
91 %!test # override pre-loaded oct-file
92 %! evalin('base','qr(1);');
93 %! dispatch('qr','length','string')
94 %! evalin('base','assert(qr("abc"),3)');
95 %! evalin('base','assert(qr(1),1)');
96 %! dispatch('qr','string');
97
98 %!test # override pre-loaded m-file
99 %! evalin('base','hanning(1);');
100 %! dispatch('hanning','length','string')
101 %! evalin('base','assert(hanning("abc"),3)');
102 %! evalin('base','assert(hanning(1),1)');
103 %! dispatch('hanning','string');