Loading...
Searching...
No Matches
ml::EventManager Class Reference

Centralized event bus for the Malena trait-based event system. More...

#include <EventManager.h>

Inheritance diagram for ml::EventManager:

Static Public Member Functions

static void clear ()
 Remove all subscriptions for all events.
static void clearPending ()
 Discard all pending operations without executing them.
template<typename EnumType>
static void fire (EnumType eventEnum, Fireable *dispatcher, const std::optional< sf::Event > &event, SystemCallback resolve=nullptr, SystemCallback reject=nullptr)
 Fire an event to all matching subscribers.
static bool isBusy ()
 Return true if the manager is currently iterating.
static void processPending ()
 Flush all pending operations immediately.
template<typename EnumType>
static void subscribe (EnumType eventEnum, EventReceiver *component)
 Register a component for an enum-keyed event.
template<typename EnumType>
static void unsubscribe (EnumType eventEnum, Core *core)
 Remove a component's subscription to one event.
static void unsubscribeAll (Core *core)
 Remove all subscriptions for a component.

Static Protected Member Functions

static void beginBusy ()
 Signal that iteration has begun.
static void deferOrExecute (std::function< void()> operation)
 Execute operation now if safe, otherwise queue it.
static void endBusy ()
 Signal that iteration has ended; flush pending operations.

Static Protected Attributes

static int busyDepth
 Iteration nesting depth. Operations are deferred while this is > 0.
static std::vector< std::function< void()> > pendingOperations
 Queue of operations pending until the current iteration completes.

Friends

class AppManager
class Core
class Fireable
class Unsubscribable

Detailed Description

Centralized event bus for the Malena trait-based event system.

Stores subscribers keyed by std::string — generated from any enum value via EnumKey::get(). Both ml::Event built-in values and user manifest event enums work identically.

All public methods take enum values directly — EnumKey::get() is called internally. Users and framework code never see raw string keys.

Inherits DeferredOperationsManager — any unsubscribe call during fire() is deferred until iteration ends, preventing iterator invalidation and use-after-free crashes.

See also
Fireable, EventReceiver, EnumKey, DeferredOperationsManager

Definition at line 40 of file EventManager.h.

Member Function Documentation

◆ beginBusy()

void ml::DeferredOperationsManager< EventManager >::beginBusy ( )
staticprotectedinherited

Signal that iteration has begun.

Increments the busy-depth counter. Multiple nested calls are safe — operations remain deferred until the outermost endBusy() is reached.

◆ clear()

void ml::EventManager::clear ( )
static

Remove all subscriptions for all events.

Deferred if called during fire().

◆ clearPending()

void ml::DeferredOperationsManager< EventManager >::clearPending ( )
staticinherited

Discard all pending operations without executing them.

Use during application shutdown or full reset when executing the queued operations would be unsafe (e.g., the objects they reference have already been destroyed).

◆ deferOrExecute()

void ml::DeferredOperationsManager< EventManager >::deferOrExecute ( std::function< void()> operation)
staticprotectedinherited

Execute operation now if safe, otherwise queue it.

If busyDepth > 0 the operation is appended to pendingOperations and will run when the current iteration completes. If the manager is idle the operation executes immediately.

Parameters
operationThe callable to execute or defer.

◆ endBusy()

void ml::DeferredOperationsManager< EventManager >::endBusy ( )
staticprotectedinherited

Signal that iteration has ended; flush pending operations.

Decrements the busy-depth counter. When the counter reaches zero, all operations in pendingOperations are executed in FIFO order and the queue is cleared.

◆ fire()

template<typename EnumType>
void ml::EventManager::fire ( EnumType eventEnum,
Fireable * dispatcher,
const std::optional< sf::Event > & event,
SystemCallback resolve = nullptr,
SystemCallback reject = nullptr )
inlinestatic

Fire an event to all matching subscribers.

Template Parameters
EnumTypeAny enum type.
Parameters
eventEnumThe enum value identifying the event.
dispatcherThe Fireable singleton owning filter logic.
eventSFML event forwarded to callbacks.
resolveCalled per-component after process() passes filter.
rejectCalled per-component when filter fails.

Definition at line 115 of file EventManager.h.

◆ isBusy()

bool ml::DeferredOperationsManager< EventManager >::isBusy ( )
staticinherited

Return true if the manager is currently iterating.

Can be used by external code that needs to know whether a destructive operation will be deferred.

Returns
true when busyDepth > 0.

◆ processPending()

void ml::DeferredOperationsManager< EventManager >::processPending ( )
staticinherited

Flush all pending operations immediately.

Normally called automatically by endBusy(). Provided as a public escape hatch for unusual teardown sequences.

Warning
Calling this while isBusy() is true can cause iterator invalidation in the currently running loop. Only call manually when you are certain it is safe.

◆ subscribe()

template<typename EnumType>
void ml::EventManager::subscribe ( EnumType eventEnum,
EventReceiver * component )
inlinestatic

Register a component for an enum-keyed event.

Template Parameters
EnumTypeAny enum type.
Parameters
eventEnumThe enum value identifying the event.
componentThe trait EventReceiver* subobject.

Definition at line 64 of file EventManager.h.

◆ unsubscribe()

template<typename EnumType>
void ml::EventManager::unsubscribe ( EnumType eventEnum,
Core * core )
inlinestatic

Remove a component's subscription to one event.

Deferred if called during fire().

Template Parameters
EnumTypeAny enum type.
Parameters
eventEnumThe event to unsubscribe from.
coreThe component's Core* identity.

Definition at line 79 of file EventManager.h.

◆ unsubscribeAll()

void ml::EventManager::unsubscribeAll ( Core * core)
static

Remove all subscriptions for a component.

Called by Core::~Core() and Unsubscribable::unsubscribeAll(). Deferred if called during fire().

Parameters
coreThe component's Core* identity.

◆ AppManager

friend class AppManager
friend

Definition at line 124 of file EventManager.h.

◆ Core

friend class Core
friend

Definition at line 126 of file EventManager.h.

◆ Fireable

friend class Fireable
friend

Definition at line 125 of file EventManager.h.

◆ Unsubscribable

friend class Unsubscribable
friend

Definition at line 127 of file EventManager.h.

Member Data Documentation

◆ busyDepth

int ml::DeferredOperationsManager< EventManager >::busyDepth
inlinestaticprotectedinherited

Iteration nesting depth. Operations are deferred while this is > 0.

Definition at line 69 of file DeferredOperationsManager.h.

◆ pendingOperations

std::vector<std::function<void()> > ml::DeferredOperationsManager< EventManager >::pendingOperations
inlinestaticprotectedinherited

Queue of operations pending until the current iteration completes.

Definition at line 72 of file DeferredOperationsManager.h.


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