aboutsummaryrefslogtreecommitdiff
path: root/mwe/events/include/eventHandler.h
diff options
context:
space:
mode:
Diffstat (limited to 'mwe/events/include/eventHandler.h')
-rw-r--r--mwe/events/include/eventHandler.h39
1 files changed, 39 insertions, 0 deletions
diff --git a/mwe/events/include/eventHandler.h b/mwe/events/include/eventHandler.h
new file mode 100644
index 0000000..c17c586
--- /dev/null
+++ b/mwe/events/include/eventHandler.h
@@ -0,0 +1,39 @@
+#pragma once
+#include "event.h"
+#include <iostream>
+#include <functional>
+template<typename EventType>
+using EventHandler = std::function<void(const EventType& e)>;
+class EventHandlerWrapperInterface {
+public:
+ void Exec(const Event& e)
+ {
+ Call(e);
+ }
+
+ virtual std::string GetType() const = 0;
+
+private:
+ virtual void Call(const Event& e) = 0;
+};
+
+template<typename EventType>
+class EventHandlerWrapper : public EventHandlerWrapperInterface {
+public:
+ explicit EventHandlerWrapper(const EventHandler<EventType>& handler)
+ : m_handler(handler)
+ , m_handlerType(m_handler.target_type().name()) {};
+
+private:
+ void Call(const Event& e) override
+ {
+ if (e.GetEventType() == EventType::GetStaticEventType()) {
+ m_handler(static_cast<const EventType&>(e));
+ }
+ }
+
+ std::string GetType() const override { return m_handlerType; }
+
+ EventHandler<EventType> m_handler;
+ const std::string m_handlerType;
+};