Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::core::EventBus Class Reference

Lets systems react to events without knowing who sends them. More...

#include <aegis2d/core/event_bus.hpp>

Public Types

template<typename Event >
using Handler = std::function< void(const Event &)>
 Callback for events of type Event.
 

Public Member Functions

template<typename Event >
SubscriptionId subscribe (Handler< Event > handler)
 Registers a handler for events of type Event.
 
bool unsubscribe (SubscriptionId id)
 Removes a handler.
 
template<typename Event >
void publish (const Event &event)
 Calls every handler of type Event with event, before returning.
 
template<typename Event >
std::size_t subscriber_count () const
 Number of handlers registered for events of type Event.
 
void clear ()
 Removes every handler of every event type.
 

Detailed Description

Lets systems react to events without knowing who sends them.

Any copyable or non-copyable type can be an event: struct EntityDestroyed { Entity entity; };. publish() calls the handlers of that exact type immediately, in subscription order.

Handlers may publish, subscribe and unsubscribe while an event is being dispatched: a handler added during a dispatch is first called by the next publish(), and a handler removed during a dispatch is not called anymore, even by the current one.

The bus is not thread-safe: use it from the game thread only.

Member Typedef Documentation

◆ Handler

template<typename Event >
using aegis2d::core::EventBus::Handler = std::function<void(const Event&)>

Callback for events of type Event.

Member Function Documentation

◆ clear()

void aegis2d::core::EventBus::clear ( )
inline

Removes every handler of every event type.

◆ publish()

template<typename Event >
void aegis2d::core::EventBus::publish ( const Event &  event)
inline

Calls every handler of type Event with event, before returning.

Parameters
eventEvent to dispatch; it is passed by reference to each handler.

◆ subscribe()

template<typename Event >
SubscriptionId aegis2d::core::EventBus::subscribe ( Handler< Event >  handler)
inline

Registers a handler for events of type Event.

Parameters
handlerCallback called for each published Event.
Returns
Identifier to pass to unsubscribe().

◆ subscriber_count()

template<typename Event >
std::size_t aegis2d::core::EventBus::subscriber_count ( ) const
inline

Number of handlers registered for events of type Event.

Returns
The handler count.

◆ unsubscribe()

bool aegis2d::core::EventBus::unsubscribe ( SubscriptionId  id)
inline

Removes a handler.

Parameters
idIdentifier returned by subscribe().
Returns
true if the handler was registered, false if id is unknown.

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