diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-24 10:53:02 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-24 10:53:02 +0200 |
commit | a8a7ff90dfd626ea76dd9e9fe00c7854a7b9f31f (patch) | |
tree | 2724ddce9d181770e7ca745900af76ed64194451 /mwe/events/include/eventHandler.h | |
parent | 39054829fa69bcfa2b468015dc3852a2f8deac9f (diff) | |
parent | b5e83d076f356c6d01b7bbc1f033db4850356c0d (diff) |
Merge branch 'wouter/events-poc' of github.com:lonkaars/crepe
Diffstat (limited to 'mwe/events/include/eventHandler.h')
-rw-r--r-- | mwe/events/include/eventHandler.h | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/mwe/events/include/eventHandler.h b/mwe/events/include/eventHandler.h new file mode 100644 index 0000000..e5b99d6 --- /dev/null +++ b/mwe/events/include/eventHandler.h @@ -0,0 +1,48 @@ +#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 }; +}; |