aboutsummaryrefslogtreecommitdiff
path: root/mwe/events/include/eventManager.h
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-10-23 21:13:28 +0200
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-10-23 21:13:28 +0200
commit51c8a51b53a850265955a3e4bc45b40ad3f8c477 (patch)
treec52258a911e52d0a5469942e0a11e373e92af0a9 /mwe/events/include/eventManager.h
parent94d591b69c45470480db477bc4000ef35019e18b (diff)
custom commit poc working
Diffstat (limited to 'mwe/events/include/eventManager.h')
-rw-r--r--mwe/events/include/eventManager.h63
1 files changed, 51 insertions, 12 deletions
diff --git a/mwe/events/include/eventManager.h b/mwe/events/include/eventManager.h
index 93def36..709796a 100644
--- a/mwe/events/include/eventManager.h
+++ b/mwe/events/include/eventManager.h
@@ -2,17 +2,56 @@
#include <unordered_map>
#include <memory>
#include "event.h"
+#include "keyCodes.h"
#include "eventHandler.h"
-using EventType = int;
-using EventId = int;
-class EventManager{
- public:
-
- void subscribe(EventId eventId,std::unique_ptr<IEventHandlerWrapper>&& handler);
- void unsubscribe(EventId eventId, const std::string& handlerName);
- void dispatchEvents();
- void triggerEvent(EventId eventId);
- void queueEvent();
- private:
- std::unordered_map<EventType, std::vector<std::unique_ptr<IEventHandlerWrapper>>> m_subscribers;
+#include <vector>
+// using EventType = std::uint32_t;
+// using EventId = std::uint64_t;
+
+class EventManager {
+public:
+ EventManager(const EventManager&) = delete;
+ const EventManager& operator=(const EventManager&) = delete;
+ static EventManager& getInstance() {
+ static EventManager instance;
+ return instance;
+ }
+
+ void shutdown();
+ void subscribe(int eventType, std::unique_ptr<IEventHandlerWrapper>&& handler, int eventId);
+ void unsubscribe(int eventType, const std::string& handlerName, int eventId);
+ void triggerEvent(const Event& event_, int eventId);
+ void queueEvent(std::unique_ptr<Event>&& event_, int eventId);
+ void dispatchEvents();
+
+private:
+ EventManager() = default;
+ std::vector<std::pair<std::unique_ptr<Event>, int>> m_eventsQueue;
+ std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>> m_subscribers;
+ std::unordered_map<int, std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>> m_subscribersByEventId;
};
+
+
+template<typename EventType>
+inline void subscribe(const EventHandler<EventType>& callback, int eventId = 0, const bool unsubscribeOnSuccess = false)
+{
+ std::unique_ptr<IEventHandlerWrapper> handler = std::make_unique<EventHandlerWrapper<EventType>>(callback, unsubscribeOnSuccess);
+ EventManager::getInstance().subscribe(EventType::getStaticEventType(), std::move(handler), eventId);
+}
+
+template<typename EventType>
+inline void unsubscribe(const EventHandler<EventType>& callback, int eventId = 0)
+{
+ const std::string handlerName = callback.target_type().name();
+ EventManager::getInstance().unsubscribe(EventType::getStaticEventType(), handlerName, eventId);
+}
+
+inline void triggerEvent(const Event& triggeredEvent, int eventId = 0)
+{
+ EventManager::getInstance().triggerEvent(triggeredEvent, eventId);
+}
+
+inline void queueEvent(std::unique_ptr<Event>&& queuedEvent, int eventId = 0)
+{
+ EventManager::getInstance().queueEvent(std::forward<std::unique_ptr<Event>>(queuedEvent), eventId);
+}