ORE Studio 0.0.4
Loading...
Searching...
No Matches
Public Member Functions | List of all members
event_bus Class Referencefinal

A typed, thread-safe, in-process publish/subscribe event bus. More...

#include <event_bus.hpp>

Collaboration diagram for event_bus:
Collaboration graph

Public Member Functions

 event_bus (const event_bus &)=delete
 
event_busoperator= (const event_bus &)=delete
 
 event_bus (event_bus &&)=delete
 
event_busoperator= (event_bus &&)=delete
 
template<typename Event >
subscription subscribe (std::function< void(const Event &)> handler)
 Subscribe to events of a specific type.
 
template<typename Event >
void publish (const Event &event)
 Publish an event to all subscribers.
 
template<typename Event >
std::size_t subscriber_count () const
 Get the number of subscribers for a specific event type.
 

Detailed Description

A typed, thread-safe, in-process publish/subscribe event bus.

The event bus provides a decoupled communication mechanism between components. Publishers emit events without knowing who subscribes, and subscribers receive events without knowing who publishes them.

Thread Safety:

Usage:

// Subscribe to an event type
auto sub = bus.subscribe<my_event>([](const my_event& e) {
// Handle event
});
// Publish an event
bus.publish(my_event{...});
// Subscription automatically cleaned up when 'sub' goes out of scope
A typed, thread-safe, in-process publish/subscribe event bus.
Definition event_bus.hpp:119
void publish(const Event &event)
Publish an event to all subscribers.
Definition event_bus.hpp:188
subscription subscribe(std::function< void(const Event &)> handler)
Subscribe to events of a specific type.
Definition event_bus.hpp:157

Member Function Documentation

◆ subscribe()

template<typename Event >
subscription subscribe ( std::function< void(const Event &)>  handler)

Subscribe to events of a specific type.

Template Parameters
EventThe event type to subscribe to.
Parameters
handlerThe callback function to invoke when an event is published.
Returns
A subscription handle that unsubscribes when destroyed.
Note
The handler is invoked synchronously during publish(). It should be non-blocking to avoid holding the bus lock for extended periods.

◆ publish()

template<typename Event >
void publish ( const Event &  event)

Publish an event to all subscribers.

Template Parameters
EventThe event type being published.
Parameters
eventThe event instance to publish.

All registered handlers for this event type will be invoked synchronously. Exceptions thrown by handlers are caught and logged, but do not prevent other handlers from being called.

Here is the caller graph for this function:

◆ subscriber_count()

template<typename Event >
std::size_t subscriber_count ( ) const

Get the number of subscribers for a specific event type.

Template Parameters
EventThe event type to query.
Returns
The number of active subscriptions for this event type.