MochiEngine
Loading...
Searching...
No Matches
Mochi::Event::EventBus Class Reference

The event bus is a Pub-Sub implementation that accepts any type as event data. More...

#include <EventBus.h>

Public Member Functions

template<typename EventType>
SubscriptionHandler Subscribe (std::function< void(const EventType &)> callback)
 Subscribe to a specific event type.
template<typename EventType>
void Unsubscribe (SubscriptionHandler handler)
 Unsubscribe from a specific event. If the event type didn't exist or the handler wasn't subscribed, nothing happenns.
template<typename EventType>
void Publish (const EventType &event)
 Publish an event, triggering all callbacks subscribed to it with the provided data.

Detailed Description

The event bus is a Pub-Sub implementation that accepts any type as event data.

A subscription handler is the identifier of the subscription, needed on a per-subscription basis in order to unsubscribe for it.

Guarantees:

  • Anyone can subscribe to any type of event at any time.
  • Anyone can publish any type of event at any time.
  • Anyone can unsubscribe from an event at any time.

Non responsible:

  • Publishing to an event where a destroyed class was subscribed and didn't unsubscribe can cause unexpected consecuences.

Member Function Documentation

◆ Publish()

template<typename EventType>
void Mochi::Event::EventBus::Publish ( const EventType & event)
inline

Publish an event, triggering all callbacks subscribed to it with the provided data.

Parameters
EventTypeThe event type to which to publish.
eventThe event data of type EventType.

◆ Subscribe()

template<typename EventType>
SubscriptionHandler Mochi::Event::EventBus::Subscribe ( std::function< void(const EventType &)> callback)
inline

Subscribe to a specific event type.

Parameters
EventTypeThe event type to which to subscribe. Whatever type works. It is recommended to use a type (struct or class) specifically created for that event.
callbackThe callback to be called when the event is published.

◆ Unsubscribe()

template<typename EventType>
void Mochi::Event::EventBus::Unsubscribe ( SubscriptionHandler handler)
inline

Unsubscribe from a specific event. If the event type didn't exist or the handler wasn't subscribed, nothing happenns.

Parameters
EventTypeThe event type from which to unsubscribe.
handlerThe identifier of the subscription.

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