aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api/EventManager.h
blob: 93e9ca272433ddb75e3e4017ca4a8c1a4a3b99f6 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#pragma once

#include <functional>
#include <memory>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <vector>

#include "Event.h"
#include "EventHandler.h"

namespace crepe {
//! typedef for subscription value
typedef int subscription_t;

/**
 * \class EventManager
 * \brief Manages event subscriptions, triggers, and queues, enabling decoupled event handling.
 * 
 * The `EventManager` acts as a centralized event system. It allows for registering callbacks
 * for specific event types, triggering events synchronously, queueing events for later
 * processing, and managing subscriptions via unique identifiers.
 */
class EventManager {
public:
	static constexpr int CHANNEL_ALL = -1;

	/**
	 * \brief Get the singleton instance of the EventManager.
	 * 
	 * This method returns the unique instance of the EventManager, creating it if it
	 * doesn't already exist. Ensures only one instance is active in the program.
	 * 
	 * \return Reference to the singleton instance of the EventManager.
	 */
	static EventManager & get_instance();

	/**
	 * \brief Subscribe to a specific event type.
	 * 
	 * Registers a callback for a given event type and optional channel. Each callback
	 * is assigned a unique subscription ID that can be used for later unsubscription.
	 * 
	 * \tparam EventType The type of the event to subscribe to.
	 * \param callback The callback function to be invoked when the event is triggered.
	 * \param channel The channel number to subscribe to (default is CHANNEL_ALL, which listens to all channels).
	 * \return A unique subscription ID associated with the registered callback.
	 */
	template <typename EventType>
	subscription_t subscribe(const EventHandler<EventType> & callback, int channel = CHANNEL_ALL);

	/**
	 * \brief Unsubscribe a previously registered callback.
	 * 
	 * Removes a callback from the subscription list based on its unique subscription ID.
	 * 
	 * \param event_id The unique subscription ID of the callback to remove.
	 */
	void unsubscribe(subscription_t event_id);

	/**
	 * \brief Trigger an event immediately.
	 * 
	 * Synchronously invokes all registered callbacks for the given event type on the specified channel.
	 * 
	 * \tparam EventType The type of the event to trigger.
	 * \param event The event instance to pass to the callbacks.
	 * \param channel The channel to trigger the event on (default is CHANNEL_ALL, which triggers on all channels).
	 */
	template <typename EventType>
	void trigger_event(const EventType & event, int channel = CHANNEL_ALL);

	/**
	 * \brief Queue an event for later processing.
	 * 
	 * Adds an event to the event queue to be processed during the next call to `dispatch_events`.
	 * 
	 * \tparam EventType The type of the event to queue.
	 * \param event The event instance to queue.
	 * \param channel The channel to associate with the event (default is CHANNEL_ALL).
	 */
	template <typename EventType>
	void queue_event(const EventType & event, int channel = CHANNEL_ALL);

	/**
	 * \brief Process all queued events.
	 * 
	 * Iterates through the event queue and triggers callbacks for each queued event.
	 * Events are removed from the queue once processed.
	 */
	void dispatch_events();

	/**
	 * \brief Clear all subscriptions.
	 * 
	 * Removes all registered event handlers and clears the subscription list.
	 */
	void clear();

private:
	/**
	 * \brief Default constructor for the EventManager.
	 * 
	 * Constructor is private to enforce the singleton pattern.
	 */
	EventManager() = default;

	/**
	 * \struct QueueEntry
	 * \brief Represents an entry in the event queue.
	 */
	struct QueueEntry {
		std::unique_ptr<Event> event; ///< The event instance.
		int channel = CHANNEL_ALL;    ///< The channel associated with the event.
		std::type_index type;         ///< The type of the event.
	};

	/**
	 * \struct CallbackEntry
	 * \brief Represents a registered event handler callback.
	 */
	struct CallbackEntry {
		std::unique_ptr<IEventHandlerWrapper> callback; ///< The callback function wrapper.
		int channel = CHANNEL_ALL;                      ///< The channel this callback listens to.
		subscription_t id = -1;                         ///< Unique subscription ID.
	};

	//! The queue of events to be processed during dispatch.
	std::vector<QueueEntry> events_queue;

	//! A map of event type to registered callbacks.
	std::unordered_map<std::type_index, std::vector<CallbackEntry>> subscribers;

	//! Counter to generate unique subscription IDs.
	subscription_t subscription_counter = 0;
};

} // namespace crepe

#include "EventManager.hpp"