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 (octave_fcn_handle_h) |
|
24 #define octave_fcn_handle_h 1 |
|
25 |
|
26 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
|
27 #pragma interface |
|
28 #endif |
|
29 |
|
30 #include <iostream> |
|
31 #include <string> |
|
32 |
|
33 #include "oct-alloc.h" |
|
34 |
|
35 #include "ov-base.h" |
4654
|
36 #include "ov-base-mat.h" |
4343
|
37 #include "ov-fcn.h" |
4654
|
38 #include "ov-typeinfo.h" |
4343
|
39 #include "symtab.h" |
|
40 |
|
41 // Function handles. |
|
42 |
4654
|
43 class fcn_handle_elt |
4343
|
44 { |
|
45 public: |
|
46 |
4654
|
47 fcn_handle_elt (void) : fcn (0), nm ("@[]") { } |
4639
|
48 |
4654
|
49 fcn_handle_elt (octave_function *f, const std::string& n) |
4649
|
50 : fcn (f), nm (std::string ("@") + n) { } |
4343
|
51 |
4654
|
52 fcn_handle_elt (const fcn_handle_elt& fhe) |
|
53 : fcn (fhe.fcn), nm (fhe.nm) { } |
4343
|
54 |
4654
|
55 fcn_handle_elt& operator = (const fcn_handle_elt& fhe) |
4343
|
56 { |
4654
|
57 if (this != &fhe) |
4343
|
58 { |
4654
|
59 fcn = fhe.fcn; |
|
60 nm = fhe.nm; |
4343
|
61 } |
|
62 |
|
63 return *this; |
|
64 } |
|
65 |
4654
|
66 ~fcn_handle_elt (void) { } |
4343
|
67 |
4654
|
68 octave_function *function_value (void) { return fcn; } |
4343
|
69 |
|
70 std::string name (void) const { return nm; } |
|
71 |
|
72 private: |
|
73 |
|
74 // The function we are handling. |
|
75 octave_function *fcn; |
|
76 |
4649
|
77 // The name of the handle, including the "@". |
4343
|
78 std::string nm; |
4654
|
79 }; |
|
80 |
|
81 class fcn_handle_array : public ArrayN<fcn_handle_elt> |
|
82 { |
|
83 public: |
|
84 |
|
85 fcn_handle_array (void) : ArrayN<fcn_handle_elt> () { } |
|
86 |
|
87 fcn_handle_array (const dim_vector& dv, |
|
88 const fcn_handle_elt& val = resize_fill_value ()) |
|
89 : ArrayN<fcn_handle_elt> (dv, val) { } |
|
90 |
|
91 fcn_handle_array (octave_function *f, const std::string& nm) |
|
92 : ArrayN<fcn_handle_elt> (dim_vector (1, 1), fcn_handle_elt (f, nm)) { } |
|
93 |
|
94 fcn_handle_array (const ArrayN<fcn_handle_elt>& fa) |
|
95 : ArrayN<fcn_handle_elt> (fa) { } |
|
96 |
|
97 fcn_handle_array (const fcn_handle_array& fa) |
|
98 : ArrayN<fcn_handle_elt> (fa) { } |
|
99 |
|
100 ~fcn_handle_array (void) { } |
|
101 |
|
102 fcn_handle_array& operator = (const fcn_handle_array& fa) |
|
103 { |
|
104 if (this != &fa) |
|
105 ArrayN<fcn_handle_elt>::operator = (fa); |
|
106 |
|
107 return *this; |
|
108 } |
|
109 |
|
110 fcn_handle_array squeeze (void) const |
|
111 { return ArrayN<fcn_handle_elt>::squeeze (); } |
|
112 |
|
113 boolNDArray all (int dim = -1) const; |
|
114 boolNDArray any (int dim = -1) const; |
|
115 |
|
116 ArrayN<std::string> names (void) const; |
|
117 |
|
118 static int compute_index (Array<int>& ra_idx, |
|
119 const dim_vector& dimensions); |
|
120 |
|
121 static fcn_handle_elt resize_fill_value (void) |
|
122 { |
|
123 static fcn_handle_elt nil_handle = fcn_handle_elt (); |
|
124 return nil_handle; |
|
125 } |
|
126 }; |
|
127 |
|
128 class |
|
129 octave_fcn_handle : public octave_base_matrix<fcn_handle_array> |
|
130 { |
|
131 public: |
|
132 |
|
133 octave_fcn_handle (void) |
|
134 : octave_base_matrix<fcn_handle_array> () { } |
|
135 |
|
136 octave_fcn_handle (octave_function *f, const std::string& n) |
|
137 : octave_base_matrix<fcn_handle_array> |
|
138 (fcn_handle_array (dim_vector (1, 1), fcn_handle_elt (f, n))) { } |
|
139 |
|
140 octave_fcn_handle (const fcn_handle_array& fha) |
|
141 : octave_base_matrix<fcn_handle_array> (fha) { } |
|
142 |
|
143 octave_fcn_handle (const octave_fcn_handle& fh) |
|
144 : octave_base_matrix<fcn_handle_array> (fh) { } |
|
145 |
|
146 ~octave_fcn_handle (void) { } |
|
147 |
|
148 octave_value *clone (void) const { return new octave_fcn_handle (*this); } |
|
149 octave_value *empty_clone (void) const { return new octave_fcn_handle (); } |
|
150 |
|
151 bool is_defined (void) const { return true; } |
|
152 |
|
153 bool is_function_handle (void) const { return true; } |
|
154 |
|
155 octave_function *function_value (bool = false); |
|
156 |
|
157 std::string name (void) const; |
|
158 |
|
159 octave_fcn_handle *fcn_handle_value (bool = false) { return this; } |
|
160 |
|
161 fcn_handle_array fcn_handle_array_value (void) const { return matrix; } |
|
162 |
|
163 ArrayN<std::string> name_array (void) const { return matrix.names (); } |
|
164 |
|
165 bool print_as_scalar (void) const { return true; } |
|
166 |
|
167 void print (std::ostream& os, bool pr_as_read_syntax = false) const; |
|
168 |
|
169 void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const; |
|
170 |
|
171 private: |
4343
|
172 |
4612
|
173 DECLARE_OCTAVE_ALLOCATOR |
4343
|
174 |
4612
|
175 DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA |
4343
|
176 }; |
|
177 |
|
178 extern octave_value make_fcn_handle (const std::string& nm); |
|
179 |
|
180 #endif |
|
181 |
|
182 /* |
|
183 ;;; Local Variables: *** |
|
184 ;;; mode: C++ *** |
|
185 ;;; End: *** |
|
186 */ |