diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2018-01-12 12:48:42 +0100 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2018-01-13 03:39:34 +0100 |
commit | b009754d1bc09d9cb26b35e41146029491044b6d (patch) | |
tree | 2226bbd839023002dad3132ec565c99c1b1220a8 /src/Event.hpp | |
parent | Fixed some incorrect types for events (diff) | |
download | AltCraft-b009754d1bc09d9cb26b35e41146029491044b6d.tar AltCraft-b009754d1bc09d9cb26b35e41146029491044b6d.tar.gz AltCraft-b009754d1bc09d9cb26b35e41146029491044b6d.tar.bz2 AltCraft-b009754d1bc09d9cb26b35e41146029491044b6d.tar.lz AltCraft-b009754d1bc09d9cb26b35e41146029491044b6d.tar.xz AltCraft-b009754d1bc09d9cb26b35e41146029491044b6d.tar.zst AltCraft-b009754d1bc09d9cb26b35e41146029491044b6d.zip |
Diffstat (limited to '')
-rw-r--r-- | src/Event.hpp | 32 |
1 files changed, 15 insertions, 17 deletions
diff --git a/src/Event.hpp b/src/Event.hpp index 0f60d1a..4cdcdc3 100644 --- a/src/Event.hpp +++ b/src/Event.hpp @@ -63,9 +63,12 @@ public: class EventListener { friend class EventSystem; using HandlerType = std::function<void(const Event&)>; - std::queue<Event> events; std::map<size_t, HandlerType> handlers; - std::recursive_mutex mutex; + std::recursive_mutex handlersMutex; + std::queue<Event> events; + std::recursive_mutex eventsMutex; + std::queue<Event> rawEvents; + std::recursive_mutex rawEventsMutex; public: EventListener(); @@ -77,13 +80,13 @@ public: bool NotEmpty(); - void WaitEvent(); - void RegisterHandler(size_t eventId, const HandlerType &data); void RegisterHandler(const char *eventId, const HandlerType & data) { RegisterHandler(StrHash(eventId), data); } + + void PollEvents(); }; class EventSystem { @@ -96,33 +99,28 @@ public: static void PushEvent(size_t eventId, T data) { Event event(eventId, data); + std::lock_guard<std::recursive_mutex> lock(EventSystem::listenersMutex); for (auto& listener : listeners) { - std::lock_guard<std::recursive_mutex> lock(EventSystem::listenersMutex); - - auto it = listener->handlers.find(eventId); - if (it == listener->handlers.end()) - continue; - - listener->events.push(event); + std::lock_guard<std::recursive_mutex> rawEventLock (listener->rawEventsMutex); + listener->rawEvents.push(event); } } template <typename T> static void DirectEventCall(size_t eventId, T data) { Event event(eventId, data); - + std::lock_guard<std::recursive_mutex> lock(EventSystem::listenersMutex); for (auto & listener : listeners) { - std::lock_guard<std::recursive_mutex> lock(EventSystem::listenersMutex); - + std::lock_guard<std::recursive_mutex> handlersLock (listener->handlersMutex); auto it = listener->handlers.find(eventId); if (it == listener->handlers.end()) - continue; + continue; it->second(event); } } }; -#define PUSH_EVENT(eventName, data) EventSystem::PushEvent(StrHash(eventName),data); LOG(INFO)<<"PUSH_EVENT "<<eventName; +#define PUSH_EVENT(eventName, data) EventSystem::PushEvent(StrHash(eventName),data) //; LOG(INFO)<<"PUSH_EVENT "<<eventName; -#define DIRECT_EVENT_CALL(eventName,data) EventSystem::DirectEventCall(StrHash(eventName),data); LOG(INFO)<<"DIRECT_CALL "<<eventName;
\ No newline at end of file +#define DIRECT_EVENT_CALL(eventName,data) EventSystem::DirectEventCall(StrHash(eventName),data) //; LOG(INFO)<<"DIRECT_CALL "<<eventName;
\ No newline at end of file |