summaryrefslogtreecommitdiffstats
path: root/src/Event.cpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-12-29 13:09:51 +0100
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2018-01-13 03:39:33 +0100
commitfa32e7a9f46c7da39a527d0ebc049e3720068d8e (patch)
tree8d5910c7eb87f7ef338bf68e75aeb1a98f931ac9 /src/Event.cpp
parentRemoved previous implementation of event-system (diff)
downloadAltCraft-fa32e7a9f46c7da39a527d0ebc049e3720068d8e.tar
AltCraft-fa32e7a9f46c7da39a527d0ebc049e3720068d8e.tar.gz
AltCraft-fa32e7a9f46c7da39a527d0ebc049e3720068d8e.tar.bz2
AltCraft-fa32e7a9f46c7da39a527d0ebc049e3720068d8e.tar.lz
AltCraft-fa32e7a9f46c7da39a527d0ebc049e3720068d8e.tar.xz
AltCraft-fa32e7a9f46c7da39a527d0ebc049e3720068d8e.tar.zst
AltCraft-fa32e7a9f46c7da39a527d0ebc049e3720068d8e.zip
Diffstat (limited to 'src/Event.cpp')
-rw-r--r--src/Event.cpp49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/Event.cpp b/src/Event.cpp
index 00f67c0..c857947 100644
--- a/src/Event.cpp
+++ b/src/Event.cpp
@@ -1 +1,50 @@
#include "Event.hpp"
+
+std::list<EventListener*> EventSystem::listeners;
+std::mutex EventSystem::listenersMutex;
+
+EventListener::EventListener() {
+ std::lock_guard<std::mutex> listenersLock(EventSystem::listenersMutex);
+ EventSystem::listeners.push_back(this);
+}
+
+EventListener::~EventListener() {
+ std::lock_guard<std::mutex> listenersLock(EventSystem::listenersMutex);
+ EventSystem::listeners.remove(this);
+}
+
+void EventListener::HandleEvent() {
+ std::lock_guard<std::mutex> lock(eventsQueueMutex);
+ std::lock_guard<std::mutex> lockHandlers(handlersMutex);
+ Event event = events.front();
+ events.pop();
+ if (handlers[event.id]) {
+ handlers[event.id](event);
+ }
+}
+
+void EventListener::HandleAllEvents() {
+ std::lock_guard<std::mutex> lock(eventsQueueMutex);
+ std::lock_guard<std::mutex> lockHandlers(handlersMutex);
+ while (!events.empty()) {
+ Event event = events.front();
+ events.pop();
+ if (handlers[event.id]) {
+ handlers[event.id](event);
+ }
+ }
+}
+
+bool EventListener::NotEmpty() {
+ std::lock_guard<std::mutex> lock(eventsQueueMutex);
+ return !events.empty();
+}
+
+void EventListener::WaitEvent() {
+ eventsQueueMutex.lock();
+ while (events.empty()) {
+ eventsQueueMutex.unlock();
+ eventsQueueMutex.lock();
+ }
+ eventsQueueMutex.unlock();
+} \ No newline at end of file