diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-10-01 13:40:56 +0200 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-10-01 13:40:56 +0200 |
commit | 4628638f952f882e494788ca1cf880a029eba5ab (patch) | |
tree | 894b2ef38569dc16415ea2abaeaac2ee0332404f /mwe/events/include/eventHandler.h | |
parent | f4560e02f703f1c6f857c8e5af63fa9fc4ca6438 (diff) |
first try at events
Diffstat (limited to 'mwe/events/include/eventHandler.h')
-rw-r--r-- | mwe/events/include/eventHandler.h | 39 |
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; +}; |