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

Create and publish events. More...

#include <EventEmitter.h>

Public Member Functions

 EventEmitter ()
 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 removeEventListener (int id)
 Remove an event listener from an event. More...
 
bool removeAllEventListeners (const std::string &event)
 Remove all event listeners of an event. More...
 
void clear ()
 Remove all events and event listeners. More...
 
template<typename... Args>
void emit (const std::string &event, Args...args)
 Fire an event. More...
 
bool suspendEventListener (const std::string &event, int id, bool suspend)
 Suspend the execution of an event listener. More...
 
bool suspendEventListener (int id, bool suspend)
 Suspend the execution of an event listener. More...
 
bool isEventListenerSuspended (const std::string &event, int id) const
 Check if an event listener is suspended or not. More...
 
bool isEventListenerSuspended (int id) const
 Check if an event listener is suspended or not. 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...
 
std::vector< std::string > getEvents () const
 Get the list of registered events. More...
 
void setActive (bool active)
 Set whether or not the emitter is active. More...
 
bool isActive () const
 Check if the event emitter is active. More...
 

Detailed Description

Create and publish events.

Definition at line 43 of file EventEmitter.h.

Constructor & Destructor Documentation

◆ EventEmitter() [1/3]

ime::EventEmitter::EventEmitter ( )

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

◆ clear()

void ime::EventEmitter::clear ( )

Remove all events and event listeners.

See also
removeAllEventListeners, removeAllEventListener

◆ 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

◆ getEvents()

std::vector< std::string > ime::EventEmitter::getEvents ( ) const

Get the list of registered events.

Returns
A list of event names currently registered with the emitter

◆ 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

◆ isActive()

bool ime::EventEmitter::isActive ( ) const

Check if the event emitter is active.

Returns
True if active, otherwise false
See also
setActive

◆ isEventListenerSuspended() [1/2]

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

Check if an event listener is suspended or not.

Parameters
eventThe name of the event the event listener is subscribed to
idThe event listeners identifier
Returns
True if it is suspended, otherwise false

This function also returns false if the specified event or event listener do not exist

See also
suspendEventListener(const std::string&, int, bool) and suspendEventListener(int, bool)

◆ isEventListenerSuspended() [2/2]

bool ime::EventEmitter::isEventListenerSuspended ( int  id) const

Check if an event listener is suspended or not.

Parameters
idThe event listeners identifier
Returns
True if it is suspended, otherwise false

This function also returns false if event listener does not exist. In addition, the function searches for the event listener in all events. Therefore it may be slower than suspendEventListener(const std::string&, int, bool)

See also
isEventListenerSuspended(const std::string&, int)

◆ 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
See also
clear

◆ removeEventListener() [1/2]

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
See also
clear, removeEventListener(int);

◆ removeEventListener() [2/2]

bool ime::EventEmitter::removeEventListener ( int  id)

Remove an event listener from an event.

Parameters
idIdentification number of the event listener to be removed
Returns
True if the event listener was removed or false if no such event listener exists

This function searches for the event listener in all events. Therefore it may be slower than removeEventListener(const std::string&, int) which searches in a specific event

See also
removeEventListener(const std::string&, int)

◆ setActive()

void ime::EventEmitter::setActive ( bool  active)

Set whether or not the emitter is active.

Parameters
activeTrue to activate or false to deactivate

When the emitter is deactivated, it stops emitting events. This means than all calls to emit() will be ignored

By default, the emitter is activated

See also
isEnabled

◆ suspendEventListener() [1/2]

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

Suspend the execution of an event listener.

Parameters
eventThe name of the event the listener is subscribed to
idThe event listeners identification number
suspendTrue to suspend or false to unsuspend
Returns
True if the event listener was suspended or unsuspended, or false if either the event or the event listener does not exist

When suspended, the event listener will be ignored and not invoked/notified when the event its listening for is emitted/fired

By default an event listener is not suspended

See also
isEventListenerSuspended, suspendEventListener(int, bool)

◆ suspendEventListener() [2/2]

bool ime::EventEmitter::suspendEventListener ( int  id,
bool  suspend 
)

Suspend the execution of an event listener.

Parameters
idThe event listeners identification number
suspendTrue to suspend or false to unsuspend
Returns
True if the event listener was suspended or unsuspended, or false if either the event or the event listener does not exist

When suspended, the event listener will be ignored and not invoked/notified when the event its listening for is emitted/fired.

Note that this function will search for the event listener in all events. Therefore it may be slower than suspendEventListener(const std::string&, int, bool)

By default an event listener is not suspended

See also
isEventListenerSuspended, suspendEventListener(const std::string&, int, bool)

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