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
|
#pragma once
#include <functional>
#include <memory>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <vector>
#include "Event.h"
#include "EventHandler.h"
namespace crepe {
static constexpr int CHANNEL_ALL = -1;
/**
* \class EventManager
* \brief The EventManager class is responsible for managing the subscription, triggering,
* and queueing of events. It handles events and dispatches them to appropriate subscribers.
*/
class EventManager {
public:
/**
* \brief Get the singleton instance of the EventManager.
*
* This method returns the unique instance of the EventManager, creating it on the first call.
*
* \return Reference to the EventManager instance.
*/
static EventManager & get_instance();
/**
* \brief Subscribe to an event.
*
* This method allows the registration of a callback for a specific event type and channel.
*
* \tparam EventType The type of the event to subscribe to.
* \param callback The callback function to invoke when the event is triggered.
* \param channel The channel number to subscribe to (default is 0).
*/
template <typename EventType>
void subscribe(const EventHandler<EventType> & callback, int channel = CHANNEL_ALL, int priority = 0);
/**
* \brief Unsubscribe from an event.
*
* This method removes a previously registered callback from an event.
*
* \tparam EventType The type of the event to unsubscribe from.
* \param callback The callback function to remove from the subscription list.
* \param channel The event ID to unsubscribe from.
*/
template <typename EventType>
void unsubscribe(const EventHandler<EventType> &, int channel = CHANNEL_ALL);
/**
* \brief Trigger an event.
*
* This method invokes the appropriate callback(s) for the specified event.
*
* \tparam EventType The type of the event to trigger.
* \param event The event data to pass to the callback.
* \param channel The channel from which to trigger the event (default is 0).
*/
template <typename EventType>
void trigger_event(const EventType & event, int channel = CHANNEL_ALL);
/**
* \brief Queue an event for later processing.
*
* This method adds an event to the event queue, which will be processed in the
* dispatch_events function.
*
* \tparam EventType The type of the event to queue.
* \param event The event to queue.
* \param channel The channel number for the event (default is 0).
*/
template <typename EventType>
void queue_event(const EventType & event, int channel = CHANNEL_ALL,int priority = 0);
/**
* \brief Dispatch all queued events.
*
* This method processes all events in the event queue and triggers the corresponding
* callbacks for each event.
*/
void dispatch_events();
/**
* \brief clears all subscribers
*
*/
void clear();
private:
struct QueueEntry {
std::unique_ptr<Event> event;
int channel = 0;
std::type_index type;
int priority = 0;
};
struct CallbackEntry {
std::unique_ptr<IEventHandlerWrapper> callback;
int channel = 0;
std::type_index type;
int priority = 0;
};
/**
* \brief Default constructor for the EventManager.
*
* This constructor is private to enforce the singleton pattern.
*/
EventManager() = default;
//! The queue of events to be processed.
std::vector<QueueEntry> events_queue;
//! Registered event handlers.
std::unordered_map<std::type_index, std::vector<std::unique_ptr<IEventHandlerWrapper>>>
subscribers;
//! Event handlers indexed by event ID.
std::unordered_map<
std::type_index,
std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>>
subscribers_by_event_id;
};
} // namespace crepe
#include "EventManager.hpp"
|