|
R-Type
Distributed multiplayer game engine in C++
|
Type-safe event publication/subscription system. More...
#include <EventBus.hpp>

Public Types | |
| template<typename T > | |
| using | EventCallback = std::function< void(const T &)> |
| Callback type for events. | |
Public Member Functions | |
| EventBus ()=default | |
| Default constructor. | |
| ~EventBus ()=default | |
| Default destructor. | |
| template<typename T > | |
| size_t | subscribe (EventCallback< T > callback) |
| Subscribe to a specific event type. | |
| template<typename T > | |
| void | publish (const T &event) |
| Publish an event to all subscribers. | |
| void | clear () |
| Clear all subscriptions. | |
Private Attributes | |
| std::unordered_map< std::type_index, std::vector< std::function< void(const IEvent &)> > > | _subscribers |
Type-safe event publication/subscription system.
EventBus enables decoupled communication between different game components. It uses the Observer pattern to distribute events to interested subscribers.
Architecture:
Usage example:
Definition at line 44 of file EventBus.hpp.
| using EventBus::EventCallback = std::function<void(const T &)> |
Callback type for events.
| T | Event type (must inherit from IEvent) |
Definition at line 51 of file EventBus.hpp.
|
default |
Default constructor.
|
default |
Default destructor.
| void EventBus::clear | ( | ) |
Clear all subscriptions.
Removes all registered callbacks for all event types. Useful for system reset or avoiding memory leaks.
| void EventBus::publish | ( | const T & | event | ) |
Publish an event to all subscribers.
Notifies all registered callbacks for this event type. Callbacks are called synchronously in registration order.
| T | Event type (must inherit from IEvent) |
| event | Event to publish |
Definition at line 116 of file EventBus.hpp.
References _subscribers, and LOG_DEBUG.
Referenced by GameLoop::handleLeftRoom(), Rendering::InitializeConnectionMenu(), Rendering::InitializeCreateRoomMenu(), Rendering::InitializeDefeatMenu(), Rendering::InitializeMainMenu(), Rendering::InitializeRoomListMenu(), Rendering::InitializeServerListMenu(), Rendering::InitializeSettingsMenu(), Rendering::InitializeVictoryMenu(), Rendering::InitializeWaitingRoomMenu(), Replicator::networkThreadLoop(), Replicator::processMessages(), Rendering::SubscribeToConnectionEvents(), and Rendering::UpdateUI().
| size_t EventBus::subscribe | ( | EventCallback< T > | callback | ) |
Subscribe to a specific event type.
Registers a callback that will be called each time an event of type T is published on this bus.
| T | Event type (must inherit from IEvent) |
| callback | Function called when event is published |
Definition at line 108 of file EventBus.hpp.
References _subscribers, and LOG_DEBUG.
Referenced by GameLoop::initialize(), and Rendering::SubscribeToConnectionEvents().
|
private |
Definition at line 103 of file EventBus.hpp.
Referenced by publish(), and subscribe().