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

Type-safe event publication/subscription system. More...

#include <EventBus.hpp>

Collaboration diagram for EventBus:
Collaboration graph

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
 

Detailed Description

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:

  • Components subscribe to specific event types
  • When an event is published, all concerned subscribers are notified
  • Type-safe thanks to C++ templates

Usage example:

EventBus eventBus;
// Subscribe to an event
eventBus.subscribe<InputEvent>([](const InputEvent& e) {
std::cout << "Input received!" << std::endl;
});
// Publish an event
@ SHOOT
Fire weapon.
@ PRESSED
Input just pressed this frame.
Type-safe event publication/subscription system.
Definition EventBus.hpp:44
void publish(const T &event)
Publish an event to all subscribers.
Definition EventBus.hpp:116
size_t subscribe(EventCallback< T > callback)
Subscribe to a specific event type.
Definition EventBus.hpp:108
Event representing a player input action.

Definition at line 44 of file EventBus.hpp.

Member Typedef Documentation

◆ EventCallback

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

Callback type for events.

Template Parameters
TEvent type (must inherit from IEvent)

Definition at line 51 of file EventBus.hpp.

Constructor & Destructor Documentation

◆ EventBus()

EventBus::EventBus ( )
default

Default constructor.

◆ ~EventBus()

EventBus::~EventBus ( )
default

Default destructor.

Member Function Documentation

◆ clear()

void EventBus::clear ( )

Clear all subscriptions.

Removes all registered callbacks for all event types. Useful for system reset or avoiding memory leaks.

◆ publish()

template<typename T >
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.

Template Parameters
TEvent type (must inherit from IEvent)
Parameters
eventEvent to publish
Note
If no subscriber exists, the event is ignored
Exceptions in callbacks are not handled

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().

◆ subscribe()

template<typename T >
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.

Template Parameters
TEvent type (must inherit from IEvent)
Parameters
callbackFunction called when event is published
Returns
Subscription identifier (for future unsubscription)
Note
A component can subscribe multiple times to the same type
Callbacks are called in registration order

Definition at line 108 of file EventBus.hpp.

References _subscribers, and LOG_DEBUG.

Referenced by GameLoop::initialize(), and Rendering::SubscribeToConnectionEvents().

Member Data Documentation

◆ _subscribers

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

Definition at line 103 of file EventBus.hpp.

Referenced by publish(), and subscribe().


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