aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api/EventManager.h
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-20 21:51:30 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-20 21:51:30 +0100
commit8e72968e294cbc4ac6e9ff09bd94cde1775d735b (patch)
tree1790d90772a1cd37feff180a5dacfae33c305a68 /src/crepe/api/EventManager.h
parent3a5a61399cea453c5fc25e3b999ef9a751fd4448 (diff)
nitpick #34
Diffstat (limited to 'src/crepe/api/EventManager.h')
-rw-r--r--src/crepe/api/EventManager.h39
1 files changed, 29 insertions, 10 deletions
diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h
index bd9772a..348a04d 100644
--- a/src/crepe/api/EventManager.h
+++ b/src/crepe/api/EventManager.h
@@ -1,8 +1,6 @@
#pragma once
-#include <functional>
#include <memory>
-#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <vector>
@@ -11,8 +9,17 @@
#include "EventHandler.h"
namespace crepe {
-//! typedef for subscription value
-typedef int subscription_t;
+
+//! Event listener unique ID
+typedef size_t subscription_t;
+
+/**
+ * \brief Event channel
+ *
+ * Events can be sent to a specific channel, which prevents listeners on other channels from
+ * being called. The default channel is EventManager::CHANNEL_ALL, which calls all listeners.
+ */
+typedef size_t event_channel_t;
/**
* \class EventManager
@@ -24,7 +31,7 @@ typedef int subscription_t;
*/
class EventManager {
public:
- static constexpr int CHANNEL_ALL = -1;
+ static constexpr const event_channel_t CHANNEL_ALL = -1;
/**
* \brief Get the singleton instance of the EventManager.
@@ -49,7 +56,7 @@ public:
*/
template <typename EventType>
subscription_t subscribe(const EventHandler<EventType> & callback,
- int channel = CHANNEL_ALL);
+ event_channel_t channel = CHANNEL_ALL);
/**
* \brief Unsubscribe a previously registered callback.
@@ -70,7 +77,7 @@ public:
* \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);
+ void trigger_event(const EventType & event, event_channel_t channel = CHANNEL_ALL);
/**
* \brief Queue an event for later processing.
@@ -82,7 +89,7 @@ public:
* \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);
+ void queue_event(const EventType & event, event_channel_t channel = CHANNEL_ALL);
/**
* \brief Process all queued events.
@@ -113,17 +120,29 @@ private:
*/
struct QueueEntry {
std::unique_ptr<Event> event; ///< The event instance.
- int channel = CHANNEL_ALL; ///< The channel associated with the event.
+ event_channel_t channel = CHANNEL_ALL; ///< The channel associated with the event.
std::type_index type; ///< The type of the event.
};
/**
+ * \brief Internal event handler
+ *
+ * This function processes a single event, and is used to process events both during
+ * EventManager::dispatch_events and inside EventManager::trigger_event
+ *
+ * \param type \c typeid of concrete Event class
+ * \param channel Event channel
+ * \param data Event data
+ */
+ void handle_event(std::type_index type, event_channel_t channel, const Event & data);
+
+ /**
* \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.
+ event_channel_t channel = CHANNEL_ALL; ///< The channel this callback listens to.
subscription_t id = -1; ///< Unique subscription ID.
};