Skip to content

Class EventPool

ClassList > EventPool

Type-safe event dispatcher with deferred execution queue. More...

  • #include <Events.h>

Public Types

Type Name
typedef std::function< void(const Event &)> Handler
Handler function type for events of type Event.

Public Functions

Type Name
void EmitGlobalEvent ()
Emits a global event to all systems.
void Process (int max_events=1000)
Dispatches all enqueued events in FIFO order.
void emit (const Event & event)
Enqueues an event for deferred dispatch.
void subscribe (Handler< Event > handler)
Subscribes a handler to events of type Event.

Detailed Description

EventPool allows any component to subscribe to events of a specific type and emit events to be dispatched in a controlled, deferred manner. Type safety is enforced at compile time via templates, while runtime dispatch uses std::type_index.

Usage:

EventPool pool;

// Subscribe to an event type
pool.subscribe<MyEvent>([](const MyEvent& e) {
    // Handle event
});

// Enqueue an event (not dispatched yet)
pool.emit(MyEvent{param1, param2});

// Dispatch all queued events (call once per frame, after UI rendering)
pool.process();

Note:

Thread-safety: Not thread-safe. All subscribe/emit/process must occur on main thread.

Note:

Event chains: If a handler calls emit(), the new event is appended to the end of the queue and processed in the same process() call (breadth-first ordering).

Public Types Documentation

typedef Handler

Handler function type for events of type Event.

using EventPool::Handler =  std::function<void(const Event&)>;

Template parameters:

  • Event The event type to handle

Public Functions Documentation

function EmitGlobalEvent

Emits a global event to all systems.

void EventPool::EmitGlobalEvent () 

Note:

TODO: Define global event semantics


function Process

Dispatches all enqueued events in FIFO order.

inline void EventPool::Process (
    int max_events=1000
) 

Processes every event currently in the queue, up to max_events per call. Events emitted by handlers during processing are appended to the end of the queue and handled within the same call, guaranteeing deterministic breadth-first ordering for event chains. The cap prevents an infinite loop if a badly-formed handler continuously re-emits events.

Parameters:

  • max_events Maximum number of events to dispatch in a single call (default: 1000).

Note:

Call once per frame, after the UI layer has finished rendering.


function emit

Enqueues an event for deferred dispatch.

template<typename Event>
inline void EventPool::emit (
    const Event & event
) 

The event is copied and stored in the internal queue. It will not be dispatched until process() is called. If called from within a handler during process(), the new event is appended to the end of the queue and processed in the same process() call.

Template parameters:

  • Event The event type to emit

Parameters:

  • event The event instance to enqueue (copied into the queue)

function subscribe

Subscribes a handler to events of type Event.

template<typename Event>
inline void EventPool::subscribe (
    Handler < Event > handler
) 

Registers a callback to be invoked whenever an event of type Event is dispatched via process(). Multiple handlers can subscribe to the same event type and will be called in subscription order.

Template parameters:

  • Event The event type to subscribe to

Parameters:

  • handler Callback function receiving const Event&

Note:

Handlers should not assume execution order relative to other handlers.



The documentation for this class was generated from the following file src/editor/Events.h