aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api/EventHandler.h
blob: 0ab90def9ee7a387a3fd5932f9034f453ea5e576 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#pragma once
#include "Event.h"
#include <functional>
#include <iostream>
#include <typeindex>

/**
 * \brief A type alias for an event handler function.
 * 
 * The EventHandler is a std::function that takes an EventType reference and returns a boolean value 
 * indicating whether the event is handled.
 * 
 * \tparam EventType The type of event this handler will handle.
 */
// TODO: typedef
template <typename EventType>
using EventHandler = std::function<bool(const EventType & e)>;

/**
 * \class IEventHandlerWrapper
 * \brief An abstract base class for event handler wrappers.
 * 
 * This class provides the interface for handling events. Derived classes must implement the
 * `call()` method to process events and the `get_type()` method to return the handler's type.
 */
class IEventHandlerWrapper {
public:
	/**
     * \brief Virtual destructor for IEventHandlerWrapper.
     */
	virtual ~IEventHandlerWrapper() = default;

	/**
     * \brief Executes the handler with the given event.
     * 
     * This method calls the `call()` method of the derived class, passing the event to the handler.
     * 
     * \param e The event to be processed.
     * \return A boolean value indicating whether the event is handled.
     */
	bool exec(const Event & e);

	/**
     * \brief Get the type of the event handler.
     * 
     * This method returns the type of the event handler as a string.
     * 
     * \return A string representing the handler's type.
     */
	virtual std::string get_type() const = 0;

private:
	/**
     * \brief The method responsible for handling the event.
     * 
     * This method is implemented by derived classes to process the event.
     * 
     * \param e The event to be processed.
     * \return A boolean value indicating whether the event is handled.
     */
	virtual bool call(const Event & e) = 0;
};

/**
 * \class EventHandlerWrapper
 * \brief A wrapper for event handler functions.
 * 
 * This class wraps an event handler function of a specific event type. It implements the 
 * `call()` and `get_type()` methods to allow the handler to be executed and its type to be 
 * queried.
 * 
 * \tparam EventType The type of event this handler will handle.
 */
template <typename EventType>
class EventHandlerWrapper : public IEventHandlerWrapper {
public:
	/**
     * \brief Constructs an EventHandlerWrapper with a given handler.
     * 
     * The constructor takes an event handler function and stores it in the wrapper.
     * 
     * \param handler The event handler function.
     */
	explicit EventHandlerWrapper(const EventHandler<EventType> & handler)
		: m_handler(handler), m_handler_type(m_handler.target_type().name()) {}

private:
	/**
     * \brief Calls the stored event handler with the event.
     * 
     * This method casts the event to the appropriate type and calls the handler.
     * 
     * \param e The event to be handled.
     * \return A boolean value indicating whether the event is handled.
     */
	bool call(const Event & e) override {
		return m_handler(static_cast<const EventType &>(e));
	}

	/**
     * \brief Returns the type of the handler.
     * 
     * This method returns a string representing the type of the event handler.
     * 
     * \return The handler type as a string.
     */
	std::string get_type() const override { return m_handler_type; }
	//! The event handler function.
	EventHandler<EventType> m_handler;
	//! The type name of the handler function.
	const std::string m_handler_type;
};