Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXCallback.h
1 /********************************************************************************
2 * *
3 * C a l l B a c k C l a s s *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2016,2017 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXCALLBACK_H
22 #define FXCALLBACK_H
23 
24 namespace FX {
25 
26 /********************************************************************************/
27 
28 // Bland template declaration
29 template<typename TYPE>
30 class FXCallback;
31 
32 /********************************************************************************/
33 
34 // Specialization of callback mechanism for callback with signature:
35 //
36 // RT FUNC()
37 //
38 // Both free functions and member functions may be called.
39 template<typename RT>
40 class FXAPI FXCallback<RT ()> {
41 private:
42  typedef RT (*METHOD)(const void*);
43 private:
44  METHOD method;
45  const void* object;
46 private:
47 
48  // Stub call to method
49  template <typename T,RT (T::*mfn)()>
50  static RT method_call(const void* obj){ return (static_cast<T*>(const_cast<void*>(obj))->*mfn)(); }
51 
52  // Stub call to const method
53  template <typename T,RT (T::*mfn)() const>
54  static RT const_method_call(const void* obj){ return (static_cast<const T*>(obj)->*mfn)(); }
55 
56  // Stub call to function
57  template<RT (*fn)()>
58  static RT function_call(const void*){ return (fn)(); }
59 
60  // Fallback function
61  static RT default_call(const void*){ return RT(); }
62 
63 public:
64 
65  // Initialize to default
66  FXCallback():method(&default_call),object(0){ }
67 
68  // Copy constructor
69  FXCallback(const FXCallback& org):method(org.method),object(org.object){ }
70 
71  // Assignment operator
72  FXCallback& operator=(const FXCallback& org){ method=org.method; object=org.object; return *this; }
73 
74  // Invoke the function
75  RT operator()() const { return (*method)(object); }
76 
77  // Connect to member function of object
78  template<typename TYPE,RT (TYPE::* mfn)()>
79  inline void connect(TYPE* obj){ method=&method_call<TYPE,mfn>; object=obj; }
80 
81  // Connect to member function of object
82  template<typename TYPE,RT (TYPE::* mfn)() const>
83  inline void connect(const TYPE* obj){ method=&const_method_call<TYPE,mfn>; object=obj; }
84 
85  // Connect to plain function
86  template<RT (*fn)()>
87  inline void connect(){ method=&function_call<fn>; object=NULL; }
88 
89  // Return trye if connected
90  bool connected() const { return (method!=&default_call); }
91 
92  // Disconnect resets to default
93  void disconnect(){ method=&default_call; object=NULL; }
94  };
95 
96 /********************************************************************************/
97 
98 // Specialization of callback mechanism for callback with signature:
99 //
100 // RT FUNC(PT1)
101 //
102 // Both free functions and member functions may be called.
103 template<typename RT,typename PT1>
104 class FXAPI FXCallback<RT (PT1)> {
105 private:
106  typedef RT (*METHOD)(const void*,PT1);
107 private:
108  METHOD method;
109  const void* object;
110 private:
111 
112  // Stub call to method
113  template <typename T,RT (T::*mfn)(PT1)>
114  static RT method_call(const void* obj,PT1 p1){ return (static_cast<T*>(const_cast<void*>(obj))->*mfn)(p1); }
115 
116  // Stub call to const method
117  template <typename T,RT (T::*mfn)(PT1) const>
118  static RT const_method_call(const void* obj,PT1 p1){ return (static_cast<const T*>(obj)->*mfn)(p1); }
119 
120  // Stub call to function
121  template<RT (*fn)(PT1)>
122  static RT function_call(const void*,PT1 p1){ return (fn)(p1); }
123 
124  // Fallback function
125  static RT default_call(const void*,PT1){ return RT(); }
126 
127 public:
128 
129  // Initialize to default
130  FXCallback():method(&default_call),object(0){ }
131 
132  // Copy constructor
133  FXCallback(const FXCallback& org):method(org.method),object(org.object){ }
134 
135  // Assignment operator
136  FXCallback& operator=(const FXCallback& org){ method=org.method; object=org.object; return *this; }
137 
138  // Invoke the function
139  RT operator()(PT1 p1) const { return (*method)(object,p1); }
140 
141  // Connect to member function of object
142  template<typename TYPE,RT (TYPE::* mfn)(PT1)>
143  inline void connect(TYPE* obj){ method=&method_call<TYPE,mfn>; object=obj; }
144 
145  // Connect to member function of object
146  template<typename TYPE,RT (TYPE::* mfn)(PT1) const>
147  inline void connect(const TYPE* obj){ method=&const_method_call<TYPE,mfn>; object=obj; }
148 
149  // Connect to plain function
150  template<RT (*fn)(PT1)>
151  inline void connect(){ method=&function_call<fn>; object=NULL; }
152 
153  // Return trye if connected
154  bool connected() const { return (method!=&default_call); }
155 
156  // Disconnect resets to default
157  void disconnect(){ method=&default_call; object=NULL; }
158  };
159 
160 /********************************************************************************/
161 
162 // Specialization of callback mechanism for callback with signature:
163 //
164 // RT FUNC(PT1,PT2)
165 //
166 // Both free functions and member functions may be called.
167 template<typename RT,typename PT1,typename PT2>
168 class FXAPI FXCallback<RT (PT1,PT2)> {
169 private:
170  typedef RT (*METHOD)(const void*,PT1,PT2);
171 private:
172  METHOD method;
173  const void* object;
174 private:
175 
176  // Stub call to method
177  template <typename T,RT (T::*mfn)(PT1,PT2)>
178  static RT method_call(const void* obj,PT1 p1,PT2 p2){ return (static_cast<T*>(const_cast<void*>(obj))->*mfn)(p1,p2); }
179 
180  // Stub call to const method
181  template <typename T,RT (T::*mfn)(PT1,PT2) const>
182  static RT const_method_call(const void* obj,PT1 p1,PT2 p2){ return (static_cast<const T*>(obj)->*mfn)(p1,p2); }
183 
184  // Stub call to function
185  template<RT (*fn)(PT1,PT2)>
186  static RT function_call(const void*,PT1 p1,PT2 p2){ return (fn)(p1,p2); }
187 
188  // Fallback function
189  static RT default_call(const void*,PT1,PT2){ return RT(); }
190 
191 public:
192 
193  // Initialize to default
194  FXCallback():method(&default_call),object(0){ }
195 
196  // Copy constructor
197  FXCallback(const FXCallback& org):method(org.method),object(org.object){ }
198 
199  // Assignment operator
200  FXCallback& operator=(const FXCallback& org){ method=org.method; object=org.object; return *this; }
201 
202  // Invoke the function
203  RT operator()(PT1 p1,PT2 p2) const { return (*method)(object,p1,p2); }
204 
205  // Connect to member function of object
206  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2)>
207  inline void connect(TYPE* obj){ method=&method_call<TYPE,mfn>; object=obj; }
208 
209  // Connect to member function of object
210  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2) const>
211  inline void connect(const TYPE* obj){ method=&const_method_call<TYPE,mfn>; object=obj; }
212 
213  // Connect to plain function
214  template<RT (*fn)(PT1,PT2)>
215  inline void connect(){ method=&function_call<fn>; object=NULL; }
216 
217  // Return trye if connected
218  bool connected() const { return (method!=&default_call); }
219 
220  // Disconnect resets to default
221  void disconnect(){ method=&default_call; object=NULL; }
222  };
223 
224 /********************************************************************************/
225 
226 // Specialization of callback mechanism for callback with signature:
227 //
228 // RT FUNC(PT1,PT2,PT3)
229 //
230 // Both free functions and member functions may be called.
231 template<typename RT,typename PT1,typename PT2,typename PT3>
232 class FXAPI FXCallback<RT (PT1,PT2,PT3)> {
233 private:
234  typedef RT (*METHOD)(const void*,PT1,PT2,PT3);
235 private:
236  METHOD method;
237  const void* object;
238 private:
239 
240  // Stub call to method
241  template <typename T,RT (T::*mfn)(PT1,PT2,PT3)>
242  static RT method_call(const void* obj,PT1 p1,PT2 p2,PT3 p3){ return (static_cast<T*>(const_cast<void*>(obj))->*mfn)(p1,p2,p3); }
243 
244  // Stub call to const method
245  template <typename T,RT (T::*mfn)(PT1,PT2,PT3) const>
246  static RT const_method_call(const void* obj,PT1 p1,PT2 p2,PT3 p3){ return (static_cast<const T*>(obj)->*mfn)(p1,p2,p3); }
247 
248  // Stub call to function
249  template<RT (*fn)(PT1,PT2,PT3)>
250  static RT function_call(const void*,PT1 p1,PT2 p2,PT3 p3){ return (fn)(p1,p2,p3); }
251 
252  // Fallback function
253  static RT default_call(const void*,PT1,PT2,PT3){ return RT(); }
254 
255 public:
256 
257  // Initialize to default
258  FXCallback():method(&default_call),object(0){ }
259 
260  // Copy constructor
261  FXCallback(const FXCallback& org):method(org.method),object(org.object){ }
262 
263  // Assignment operator
264  FXCallback& operator=(const FXCallback& org){ method=org.method; object=org.object; return *this; }
265 
266  // Invoke the function
267  RT operator()(PT1 p1,PT2 p2,PT3 p3) const { return (*method)(object,p1,p2,p3); }
268 
269  // Connect to member function of object
270  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2,PT3)>
271  inline void connect(TYPE* obj){ method=&method_call<TYPE,mfn>; object=obj; }
272 
273  // Connect to member function of object
274  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2,PT3) const>
275  inline void connect(const TYPE* obj){ method=&const_method_call<TYPE,mfn>; object=obj; }
276 
277  // Connect to plain function
278  template<RT (*fn)(PT1,PT2,PT3)>
279  inline void connect(){ method=&function_call<fn>; object=NULL; }
280 
281  // Return trye if connected
282  bool connected() const { return (method!=&default_call); }
283 
284  // Disconnect resets to default
285  void disconnect(){ method=&default_call; object=NULL; }
286  };
287 
288 /********************************************************************************/
289 
290 // Specialization of callback mechanism for callback with signature:
291 //
292 // RT FUNC(PT1,PT2,PT3,PT4)
293 //
294 // Both free functions and member functions may be called.
295 template<typename RT,typename PT1,typename PT2,typename PT3,typename PT4>
296 class FXAPI FXCallback<RT (PT1,PT2,PT3,PT4)> {
297 private:
298  typedef RT (*METHOD)(const void*,PT1,PT2,PT3,PT4);
299 private:
300  METHOD method;
301  const void* object;
302 private:
303 
304  // Stub call to method
305  template <typename T,RT (T::*mfn)(PT1,PT2,PT3,PT4)>
306  static RT method_call(const void* obj,PT1 p1,PT2 p2,PT3 p3,PT4 p4){ return (static_cast<T*>(const_cast<void*>(obj))->*mfn)(p1,p2,p3,p4); }
307 
308  // Stub call to const method
309  template <typename T,RT (T::*mfn)(PT1,PT2,PT3,PT4) const>
310  static RT const_method_call(const void* obj,PT1 p1,PT2 p2,PT3 p3,PT4 p4){ return (static_cast<const T*>(obj)->*mfn)(p1,p2,p3,p4); }
311 
312  // Stub call to function
313  template<RT (*fn)(PT1,PT2,PT3,PT4)>
314  static RT function_call(const void*,PT1 p1,PT2 p2,PT3 p3,PT4 p4){ return (fn)(p1,p2,p3,p4); }
315 
316  // Fallback function
317  static RT default_call(const void*,PT1,PT2,PT3,PT4){ return RT(); }
318 
319 public:
320 
321  // Initialize to default
322  FXCallback():method(&default_call),object(0){ }
323 
324  // Copy constructor
325  FXCallback(const FXCallback& org):method(org.method),object(org.object){ }
326 
327  // Assignment operator
328  FXCallback& operator=(const FXCallback& org){ method=org.method; object=org.object; return *this; }
329 
330  // Invoke the function
331  RT operator()(PT1 p1,PT2 p2,PT3 p3,PT4 p4) const { return (*method)(object,p1,p2,p3,p4); }
332 
333  // Connect to member function of object
334  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2,PT3,PT4)>
335  inline void connect(TYPE* obj){ method=&method_call<TYPE,mfn>; object=obj; }
336 
337  // Connect to member function of object
338  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2,PT3,PT4) const>
339  inline void connect(const TYPE* obj){ method=&const_method_call<TYPE,mfn>; object=obj; }
340 
341  // Connect to plain function
342  template<RT (*fn)(PT1,PT2,PT3)>
343  inline void connect(){ method=&function_call<fn>; object=NULL; }
344 
345  // Return trye if connected
346  bool connected() const { return (method!=&default_call); }
347 
348  // Disconnect resets to default
349  void disconnect(){ method=&default_call; object=NULL; }
350  };
351 
352 }
353 
354 #endif

Copyright © 1997-2016 Jeroen van der Zijp