Create and publish events. More...
#include <EventEmitter.h>
Public Member Functions | |
EventEmitter () | |
Default constructor. More... | |
EventEmitter (const EventEmitter &) | |
Copy constructor. More... | |
EventEmitter & | operator= (const EventEmitter &) |
Copy assignment operator. More... | |
EventEmitter (EventEmitter &&) noexcept | |
Move constructor. More... | |
EventEmitter & | operator= (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... | |
Create and publish events.
Definition at line 43 of file EventEmitter.h.
ime::EventEmitter::EventEmitter | ( | ) |
Default constructor.
ime::EventEmitter::EventEmitter | ( | const EventEmitter & | ) |
Copy constructor.
|
noexcept |
Move constructor.
int ime::EventEmitter::addEventListener | ( | const std::string & | event, |
Callback< Args... > | callback | ||
) |
Add an event listener (callback) to an event.
event | Event to add event listener to |
callback | Function to execute when the event is fired |
Every event listener has a unique identification number. This number must be remembered in order to remove the event listener.
int ime::EventEmitter::addOnceEventListener | ( | const std::string & | event, |
Callback< Args... > | callback | ||
) |
Add an event listener to an event.
event | Event to add event listener to |
callback | Function to execute when the event is fired |
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
void ime::EventEmitter::clear | ( | ) |
Remove all events and event listeners.
void ime::EventEmitter::emit | ( | const std::string & | event, |
Args... | args | ||
) |
Fire an event.
event | Name of the event to fire |
args | Arguments to be passed to event listeners |
std::size_t ime::EventEmitter::getEventListenerCount | ( | const std::string & | event | ) | const |
Get the number of event listeners currently registered to an event.
event | Event to get number of event listeners for |
std::vector< std::string > ime::EventEmitter::getEvents | ( | ) | const |
Get the list of registered events.
std::size_t ime::EventEmitter::getEventsCount | ( | ) | const |
Get the current number of created events.
bool ime::EventEmitter::hasEvent | ( | const std::string & | event | ) | const |
Check if an event exists or not.
event | Name of the event to check |
bool ime::EventEmitter::hasEventListener | ( | const std::string & | event, |
int | id | ||
) | const |
Check if an event has a certain event listener.
event | Name of the event |
id | Identification number of the listener to be checked |
bool ime::EventEmitter::isActive | ( | ) | const |
bool ime::EventEmitter::isEventListenerSuspended | ( | const std::string & | event, |
int | id | ||
) | const |
Check if an event listener is suspended or not.
event | The name of the event the event listener is subscribed to |
id | The event listeners identifier |
This function also returns false if the specified event or event listener do not exist
bool ime::EventEmitter::isEventListenerSuspended | ( | int | id | ) | const |
Check if an event listener is suspended or not.
id | The event listeners identifier |
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)
int ime::EventEmitter::on | ( | const std::string & | event, |
Callback< Args... > | callback | ||
) |
Add an event listener to an event.
event | Event to add event listener to |
callback | Function to execute when the event is fired |
This function does the same thing as the addEventListener() function. It just provides a slightly more readable syntax:
EventEmitter & ime::EventEmitter::operator= | ( | const EventEmitter & | ) |
Copy assignment operator.
|
noexcept |
Move assignment operator.
bool ime::EventEmitter::removeAllEventListeners | ( | const std::string & | event | ) |
bool ime::EventEmitter::removeEventListener | ( | const std::string & | event, |
int | id | ||
) |
Remove an event listener from an event.
event | Event to remove listener from |
id | Identification number of the event listener to be removed |
bool ime::EventEmitter::removeEventListener | ( | int | id | ) |
Remove an event listener from an event.
id | Identification number of the event listener to be removed |
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
void ime::EventEmitter::setActive | ( | bool | active | ) |
Set whether or not the emitter is active.
active | True 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
bool ime::EventEmitter::suspendEventListener | ( | const std::string & | event, |
int | id, | ||
bool | suspend | ||
) |
Suspend the execution of an event listener.
event | The name of the event the listener is subscribed to |
id | The event listeners identification number |
suspend | True to suspend or false to unsuspend |
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
bool ime::EventEmitter::suspendEventListener | ( | int | id, |
bool | suspend | ||
) |
Suspend the execution of an event listener.
id | The event listeners identification number |
suspend | True to suspend or false to unsuspend |
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