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,2021 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 public:
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  // Equality operators
75  FXbool operator==(const FXCallback& other) const { return method==other.method && object==other.object; }
76  FXbool operator!=(const FXCallback& other) const { return method!=other.method || object!=other.object; }
77 
78  // Invoke the function
79  RT operator()() const { return (*method)(object); }
80 
81  // Connect to member function of object
82  template<typename TYPE,RT (TYPE::* mfn)()>
83  inline void connect(TYPE* obj){ method=&method_call<TYPE,mfn>; object=obj; }
84 
85  // Connect to member function of object
86  template<typename TYPE,RT (TYPE::* mfn)() const>
87  inline void connect(const TYPE* obj){ method=&const_method_call<TYPE,mfn>; object=obj; }
88 
89  // Connect to plain function
90  template<RT (*fn)()>
91  inline void connect(){ method=&function_call<fn>; object=NULL; }
92 
93  // Return trye if connected
94  bool connected() const { return (method!=&default_call); }
95 
96  // Disconnect resets to default
97  void disconnect(){ method=&default_call; object=NULL; }
98 
99  // Disconnect conditionally
100  void disconnect(const void* obj){ if(obj==object){ disconnect(); } }
101 
102  // Create default callback
103  static inline FXCallback create(){ return FXCallback(); }
104 
105  // Create callback to member function of object
106  template<typename TYPE,RT (TYPE::* mfn)()>
107  static inline FXCallback create(TYPE* obj){ return FXCallback(&method_call<TYPE,mfn>,obj); }
108 
109  // Create callback to member function of constant object
110  template<typename TYPE,RT (TYPE::* mfn)() const>
111  static inline FXCallback create(const TYPE* obj){ return FXCallback(&const_method_call<TYPE,mfn>,obj); }
112 
113  // Create callback to function
114  template<RT (*fn)()>
115  static inline FXCallback create(){ return FXCallback(&function_call<fn>,NULL); }
116  };
117 
118 /********************************************************************************/
119 
120 // Specialization of callback mechanism for callback with signature:
121 //
122 // RT FUNC(PT1)
123 //
124 // Both free functions and member functions may be called.
125 template<typename RT,typename PT1>
126 class FXAPI FXCallback<RT (PT1)> {
127 public:
128  typedef RT (*METHOD)(const void*,PT1);
129 private:
130  METHOD method;
131  const void* object;
132 private:
133 
134  // Stub call to method
135  template <typename T,RT (T::*mfn)(PT1)>
136  static RT method_call(const void* obj,PT1 p1){ return (static_cast<T*>(const_cast<void*>(obj))->*mfn)(p1); }
137 
138  // Stub call to const method
139  template <typename T,RT (T::*mfn)(PT1) const>
140  static RT const_method_call(const void* obj,PT1 p1){ return (static_cast<const T*>(obj)->*mfn)(p1); }
141 
142  // Stub call to function
143  template<RT (*fn)(PT1)>
144  static RT function_call(const void*,PT1 p1){ return (fn)(p1); }
145 
146  // Fallback function
147  static RT default_call(const void*,PT1){ return RT(); }
148 
149 public:
150 
151  // Initialize to default
152  FXCallback():method(&default_call),object(0){ }
153 
154  // Copy constructor
155  FXCallback(const FXCallback& org):method(org.method),object(org.object){ }
156 
157  // Assignment operator
158  FXCallback& operator=(const FXCallback& org){ method=org.method; object=org.object; return *this; }
159 
160  // Equality operators
161  FXbool operator==(const FXCallback& other) const { return method==other.method && object==other.object; }
162  FXbool operator!=(const FXCallback& other) const { return method!=other.method || object!=other.object; }
163 
164  // Invoke the function
165  RT operator()(PT1 p1) const { return (*method)(object,p1); }
166 
167  // Connect to member function of object
168  template<typename TYPE,RT (TYPE::* mfn)(PT1)>
169  inline void connect(TYPE* obj){ method=&method_call<TYPE,mfn>; object=obj; }
170 
171  // Connect to member function of object
172  template<typename TYPE,RT (TYPE::* mfn)(PT1) const>
173  inline void connect(const TYPE* obj){ method=&const_method_call<TYPE,mfn>; object=obj; }
174 
175  // Connect to plain function
176  template<RT (*fn)(PT1)>
177  inline void connect(){ method=&function_call<fn>; object=NULL; }
178 
179  // Return trye if connected
180  bool connected() const { return (method!=&default_call); }
181 
182  // Disconnect resets to default
183  void disconnect(){ method=&default_call; object=NULL; }
184 
185  // Disconnect conditionally
186  void disconnect(const void* obj){ if(obj==object){ disconnect(); } }
187 
188  // Create default callback
189  static inline FXCallback create(){ return FXCallback(); }
190 
191  // Create callback to member function of object
192  template<typename TYPE,RT (TYPE::* mfn)(PT1)>
193  static inline FXCallback create(TYPE* obj){ return FXCallback(&method_call<TYPE,mfn>,obj); }
194 
195  // Create callback to member function of constant object
196  template<typename TYPE,RT (TYPE::* mfn)(PT1) const>
197  static inline FXCallback create(const TYPE* obj){ return FXCallback(&const_method_call<TYPE,mfn>,obj); }
198 
199  // Create callback to function
200  template<RT (*fn)(PT1)>
201  static inline FXCallback create(){ return FXCallback(&function_call<fn>,NULL); }
202  };
203 
204 /********************************************************************************/
205 
206 // Specialization of callback mechanism for callback with signature:
207 //
208 // RT FUNC(PT1,PT2)
209 //
210 // Both free functions and member functions may be called.
211 template<typename RT,typename PT1,typename PT2>
212 class FXAPI FXCallback<RT (PT1,PT2)> {
213 public:
214  typedef RT (*METHOD)(const void*,PT1,PT2);
215 private:
216  METHOD method;
217  const void* object;
218 private:
219 
220  // Stub call to method
221  template <typename T,RT (T::*mfn)(PT1,PT2)>
222  static RT method_call(const void* obj,PT1 p1,PT2 p2){ return (static_cast<T*>(const_cast<void*>(obj))->*mfn)(p1,p2); }
223 
224  // Stub call to const method
225  template <typename T,RT (T::*mfn)(PT1,PT2) const>
226  static RT const_method_call(const void* obj,PT1 p1,PT2 p2){ return (static_cast<const T*>(obj)->*mfn)(p1,p2); }
227 
228  // Stub call to function
229  template<RT (*fn)(PT1,PT2)>
230  static RT function_call(const void*,PT1 p1,PT2 p2){ return (fn)(p1,p2); }
231 
232  // Fallback function
233  static RT default_call(const void*,PT1,PT2){ return RT(); }
234 
235 public:
236 
237  // Initialize to default
238  FXCallback():method(&default_call),object(0){ }
239 
240  // Copy constructor
241  FXCallback(const FXCallback& org):method(org.method),object(org.object){ }
242 
243  // Assignment operator
244  FXCallback& operator=(const FXCallback& org){ method=org.method; object=org.object; return *this; }
245 
246  // Equality operators
247  FXbool operator==(const FXCallback& other) const { return method==other.method && object==other.object; }
248  FXbool operator!=(const FXCallback& other) const { return method!=other.method || object!=other.object; }
249 
250  // Invoke the function
251  RT operator()(PT1 p1,PT2 p2) const { return (*method)(object,p1,p2); }
252 
253  // Connect to member function of object
254  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2)>
255  inline void connect(TYPE* obj){ method=&method_call<TYPE,mfn>; object=obj; }
256 
257  // Connect to member function of object
258  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2) const>
259  inline void connect(const TYPE* obj){ method=&const_method_call<TYPE,mfn>; object=obj; }
260 
261  // Connect to plain function
262  template<RT (*fn)(PT1,PT2)>
263  inline void connect(){ method=&function_call<fn>; object=NULL; }
264 
265  // Return trye if connected
266  bool connected() const { return (method!=&default_call); }
267 
268  // Disconnect resets to default
269  void disconnect(){ method=&default_call; object=NULL; }
270 
271  // Disconnect conditionally
272  void disconnect(const void* obj){ if(obj==object){ disconnect(); } }
273 
274  // Create default callback
275  static inline FXCallback create(){ return FXCallback(); }
276 
277  // Create callback to member function of object
278  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2)>
279  static inline FXCallback create(TYPE* obj){ return FXCallback(&method_call<TYPE,mfn>,obj); }
280 
281  // Create callback to member function of constant object
282  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2) const>
283  static inline FXCallback create(const TYPE* obj){ return FXCallback(&const_method_call<TYPE,mfn>,obj); }
284 
285  // Create callback to function
286  template<RT (*fn)(PT1,PT2)>
287  static inline FXCallback create(){ return FXCallback(&function_call<fn>,NULL); }
288  };
289 
290 /********************************************************************************/
291 
292 // Specialization of callback mechanism for callback with signature:
293 //
294 // RT FUNC(PT1,PT2,PT3)
295 //
296 // Both free functions and member functions may be called.
297 template<typename RT,typename PT1,typename PT2,typename PT3>
298 class FXAPI FXCallback<RT (PT1,PT2,PT3)> {
299 public:
300  typedef RT (*METHOD)(const void*,PT1,PT2,PT3);
301 private:
302  METHOD method;
303  const void* object;
304 private:
305 
306  // Stub call to method
307  template <typename T,RT (T::*mfn)(PT1,PT2,PT3)>
308  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); }
309 
310  // Stub call to const method
311  template <typename T,RT (T::*mfn)(PT1,PT2,PT3) const>
312  static RT const_method_call(const void* obj,PT1 p1,PT2 p2,PT3 p3){ return (static_cast<const T*>(obj)->*mfn)(p1,p2,p3); }
313 
314  // Stub call to function
315  template<RT (*fn)(PT1,PT2,PT3)>
316  static RT function_call(const void*,PT1 p1,PT2 p2,PT3 p3){ return (fn)(p1,p2,p3); }
317 
318  // Fallback function
319  static RT default_call(const void*,PT1,PT2,PT3){ return RT(); }
320 
321 public:
322 
323  // Initialize to default
324  FXCallback():method(&default_call),object(0){ }
325 
326  // Copy constructor
327  FXCallback(const FXCallback& org):method(org.method),object(org.object){ }
328 
329  // Assignment operator
330  FXCallback& operator=(const FXCallback& org){ method=org.method; object=org.object; return *this; }
331 
332  // Equality operators
333  FXbool operator==(const FXCallback& other) const { return method==other.method && object==other.object; }
334  FXbool operator!=(const FXCallback& other) const { return method!=other.method || object!=other.object; }
335 
336  // Invoke the function
337  RT operator()(PT1 p1,PT2 p2,PT3 p3) const { return (*method)(object,p1,p2,p3); }
338 
339  // Connect to member function of object
340  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2,PT3)>
341  inline void connect(TYPE* obj){ method=&method_call<TYPE,mfn>; object=obj; }
342 
343  // Connect to member function of object
344  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2,PT3) const>
345  inline void connect(const TYPE* obj){ method=&const_method_call<TYPE,mfn>; object=obj; }
346 
347  // Connect to plain function
348  template<RT (*fn)(PT1,PT2,PT3)>
349  inline void connect(){ method=&function_call<fn>; object=NULL; }
350 
351  // Return trye if connected
352  bool connected() const { return (method!=&default_call); }
353 
354  // Disconnect resets to default
355  void disconnect(){ method=&default_call; object=NULL; }
356 
357  // Disconnect conditionally
358  void disconnect(const void* obj){ if(obj==object){ disconnect(); } }
359 
360  // Create default callback
361  static inline FXCallback create(){ return FXCallback(); }
362 
363  // Create callback to member function of object
364  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2,PT3)>
365  static inline FXCallback create(TYPE* obj){ return FXCallback(&method_call<TYPE,mfn>,obj); }
366 
367  // Create callback to member function of constant object
368  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2,PT3) const>
369  static inline FXCallback create(const TYPE* obj){ return FXCallback(&const_method_call<TYPE,mfn>,obj); }
370 
371  // Create callback to function
372  template<RT (*fn)(PT1,PT2,PT3)>
373  static inline FXCallback create(){ return FXCallback(&function_call<fn>,NULL); }
374  };
375 
376 /********************************************************************************/
377 
378 // Specialization of callback mechanism for callback with signature:
379 //
380 // RT FUNC(PT1,PT2,PT3,PT4)
381 //
382 // Both free functions and member functions may be called.
383 template<typename RT,typename PT1,typename PT2,typename PT3,typename PT4>
384 class FXAPI FXCallback<RT (PT1,PT2,PT3,PT4)> {
385 public:
386  typedef RT (*METHOD)(const void*,PT1,PT2,PT3,PT4);
387 private:
388  METHOD method;
389  const void* object;
390 private:
391 
392  // Stub call to method
393  template <typename T,RT (T::*mfn)(PT1,PT2,PT3,PT4)>
394  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); }
395 
396  // Stub call to const method
397  template <typename T,RT (T::*mfn)(PT1,PT2,PT3,PT4) const>
398  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); }
399 
400  // Stub call to function
401  template<RT (*fn)(PT1,PT2,PT3,PT4)>
402  static RT function_call(const void*,PT1 p1,PT2 p2,PT3 p3,PT4 p4){ return (fn)(p1,p2,p3,p4); }
403 
404  // Fallback function
405  static RT default_call(const void*,PT1,PT2,PT3,PT4){ return RT(); }
406 
407 public:
408 
409  // Initialize to default
410  FXCallback():method(&default_call),object(0){ }
411 
412  // Copy constructor
413  FXCallback(const FXCallback& org):method(org.method),object(org.object){ }
414 
415  // Assignment operator
416  FXCallback& operator=(const FXCallback& org){ method=org.method; object=org.object; return *this; }
417 
418  // Equality operators
419  FXbool operator==(const FXCallback& other) const { return method==other.method && object==other.object; }
420  FXbool operator!=(const FXCallback& other) const { return method!=other.method || object!=other.object; }
421 
422  // Invoke the function
423  RT operator()(PT1 p1,PT2 p2,PT3 p3,PT4 p4) const { return (*method)(object,p1,p2,p3,p4); }
424 
425  // Connect to member function of object
426  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2,PT3,PT4)>
427  inline void connect(TYPE* obj){ method=&method_call<TYPE,mfn>; object=obj; }
428 
429  // Connect to member function of object
430  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2,PT3,PT4) const>
431  inline void connect(const TYPE* obj){ method=&const_method_call<TYPE,mfn>; object=obj; }
432 
433  // Connect to plain function
434  template<RT (*fn)(PT1,PT2,PT3,PT4)>
435  inline void connect(){ method=&function_call<fn>; object=NULL; }
436 
437  // Return trye if connected
438  bool connected() const { return (method!=&default_call); }
439 
440  // Disconnect resets to default
441  void disconnect(){ method=&default_call; object=NULL; }
442 
443  // Disconnect conditionally
444  void disconnect(const void* obj){ if(obj==object){ disconnect(); } }
445 
446  // Create default callback
447  static inline FXCallback create(){ return FXCallback(); }
448 
449  // Create callback to member function of object
450  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2,PT3,PT4)>
451  static inline FXCallback create(TYPE* obj){ return FXCallback(&method_call<TYPE,mfn>,obj); }
452 
453  // Create callback to member function of constant object
454  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2,PT3,PT4) const>
455  static inline FXCallback create(const TYPE* obj){ return FXCallback(&const_method_call<TYPE,mfn>,obj); }
456 
457  // Create callback to function
458  template<RT (*fn)(PT1,PT2,PT3,PT4)>
459  static inline FXCallback create(){ return FXCallback(&function_call<fn>,NULL); }
460  };
461 
462 }
463 
464 #endif
Definition: FXCallback.h:30
Definition: FX4Splitter.h:28

Copyright © 1997-2020 Jeroen van der Zijp