summaryrefslogtreecommitdiffstats
path: root/src/core/Event.cpp
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-07-24 16:52:24 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-07-24 16:52:24 +0200
commit6a00886804c53883d919f008f6ec47a574d86607 (patch)
tree2dbe9fdb0a0d5f068770d49a3c960d36958f27d5 /src/core/Event.cpp
parent2017-07-21 (diff)
downloadAltCraft-6a00886804c53883d919f008f6ec47a574d86607.tar
AltCraft-6a00886804c53883d919f008f6ec47a574d86607.tar.gz
AltCraft-6a00886804c53883d919f008f6ec47a574d86607.tar.bz2
AltCraft-6a00886804c53883d919f008f6ec47a574d86607.tar.lz
AltCraft-6a00886804c53883d919f008f6ec47a574d86607.tar.xz
AltCraft-6a00886804c53883d919f008f6ec47a574d86607.tar.zst
AltCraft-6a00886804c53883d919f008f6ec47a574d86607.zip
Diffstat (limited to 'src/core/Event.cpp')
-rw-r--r--src/core/Event.cpp76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/core/Event.cpp b/src/core/Event.cpp
new file mode 100644
index 0000000..10b2eaa
--- /dev/null
+++ b/src/core/Event.cpp
@@ -0,0 +1,76 @@
+#include <core/Event.hpp>
+#include <easylogging++.h>
+
+std::queue <Event> EventAgregator::eventsToHandle;
+std::mutex EventAgregator::queueMutex;
+bool EventAgregator::isStarted = false;
+std::vector<EventListener*> EventAgregator::listeners;
+std::mutex EventAgregator::listenersMutex;
+
+void EventAgregator::EventHandlingLoop() {
+ while (true) {
+ queueMutex.lock();
+ if (!eventsToHandle.empty()) {
+ auto queue = eventsToHandle;
+ while (!eventsToHandle.empty())
+ eventsToHandle.pop();
+ queueMutex.unlock();
+
+ while (!queue.empty()) {
+ auto event = queue.front();
+ listenersMutex.lock();
+ for (auto& listener : listeners) {
+ LOG(INFO)<<"Listener notified about event";
+ listener->PushEvent(event);
+ }
+ listenersMutex.unlock();
+ queue.pop();
+ }
+
+ queueMutex.lock();
+ }
+ queueMutex.unlock();
+ }
+}
+
+void EventAgregator::RegisterListener(EventListener &listener) {
+ listenersMutex.lock();
+ LOG(INFO)<<"Registered handler "<<&listener;
+ listeners.push_back(&listener);
+ listenersMutex.unlock();
+}
+
+void EventAgregator::UnregisterListener(EventListener &listener) {
+ listenersMutex.lock();
+ LOG(INFO)<<"Unregistered handler "<<&listener;
+ listeners.erase(std::find(listeners.begin(), listeners.end(), &listener));
+ listenersMutex.unlock();
+}
+
+
+
+EventListener::EventListener() {
+ EventAgregator::RegisterListener(*this);
+}
+
+EventListener::~EventListener() {
+ EventAgregator::UnregisterListener(*this);
+}
+
+void EventListener::PushEvent(Event event) {
+ eventsMutex.lock();
+ LOG(INFO)<<"Pushed event to queue";
+ events.push(event);
+ eventsMutex.unlock();
+}
+
+/*void EventListener::RegisterHandler(EventType type, std::function<void(void*)> handler) {
+ handlers[type] = handler;
+}*/
+
+bool EventListener::IsEventsQueueIsNotEmpty() {
+ eventsMutex.lock();
+ bool value = !events.empty();
+ eventsMutex.unlock();
+ return value;
+} \ No newline at end of file