All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
CallbackCollection.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include <algorithm>
6 #include <set>
7 #include "Types.h"
8 #include "ICallback.h"
9 
10 namespace Eegeo
11 {
12  namespace Helpers
13  {
14  template <typename TCallback>
16  {
17  protected:
18  std::set<TCallback*> m_callbacks;
19 
20  public:
21  void AddCallback(TCallback& callback)
22  {
23  TCallback* pCallback = &callback;
24  Eegeo_ASSERT(std::find(m_callbacks.begin(), m_callbacks.end(), pCallback) == m_callbacks.end());
25  m_callbacks.insert(pCallback);
26  }
27 
28  void RemoveCallback(TCallback& callback)
29  {
30  TCallback* pCallback = &callback;
31  Eegeo_ASSERT(std::find(m_callbacks.begin(), m_callbacks.end(), pCallback) != m_callbacks.end());
32  m_callbacks.erase(pCallback);
33  }
34  };
35 
36  class CallbackCollection0 : public CallbackCollectionBase<ICallback0>
37  {
38  public:
39  void ExecuteCallbacks() const
40  {
41  for(std::set<ICallback0*>::iterator it = this->m_callbacks.begin();
42  it != this->m_callbacks.end();
43  ++ it)
44  {
45  ICallback0& callback = **it;
46  callback();
47  }
48  }
49  };
50 
51  template <typename TItem>
52  class CallbackCollection1 : public CallbackCollectionBase<ICallback1<TItem> >
53  {
54  public:
55  void ExecuteCallbacks(TItem& item) const
56  {
57  for(typename std::set<ICallback1<TItem>*>::iterator it = this->m_callbacks.begin();
58  it != this->m_callbacks.end();
59  ++ it)
60  {
61  ICallback1<TItem>& callback = **it;
62  callback(item);
63  }
64  }
65  };
66 
67  template <typename TItem1, typename TItem2>
68  class CallbackCollection2 : public CallbackCollectionBase<ICallback2<TItem1, TItem2> >
69  {
70  public:
71  void ExecuteCallbacks(TItem1& item1, TItem2& item2) const
72  {
73  for(typename std::set<ICallback2<TItem1, TItem2>*>::iterator it = this->m_callbacks.begin();
74  it != this->m_callbacks.end();
75  ++ it)
76  {
77  ICallback2<TItem1, TItem2>& callback = **it;
78  callback(item1, item2);
79  }
80  }
81  };
82 
83  template <typename TItem1, typename TItem2, typename TItem3>
84  class CallbackCollection3 : public CallbackCollectionBase<ICallback3<TItem1, TItem2, TItem3> >
85  {
86  public:
87  void ExecuteCallbacks(TItem1& item1, TItem2& item2, TItem3& item3) const
88  {
89  for(typename std::set<ICallback3<TItem1, TItem2, TItem3>*>::iterator it = this->m_callbacks.begin();
90  it != this->m_callbacks.end();
91  ++ it)
92  {
93  ICallback3<TItem1, TItem2, TItem3>& callback = **it;
94  callback(item1, item2, item3);
95  }
96  }
97  };
98  }
99 }