15396
|
1 /* |
|
2 |
|
3 Copyright (C) 1993-2012 John W. Eaton |
|
4 Copyright (C) 2009-2010 VZLU Prague |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (octave_action_container_h) |
|
25 #define octave_action_container_h 1 |
|
26 |
|
27 // This class allows registering actions in a list for later |
|
28 // execution, either explicitly or when the container goes out of |
|
29 // scope. |
|
30 |
|
31 // FIXME -- is there a better name for this class? |
|
32 |
|
33 class |
|
34 OCTINTERP_API |
|
35 action_container |
|
36 { |
|
37 public: |
|
38 |
|
39 // A generic unwind_protect element. Knows how to run itself and |
|
40 // discard itself. Also, contains a pointer to the next element. |
|
41 class elem |
|
42 { |
|
43 public: |
|
44 elem (void) { } |
|
45 |
|
46 virtual void run (void) { } |
|
47 |
|
48 virtual ~elem (void) { } |
|
49 |
|
50 friend class action_container; |
|
51 |
|
52 private: |
|
53 |
|
54 // No copying! |
|
55 |
|
56 elem (const elem&); |
|
57 |
|
58 elem& operator = (const elem&); |
|
59 }; |
|
60 |
|
61 // An element that merely runs a void (*)(void) function. |
|
62 |
|
63 class fcn_elem : public elem |
|
64 { |
|
65 public: |
|
66 fcn_elem (void (*fptr) (void)) |
|
67 : e_fptr (fptr) { } |
|
68 |
|
69 void run (void) { e_fptr (); } |
|
70 |
|
71 private: |
|
72 void (*e_fptr) (void); |
|
73 }; |
|
74 |
|
75 // An element that stores a variable of type T along with a void (*) (T) |
|
76 // function pointer, and calls the function with the parameter. |
|
77 |
|
78 template <class T> |
|
79 class fcn_arg_elem : public elem |
|
80 { |
|
81 public: |
|
82 fcn_arg_elem (void (*fcn) (T), T arg) |
|
83 : e_fcn (fcn), e_arg (arg) { } |
|
84 |
|
85 void run (void) { e_fcn (e_arg); } |
|
86 |
|
87 private: |
|
88 |
|
89 // No copying! |
|
90 |
|
91 fcn_arg_elem (const fcn_arg_elem&); |
|
92 |
|
93 fcn_arg_elem& operator = (const fcn_arg_elem&); |
|
94 |
|
95 void (*e_fcn) (T); |
|
96 T e_arg; |
|
97 }; |
|
98 |
|
99 // An element that stores a variable of type T along with a |
|
100 // void (*) (const T&) function pointer, and calls the function with |
|
101 // the parameter. |
|
102 |
|
103 template <class T> |
|
104 class fcn_crefarg_elem : public elem |
|
105 { |
|
106 public: |
|
107 fcn_crefarg_elem (void (*fcn) (const T&), const T& arg) |
|
108 : e_fcn (fcn), e_arg (arg) { } |
|
109 |
|
110 void run (void) { e_fcn (e_arg); } |
|
111 |
|
112 private: |
|
113 void (*e_fcn) (const T&); |
|
114 T e_arg; |
|
115 }; |
|
116 |
|
117 // An element for calling a member function. |
|
118 |
|
119 template <class T> |
|
120 class method_elem : public elem |
|
121 { |
|
122 public: |
|
123 method_elem (T *obj, void (T::*method) (void)) |
|
124 : e_obj (obj), e_method (method) { } |
|
125 |
|
126 void run (void) { (e_obj->*e_method) (); } |
|
127 |
|
128 private: |
|
129 |
|
130 T *e_obj; |
|
131 void (T::*e_method) (void); |
|
132 |
|
133 // No copying! |
|
134 |
|
135 method_elem (const method_elem&); |
|
136 |
|
137 method_elem operator = (const method_elem&); |
|
138 }; |
|
139 |
|
140 // An element for calling a member function with a single argument |
|
141 |
|
142 template <class T, class A> |
|
143 class method_arg_elem : public elem |
|
144 { |
|
145 public: |
|
146 method_arg_elem (T *obj, void (T::*method) (A), A arg) |
|
147 : e_obj (obj), e_method (method), e_arg (arg) { } |
|
148 |
|
149 void run (void) { (e_obj->*e_method) (e_arg); } |
|
150 |
|
151 private: |
|
152 |
|
153 T *e_obj; |
|
154 void (T::*e_method) (A); |
|
155 A e_arg; |
|
156 |
|
157 // No copying! |
|
158 |
|
159 method_arg_elem (const method_arg_elem&); |
|
160 |
|
161 method_arg_elem operator = (const method_arg_elem&); |
|
162 }; |
|
163 |
|
164 // An element for calling a member function with a single argument |
|
165 |
|
166 template <class T, class A> |
|
167 class method_crefarg_elem : public elem |
|
168 { |
|
169 public: |
|
170 method_crefarg_elem (T *obj, void (T::*method) (const A&), const A& arg) |
|
171 : e_obj (obj), e_method (method), e_arg (arg) { } |
|
172 |
|
173 void run (void) { (e_obj->*e_method) (e_arg); } |
|
174 |
|
175 private: |
|
176 |
|
177 T *e_obj; |
|
178 void (T::*e_method) (const A&); |
|
179 A e_arg; |
|
180 |
|
181 // No copying! |
|
182 |
|
183 method_crefarg_elem (const method_crefarg_elem&); |
|
184 |
|
185 method_crefarg_elem operator = (const method_crefarg_elem&); |
|
186 }; |
|
187 |
|
188 // An element that stores arbitrary variable, and restores it. |
|
189 |
|
190 template <class T> |
|
191 class restore_var_elem : public elem |
|
192 { |
|
193 public: |
|
194 restore_var_elem (T& ref, const T& val) |
|
195 : e_ptr (&ref), e_val (val) { } |
|
196 |
|
197 void run (void) { *e_ptr = e_val; } |
|
198 |
|
199 private: |
|
200 |
|
201 // No copying! |
|
202 |
|
203 restore_var_elem (const restore_var_elem&); |
|
204 |
|
205 restore_var_elem& operator = (const restore_var_elem&); |
|
206 |
|
207 T *e_ptr, e_val; |
|
208 }; |
|
209 |
|
210 // Deletes a class allocated using new. |
|
211 |
|
212 template <class T> |
|
213 class delete_ptr_elem : public elem |
|
214 { |
|
215 public: |
|
216 delete_ptr_elem (T *ptr) |
|
217 : e_ptr (ptr) { } |
|
218 |
|
219 void run (void) { delete e_ptr; } |
|
220 |
|
221 private: |
|
222 |
|
223 T *e_ptr; |
|
224 |
|
225 // No copying! |
|
226 |
|
227 delete_ptr_elem (const delete_ptr_elem&); |
|
228 |
|
229 delete_ptr_elem operator = (const delete_ptr_elem&); |
|
230 }; |
|
231 |
|
232 action_container (void) { } |
|
233 |
|
234 virtual ~action_container (void) { } |
|
235 |
|
236 virtual void add (elem *new_elem) = 0; |
|
237 |
|
238 // Call to void func (void). |
|
239 void add_fcn (void (*fcn) (void)) |
|
240 { |
|
241 add (new fcn_elem (fcn)); |
|
242 } |
|
243 |
|
244 // Call to void func (T). |
|
245 template <class T> |
|
246 void add_fcn (void (*action) (T), T val) |
|
247 { |
|
248 add (new fcn_arg_elem<T> (action, val)); |
|
249 } |
|
250 |
|
251 // Call to void func (const T&). |
|
252 template <class T> |
|
253 void add_fcn (void (*action) (const T&), const T& val) |
|
254 { |
|
255 add (new fcn_crefarg_elem<T> (action, val)); |
|
256 } |
|
257 |
|
258 // Call to T::method (void). |
|
259 template <class T> |
|
260 void add_method (T *obj, void (T::*method) (void)) |
|
261 { |
|
262 add (new method_elem<T> (obj, method)); |
|
263 } |
|
264 |
|
265 // Call to T::method (A). |
|
266 template <class T, class A> |
|
267 void add_method (T *obj, void (T::*method) (A), A arg) |
|
268 { |
|
269 add (new method_arg_elem<T, A> (obj, method, arg)); |
|
270 } |
|
271 |
|
272 // Call to T::method (const A&). |
|
273 template <class T, class A> |
|
274 void add_method (T *obj, void (T::*method) (const A&), const A& arg) |
|
275 { |
|
276 add (new method_crefarg_elem<T, A> (obj, method, arg)); |
|
277 } |
|
278 |
|
279 // Call to delete (T*). |
|
280 |
|
281 template <class T> |
|
282 void add_delete (T *obj) |
|
283 { |
|
284 add (new delete_ptr_elem<T> (obj)); |
|
285 } |
|
286 |
|
287 // Protect any variable. |
|
288 template <class T> |
|
289 void protect_var (T& var) |
|
290 { |
|
291 add (new restore_var_elem<T> (var, var)); |
|
292 } |
|
293 |
|
294 // Protect any variable, value given. |
|
295 template <class T> |
|
296 void protect_var (T& var, const T& val) |
|
297 { |
|
298 add (new restore_var_elem<T> (var, val)); |
|
299 } |
|
300 |
|
301 operator bool (void) const { return ! empty (); } |
|
302 |
|
303 virtual void run_first (void) = 0; |
|
304 |
|
305 void run (size_t num) |
|
306 { |
|
307 if (num > size ()) |
|
308 num = size (); |
|
309 |
|
310 for (size_t i = 0; i < num; i++) |
|
311 run_first (); |
|
312 } |
|
313 |
|
314 void run (void) { run (size ()); } |
|
315 |
|
316 virtual void discard_first (void) = 0; |
|
317 |
|
318 void discard (size_t num) |
|
319 { |
|
320 if (num > size ()) |
|
321 num = size (); |
|
322 |
|
323 for (size_t i = 0; i < num; i++) |
|
324 discard_first (); |
|
325 } |
|
326 |
|
327 void discard (void) { discard (size ()); } |
|
328 |
|
329 virtual size_t size (void) const = 0; |
|
330 |
|
331 bool empty (void) const { return size () == 0; } |
|
332 |
|
333 private: |
|
334 |
|
335 // No copying! |
|
336 |
|
337 action_container (const action_container&); |
|
338 |
|
339 action_container& operator = (const action_container&); |
|
340 }; |
|
341 |
|
342 #endif |