MAK Legion 1.1 API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
callback.h
Go to the documentation of this file.
1 /*********************************************************************************
2 ** Copyright (c) 2020 MAK Technologies, Inc.
3 ** All rights reserved.
4 *********************************************************************************/
5 
8 
9 #pragma once
10 
11 #include <functional>
12 
17 
18 /*
19 usage:
20 
21 A Callback is an object that is created to hold a function and can called at a later time. It can either reference
22 a static class function, global function, or class member function on an object.
23 
24 The template Callback class is typedef to a specific function signature (i.e: return( parameter values)) by
25 declaring the function signature in the callback template
26 
27 typedef Callback<void(int, double)> FooCallback;
28 
29 To create a Callback of a static function, you use the function address:
30 
31 void foo(int a, double b) {...}
32 
33 FooCallback f(foo);
34 
35 to create a Callback of a member function, you need to use the object's pointer and a member function pointer to a function
36 with the same signature:
37 
38 class Foo
39 {
40  void doStuff(int a, double b) {...}
41 }
42 
43 Foo a;
44 
45 FooCallback f(&a, &Foo::doStuff);
46 
47 At some other point in the code, you can invoke the callbacks as though the delegate were a function
48 
49 int a =1;
50 double b = 20.0;
51 
52 f(a, b);
53 
54 */
55 
56 namespace Legion
57 {
58  //forward definitions of the function capture classes used in the delegate
59  template <typename Ret, typename... Params> class AbstractFunctionCapture;
60  template <typename Ret, typename... Params> class StaticFunctionCapture;
61  template <typename Ret, typename T, typename Method, typename... Params> class MethodCapture;
62  template <typename Ret, typename... Params> class StdFunctionCapture;
63 
66  template<typename T> class Callback;
67  template <typename Ret, typename... Params>
68  class Callback<Ret (Params...)>
69  {
70  private:
72  int myType;
73 
74  public:
77  :myFunctionCapture(0)
78  , myType(0)
79  {}
80 
82  Callback(Ret(*func)(Params...))
83  :myFunctionCapture(new StaticFunctionCapture<Ret, Params...>(func))
84  , myType(1)
85  {}
86 
88  template <typename T, typename Method>
89  Callback(T *object, Method method)
90  : myFunctionCapture(new MethodCapture<Ret, T, Method, Params...>(object, method))
91  , myType(2)
92  {}
93 
95  Callback(std::function<Ret(Params...)>& stdFunc)
96  : myFunctionCapture(new StdFunctionCapture<Ret, Params...>(stdFunc))
97  , myType(3)
98  {}
99 
100 
102  Callback(const Callback& orig)
103  {
104  if (orig.myFunctionCapture)
105  {
106  myFunctionCapture = orig.myFunctionCapture->clone();
107  }
108  else
109  {
110  myFunctionCapture = 0;
111  }
112 
113  myType = orig.myType;
114  }
115 
118  {
119  if (this != &orig)
120  {
121  if (orig.myFunctionCapture)
122  {
123  myFunctionCapture = orig.myFunctionCapture->clone();
124  }
125  else
126  {
127  myFunctionCapture = 0;
128  }
129 
130  myType = orig.myType;
131  }
132 
133  return *this;
134  }
135 
137  ~Callback(void) { delete myFunctionCapture; }
138 
140  bool isValid() { return myFunctionCapture != 0; }
141 
143  Ret operator()(Params... params) const
144  {
145  return myFunctionCapture->invoke(params...);
146  }
147 
149  bool operator==(const Callback& rhs) const
150  {
151  if (myType != rhs.myType)
152  {
153  return false;
154  }
155 
156  return (*myFunctionCapture) == (*(rhs.myFunctionCapture));
157  }
158 
160  bool operator!=(const Callback& rhs) const
161  {
162  return !(*this == rhs);
163  }
164  };
165 
166 
167  //--------------------------------Implementation details of capturing a static function or member function-------------------------------------------------------
168 
169 
171  template <typename Ret, typename... Params>
172  class AbstractFunctionCapture
173  {
174  public:
175  virtual Ret invoke(Params... params) const = 0;
176  virtual AbstractFunctionCapture* clone() const = 0;
177 
178  virtual bool operator==(const AbstractFunctionCapture<Ret, Params...>& rhs) const = 0;
179 
181  {
182  return !(*this == rhs);
183  }
184  };
185 
186 
189  template <typename Ret, typename... Params>
190  class StaticFunctionCapture : public AbstractFunctionCapture<Ret, Params...>
191  {
192  private:
193  Ret(*myFunction)(Params... params);
194 
195  public:
196  StaticFunctionCapture(Ret(*func)(Params... params))
197  : myFunction(func)
198  {}
199 
201  {
202  myFunction = orig.myFunction;
203  }
204 
206  {
207  if (this != &orig)
208  {
209  myFunction = orig.myFunction;
210  }
211 
212  return *this;
213  }
214 
216  {
218  return nc;
219  }
220 
221  Ret invoke(Params... params) const
222  {
223  return (*myFunction)(params...);
224  }
225 
227  {
229  return myFunction == rhsp->myFunction;
230  }
231  };
232 
233 
236  template <typename Ret, typename T, typename Method, typename... Params>
237  class MethodCapture : public AbstractFunctionCapture<Ret, Params...>
238  {
239  private:
240  void* myObject;
241  Method myMethod;
242 
243  public:
244  MethodCapture(void *object, Method method)
245  : myObject(object)
246  , myMethod(method)
247  {}
248 
250  {
251  myObject = orig.myObject;
252  myMethod = orig.myMethod;
253  }
254 
256  {
258  return nc;
259  }
260 
262  {
263  if (this != &orig)
264  {
265  myObject = orig.myObject;
266  myMethod = orig.myMethod;
267  }
268 
269  return *this;
270  }
271 
272  Ret invoke(Params... params) const
273  {
274  T *obj = static_cast<T *>(myObject);
275  return (obj->*myMethod)(params...);
276  }
277 
279  {
280  MethodCapture* rhsp = (MethodCapture*)&rhs;
281  return (myObject == rhsp->myObject) && (myMethod == rhsp->myMethod);
282  }
283  };
284 
285 
288  template <typename Ret, typename... Params>
289  class StdFunctionCapture : public AbstractFunctionCapture<Ret, Params...>
290  {
291  private:
292  std::function<Ret(Params...)> myFunc;
293 
294  public:
295  StdFunctionCapture(const std::function<Ret(Params...)>& func)
296  : myFunc(func)
297  {}
298 
300  {
301  myFunc = orig.myFunc;
302  }
303 
305  {
307  return nc;
308  }
309 
311  {
312  if (this != &orig)
313  {
314  myFunc = orig.myFunc;
315  }
316 
317  return *this;
318  }
319 
320  Ret invoke(Params... params) const
321  {
322  return myFunc(params...);
323  }
324 
326  {
327  StdFunctionCapture* rhsp = (StdFunctionCapture*)&rhs;
328  return *(long*)(char*)(&myFunc) == *(long*)(char*)(&rhsp->myFunc);
329  }
330  };
331 }
std::function callback is used to bind std::functions, which are typically lambdas The return type an...
Definition: callback.h:62
virtual Ret invoke(Params...params) const =0
bool isValid()
returns if this callback object points to valid function
Definition: callback.h:140
Member function callback is used to bind class member functions The return type, single parameter...
Definition: callback.h:61
virtual bool operator==(const AbstractFunctionCapture< Ret, Params...> &rhs) const =0
std::function< Ret(Params...)> myFunc
Definition: callback.h:292
StaticFunctionCapture(const StaticFunctionCapture &orig)
Definition: callback.h:200
MethodCapture & operator=(const MethodCapture &orig)
Definition: callback.h:261
bool operator!=(const Callback &rhs) const
not equals operator
Definition: callback.h:160
int myType
Definition: callback.h:72
Abstract Callback template the base abstract callback class that is used internally to the delegate...
Definition: callback.h:59
Callback()
default empty constructor
Definition: callback.h:76
StdFunctionCapture * clone() const
Definition: callback.h:304
Callback template for binding static or member functions for callback at later time The return type a...
Definition: callback.h:66
bool operator==(const Callback &rhs) const
equality operator that checks internal function pointers
Definition: callback.h:149
Callback(Ret(*func)(Params...))
constructor using a static function
Definition: callback.h:82
StaticFunctionCapture * clone() const
Definition: callback.h:215
void * myObject
Definition: callback.h:240
StdFunctionCapture & operator=(const StdFunctionCapture &orig)
Definition: callback.h:310
StaticFunctionCapture & operator=(const StaticFunctionCapture &orig)
Definition: callback.h:205
Callback(T *object, Method method)
constructor using a class member functions
Definition: callback.h:89
Ret invoke(Params...params) const
Definition: callback.h:320
Callback(const Callback &orig)
copy constructor
Definition: callback.h:102
StaticFunctionCapture(Ret(*func)(Params...params))
Definition: callback.h:196
~Callback(void)
destructor
Definition: callback.h:137
bool operator==(const AbstractFunctionCapture< Ret, Params...> &rhs) const
Definition: callback.h:325
Method myMethod
Definition: callback.h:241
virtual bool operator!=(const AbstractFunctionCapture< Ret, Params...> &rhs) const
Definition: callback.h:180
Ret invoke(Params...params) const
Definition: callback.h:272
bool operator==(const AbstractFunctionCapture< Ret, Params...> &rhs) const
Definition: callback.h:226
Ret(* myFunction)(Params...params)
Definition: callback.h:193
MethodCapture(void *object, Method method)
Definition: callback.h:244
virtual AbstractFunctionCapture * clone() const =0
Ret operator()(Params...params) const
execute operator
Definition: callback.h:143
Static member function (or global function) callback is used to bind global or static class functions...
Definition: callback.h:60
bool operator==(const AbstractFunctionCapture< Ret, Params...> &rhs) const
Definition: callback.h:278
MethodCapture * clone() const
Definition: callback.h:255
Ret invoke(Params...params) const
Definition: callback.h:221
Callback & operator=(const Callback &orig)
assignment operator
Definition: callback.h:117
MethodCapture(const MethodCapture &orig)
Definition: callback.h:249
AbstractFunctionCapture< Ret, Params...> * myFunctionCapture
Definition: callback.h:71

Document ID: Generated on Wed May 26 15:17:05 EDT 2021 from SVN revision 229676
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)