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,2024 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  // Initialize with method and object
49  FXCallback(Method m,const void* o):method(m),object(o){ }
50 
51  // Stub call to method
52  template <typename T,RT (T::*mfn)()>
53  static RT MethodCall(const void* obj){ return (static_cast<T*>(const_cast<void*>(obj))->*mfn)(); }
54 
55  // Stub call to const method
56  template <typename T,RT (T::*mfn)() const>
57  static RT ConstMethodCall(const void* obj){ return (static_cast<const T*>(obj)->*mfn)(); }
58 
59  // Stub call to function
60  template<RT (*fn)()>
61  static RT FunctionCall(const void*){ return (fn)(); }
62 
63  // Fallback function
64  static RT DefaultCall(const void*){ return RT(); }
65 
66 public:
67 
68  // Initialize to default
69  FXCallback():method(&DefaultCall),object(nullptr){ }
70 
71  // Copy constructor
72  FXCallback(const FXCallback& org):method(org.method),object(org.object){ }
73 
74  // Assignment operator
75  FXCallback& operator=(const FXCallback& org){ method=org.method; object=org.object; return *this; }
76 
77  // Equality operators
78  FXbool operator==(const FXCallback& other) const { return method==other.method && object==other.object; }
79  FXbool operator!=(const FXCallback& other) const { return method!=other.method || object!=other.object; }
80 
81  // Invoke the function
82  RT operator()() const { return (*method)(object); }
83 
84  // Connect to member function of object
85  template<typename TYPE,RT (TYPE::* mfn)()>
86  inline void connect(TYPE* obj){ method=&MethodCall<TYPE,mfn>; object=obj; }
87 
88  // Connect to member function of object
89  template<typename TYPE,RT (TYPE::* mfn)() const>
90  inline void connect(const TYPE* obj){ method=&ConstMethodCall<TYPE,mfn>; object=obj; }
91 
92  // Connect to plain function
93  template<RT (*fn)()>
94  inline void connect(){ method=&FunctionCall<fn>; object=nullptr; }
95 
96  // Return true if connected
97  bool connected() const { return (method!=&DefaultCall); }
98 
99  // Disconnect resets to default
100  void disconnect(){ method=&DefaultCall; object=nullptr; }
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(&MethodCall<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(&ConstMethodCall<TYPE,mfn>,obj); }
112 
113  // Create callback to function
114  template<RT (*fn)()>
115  static inline FXCallback create(){ return FXCallback(&FunctionCall<fn>,nullptr); }
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  // Initialize with method and object
135  FXCallback(Method m,const void* o):method(m),object(o){ }
136 
137  // Stub call to method
138  template <typename T,RT (T::*mfn)(PT1)>
139  static RT MethodCall(const void* obj,PT1 p1){ return (static_cast<T*>(const_cast<void*>(obj))->*mfn)(p1); }
140 
141  // Stub call to const method
142  template <typename T,RT (T::*mfn)(PT1) const>
143  static RT ConstMethodCall(const void* obj,PT1 p1){ return (static_cast<const T*>(obj)->*mfn)(p1); }
144 
145  // Stub call to function
146  template<RT (*fn)(PT1)>
147  static RT FunctionCall(const void*,PT1 p1){ return (fn)(p1); }
148 
149  // Fallback function
150  static RT DefaultCall(const void*,PT1){ return RT(); }
151 
152 public:
153 
154  // Initialize to default
155  FXCallback():method(&DefaultCall),object(nullptr){ }
156 
157  // Copy constructor
158  FXCallback(const FXCallback& org):method(org.method),object(org.object){ }
159 
160  // Assignment operator
161  FXCallback& operator=(const FXCallback& org){ method=org.method; object=org.object; return *this; }
162 
163  // Equality operators
164  FXbool operator==(const FXCallback& other) const { return method==other.method && object==other.object; }
165  FXbool operator!=(const FXCallback& other) const { return method!=other.method || object!=other.object; }
166 
167  // Invoke the function
168  RT operator()(PT1 p1) const { return (*method)(object,p1); }
169 
170  // Connect to member function of object
171  template<typename TYPE,RT (TYPE::* mfn)(PT1)>
172  inline void connect(TYPE* obj){ method=&MethodCall<TYPE,mfn>; object=obj; }
173 
174  // Connect to member function of object
175  template<typename TYPE,RT (TYPE::* mfn)(PT1) const>
176  inline void connect(const TYPE* obj){ method=&ConstMethodCall<TYPE,mfn>; object=obj; }
177 
178  // Connect to plain function
179  template<RT (*fn)(PT1)>
180  inline void connect(){ method=&FunctionCall<fn>; object=nullptr; }
181 
182  // Return true if connected
183  bool connected() const { return (method!=&DefaultCall); }
184 
185  // Disconnect resets to default
186  void disconnect(){ method=&DefaultCall; object=nullptr; }
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(&MethodCall<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(&ConstMethodCall<TYPE,mfn>,obj); }
198 
199  // Create callback to function
200  template<RT (*fn)(PT1)>
201  static inline FXCallback create(){ return FXCallback(&FunctionCall<fn>,nullptr); }
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  // Initialize with method and object
221  FXCallback(Method m,const void* o):method(m),object(o){ }
222 
223  // Stub call to method
224  template <typename T,RT (T::*mfn)(PT1,PT2)>
225  static RT MethodCall(const void* obj,PT1 p1,PT2 p2){ return (static_cast<T*>(const_cast<void*>(obj))->*mfn)(p1,p2); }
226 
227  // Stub call to const method
228  template <typename T,RT (T::*mfn)(PT1,PT2) const>
229  static RT ConstMethodCall(const void* obj,PT1 p1,PT2 p2){ return (static_cast<const T*>(obj)->*mfn)(p1,p2); }
230 
231  // Stub call to function
232  template<RT (*fn)(PT1,PT2)>
233  static RT FunctionCall(const void*,PT1 p1,PT2 p2){ return (fn)(p1,p2); }
234 
235  // Fallback function
236  static RT DefaultCall(const void*,PT1,PT2){ return RT(); }
237 
238 public:
239 
240  // Initialize to default
241  FXCallback():method(&DefaultCall),object(nullptr){ }
242 
243  // Copy constructor
244  FXCallback(const FXCallback& org):method(org.method),object(org.object){ }
245 
246  // Assignment operator
247  FXCallback& operator=(const FXCallback& org){ method=org.method; object=org.object; return *this; }
248 
249  // Equality operators
250  FXbool operator==(const FXCallback& other) const { return method==other.method && object==other.object; }
251  FXbool operator!=(const FXCallback& other) const { return method!=other.method || object!=other.object; }
252 
253  // Invoke the function
254  RT operator()(PT1 p1,PT2 p2) const { return (*method)(object,p1,p2); }
255 
256  // Connect to member function of object
257  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2)>
258  inline void connect(TYPE* obj){ method=&MethodCall<TYPE,mfn>; object=obj; }
259 
260  // Connect to member function of object
261  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2) const>
262  inline void connect(const TYPE* obj){ method=&ConstMethodCall<TYPE,mfn>; object=obj; }
263 
264  // Connect to plain function
265  template<RT (*fn)(PT1,PT2)>
266  inline void connect(){ method=&FunctionCall<fn>; object=nullptr; }
267 
268  // Return true if connected
269  bool connected() const { return (method!=&DefaultCall); }
270 
271  // Disconnect resets to default
272  void disconnect(){ method=&DefaultCall; object=nullptr; }
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(&MethodCall<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(&ConstMethodCall<TYPE,mfn>,obj); }
284 
285  // Create callback to function
286  template<RT (*fn)(PT1,PT2)>
287  static inline FXCallback create(){ return FXCallback(&FunctionCall<fn>,nullptr); }
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  // Initialize with method and object
307  FXCallback(Method m,const void* o):method(m),object(o){ }
308 
309  // Stub call to method
310  template <typename T,RT (T::*mfn)(PT1,PT2,PT3)>
311  static RT MethodCall(const void* obj,PT1 p1,PT2 p2,PT3 p3){ return (static_cast<T*>(const_cast<void*>(obj))->*mfn)(p1,p2,p3); }
312 
313  // Stub call to const method
314  template <typename T,RT (T::*mfn)(PT1,PT2,PT3) const>
315  static RT ConstMethodCall(const void* obj,PT1 p1,PT2 p2,PT3 p3){ return (static_cast<const T*>(obj)->*mfn)(p1,p2,p3); }
316 
317  // Stub call to function
318  template<RT (*fn)(PT1,PT2,PT3)>
319  static RT FunctionCall(const void*,PT1 p1,PT2 p2,PT3 p3){ return (fn)(p1,p2,p3); }
320 
321  // Fallback function
322  static RT DefaultCall(const void*,PT1,PT2,PT3){ return RT(); }
323 
324 public:
325 
326  // Initialize to default
327  FXCallback():method(&DefaultCall),object(nullptr){ }
328 
329  // Copy constructor
330  FXCallback(const FXCallback& org):method(org.method),object(org.object){ }
331 
332  // Assignment operator
333  FXCallback& operator=(const FXCallback& org){ method=org.method; object=org.object; return *this; }
334 
335  // Equality operators
336  FXbool operator==(const FXCallback& other) const { return method==other.method && object==other.object; }
337  FXbool operator!=(const FXCallback& other) const { return method!=other.method || object!=other.object; }
338 
339  // Invoke the function
340  RT operator()(PT1 p1,PT2 p2,PT3 p3) const { return (*method)(object,p1,p2,p3); }
341 
342  // Connect to member function of object
343  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2,PT3)>
344  inline void connect(TYPE* obj){ method=&MethodCall<TYPE,mfn>; object=obj; }
345 
346  // Connect to member function of object
347  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2,PT3) const>
348  inline void connect(const TYPE* obj){ method=&ConstMethodCall<TYPE,mfn>; object=obj; }
349 
350  // Connect to plain function
351  template<RT (*fn)(PT1,PT2,PT3)>
352  inline void connect(){ method=&FunctionCall<fn>; object=nullptr; }
353 
354  // Return true if connected
355  bool connected() const { return (method!=&DefaultCall); }
356 
357  // Disconnect resets to default
358  void disconnect(){ method=&DefaultCall; object=nullptr; }
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(&MethodCall<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(&ConstMethodCall<TYPE,mfn>,obj); }
370 
371  // Create callback to function
372  template<RT (*fn)(PT1,PT2,PT3)>
373  static inline FXCallback create(){ return FXCallback(&FunctionCall<fn>,nullptr); }
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  // Initialize with method and object
393  FXCallback(Method m,const void* o):method(m),object(o){ }
394 
395  // Stub call to method
396  template <typename T,RT (T::*mfn)(PT1,PT2,PT3,PT4)>
397  static RT MethodCall(const void* obj,PT1 p1,PT2 p2,PT3 p3,PT4 p4){ return (static_cast<T*>(const_cast<void*>(obj))->*mfn)(p1,p2,p3,p4); }
398 
399  // Stub call to const method
400  template <typename T,RT (T::*mfn)(PT1,PT2,PT3,PT4) const>
401  static RT ConstMethodCall(const void* obj,PT1 p1,PT2 p2,PT3 p3,PT4 p4){ return (static_cast<const T*>(obj)->*mfn)(p1,p2,p3,p4); }
402 
403  // Stub call to function
404  template<RT (*fn)(PT1,PT2,PT3,PT4)>
405  static RT FunctionCall(const void*,PT1 p1,PT2 p2,PT3 p3,PT4 p4){ return (fn)(p1,p2,p3,p4); }
406 
407  // Fallback function
408  static RT DefaultCall(const void*,PT1,PT2,PT3,PT4){ return RT(); }
409 
410 public:
411 
412  // Initialize to default
413  FXCallback():method(&DefaultCall),object(nullptr){ }
414 
415  // Copy constructor
416  FXCallback(const FXCallback& org):method(org.method),object(org.object){ }
417 
418  // Assignment operator
419  FXCallback& operator=(const FXCallback& org){ method=org.method; object=org.object; return *this; }
420 
421  // Equality operators
422  FXbool operator==(const FXCallback& other) const { return method==other.method && object==other.object; }
423  FXbool operator!=(const FXCallback& other) const { return method!=other.method || object!=other.object; }
424 
425  // Invoke the function
426  RT operator()(PT1 p1,PT2 p2,PT3 p3,PT4 p4) const { return (*method)(object,p1,p2,p3,p4); }
427 
428  // Connect to member function of object
429  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2,PT3,PT4)>
430  inline void connect(TYPE* obj){ method=&MethodCall<TYPE,mfn>; object=obj; }
431 
432  // Connect to member function of object
433  template<typename TYPE,RT (TYPE::* mfn)(PT1,PT2,PT3,PT4) const>
434  inline void connect(const TYPE* obj){ method=&ConstMethodCall<TYPE,mfn>; object=obj; }
435 
436  // Connect to plain function
437  template<RT (*fn)(PT1,PT2,PT3,PT4)>
438  inline void connect(){ method=&FunctionCall<fn>; object=nullptr; }
439 
440  // Return true if connected
441  bool connected() const { return (method!=&DefaultCall); }
442 
443  // Disconnect resets to default
444  void disconnect(){ method=&DefaultCall; object=nullptr; }
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(&MethodCall<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(&ConstMethodCall<TYPE,mfn>,obj); }
456 
457  // Create callback to function
458  template<RT (*fn)(PT1,PT2,PT3,PT4)>
459  static inline FXCallback create(){ return FXCallback(&FunctionCall<fn>,nullptr); }
460  };
461 
462 }
463 
464 #endif
Definition: FXCallback.h:30
Definition: FX4Splitter.h:28

Copyright © 1997-2022 Jeroen van der Zijp