A header-only event emitter implementation written in C++ 20.
#include "EventEmitter.hpp"
auto diff = [](int a, int b) { std::cout << "a - b = " << a - b << '\n'; };
EventEmitter emitter;
emitter.on("diff", diff);
emitter.emit("diff", 1, 2);
// prints: a - b = -1
Adds a listener to the emitter, listening for the event specified. Essentially adds the string to the map and adds the functor to a list of functors to run when the event is emitted.
emitter.on("login_event", on_login);
Parameter | Type | Description |
---|---|---|
event_name |
string_view |
Required. The event name to listen for. |
callback |
Event |
Required. The actual callable function to run when at the time of the event. |
Emits the event to the list of listeners, calling them all with the arguments provided.
emitter.emit("login_event", user_name, timestamp);
Parameter | Type | Description |
---|---|---|
event_name |
string_view |
Required. The event to emit to the list of listeners. |
args |
Args... |
The arguments to pass to the listening functions. |