GlPortal Class Reference  0.1.1
Source code documentation of the free and open 3D action puzzle game.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EventDispatcher.hpp
Go to the documentation of this file.
1 #ifndef DISPATCHER_HPP
2 #define DISPATCHER_HPP
3 
4 #include <functional>
5 #include <list>
6 #include <unordered_map>
7 #include <utility>
8 
10 
11 namespace radix {
12 
14 public:
15  using Callback = std::function<void(const Event&)>;
16  using CallbackList = std::list<Callback>;
17  using ObserverMap = std::unordered_map<EventType, CallbackList>;
18  using CallbackPointer = std::pair<EventType, CallbackList::iterator>;
19 
20  class CallbackHolder final : public CallbackPointer {
21  private:
23 
24  public:
26  dispatcher(nullptr) {
27  }
28 
29  CallbackHolder(EventDispatcher *dispatcher, EventType &et, CallbackList::iterator &it) :
30  CallbackPointer(et, it),
31  dispatcher(dispatcher) {
32  }
33 
34  // Handy call operator to directly call the callback
35  void operator()(const Event &e) {
36  (*second)(e);
37  }
38 
39  // No copy
40  CallbackHolder(CallbackHolder&) = delete;
42 
43  // Allow movement
45  CallbackPointer(o),
46  dispatcher(o.dispatcher) {
47  o.dispatcher = nullptr;
48  }
50  CallbackPointer::operator=(o);
51  dispatcher = o.dispatcher;
52  o.dispatcher = nullptr;
53  return *this;
54  }
55 
57  if (dispatcher) {
58  dispatcher->removeObserver(*this);
59  }
60  }
61  };
62 
63 private:
66 
67 public:
69 
70 
71  /* Observers */
72  CallbackHolder addObserver(EventType type, const Callback &method);
73  CallbackPointer addObserverRaw(EventType type, const Callback &method);
74 
75  void removeObserver(const CallbackPointer &ptr);
76  template<class... CPTypes>
77  void removeObserver(CallbackPointer cb0, const CPTypes & ...cbN) {
78  removeObserver(cb0);
79  removeObserver(cbN...);
80  }
81 
82 
83  /* Wildcard observers */
84  CallbackHolder observeWildcard(const Callback &method);
86 
87  void unobserveWildcard(const CallbackPointer &ptr);
88  // Templated unobserveWildcard intentionally unimplemented: wildcard is a debug feature.
89 
90 
91  /* Event dispatching */
92  void dispatch(const Event &event);
93 };
94 
95 } /* namespace radix */
96 #endif /* DISPATCHER_HPP */
Definition: GameController.hpp:7
~CallbackHolder()
Definition: EventDispatcher.hpp:56
CallbackHolder(CallbackHolder &&o)
Definition: EventDispatcher.hpp:44
std::function< void(const Event &)> Callback
Definition: EventDispatcher.hpp:15
Definition: Event.hpp:15
EventDispatcher * dispatcher
Definition: EventDispatcher.hpp:22
std::list< Callback > CallbackList
Definition: EventDispatcher.hpp:16
void dispatch(const Event &event)
Definition: EventDispatcher.cpp:8
Definition: EventDispatcher.hpp:13
CallbackHolder addObserver(EventType type, const Callback &method)
Definition: EventDispatcher.cpp:22
CallbackHolder()
Definition: EventDispatcher.hpp:25
void removeObserver(CallbackPointer cb0, const CPTypes &...cbN)
Definition: EventDispatcher.hpp:77
CallbackHolder(EventDispatcher *dispatcher, EventType &et, CallbackList::iterator &it)
Definition: EventDispatcher.hpp:29
CallbackPointer observeWildcardRaw(const Callback &method)
Definition: EventDispatcher.cpp:71
CallbackHolder & operator=(CallbackHolder &)=delete
void removeObserver(const CallbackPointer &ptr)
Definition: EventDispatcher.cpp:56
std::unordered_map< EventType, CallbackList > ObserverMap
Definition: EventDispatcher.hpp:17
EventDispatcher()
Definition: EventDispatcher.cpp:5
ObserverMap observerMap
Definition: EventDispatcher.hpp:64
void unobserveWildcard(const CallbackPointer &ptr)
Definition: EventDispatcher.cpp:76
Definition: EventDispatcher.hpp:20
uint32_t EventType
Definition: Event.hpp:11
std::pair< EventType, CallbackList::iterator > CallbackPointer
Definition: EventDispatcher.hpp:18
void operator()(const Event &e)
Definition: EventDispatcher.hpp:35
CallbackPointer addObserverRaw(EventType type, const Callback &method)
Definition: EventDispatcher.cpp:39
CallbackList wildcardObservers
Definition: EventDispatcher.hpp:65
CallbackHolder & operator=(CallbackHolder &&o)
Definition: EventDispatcher.hpp:49
CallbackHolder observeWildcard(const Callback &method)
Definition: EventDispatcher.cpp:65