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

Copyright © 1997-2018 Jeroen van der Zijp