EasyDelegate  2.0
Delegate and deferred callers for C++11.
delegates.hpp
Go to the documentation of this file.
1 
12 #ifndef _INCLUDE_EASYDELEGATE_DELEGATES_HPP_
13 #define _INCLUDE_EASYDELEGATE_DELEGATES_HPP_
14 
15 #include <assert.h> // assert(expr)
16 
17 #include "types.hpp"
18 #include "exceptions.hpp"
19 
20 namespace EasyDelegate
21 {
22  template <typename returnType, typename... parameters>
24 
35  class IDelegate
36  {
37  // Public Members
38  public:
40  const bool mIsMemberDelegate;
41 
42  // Protected Methods
43  protected:
49  IDelegate(const bool& isMemberDelegate) noexcept : mIsMemberDelegate(isMemberDelegate) { }
50  };
51 
52  template <typename className, typename returnType, typename... parameters>
54 
60  template <typename returnType, typename... parameters>
61  class StaticDelegate : public ITypedDelegate<returnType, parameters...>
62  {
63  public:
65  typedef StaticMethodPointer<returnType, parameters...> MethodPointer;
66 
72  StaticDelegate(const MethodPointer methodPointer) : ITypedDelegate<returnType, parameters...>(false), mMethodPointer(methodPointer) { }
73 
78  StaticDelegate(const StaticDelegate<returnType, parameters...>* other) : ITypedDelegate<returnType, parameters...>(false),
79  mMethodPointer(other->mMethodPointer) { }
80 
92  returnType invoke(parameters... params)
93  {
94  assert(mMethodPointer);
95 
96  if (!mMethodPointer)
98 
99  return ((MethodPointer)mMethodPointer)(params...);
100  }
101 
109  EASYDELEGATE_INLINE bool hasThisPointer(const void *thisPointer) const noexcept { return false; }
110 
116  EASYDELEGATE_INLINE bool callsMethod(const MethodPointer methodPointer) const noexcept { return methodPointer == mMethodPointer; };
117 
124  template <typename otherClass, typename otherReturn, typename... otherParams>
125  EASYDELEGATE_INLINE bool callsMethod(const MemberMethodPointer<otherClass, otherReturn, otherParams...> methodPointer) const noexcept { return false; }
126 
134  EASYDELEGATE_INLINE bool hasSameMethodAs(const StaticDelegate<returnType, parameters...>* other) const noexcept { return other->mMethodPointer == mMethodPointer; }
135 
143  template <typename otherClass, typename otherReturn, typename... otherParams>
145 
154  template <typename otherReturn, typename... otherParams>
155  EASYDELEGATE_INLINE bool hasSameMethodAs(const StaticDelegate<otherReturn, otherParams...>* other) const noexcept { return false; }
156 
165  template <typename otherClass, typename otherReturn, typename... otherParams>
167 
176  template <typename otherReturn, typename... otherParams>
178 
179  private:
181  const MethodPointer mMethodPointer;
182  };
183 
189  template <typename classType, typename returnType, typename... parameters>
190  class MemberDelegate : public ITypedDelegate<returnType, parameters...>
191  {
192  public:
194  typedef MemberMethodPointer<classType, returnType, parameters...> MethodPointer;
195 
206  MemberDelegate(const MethodPointer methodPointer, classType* thisPointer) : mThisPointer(thisPointer),
207  mMethodPointer(methodPointer), ITypedDelegate<returnType, parameters...>(true) { }
208 
214  mMethodPointer(other->mMethodPointer), ITypedDelegate<returnType, parameters...>(true) { }
215 
228  returnType invoke(parameters... params)
229  {
230  assert(mThisPointer);
231  assert(mMethodPointer);
232 
233  if (!mThisPointer)
235 
236  if (!mMethodPointer)
238 
239  classType *thisPointer = (classType*)thisPointer;
240  return (thisPointer->*mMethodPointer)(params...);
241  }
242 
249  EASYDELEGATE_INLINE bool hasThisPointer(const void* thisPointer) const noexcept { return mThisPointer == thisPointer; }
250 
256  EASYDELEGATE_INLINE bool callsMethod(const MethodPointer methodPointer) const noexcept { return mMethodPointer == methodPointer; }
257 
264  template <typename otherReturn, typename... otherParams>
265  EASYDELEGATE_INLINE bool callsMethod(const StaticMethodPointer<otherReturn, otherParams...> methodPointer) const noexcept { return false; }
266 
273  EASYDELEGATE_INLINE bool callsMethod(const typename ITypedDelegate<returnType, parameters...>::StaticMethodPointerType methodPointer) const noexcept { return false; }
274 
280  EASYDELEGATE_INLINE bool hasSameMethodAs(const MemberDelegate<classType, returnType, parameters...>* other) const noexcept { return other->mMethodPointer == mMethodPointer; }
281 
291  template <typename otherClass, typename otherReturn, typename... otherParams>
293 
302  template <typename otherReturn, typename... otherParams>
303  EASYDELEGATE_INLINE bool hasSameMethodAs(const StaticDelegate<otherReturn, otherParams...>* other) const noexcept { return false; }
304 
313  template <typename otherReturn, typename... otherParams>
315 
323  template <typename otherClass, typename otherReturn, typename... otherParams>
324  EASYDELEGATE_INLINE bool hasSameThisPointerAs(const MemberDelegate<otherClass, otherReturn, otherParams...>* other) const noexcept { return reinterpret_cast<void*>(mThisPointer) == reinterpret_cast<void*>(other->mThisPointer); }
325 
326  // Public Members
327  public:
329  classType* mThisPointer;
330 
331  private:
333  const MethodPointer mMethodPointer;
334  };
335 
342  template <typename returnType, typename... parameters>
343  class ITypedDelegate : public IDelegate
344  {
345  // Public Members
346  public:
348  typedef returnType ReturnType;
350  typedef StaticDelegate<returnType, parameters...> StaticDelegateType;
352  template <typename classType>
353  using MemberDelegateType = MemberDelegate<classType, returnType, parameters...>;
354 
356  typedef StaticMethodPointer<returnType, parameters...> StaticMethodPointerType;
357 
359  template <typename classType>
360  using MemberDelegateFuncPtr = returnType(classType::*)(parameters...);
361 
362  // Public Methods
363  public:
369  ITypedDelegate(const bool& isMemberDelegate) noexcept : IDelegate(isMemberDelegate) { }
370 
378  virtual bool callsMethod(const StaticMethodPointerType methodPointer) const noexcept = 0;
379 
390  template <typename className>
391  bool callsMethod(const MemberDelegateFuncPtr<className> methodPointer) const noexcept
392  {
393  // Don't try to check as a MemberDelegate if we're not actually one
394  if (!mIsMemberDelegate)
395  return false;
396 
397  // This is a hack so that the proper callsMethod function is called to get the return value
398  const MemberDelegate<className, returnType, parameters...>* memberDelegateObj = reinterpret_cast<const MemberDelegate<className, returnType, parameters...>*>(this);
399  return memberDelegateObj->callsMethod(methodPointer);
400  }
401 
409  virtual bool hasThisPointer(const void* thisPointer) const noexcept = 0;
410 
423  virtual returnType invoke(parameters... params) = 0;
424  };
425 } // End NameSpace EasyDelegate
426 #endif // _INCLUDE_EASYDELEGATE_DELEGATES_HPP_
MemberMethodPointer< classType, returnType, parameters...> MethodPointer
Helper typedef referring to a static function pointer that is compatible with this delegate...
Definition: delegates.hpp:194
A delegate of a class member method.
Definition: delegates.hpp:53
EASYDELEGATE_INLINE bool callsMethod(const StaticMethodPointer< otherReturn, otherParams...> methodPointer) const noexcept
Returns whether or not this MemberDelegate calls the given static method pointer. ...
Definition: delegates.hpp:265
EASYDELEGATE_INLINE bool hasThisPointer(const void *thisPointer) const noexcept
Returns whether or not this MemberDelegate calls against the given this pointer.
Definition: delegates.hpp:249
A type that can represent any delegate type, but it cannot be invoked without casting to a delegate t...
Definition: delegates.hpp:35
returnType(className::*)(parameters...) MemberMethodPointer
Helper typedef to a pointer of a class member method with the given signature.
Definition: types.hpp:69
EASYDELEGATE_INLINE bool hasSameMethodAs(const MemberDelegate< classType, returnType, parameters...> *other) const noexcept
Returns whether or not this MemberDelegate calls the same method as the other MemberDelegate.
Definition: delegates.hpp:280
returnType invoke(parameters...params)
Invoke the MemberDelegate.
Definition: delegates.hpp:228
classType * mThisPointer
A pointer to the this object.
Definition: delegates.hpp:329
EASYDELEGATE_INLINE bool hasSameThisPointerAs(const MemberDelegate< otherClass, otherReturn, otherParams...> *other) const noexcept
Returns whether or not this MemberDelegate calls against the same this pointer as the other...
Definition: delegates.hpp:324
ITypedDelegate(const bool &isMemberDelegate) noexcept
Constructor accepting a boolean.
Definition: delegates.hpp:369
returnType(*)(parameters...) StaticMethodPointer
Helper typedef to a pointer of a static method with the given signature.
Definition: types.hpp:65
EASYDELEGATE_INLINE bool hasThisPointer(const void *thisPointer) const noexcept
Returns whether or not this StaticDelegate calls against the given this pointer.
Definition: delegates.hpp:109
Include file containing the definitions for all exception types thrown by the EasyDelegate library...
An exception type that is thrown by the EasyDelegate library when any delegate type has attempted to ...
Definition: exceptions.hpp:51
EASYDELEGATE_INLINE bool hasSameThisPointerAs(const StaticDelegate< otherReturn, otherParams...> *other) const noexcept
Returns whether or not this StaticDelegate uses the same this pointer as the StaticDelegate we are ch...
Definition: delegates.hpp:177
const bool mIsMemberDelegate
A boolean representing whether or not this delegate is a member delegate.
Definition: delegates.hpp:40
EASYDELEGATE_INLINE bool hasSameMethodAs(const StaticDelegate< otherReturn, otherParams...> *other) const noexcept
Returns whether or not this StaticDelegate calls against the same method as the StaticDelegate we are...
Definition: delegates.hpp:155
StaticDelegate< returnType, parameters...> StaticDelegateType
Helper typedef for when building static delegates.
Definition: delegates.hpp:350
StaticMethodPointer< returnType, parameters...> StaticMethodPointerType
Helper typedef referring to a static function pointer.
Definition: delegates.hpp:356
EASYDELEGATE_INLINE bool hasSameThisPointerAs(const StaticDelegate< otherReturn, otherParams...> *other) const noexcept
Returns whether or not this MemberDelegate calls against the same this pointer as the StaticDelegate...
Definition: delegates.hpp:314
StaticMethodPointer< returnType, parameters...> MethodPointer
Helper typedef referring to a static method pointer that is compatible with this delegate.
Definition: delegates.hpp:65
Include file declaring various helper types used by the EasyDelegate library. They may also aid progr...
StaticDelegate(const StaticDelegate< returnType, parameters...> *other)
Standard copy constructor.
Definition: delegates.hpp:78
returnType ReturnType
Helper typedef referring to the return type of this delegate.
Definition: delegates.hpp:348
Namespace containing all EasyDelegate functionality.
Definition: deferredcallers.hpp:20
EASYDELEGATE_INLINE bool hasSameMethodAs(const StaticDelegate< returnType, parameters...> *other) const noexcept
Returns whether or not this StaticDelegate calls the same method as the StaticDelegate we are checkin...
Definition: delegates.hpp:134
Base delegate type.
Definition: delegates.hpp:23
#define EASYDELEGATE_INLINE
A preprocessor definition for a keyword that forces the inlining of a given method.
Definition: easydelegate.hpp:33
EASYDELEGATE_INLINE bool callsMethod(const typename ITypedDelegate< returnType, parameters...>::StaticMethodPointerType methodPointer) const noexcept
Returns whether or not this MemeberDelegate calls the given static method address.
Definition: delegates.hpp:273
A delegate of a static method.
Definition: delegates.hpp:61
MemberDelegate(const MethodPointer methodPointer, classType *thisPointer)
Constructor accepting a this pointer and a member function.
Definition: delegates.hpp:206
EASYDELEGATE_INLINE bool callsMethod(const MemberMethodPointer< otherClass, otherReturn, otherParams...> methodPointer) const noexcept
Returns whether or not this StaticDelegate calls the given class member method.
Definition: delegates.hpp:125
virtual returnType invoke(parameters...params)=0
Invoke the delegate with the given arguments and return a value, if any.
virtual bool hasThisPointer(const void *thisPointer) const noexcept=0
Returns whether or not this delegate calls against the given this pointer.
MemberDelegate(const MemberDelegate< classType, returnType, parameters...> *other)
Standard copy constructor.
Definition: delegates.hpp:213
virtual bool callsMethod(const StaticMethodPointerType methodPointer) const noexcept=0
Returns whether or not this delegate calls the given static method.
EASYDELEGATE_INLINE bool hasSameMethodAs(const MemberDelegate< otherClass, otherReturn, otherParams...> *other) const noexcept
Returns whether or not this StaticDelegate calls against the same method as the MemberDelegate we are...
Definition: delegates.hpp:144
StaticDelegate(const MethodPointer methodPointer)
Constructor accepting a static method.
Definition: delegates.hpp:72
IDelegate(const bool &isMemberDelegate) noexcept
Constructor accepting a boolean.
Definition: delegates.hpp:49
bool callsMethod(const MemberDelegateFuncPtr< className > methodPointer) const noexcept
Returns whether or not this delegate calls the given class member method address. ...
Definition: delegates.hpp:391
EASYDELEGATE_INLINE bool callsMethod(const MethodPointer methodPointer) const noexcept
Returns whether or not this MemberDelegate calls the given class member method pointer.
Definition: delegates.hpp:256
EASYDELEGATE_INLINE bool hasSameMethodAs(const MemberDelegate< otherClass, otherReturn, otherParams...> *other) const noexcept
Returns whether or not this MemberDelegate calls the same method as the other of a deferring signatur...
Definition: delegates.hpp:292
returnType(classType::*)(parameters...) MemberDelegateFuncPtr
Helper typedef referring to a class member method pointer.
Definition: delegates.hpp:360
An exception type that is thrown by the EasyDelegate library when a member delegate type has attempte...
Definition: exceptions.hpp:31
returnType invoke(parameters...params)
Invokes the StaticDelegate.
Definition: delegates.hpp:92
EASYDELEGATE_INLINE bool callsMethod(const MethodPointer methodPointer) const noexcept
Returns whether or not this StaticDelegate calls the given static method.
Definition: delegates.hpp:116
EASYDELEGATE_INLINE bool hasSameThisPointerAs(const MemberDelegate< otherClass, otherReturn, otherParams...> *other) const noexcept
Returns whether or not this StaticDelegate uses the same this pointer as the MemberDelegate we are ch...
Definition: delegates.hpp:166
EASYDELEGATE_INLINE bool hasSameMethodAs(const StaticDelegate< otherReturn, otherParams...> *other) const noexcept
Returns whether or not this MemberDelegate calls the same method as the StaticDelegate type...
Definition: delegates.hpp:303