annotate src/DLD-FUNCTIONS/dispatch.cc @ 9444:0c785ad961fa

improve behavior of builtin
author John W. Eaton <jwe@octave.org>
date Tue, 21 Jul 2009 12:43:33 -0400
parents 8f9d8776d11c
children 40dfc0c99116
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
1 /*
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
2
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8703
diff changeset
3 Copyright (C) 2001, 2005, 2006, 2007, 2008, 2009
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8703
diff changeset
4 John W. Eaton and Paul Kienzle
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
5
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6348
diff changeset
6 This file is part of Octave.
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6348
diff changeset
7
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6348
diff changeset
8 Octave is free software; you can redistribute it and/or modify it
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
9 under the terms of the GNU General Public License as published by the
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6348
diff changeset
10 Free Software Foundation; either version 3 of the License, or (at your
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6348
diff changeset
11 option) any later version.
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
12
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6348
diff changeset
13 Octave is distributed in the hope that it will be useful, but WITHOUT
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
16 for more details.
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
17
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6348
diff changeset
19 along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6348
diff changeset
20 <http://www.gnu.org/licenses/>.
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
21
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
22 */
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
23
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
25 #include <config.h>
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
26 #endif
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
27
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
28 #include <list>
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
29 #include <map>
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
30 #include <string>
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
31
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
32 #include "Cell.h"
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
33 #include "oct-map.h"
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
34 #include "defun-dld.h"
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
35 #include "ov.h"
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
36 #include "ov-fcn.h"
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
37 #include "ov-typeinfo.h"
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
38 #include "pager.h"
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
39 #include "parse.h"
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
40 #include "symtab.h"
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
41 #include "variables.h"
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
42
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
43 DEFUN_DLD (builtin, args, nargout,
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
44 "-*- texinfo -*-\n\
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
45 @deftypefn {Loadable Function} {[@dots{}]} builtin (@var{f}, @dots{})\n\
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
46 Call the base function @var{f} even if @var{f} is overloaded to\n\
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
47 some other function for the given type signature.\n\
5646
2b20d1707f29 [project @ 2006-03-06 22:38:32 by jwe]
jwe
parents: 5582
diff changeset
48 @seealso{dispatch}\n\
2b20d1707f29 [project @ 2006-03-06 22:38:32 by jwe]
jwe
parents: 5582
diff changeset
49 @end deftypefn")
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
50 {
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
51 octave_value_list retval;
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
52
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
53 int nargin = args.length ();
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
54
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
55 if (nargin > 0)
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
56 {
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
57 const std::string name (args(0).string_value ());
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
58
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
59 if (! error_state)
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
60 {
9444
0c785ad961fa improve behavior of builtin
John W. Eaton <jwe@octave.org>
parents: 9443
diff changeset
61 octave_value fcn = symbol_table::builtin_find (name);
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
62
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
63 if (fcn.is_defined ())
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
64 retval = feval (fcn.function_value (), args.splice (0, 1),
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
65 nargout);
5200
240ed0328925 [project @ 2005-03-14 15:53:44 by jwe]
jwe
parents: 5164
diff changeset
66 else
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
67 error ("builtin: lookup for symbol `%s' failed", name.c_str ());
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
68 }
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
69 else
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
70 error ("builtin: expecting function name as first argument");
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
71 }
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
72 else
5823
080c08b192d8 [project @ 2006-05-19 05:32:17 by jwe]
jwe
parents: 5775
diff changeset
73 print_usage ();
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
74
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
75 return retval;
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
76 }
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
77
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
78 DEFUN_DLD (dispatch, args, nargout,
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
79 "-*- texinfo -*-\n\
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
80 @deftypefn {Loadable Function} {} dispatch (@var{f}, @var{r}, @var{type})\n\
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
81 \n\
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
82 Replace the function @var{f} with a dispatch so that function @var{r}\n\
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
83 is called when @var{f} is called with the first argument of the named\n\
9038
fca0dc2fb042 Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
84 @var{type}. If the type is @var{any} then call @var{r} if no other type\n\
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
85 matches. The original function @var{f} is accessible using\n\
6248
7fad1fad19e1 [project @ 2007-01-22 17:28:17 by jwe]
jwe
parents: 5889
diff changeset
86 @code{builtin (@var{f}, @dots{})}.\n\
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
87 \n\
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
88 If @var{r} is omitted, clear dispatch function associated with @var{type}.\n\
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
89 \n\
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
90 If both @var{r} and @var{type} are omitted, list dispatch functions\n\
5646
2b20d1707f29 [project @ 2006-03-06 22:38:32 by jwe]
jwe
parents: 5582
diff changeset
91 for @var{f}.\n\
2b20d1707f29 [project @ 2006-03-06 22:38:32 by jwe]
jwe
parents: 5582
diff changeset
92 @seealso{builtin}\n\
2b20d1707f29 [project @ 2006-03-06 22:38:32 by jwe]
jwe
parents: 5582
diff changeset
93 @end deftypefn")
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
94 {
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
95 octave_value retval;
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
96
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
97 int nargin = args.length ();
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
98
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
99 std::string f, r, t;
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
100
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
101 if (nargin > 0 && nargin < 4)
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
102 {
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
103 if (nargin > 0)
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
104 {
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
105 f = args(0).string_value ();
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
106
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
107 if (error_state)
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
108 {
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
109 error ("dispatch: expecting first argument to be function name");
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
110 return retval;
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
111 }
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
112 }
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
113
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
114 if (nargin > 1)
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
115 {
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
116 r = args(1).string_value ();
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
117
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
118 if (error_state)
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
119 {
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
120 error ("dispatch: expecting second argument to be function name");
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
121 return retval;
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
122 }
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
123 }
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
124
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
125 if (nargin > 2)
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
126 {
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
127 t = args(2).string_value ();
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
128
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
129 if (error_state)
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
130 {
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
131 error ("dispatch: expecting third argument to be type name");
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
132 return retval;
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
133 }
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
134 }
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
135
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
136 if (nargin == 1)
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
137 {
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
138 if (nargout > 0)
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
139 {
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
140 symbol_table::fcn_info::dispatch_map_type dm
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
141 = symbol_table::get_dispatch (f);
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
142
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
143 size_t len = dm.size ();
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
144
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
145 Cell type_field (len, 1);
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
146 Cell name_field (len, 1);
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
147
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
148 symbol_table::fcn_info::dispatch_map_type::const_iterator p
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
149 = dm.begin ();
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
150
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
151 for (size_t i = 0; i < len; i++)
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
152 {
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
153 type_field(i) = p->first;
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
154 name_field(i) = p->second;
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
155
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
156 p++;
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
157 }
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
158
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
159 Octave_map m;
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
160
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
161 m.assign ("type", type_field);
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
162 m.assign ("name", name_field);
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
163
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
164 retval = m;
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
165 }
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
166 else
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
167 symbol_table::print_dispatch (octave_stdout, f);
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
168 }
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
169 else if (nargin == 2)
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
170 {
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
171 t = r;
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
172 symbol_table::clear_dispatch (f, t);
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
173 }
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
174 else
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
175 symbol_table::add_dispatch (f, t, r);
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
176 }
7336
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
177 else
745a8299c2b5 [project @ 2007-12-28 20:56:55 by jwe]
jwe
parents: 7017
diff changeset
178 print_usage ();
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
179
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
180 return retval;
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
181 }
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
182
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
183 /*
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
184
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
185 %!test # builtin function replacement
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
186 %! dispatch('sin','length','string')
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents: 5399
diff changeset
187 %! assert(sin("abc"),3)
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
188 %! assert(sin(0),0,10*eps);
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
189 %!test # 'any' function
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
190 %! dispatch('sin','exp','any')
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
191 %! assert(sin(0),1,eps);
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents: 5399
diff changeset
192 %! assert(sin("abc"),3);
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
193 %!test # 'builtin' function
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
194 %! assert(builtin('sin',0),0,eps);
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
195 %! builtin('eval','x=1;');
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
196 %! assert(x,1);
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
197 %!test # clear function mapping
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
198 %! dispatch('sin','string')
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
199 %! dispatch('sin','any')
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
200 %! assert(sin(0),0,10*eps);
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
201 %!test # oct-file replacement
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
202 %! dispatch('fft','length','string')
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
203 %! assert(fft([1,1]),[2,0]);
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents: 5399
diff changeset
204 %! assert(fft("abc"),3)
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
205 %! dispatch('fft','string');
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
206 %!test # m-file replacement
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
207 %! dispatch('hamming','length','string')
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
208 %! assert(hamming(1),1)
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents: 5399
diff changeset
209 %! assert(hamming("abc"),3)
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
210 %! dispatch('hamming','string')
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
211
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
212 %!test # override preloaded builtin
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
213 %! evalin('base','cos(1);');
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
214 %! dispatch('cos','length','string')
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents: 5399
diff changeset
215 %! evalin('base','assert(cos("abc"),3)');
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents: 5399
diff changeset
216 %! evalin('base','assert(cos(0),1,eps)');
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
217 %! dispatch('cos','string')
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
218 %!test # override pre-loaded oct-file
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
219 %! evalin('base','qr(1);');
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
220 %! dispatch('qr','length','string')
5582
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents: 5399
diff changeset
221 %! evalin('base','assert(qr("abc"),3)');
6bf56668b01a [project @ 2005-12-15 01:08:20 by jwe]
jwe
parents: 5399
diff changeset
222 %! evalin('base','assert(qr(1),1)');
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
223 %! dispatch('qr','string');
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
224 %!test # override pre-loaded m-file
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
225 %! evalin('base','hanning(1);');
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
226 %! dispatch('hanning','length','string')
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
227 %! evalin('base','assert(hanning("abc"),3)');
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
228 %! evalin('base','assert(hanning(1),1)');
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
229 %! dispatch('hanning','string');
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
230
8703
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
231 ## The following tests have been disabled because creating functions
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
232 ## on the fly causes trouble (filesystem timestamp resolution?) and so
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
233 ## leads people to complain about the failed tests when the dispatch
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
234 ## mechanism is working fine, but it is really the creation of the
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
235 ## functions that is failing. And anyway, this method of function
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
236 ## dispatch should be considered obsolete and probably removed from
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
237 ## Octave now that we have classes.
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
238 ##
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
239 ## FIXME I would rather not create dispatch_x/dispatch_y
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
240 ## in the current directory! I don't want them installed accidentally.
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
241 ##
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
242 ## %!function echo_to_file (str, name)
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
243 ## %! fid = fopen (name, 'w');
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
244 ## %! if (fid != -1)
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
245 ## %! fprintf (fid, str);
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
246 ## %! fprintf (fid, '\n');
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
247 ## %! fclose (fid);
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
248 ## %! endif
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
249 ##
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
250 ## %!test # replace base m-file
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
251 ## %! echo_to_file ('function a=dispatch_x(a)', "dispatch_x.m");
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
252 ## %! dispatch('dispatch_x','length','string')
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
253 ## %! assert(dispatch_x(3),3)
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
254 ## %! assert(dispatch_x("a"),1)
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
255 ## %! sleep (2);
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
256 ## %! echo_to_file ('function a=dispatch_x(a),++a;', "dispatch_x.m");
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
257 ## %! rehash();
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
258 ## %! assert(dispatch_x(3),4)
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
259 ## %! assert(dispatch_x("a"),1)
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
260 ## %!test
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
261 ## %! unlink("dispatch_x.m");
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
262 ##
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
263 ## %!test # replace dispatch m-file
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
264 ## %! echo_to_file ('function a=dispatch_y(a)', "dispatch_y.m");
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
265 ## %! dispatch('hello','dispatch_y','complex scalar')
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
266 ## %! assert(hello(3i),3i)
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
267 ## %! sleep (2);
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
268 ## %! echo_to_file ('function a=dispatch_y(a),++a;', "dispatch_y.m");
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
269 ## %! rehash();
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
270 ## %! assert(hello(3i),1+3i)
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
271 ## %!test
c953a6977be6 dispatch.cc: comment out troublesome tests
John W. Eaton <jwe@octave.org>
parents: 7706
diff changeset
272 ## %! unlink("dispatch_y.m");
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
273
5775
ace8d8d26933 [project @ 2006-04-24 19:13:06 by jwe]
jwe
parents: 5760
diff changeset
274 FIXME add tests for preservation of mark_as_command status.
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
275
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
276 */