The event bus is a Pub-Sub implementation that accepts any type as event data.
More...
#include <EventBus.h>
|
| 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.
|
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.
◆ 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
-
| EventType | The event type to which to publish. |
| event | The 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
-
| EventType | The event type to which to subscribe. Whatever type works. It is recommended to use a type (struct or class) specifically created for that event. |
| callback | The 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
-
| EventType | The event type from which to unsubscribe. |
| handler | The identifier of the subscription. |
The documentation for this class was generated from the following file: