R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
server::EventBus Class Reference

Type-safe event bus for the server. More...

#include <EventBus.hpp>

Inheritance diagram for server::EventBus:
Inheritance graph
Collaboration diagram for server::EventBus:
Collaboration graph

Public Types

template<typename T >
using EventCallback = std::function< void(const T &)>
 Callback type for an event.
 

Public Member Functions

 EventBus ()=default
 
 ~EventBus () override=default
 
template<typename T >
void subscribe (EventCallback< T > callback)
 Subscribe to an event type.
 
template<typename T >
void publish (const T &event)
 Publish an event.
 
void clear () override
 Clear all subscriptions.
 
- Public Member Functions inherited from server::IEventBus
virtual ~IEventBus ()=default
 

Private Attributes

std::unordered_map< std::type_index, std::vector< std::function< void(const std::any &)> > > _subscribers
 Container of subscribers per event type.
 

Detailed Description

Type-safe event bus for the server.

EventBus allows different components of the server to communicate via events without introducing direct dependencies. Each event type T can have multiple subscribers (callbacks) that are invoked when the event is published.

Subscribers are stored in a container keyed by typeid(T) and called in the order they were registered.

Example usage:

Server::EventBus bus;
bus.subscribe<MyEvent>([](const MyEvent &e){ std::cout << e.value << std::endl; });
MyEvent evt{42};
bus.publish(evt);

Definition at line 38 of file EventBus.hpp.

Member Typedef Documentation

◆ EventCallback

template<typename T >
using server::EventBus::EventCallback = std::function<void(const T &)>

Callback type for an event.

Template Parameters
TEvent type

EventCallback is a function taking a const reference to an event. It will be called whenever the corresponding event type is published.

Definition at line 51 of file EventBus.hpp.

Constructor & Destructor Documentation

◆ EventBus()

server::EventBus::EventBus ( )
default

◆ ~EventBus()

server::EventBus::~EventBus ( )
overridedefault

Member Function Documentation

◆ clear()

void server::EventBus::clear ( )
inlineoverridevirtual

Clear all subscriptions.

Completely clears the event bus, removing all subscribers for all event types.

Implements server::IEventBus.

Definition at line 96 of file EventBus.hpp.

References _subscribers.

◆ publish()

template<typename T >
void server::EventBus::publish ( const T &  event)
inline

Publish an event.

Template Parameters
TEvent type
Parameters
eventEvent instance to publish

Invokes all subscribers for type T with the provided event. If no subscribers exist, the function does nothing.

Definition at line 80 of file EventBus.hpp.

References _subscribers.

◆ subscribe()

template<typename T >
void server::EventBus::subscribe ( EventCallback< T >  callback)
inline

Subscribe to an event type.

Template Parameters
TEvent type
Parameters
callbackFunction to call when an event of type T is published

Adds the callback to the list of subscribers for type T. Callbacks are stored using type-erasure with std::any. The wrapper converts std::any back to T before invoking user callbacks.

Definition at line 63 of file EventBus.hpp.

References _subscribers.

Member Data Documentation

◆ _subscribers

std::unordered_map<std::type_index, std::vector<std::function<void(const std::any &)> > > server::EventBus::_subscribers
private

Container of subscribers per event type.

Each event type T is identified by typeid(T). Callbacks are stored using type-erasure (std::function<void(const std::any &)>), allowing multiple types to coexist in the same bus.

Definition at line 106 of file EventBus.hpp.

Referenced by clear(), publish(), and subscribe().


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