Simple C++ Game
Public Member Functions | List of all members
Dispatcher Class Reference

A class for managing events. More...

#include <Dispatcher.hpp>

Public Member Functions

 Dispatcher ()=default
 Construct a new Dispatcher object.
 
void subscribe (const std::string type, const std::function< void(const Event &)> observer)
 Subscribe a specific listener to a specific event type. More...
 
void subscribeGlobal (const std::function< void(const Event &)> observer)
 Subscribe a specific listener to all event types. More...
 
void dispatch (const Event &event)
 Dispatches an event. More...
 
void queue (const Event &event)
 Queues an event to be dispatched later. More...
 
void flush ()
 Dispatches all queued events. More...
 

Detailed Description

A class for managing events.

The Dispatcher is the main class of the event system. Publishers use the Dispatcher to post their events and subscribers use the Dispatcher to receive their events.

Member Function Documentation

◆ dispatch()

void Dispatcher::dispatch ( const Event event)

Dispatches an event.

Dispatches the provided event to all subscribed observers. All observer that subscribed to event.getType() will be invoked in the same order that they subscribed. Global subscribers are called after specific subscribers.

dispatch() should be used over queue() / flush() when the event should be handled immediately, before execution in the current method resumes. This is useful for in-frame communcation or small status updates that should be conveyed to other entities as soon as possible.

Parameters
eventThe event to dispatch

◆ flush()

void Dispatcher::flush ( )

Dispatches all queued events.

Events are dispatched in the order they were queued

◆ queue()

void Dispatcher::queue ( const Event event)

Queues an event to be dispatched later.

Queues the provided event. It will be dispatched to all subscribed observers the next time flush() is called. All observer that subscribed to event.getType() will be invoked in the same order that they subscribed. Global subscribers are called after specific subscribers.

queue() / flush() should be used over dispatch() when the event should be handled later, such as if the event is likely to trigger significant effects, like screen transitions that might require destroying the object creating the event.

Parameters
eventThe event to dispatch

◆ subscribe()

void Dispatcher::subscribe ( const std::string  type,
const std::function< void(const Event &)>  observer 
)

Subscribe a specific listener to a specific event type.

Parameters
typeThe string identifying the type of event. This is idiomatically declared as a static member in the class that produces the event.
observerThe function to be invoked when the event is dispatched. It is assumed any references used by the function will be valid for at least as long as the dispatcher exists.

◆ subscribeGlobal()

void Dispatcher::subscribeGlobal ( const std::function< void(const Event &)>  observer)

Subscribe a specific listener to all event types.

Parameters
observerThe function to be invoked when the event is dispatched. It is assumed any references used by the function will be valid for at least as long as the dispatcher exists.

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