aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api/EventManager.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/api/EventManager.h')
-rw-r--r--src/crepe/api/EventManager.h88
1 files changed, 47 insertions, 41 deletions
diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h
index 9b69b29..d3a14da 100644
--- a/src/crepe/api/EventManager.h
+++ b/src/crepe/api/EventManager.h
@@ -1,18 +1,18 @@
#pragma once
+
#include <memory>
#include <unordered_map>
#include <vector>
#include <functional>
-#include <iostream>
#include <typeindex>
#include <type_traits>
+
#include "Event.h"
#include "EventHandler.h"
-//#include "keyCodes.h"
/**
- * @class EventManager
- * @brief The EventManager class is responsible for managing the subscription, triggering,
+ * \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 {
@@ -23,16 +23,16 @@ public:
EventManager(const EventManager &) = delete;
/**
- * @brief Deleted copy assignment operator to prevent assignment of the EventManager instance.
+ * \brief Deleted copy assignment operator to prevent assignment of the EventManager instance.
*/
const EventManager & operator=(const EventManager &) = delete;
/**
- * @brief Get the singleton instance of the EventManager.
+ * \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.
+ * \return Reference to the EventManager instance.
*/
static EventManager & get_instance() {
static EventManager instance;
@@ -40,56 +40,56 @@ public:
}
/**
- * @brief Subscribe to an event.
+ * \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).
+ * \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(EventHandler<EventType> && callback, int channel = 0);
/**
- * @brief Unsubscribe from an event.
+ * \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 eventId The event ID to unsubscribe from.
+ * \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 eventId);
+ void unsubscribe(const EventHandler<EventType> &, int channel);
/**
- * @brief Trigger an event.
+ * \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).
+ * \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);
/**
- * @brief Queue an event for later processing.
+ * \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).
+ * \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(EventType&& event, int channel);
/**
- * @brief Dispatch all queued events.
+ * \brief Dispatch all queued events.
*
* This method processes all events in the event queue and triggers the corresponding
* callbacks for each event.
@@ -98,7 +98,7 @@ public:
private:
/**
- * @brief Default constructor for the EventManager.
+ * \brief Default constructor for the EventManager.
*
* This constructor is private to enforce the singleton pattern.
*/
@@ -118,7 +118,7 @@ void EventManager::subscribe(EventHandler<EventType> && callback, int channel) {
std::unique_ptr<EventHandlerWrapper<EventType>> handler = std::make_unique<EventHandlerWrapper<EventType>>(callback);
if (channel) {
- std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>& handlers_map = subscribers_by_event_id[event_type];
+ std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>& handlers_map = this->subscribers_by_event_id[event_type];
std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>::iterator handlers = handlers_map.find(channel);
if (handlers != handlers_map.end()) {
handlers->second.emplace_back(std::move(handler));
@@ -126,7 +126,7 @@ void EventManager::subscribe(EventHandler<EventType> && callback, int channel) {
handlers_map[channel].emplace_back(std::move(handler));
}
} else {
- std::vector<std::unique_ptr<IEventHandlerWrapper>>& handlers = subscribers[event_type];
+ std::vector<std::unique_ptr<IEventHandlerWrapper>>& handlers = this->subscribers[event_type];
handlers.emplace_back(std::move(handler));
}
}
@@ -142,22 +142,23 @@ void EventManager::queue_event(EventType&& event, int channel) {
channel,
event_type
);
- events_queue.push_back(std::move(tuple));
+ this->events_queue.push_back(std::move(tuple));
}
template <typename EventType>
-void EventManager::trigger_event(const EventType & event, int eventId) {
+void EventManager::trigger_event(const EventType & event, int channel) {
std::type_index event_type = std::type_index(typeid(EventType));
- if (eventId > 0) {
+ if (channel > 0) {
std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>& handlers_map = subscribers_by_event_id[event_type];
- std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>::iterator handlers_it = handlers_map.find(eventId);
+ std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>::iterator handlers_it = handlers_map.find(channel);
if (handlers_it != handlers_map.end()) {
- std::vector<std::unique_ptr<IEventHandlerWrapper>>& callbacks = handlers_it->second;
- for (std::vector<std::unique_ptr<IEventHandlerWrapper>>::iterator it = callbacks.begin(); it != callbacks.end();) {
+ std::vector<std::unique_ptr<IEventHandlerWrapper>>& handlers = handlers_it->second;
+ for (std::vector<std::unique_ptr<IEventHandlerWrapper>>::iterator it = handlers.begin(); it != handlers.end();) {
+ // erases callback if callback function returns true
if ((*it)->exec(event)) {
- it = callbacks.erase(it);
+ it = handlers.erase(it);
} else {
++it;
}
@@ -165,8 +166,13 @@ void EventManager::trigger_event(const EventType & event, int eventId) {
}
} else {
std::vector<std::unique_ptr<IEventHandlerWrapper>>& handlers = subscribers[event_type];
- for (std::vector<std::unique_ptr<IEventHandlerWrapper>>::iterator it = handlers.begin(); it != handlers.end(); ++it) {
- (*it)->exec(event);
+ for (std::vector<std::unique_ptr<IEventHandlerWrapper>>::iterator it = handlers.begin(); it != handlers.end();) {
+ // erases callback if callback function returns true
+ if ((*it)->exec(event)) {
+ it = handlers.erase(it);
+ } else {
+ ++it;
+ }
}
}
}
@@ -177,8 +183,8 @@ void EventManager::unsubscribe(const EventHandler<EventType> & callback, int cha
std::string handler_name = callback.target_type().name();
if (channel) {
- std::unordered_map<std::type_index, std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>>::iterator subscriber_list = subscribers_by_event_id.find(event_type);
- if (subscriber_list != subscribers_by_event_id.end()) {
+ std::unordered_map<std::type_index, std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>>::iterator subscriber_list = this->subscribers_by_event_id.find(event_type);
+ if (subscriber_list != this->subscribers_by_event_id.end()) {
std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>& handlers_map = subscriber_list->second;
std::unordered_map<int, std::vector<std::unique_ptr<IEventHandlerWrapper>>>::iterator handlers = handlers_map.find(channel);
if (handlers != handlers_map.end()) {
@@ -192,8 +198,8 @@ void EventManager::unsubscribe(const EventHandler<EventType> & callback, int cha
}
}
} else {
- std::unordered_map<std::type_index, std::vector<std::unique_ptr<IEventHandlerWrapper>>>::iterator handlers_it = subscribers.find(event_type);
- if (handlers_it != subscribers.end()) {
+ std::unordered_map<std::type_index, std::vector<std::unique_ptr<IEventHandlerWrapper>>>::iterator handlers_it = this->subscribers.find(event_type);
+ if (handlers_it != this->subscribers.end()) {
std::vector<std::unique_ptr<IEventHandlerWrapper>>& handlers = handlers_it->second;
for (std::vector<std::unique_ptr<IEventHandlerWrapper>>::iterator it = handlers.begin(); it != handlers.end(); ++it) {
if ((*it)->get_type() == handler_name) {