aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api/EventHandler.h
blob: eea1c794c749a492c1f97dde92243eab089d98ea (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
#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;
};