Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
Delegate.h
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/anakod/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  ****/
7 
13 #ifndef SMINGCORE_DELEGATE_H_
14 #define SMINGCORE_DELEGATE_H_
15 
16 #include <user_config.h>
17 
21 template<class ReturnType, typename... ParamsList>
23 {
24 public:
25  virtual ~IDelegateCaller() = default;
26 
31  virtual ReturnType invoke(ParamsList...) = 0;
32 
35  __forceinline void increase()
36  {
37  references++;
38  }
39 
43  __forceinline void decrease()
44  {
45  references--;
46  if (references == 0)
47  delete this;
48  }
49 private:
50  uint32_t references = 1;
51 };
52 
53 template< class >
54 class MethodCaller; /* undefined */
55 
58 template< class ClassType, class ReturnType, typename... ParamsList >
59 class MethodCaller<ReturnType (ClassType::*)(ParamsList ...)> : public IDelegateCaller<ReturnType, ParamsList...>
60 {
64  typedef ReturnType (ClassType::*MethodDeclaration)(ParamsList ...);
65 
66 public:
71  MethodCaller( ClassType* c, MethodDeclaration m ) : mClass( c ), mMethod( m ) {}
72 
77  ReturnType invoke(ParamsList... args)
78  {
79  return (mClass->*mMethod)( args... );
80  }
81 
82 private:
83  ClassType *mClass;
84  MethodDeclaration mMethod;
85 };
86 
89 template< class MethodDeclaration, class ReturnType, typename... ParamsList >
90 class FunctionCaller : public IDelegateCaller<ReturnType, ParamsList...>
91 {
92 public:
96  FunctionCaller( MethodDeclaration m ) : mMethod( m ) {}
97 
102  ReturnType invoke(ParamsList... args)
103  {
104  return (mMethod)( args... );
105  }
106 
107 private:
108  MethodDeclaration mMethod;
109 };
110 
111 template <class>
112 class Delegate; /* undefined */
113 
114 
117 template<class ReturnType, class ... ParamsList>
118 class Delegate <ReturnType (ParamsList ...)>
119 {
122  typedef ReturnType (*FunctionDeclaration)(ParamsList...);
123 
124  template<typename ClassType> using MethodDeclaration = ReturnType (ClassType::*)(ParamsList ...);
125 
126 public:
129  __forceinline Delegate()
130  {
131  impl = nullptr;
132  }
133 
134  // Class method
139  template <class ClassType>
140  __forceinline Delegate(MethodDeclaration<ClassType> m, ClassType* c)
141  {
142  if (m != NULL)
144  else
145  impl = nullptr;
146  }
147 
148  // Function
152  __forceinline Delegate(FunctionDeclaration m)
153  {
154  if (m != NULL)
155  impl = new FunctionCaller< FunctionDeclaration, ReturnType, ParamsList... >(m);
156  else
157  impl = nullptr;
158  }
159 
160  __forceinline ~Delegate()
161  {
162  if (impl != nullptr)
163  impl->decrease();
164  }
165 
170  __forceinline ReturnType operator()(ParamsList... params) const
171  {
172  return impl->invoke(params...);
173  }
174 
178  __forceinline Delegate(Delegate&& that)
179  {
180  impl = that.impl;
181  that.impl = nullptr;
182  }
183 
187  __forceinline Delegate(const Delegate& that)
188  {
189  copy(that);
190  }
191 
196  __forceinline Delegate& operator=(const Delegate& that) // copy assignment
197  {
198  copy(that);
199  return *this;
200  }
201 
206  Delegate& operator=(Delegate&& that) // move assignment
207  {
208  if (this != &that)
209  {
210  if (impl)
211  impl->decrease();
212 
213  impl = that.impl;
214  that.impl = nullptr;
215  }
216  return *this;
217  }
218 
222  __forceinline operator bool() const
223  {
224  return impl != nullptr;
225  }
226 
227 protected:
228  void copy(const Delegate& other)
229  {
230  if (impl != other.impl)
231  {
232  if (impl)
233  impl->decrease();
234  impl = other.impl;
235  if (impl)
236  impl->increase();
237  }
238  }
239 
240 private:
241  IDelegateCaller<ReturnType, ParamsList...>* impl = nullptr;
242 };
243 
245 #endif /* SMINGCORE_DELEGATE_H_ */
ReturnType invoke(ParamsList...args)
Invoke the delegate function.
Definition: Delegate.h:102
__forceinline Delegate(const Delegate &that)
Copy a delegate from another Delegate object.
Definition: Delegate.h:187
__forceinline Delegate()
Instantiate a delegate object.
Definition: Delegate.h:129
MethodCaller(ClassType *c, MethodDeclaration m)
Instantiate a delegate method caller object.
Definition: Delegate.h:71
__forceinline Delegate(FunctionDeclaration m)
Delegate a function.
Definition: Delegate.h:152
__forceinline Delegate(Delegate &&that)
Move a delegate from another object.
Definition: Delegate.h:178
Definition: Delegate.h:54
__forceinline void decrease()
Decrease the quantity of delegate caller references by one.
Definition: Delegate.h:43
__forceinline Delegate(MethodDeclaration< ClassType > m, ClassType *c)
Delegate a class method.
Definition: Delegate.h:140
Delegate function caller class.
Definition: Delegate.h:90
Definition: Delegate.h:112
__forceinline Delegate & operator=(const Delegate &that)
Copy a delegate from another Delegate object.
Definition: Delegate.h:196
ReturnType invoke(ParamsList...args)
Invoke the delegate method.
Definition: Delegate.h:77
__forceinline void increase()
Increase the quantity of delegate caller references by one.
Definition: Delegate.h:35
virtual ReturnType invoke(ParamsList...)=0
Invode the delegate.
__forceinline ReturnType operator()(ParamsList...params) const
Invoke a delegate.
Definition: Delegate.h:170
Delegate & operator=(Delegate &&that)
Move a delegate from another Delegate object.
Definition: Delegate.h:206
IDelegateCaller class.
Definition: Delegate.h:22
FunctionCaller(MethodDeclaration m)
Instantiate a delegate function caller object.
Definition: Delegate.h:96