20 #ifndef _ANCH_EVENTS_EVENT_BUS_H_ 21 #define _ANCH_EVENTS_EVENT_BUS_H_ 28 #include "events/observer.hpp" 29 #include "lessPtrCompare.hpp" 30 #include "singleton.hpp" 45 template<
typename Event>
52 std::mutex _eventMutex;
55 std::mutex _queueMutex;
61 std::queue<Event> _events;
73 EventBus(): _eventMutex(), _queueMutex(), _thread(NULL), _events(), _observers() {
99 std::lock_guard<std::mutex> lock(_eventMutex);
100 bool added = _observers.insert(&observer).second;
119 std::lock_guard<std::mutex> lock(_eventMutex);
120 _observers.erase(&observer);
138 std::lock_guard<std::mutex> lock(_eventMutex);
140 observer->notify(event);
159 std::lock_guard<std::mutex> lock(_queueMutex);
160 bool empty = _events.empty();
163 if(_thread != NULL) {
184 void processEvents() noexcept {
188 Event
event = _events.front();
190 empty = _events.empty();
191 _queueMutex.unlock();
204 #endif // _ANCH_EVENTS_EVENT_BUS_H_ An observer interface of the observers/observable design pattern.
Definition: observer.hpp:39
void removeObserver(anch::events::Observer< Event > &observer) noexcept
Definition: eventBus.hpp:118
void scheduleDeferred(const Event &event) noexcept
Definition: eventBus.hpp:158
static void FireEvent(const Event &event) noexcept
Definition: eventBus.hpp:149
static bool AddObserver(anch::events::Observer< Event > &observer) noexcept
Definition: eventBus.hpp:109
void fireEvent(const Event &event) noexcept
Definition: eventBus.hpp:137
A class which manage global events firing and events QoS.
Definition: eventBus.hpp:46
AnCH framework base namespace.
Definition: base64.hpp:28
static void RemoveObserver(anch::events::Observer< Event > &observer) noexcept
Definition: eventBus.hpp:128
static EventBus< Event > & getInstance()
Definition: singleton.hpp:42
Less comparator based on object address.
Definition: lessPtrCompare.hpp:35
static void ScheduleDeferred(const Event &event) noexcept
Definition: eventBus.hpp:176
Meyers' singleton implemtation.
Definition: singleton.hpp:35
bool addObserver(anch::events::Observer< Event > &observer) noexcept
Definition: eventBus.hpp:98