Class for creating and publishing events.
More...
#include <EventEmitter.h>
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.
◆ EventEmitter() [1/3]
ime::EventEmitter::EventEmitter |
( |
| ) |
|
|
default |
◆ EventEmitter() [2/3]
◆ EventEmitter() [3/3]
◆ addEventListener()
template<typename... Args>
int ime::EventEmitter::addEventListener |
( |
const std::string & |
event, |
|
|
Callback< Args... > |
callback |
|
) |
| |
Add an event listener (callback) to an event.
- Parameters
-
event | Event to add event listener to |
callback | Function 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
-
event | Event to add event listener to |
callback | Function 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
-
event | Name of the event to fire |
args | Arguments 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
-
event | Event 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
-
event | Name 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
-
event | Name of the event |
id | Identification 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
-
event | Event to add event listener to |
callback | Function 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]
Copy assignment operator.
◆ operator=() [2/2]
Move assignment operator.
◆ removeAllEventListeners()
bool ime::EventEmitter::removeAllEventListeners |
( |
const std::string & |
event | ) |
|
Remove all event listeners of an event.
- Parameters
-
event | Event 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
-
event | Event to remove listener from |
id | Identification 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: