Classes | Public Member Functions | List of all members
ime::EventEmitter Class Reference

Class for creating and publishing events. More...

#include <EventEmitter.h>

Inheritance diagram for ime::EventEmitter:
ime::audio::Audio ime::audio::Music ime::audio::SoundEffect

Public Member Functions

 EventEmitter ()=default
 Default constructor. More...
 
 EventEmitter (const EventEmitter &)
 Copy constructor. More...
 
EventEmitteroperator= (const EventEmitter &)
 Copy assignment operator. More...
 
 EventEmitter (EventEmitter &&) noexcept
 Move constructor. More...
 
EventEmitteroperator= (EventEmitter &&) noexcept
 Move assignment operator. More...
 
template<typename... Args>
int addEventListener (const std::string &event, Callback< Args... > callback)
 Add an event listener (callback) to an event. More...
 
template<typename... Args>
int on (const std::string &event, Callback< Args... > callback)
 Add an event listener to an event. More...
 
template<typename ... Args>
int addOnceEventListener (const std::string &event, Callback< Args... > callback)
 Add an event listener to an event. More...
 
bool removeEventListener (const std::string &event, int id)
 Remove an event listener from an event. More...
 
bool removeAllEventListeners (const std::string &event)
 Remove all event listeners of an event. More...
 
template<typename... Args>
void emit (const std::string &event, Args...args)
 Fire an event. More...
 
bool hasEvent (const std::string &event) const
 Check if an event exists or not. More...
 
std::size_t getEventListenerCount (const std::string &event) const
 Get the number of event listeners currently registered to an event. More...
 
std::size_t getEventsCount () const
 Get the current number of created events. More...
 
bool hasEventListener (const std::string &event, int id) const
 Check if an event has a certain event listener. More...
 

Detailed Description

Class for creating and publishing events.

Note
There is no function for creating an event. Events are created when a listener/callback is added for the first time. All Subsequent event listeners will be added to the created event. Events are not fired automatically, a call to the emit function must be made to fire an event and notify event listeners

Definition at line 49 of file EventEmitter.h.

Constructor & Destructor Documentation

◆ EventEmitter() [1/3]

ime::EventEmitter::EventEmitter ( )
default

Default constructor.

◆ EventEmitter() [2/3]

ime::EventEmitter::EventEmitter ( const EventEmitter )

Copy constructor.

◆ EventEmitter() [3/3]

ime::EventEmitter::EventEmitter ( EventEmitter &&  )
noexcept

Move constructor.

Member Function Documentation

◆ addEventListener()

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

Add an event listener (callback) to an event.

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

Every event listener has a unique identification number. This number must be remembered in order to remove the event listener.

Note
If the same callback function is added multiple times, It will be treated as a unique event listener and hence given an identification number

◆ addOnceEventListener()

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

Add an event listener to an event.

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

The event listener will be invoked once and subsequently removed from the event. This means that the callback will only execute when an event is fired for the first time. To execute a callback each time the an event is fired

See also
addEventListener

◆ emit()

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

Fire an event.

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

◆ getEventListenerCount()

std::size_t ime::EventEmitter::getEventListenerCount ( const std::string &  event) const

Get the number of event listeners currently registered to an event.

Parameters
eventEvent to get number of event listeners for
Returns
The number of event listeners registered to an event or 0 if no such event exists

◆ getEventsCount()

std::size_t ime::EventEmitter::getEventsCount ( ) const

Get the current number of created events.

Returns
Current umber of events

◆ hasEvent()

bool ime::EventEmitter::hasEvent ( const std::string &  event) const

Check if an event exists or not.

Parameters
eventName of the event to check
Returns
True if event exists or false if the event does not exist

◆ hasEventListener()

bool ime::EventEmitter::hasEventListener ( const std::string &  event,
int  id 
) const

Check if an event has a certain event listener.

Parameters
eventName of the event
idIdentification number of the listener to be checked
Returns
True if the specified event has an event listener with the specified id, otherwise false

◆ on()

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

Add an event listener to an event.

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

This function does the same thing as the addEventListener() function. It just provides a slightly more readable syntax:

returnButton.on("click", showMainMenu); as opposed to
returnButton.addEventListener("click", showMainMenu);

◆ operator=() [1/2]

EventEmitter& ime::EventEmitter::operator= ( const EventEmitter )

Copy assignment operator.

◆ operator=() [2/2]

EventEmitter& ime::EventEmitter::operator= ( EventEmitter &&  )
noexcept

Move assignment operator.

◆ removeAllEventListeners()

bool ime::EventEmitter::removeAllEventListeners ( const std::string &  event)

Remove all event listeners of an event.

Parameters
eventEvent to remove all listeners from
Returns
True if all listeners were removed, false if no such event exists

◆ removeEventListener()

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

Remove an event listener from an event.

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

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