diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-11 10:50:40 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-11 10:50:40 +0100 |
commit | 880a7b1fb233a67303ad5a4155dc459f79734762 (patch) | |
tree | 955faa9c02480f17cb4a674ae9a1b82dab741a63 /src/crepe/api/EventHandler.h | |
parent | 3a690f7d0c91b92b9cdfe62f44dba8db90142abc (diff) |
big cleanup events + events are now structs
Diffstat (limited to 'src/crepe/api/EventHandler.h')
-rw-r--r-- | src/crepe/api/EventHandler.h | 38 |
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; +}; |