aboutsummaryrefslogtreecommitdiff
path: root/mwe/events/include/eventHandler.h
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-31 18:41:30 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-31 18:41:30 +0100
commit8e3367b186e60eb1e33bf58a066823cb00a7566e (patch)
treec4038a31993767276efec5fa1b1a37dff3b79465 /mwe/events/include/eventHandler.h
parentb7df77d6cc26cb9ee46891d7108f01734b3104dd (diff)
parent35ef3ba91ce9e00466508f2388f4c1dd2321b505 (diff)
Merge branch 'master' into poc/audio-miniaudio
Diffstat (limited to 'mwe/events/include/eventHandler.h')
-rw-r--r--mwe/events/include/eventHandler.h46
1 files changed, 46 insertions, 0 deletions
diff --git a/mwe/events/include/eventHandler.h b/mwe/events/include/eventHandler.h
new file mode 100644
index 0000000..7414801
--- /dev/null
+++ b/mwe/events/include/eventHandler.h
@@ -0,0 +1,46 @@
+#pragma once
+
+#include "event.h"
+
+#include <functional>
+#include <iostream>
+template <typename EventType>
+using EventHandler = std::function<void(const EventType & e)>;
+
+class IEventHandlerWrapper {
+public:
+ virtual ~IEventHandlerWrapper() = default;
+
+ void exec(const Event & e);
+
+ virtual std::string getType() const = 0;
+ virtual bool isDestroyOnSuccess() const = 0;
+
+private:
+ virtual void call(const Event & e) = 0;
+};
+
+template <typename EventType>
+class EventHandlerWrapper : public IEventHandlerWrapper {
+public:
+ explicit EventHandlerWrapper(const EventHandler<EventType> & handler,
+ const bool destroyOnSuccess = false)
+ : m_handler(handler), m_handlerType(m_handler.target_type().name()),
+ m_destroyOnSuccess(destroyOnSuccess) {
+ // std::cout << m_handlerType << std::endl;
+ }
+
+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; }
+ bool isDestroyOnSuccess() const { return m_destroyOnSuccess; }
+
+ EventHandler<EventType> m_handler;
+ const std::string m_handlerType;
+ bool m_destroyOnSuccess{false};
+};