diff options
author | JAROWMR <jarorutjes07@gmail.com> | 2024-11-15 14:32:09 +0100 |
---|---|---|
committer | JAROWMR <jarorutjes07@gmail.com> | 2024-11-15 14:32:09 +0100 |
commit | 37c016f1d5902cf7bbfed08d13ee2bc6ef725add (patch) | |
tree | ce9de85b6d5ffd43d2e798b5310df883705f5ca8 /src/crepe/api/EventManager.h | |
parent | de1c6053033483c7e824f8018d75be6af424d14d (diff) | |
parent | 337a957a9e605f16287506b6afda56950e562db3 (diff) |
merge with wouter/events
Diffstat (limited to 'src/crepe/api/EventManager.h')
-rw-r--r-- | src/crepe/api/EventManager.h | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/src/crepe/api/EventManager.h b/src/crepe/api/EventManager.h new file mode 100644 index 0000000..38d2e64 --- /dev/null +++ b/src/crepe/api/EventManager.h @@ -0,0 +1,122 @@ +#pragma once + +#include <functional> +#include <memory> +#include <type_traits> +#include <typeindex> +#include <unordered_map> +#include <vector> + +#include "Event.h" +#include "EventHandler.h" + + +namespace crepe { +/** + * \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 Deleted copy constructor to prevent copying of the EventManager instance. + */ + EventManager(const EventManager &) = delete; + + /** + * \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. + * + * 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(EventHandler<EventType> && callback, int channel = 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); + + /** + * \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); + + /** + * \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(EventType && event, int channel); + + /** + * \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(); + +private: + /** + * \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<std::tuple<std::unique_ptr<Event>, int, std::type_index>> + 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" |