aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api/EventHandler.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/api/EventHandler.h')
-rw-r--r--src/crepe/api/EventHandler.h38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/crepe/api/EventHandler.h b/src/crepe/api/EventHandler.h
new file mode 100644
index 0000000..eea1c79
--- /dev/null
+++ b/src/crepe/api/EventHandler.h
@@ -0,0 +1,38 @@
+#pragma once
+#include <functional>
+#include <iostream>
+#include <typeindex>
+#include "Event.h"
+
+template <typename EventType>
+using EventHandler = std::function<bool(const EventType & e)>;
+
+class IEventHandlerWrapper {
+public:
+ virtual ~IEventHandlerWrapper() = default;
+
+ bool exec(const Event & e);
+
+ virtual std::string get_type() const = 0;
+
+private:
+ virtual bool call(const Event & e) = 0;
+};
+
+template <typename EventType>
+class EventHandlerWrapper : public IEventHandlerWrapper {
+public:
+ explicit EventHandlerWrapper(const EventHandler<EventType> & handler)
+ : m_handler(handler), m_handler_type(m_handler.target_type().name()) {
+ }
+
+private:
+ bool call(const Event & e) override {
+ return m_handler(static_cast<const EventType &>(e));
+ }
+
+ std::string get_type() const override { return m_handler_type; }
+
+ EventHandler<EventType> m_handler;
+ const std::string m_handler_type;
+};