aboutsummaryrefslogtreecommitdiff
path: root/mwe/events/include/eventHandler.h
blob: aa8f63b983053a5f26a8d0421a1cf4b3106a85c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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};
};