EasyDelegate  2.0
Delegate and deferred callers for C++11.
delegateset.hpp
Go to the documentation of this file.
1 
12 #ifndef _INCLUDE_EASYDELEGATE_DELEGATESET_HPP_
13 #define _INCLUDE_EASYDELEGATE_DELEGATESET_HPP_
14 
15 #include <set>
16 #include <unordered_set>
17 
18 #include "types.hpp"
19 #include "delegates.hpp"
20 #include "deferredcallers.hpp"
21 
22 namespace EasyDelegate
23 {
33  template <typename returnType, typename... parameters>
34  class DelegateSet : public std::set<ITypedDelegate<returnType, parameters...> *>
35  {
36  public:
38  typedef returnType (*delegateFuncPtr)(parameters...);
39 
40  public:
42  typedef StaticDelegate<returnType, parameters...> StaticDelegateType;
44  template <typename classType>
45  using MemberDelegateType = MemberDelegate<classType, returnType, parameters...>;
47  typedef returnType ReturnType;
49  typedef ITypedDelegate<returnType, parameters...> StoredDelegateType;
50 
52  typedef returnType(*StaticDelegateFuncPtr)(parameters...);
54  template <typename classType>
55  using MemberDelegateFuncPtr = returnType(classType::*)(parameters...);
56 
57  #ifndef EASYDELEGATE_NO_DEFERRED_CALLING
58 
62  template <typename classType>
63  using DeferredMemberCallerType = DeferredMemberCaller<classType, returnType, parameters...>;
64 
69  typedef DeferredStaticCaller<returnType, parameters...> DeferredStaticCallerType;
70  #endif
71 
73  typedef std::set<returnType> ReturnSetType;
74 
77  {
78  for (auto it = this->begin(); it != this->end(); it++)
79  delete *it;
80  }
81 
94  EASYDELEGATE_INLINE void invoke(parameters... params) const
95  {
96  for (auto it = this->begin(); it != this->end(); it++)
97  (*it)->invoke(params...);
98  }
99 
113  EASYDELEGATE_INLINE void invoke(std::set<returnType>& out, parameters... params) const
114  {
115  for (auto it = this->begin(); it != this->end(); it++)
116  out.insert(out.end(), (*it)->invoke(params...));
117  }
118 
126  {
127  this->insert(this->end(), delegateInstance);
128  }
129 
137  {
138  this->push_back(delegateInstance);
139  }
140 
153  template <typename className>
154  EASYDELEGATE_INLINE void removeDelegateByMethod(const MemberDelegateFuncPtr<className> method, const bool& deleteInstances=true, std::unordered_set<StoredDelegateType *>* out=NULL)
155  {
156  for (auto it = this->begin(); it != this->end(); it++)
157  {
158  StoredDelegateType* current = *it;
159 
160  if (current->callsMethod(method))
161  {
162  if (deleteInstances)
163  delete current;
164  else if (out)
165  out->insert(out->end(), current);
166 
167  this->erase(current);
168  }
169  }
170  }
171 
184  void removeDelegateByMethod(StaticDelegateFuncPtr methodPointer, const bool& deleteInstances=true, std::unordered_set<StoredDelegateType *>* out=NULL)
185  {
186  for (auto it = this->begin(); it != this->end(); it++)
187  {
188  StoredDelegateType* current = *it;
189 
190  if (current->callsMethod(methodPointer))
191  {
192  if (deleteInstances)
193  delete current;
194  else if (out)
195  out->insert(out->end(), current);
196 
197  this->erase(current);
198  }
199  }
200  }
201 
214  void removeDelegateByThisPointer(const void* thisPtr, const bool& deleteInstances=true, std::unordered_set<StoredDelegateType *>* out=NULL)
215  {
216  for (auto it = this->begin(); it != this->end(); it++)
217  {
218  StoredDelegateType* current = *it;
219 
220  if (current->mIsMemberDelegate && current->hasThisPointer(thisPtr))
221  {
222  if (deleteInstances)
223  delete current;
224  else if (out)
225  out->insert(out->end(), current);
226 
227  this->erase(current);
228  }
229  }
230  }
231 
240  StoredDelegateType* removeDelegate(StoredDelegateType* instance, const bool& deleteInstance=true)
241  {
242  for (auto it = this->begin(); it != this->end(); it++)
243  {
244  StoredDelegateType* current = *it;
245 
246  if (current == instance)
247  {
248  if (deleteInstance)
249  delete current;
250 
251  this->erase(current);
252 
253  if (deleteInstance)
254  return NULL;
255 
256  return current;
257  }
258  }
259 
260  return NULL;
261  }
262  };
263 }
264 #endif // _INCLUDE_EASYDELEGATE_DELEGATESET_HPP_
A delegate of a class member method.
Definition: delegates.hpp:53
void removeDelegateByThisPointer(const void *thisPtr, const bool &deleteInstances=true, std::unordered_set< StoredDelegateType * > *out=NULL)
Removes a all MemberDelegate types from the set that have a given 'this' pointer address to call agai...
Definition: delegateset.hpp:214
EASYDELEGATE_INLINE void push_back(StoredDelegateType *delegateInstance)
Pushes a delegate instance to the end of the set.
Definition: delegateset.hpp:125
std::set< returnType > ReturnSetType
Helper typedef to an std::set that is compatible with the return types of delegates stored here...
Definition: delegateset.hpp:73
~DelegateSet(void)
Standard destructor.
Definition: delegateset.hpp:76
EASYDELEGATE_INLINE void operator+=(StoredDelegateType *delegateInstance)
Pushes a delegate instance to the end of the set.
Definition: delegateset.hpp:136
Include file declaring the deferred caller types for use in systems where deferred calls is a necessi...
const bool mIsMemberDelegate
A boolean representing whether or not this delegate is a member delegate.
Definition: delegates.hpp:40
Include file declaring various helper types used by the EasyDelegate library. They may also aid progr...
DeferredStaticCaller< returnType, parameters...> DeferredStaticCallerType
A helper typedef to a DeferredStaticCaller matches the signatures stored in this delegate set...
Definition: delegateset.hpp:69
Namespace containing all EasyDelegate functionality.
Definition: deferredcallers.hpp:20
returnType(* StaticDelegateFuncPtr)(parameters...)
Helper typedef referring to a static function pointer.
Definition: delegateset.hpp:52
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
A delegate of a static method.
Definition: delegates.hpp:61
returnType ReturnType
Helper typedef for when wanting the return type of this set.
Definition: delegateset.hpp:47
EASYDELEGATE_INLINE void invoke(parameters...params) const
Invoke all delegates in the set, ignoring return values.
Definition: delegateset.hpp:94
A deferred caller type for class member methods.
Definition: deferredcallers.hpp:66
virtual bool hasThisPointer(const void *thisPointer) const noexcept=0
Returns whether or not this delegate calls against the given this pointer.
StoredDelegateType * removeDelegate(StoredDelegateType *instance, const bool &deleteInstance=true)
Removes a given delegate by its address.
Definition: delegateset.hpp:240
virtual bool callsMethod(const StaticMethodPointerType methodPointer) const noexcept=0
Returns whether or not this delegate calls the given static method.
EASYDELEGATE_INLINE void removeDelegateByMethod(const MemberDelegateFuncPtr< className > method, const bool &deleteInstances=true, std::unordered_set< StoredDelegateType * > *out=NULL)
Removes all delegates from the set that have the given class member method address for its method...
Definition: delegateset.hpp:154
void removeDelegateByMethod(StaticDelegateFuncPtr methodPointer, const bool &deleteInstances=true, std::unordered_set< StoredDelegateType * > *out=NULL)
Removes all delegates from the set that have the given static method address for it's method...
Definition: delegateset.hpp:184
EASYDELEGATE_INLINE void invoke(std::set< returnType > &out, parameters...params) const
Invoke all delegates in the set, storing return values in out.
Definition: delegateset.hpp:113
ITypedDelegate< returnType, parameters...> StoredDelegateType
Helper typedef to construct the StaticDelegate signature from the template.
Definition: delegateset.hpp:49
returnType(classType::*)(parameters...) MemberDelegateFuncPtr
Helper typedef referring to a member function pointer.
Definition: delegateset.hpp:55
A set of delegate instances that provides helper methods to invoke all contained delegates.
Definition: delegateset.hpp:34
StaticDelegate< returnType, parameters...> StaticDelegateType
Helper typedef for when building static delegates for this set.
Definition: delegateset.hpp:42
returnType(* delegateFuncPtr)(parameters...)
Helper typedef to construct the function pointer signature from the template.
Definition: delegateset.hpp:38
Include file declaring various delegate types.
A deferred caller type for static methods.
Definition: deferredcallers.hpp:76