|
R-Type
Distributed multiplayer game engine in C++
|
Type-safe event bus for the server. More...
#include <EventBus.hpp>


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. | |
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:
Definition at line 38 of file EventBus.hpp.
| using server::EventBus::EventCallback = std::function<void(const T &)> |
Callback type for an event.
| T | Event 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.
|
default |
|
overridedefault |
|
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.
|
inline |
Publish an event.
| T | Event type |
| event | Event 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.
|
inline |
Subscribe to an event type.
| T | Event type |
| callback | Function 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.
|
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().