Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
Event::Resource::EventManager Class Reference

Thread-safe event manager for registering, queuing, and dispatching events. More...

#include <EventManager.hpp>

Classes

struct  DirectCallbackSchedulerTag

Public Types

using EventTypeID = size_t
 Type identifier for event types.
using EventCallbackID = size_t
 Unique identifier for registered event callbacks.

Public Member Functions

 EventManager ()=default
 Default constructor.
 ~EventManager ()
 EventManager (const EventManager &)=delete
EventManageroperator= (const EventManager &)=delete
 EventManager (EventManager &&other) noexcept
 Move constructor.
EventManageroperator= (EventManager &&other) noexcept
 Move assignment operator.
template<typename TEvent, typename TScheduler = DirectCallbackSchedulerTag, typename TCallBack>
EventCallbackID RegisterCallback (TCallBack &&callback)
template<typename TEvent>
void PushEvent (const TEvent &event)
 Queue an event for processing.
template<typename TScheduler>
void ProcessEvents (void)
 Process all queued events for a specific scheduler.
template<typename TEvent, typename TScheduler = DirectCallbackSchedulerTag>
void UnregisterCallback (EventCallbackID callbackID)
 Unregister a previously registered callback.

Private Member Functions

template<typename TEvent, typename TCallBack, typename TScheduler>
EventCallbackID _RegisterCallbackImpl (TCallBack &&callback)

Static Private Member Functions

static EventCallbackID _GenerateRandomID ()
template<typename TEvent>
static EventTypeID _GetId (void)

Private Attributes

std::unordered_map< std::type_index, std::unordered_map< EventTypeID, std::shared_ptr< Utils::IEventContainer > > > _eventCallbacks
std::unordered_map< std::type_index, std::queue< std::pair< EventTypeID, std::any > > > _eventQueue
std::mutex _queueMutex
std::mutex _callbacksMutex

Detailed Description

Thread-safe event manager for registering, queuing, and dispatching events.

Manages event callbacks and event queues per scheduler type. Events are queued when pushed and processed during the corresponding scheduler execution. All operations are thread-safe.

Member Typedef Documentation

◆ EventCallbackID

Unique identifier for registered event callbacks.

◆ EventTypeID

Type identifier for event types.

Constructor & Destructor Documentation

◆ EventManager() [1/3]

Event::Resource::EventManager::EventManager ( )
default

Default constructor.

◆ ~EventManager()

Event::Resource::EventManager::~EventManager ( )
inline

◆ EventManager() [2/3]

Event::Resource::EventManager::EventManager ( const EventManager & )
delete

◆ EventManager() [3/3]

Event::Resource::EventManager::EventManager ( EventManager && other)
inlinenoexcept

Move constructor.

Parameters
otherThe EventManager to move from.

Member Function Documentation

◆ _GenerateRandomID()

EventCallbackID Event::Resource::EventManager::_GenerateRandomID ( )
inlinestaticprivate

◆ _GetId()

template<typename TEvent>
EventTypeID Event::Resource::EventManager::_GetId ( void )
inlinestaticprivate

◆ _RegisterCallbackImpl()

template<typename TEvent, typename TCallBack, typename TScheduler>
EventCallbackID Event::Resource::EventManager::_RegisterCallbackImpl ( TCallBack && callback)
inlineprivate

◆ operator=() [1/2]

EventManager & Event::Resource::EventManager::operator= ( const EventManager & )
delete

◆ operator=() [2/2]

EventManager & Event::Resource::EventManager::operator= ( EventManager && other)
inlinenoexcept

Move assignment operator.

Parameters
otherThe EventManager to move from.
Returns
Reference to this EventManager.

◆ ProcessEvents()

template<typename TScheduler>
void Event::Resource::EventManager::ProcessEvents ( void )
inline

Process all queued events for a specific scheduler.

Dequeues and triggers all callbacks registered for the given scheduler type. This method is typically called by the scheduler during its execution phase.

Template Parameters
TSchedulerThe scheduler type whose events should be processed.
Parameters
coreReference to the engine core.

◆ PushEvent()

template<typename TEvent>
void Event::Resource::EventManager::PushEvent ( const TEvent & event)
inline

Queue an event for processing.

The event is added to the queue for each scheduler that has registered callbacks for this event type. Events are processed during the corresponding scheduler execution.

Template Parameters
TEventThe event type.
Parameters
eventThe event instance to queue.

◆ RegisterCallback()

template<typename TEvent, typename TScheduler = DirectCallbackSchedulerTag, typename TCallBack>
EventCallbackID Event::Resource::EventManager::RegisterCallback ( TCallBack && callback)
inline
   @brief Register a callback for an event type on a specific scheduler.
   @tparam TEvent The event type to listen for.
   @tparam TScheduler The scheduler type on which to process this event.
   @tparam TCallBack The callback type (auto-deduced).
  • *
    Parameters
    callbackThe callback function with signature void(const TEvent&).
    Returns
    Unique identifier for the registered callback.

◆ UnregisterCallback()

template<typename TEvent, typename TScheduler = DirectCallbackSchedulerTag>
void Event::Resource::EventManager::UnregisterCallback ( EventCallbackID callbackID)
inline

Unregister a previously registered callback.

Removes the callback identified by the given ID for the specified event type and scheduler. Logs a warning if the callback or event type is not found.

Template Parameters
TEventThe event type the callback was registered for.
TSchedulerThe scheduler type (defaults to Immediate call tag).
Parameters
callbackIDThe unique identifier returned by RegisterCallback.

Member Data Documentation

◆ _callbacksMutex

std::mutex Event::Resource::EventManager::_callbacksMutex
private

◆ _eventCallbacks

std::unordered_map<std::type_index, std::unordered_map<EventTypeID, std::shared_ptr<Utils::IEventContainer> > > Event::Resource::EventManager::_eventCallbacks
private

◆ _eventQueue

std::unordered_map<std::type_index, std::queue<std::pair<EventTypeID, std::any> > > Event::Resource::EventManager::_eventQueue
private

◆ _queueMutex

std::mutex Event::Resource::EventManager::_queueMutex
private

The documentation for this class was generated from the following file: