Public Types | Public Member Functions | Static Public Member Functions | List of all members
ime::EventDispatcher Class Reference

A singleton class that creates a communication interface between separate parts of a program through event dispatching. More...

#include <EventDispatcher.h>

Public Types

using Ptr = std::shared_ptr< EventDispatcher >
 Shared EventDispatcher pointer. More...
 

Public Member Functions

 EventDispatcher (const EventDispatcher &)=delete
 Copy constructor. More...
 
EventDispatcheroperator= (const EventDispatcher &)=delete
 Assignment operator. More...
 
template<typename... Args>
int onEvent (const std::string &event, Callback< Args... > callback)
 Add an event listener to an event. More...
 
template<typename... Args>
void dispatchEvent (const std::string &event, Args &&...args)
 Fire an event. More...
 
bool removeEventListener (const std::string &event, int id)
 Remove an event listener from an event. More...
 

Static Public Member Functions

static EventDispatcher::Ptr instance ()
 Get class instance. More...
 

Detailed Description

A singleton class that creates a communication interface between separate parts of a program through event dispatching.

The global event emitter is available to anything class that needs it (class, function etc..).

Its responsibility is to decouple classes from one another. You can emit a signal and anyone listening for that signal will pick it up without knowing and caring where the signal came from. Here is a simple example:

// main.cpp
// We subscribe to a loading event that will be dispatched by some scene
// we don't know or care about, we are just interested in knowing that
// resource loading is complete
EventDispatcher::instance()->onEvent("loadingComplete", Callback<>([&engine] {
engine.popScene(); // Remove the scene that emitted the event
engine.pushScene(gameplayScene); // Start the gameplay scene
}));
engine.pushScene(loadingScene);
engine.run();
// LoadingScene.cpp
// ...
// The caller does not need to keep asking the loading scene if it has
// finished loading the game assets, the scene lets everyone whose is
// interested know when it is done without knowing and caring who they are
EventDispatcher::instance::dispatchEvent("loadingComplete");
static EventDispatcher::Ptr instance()
Get class instance.
Note
This classes instance is accessible from anywhere withing the program, however the instance is destroyed when the last pointer to it goes out of scope. This means that all event listeners that have been registered will be destroyed and a call to dispatchEvent will not do anything. Therefore there must be at least one pointer to the class instance that keeps it alive for as long as its being used. The Engine keeps an instance alive for as long as it is running, therefore you should use the global dispatcher only when the engine is running or keep an instance alive yourself

Definition at line 38 of file EventDispatcher.h.

Member Typedef Documentation

◆ Ptr

using ime::EventDispatcher::Ptr = std::shared_ptr<EventDispatcher>

Shared EventDispatcher pointer.

Definition at line 40 of file EventDispatcher.h.

Constructor & Destructor Documentation

◆ EventDispatcher()

ime::EventDispatcher::EventDispatcher ( const EventDispatcher )
delete

Copy constructor.

Member Function Documentation

◆ dispatchEvent()

template<typename... Args>
void ime::EventDispatcher::dispatchEvent ( const std::string &  event,
Args &&...  args 
)

Fire an event.

Parameters
eventName of the event to fire
argsArguments to be passed to event listeners

This function will invoke all event listeners of the specified event

◆ instance()

static EventDispatcher::Ptr ime::EventDispatcher::instance ( )
static

Get class instance.

Returns
Shared pointer to class instance

◆ onEvent()

template<typename... Args>
int ime::EventDispatcher::onEvent ( const std::string &  event,
Callback< Args... >  callback 
)

Add an event listener to an event.

Parameters
eventEvent to add an event listener to
callbackFunction to execute when the event is fired
Returns
The event listeners identification number

◆ operator=()

EventDispatcher & ime::EventDispatcher::operator= ( const EventDispatcher )
delete

Assignment operator.

◆ removeEventListener()

bool ime::EventDispatcher::removeEventListener ( const std::string &  event,
int  id 
)

Remove an event listener from an event.

Parameters
eventEvent to remove event listener from
idIdentification number of the event listener to be removed
Returns
True if the event listener was removed from the specified event or false if the specified event does not have an event listener with the specified id

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