Single-header C++ Event System.
- Easy to use external API
- Simple, intuitive syntax
- No binding or passing in lambdas required
- Events are simple PODS
- No inheritance of a base
Event
class - No explicit registering of events
- No inheritance of a base
- Efficient operation
- Suitable for real-time applications
- Delayed event queues
- Fired events can be pushed to a queue for deferred execution
- Asynchronous events
- C++17
#include "EventManager.h"
const int key_w = 87;
struct KeyPressEvent
{
int key_code;
}
void on_key_press(KeyPressEvent* event)
{
// ...
}
int main()
{
EventManager::listen<KeyPressEvent>(&on_key_press);
EventManager::fire<KeyPressEvent>(key_w);
}
#include "EventManager.h"
const int key_w = 87;
struct KeyPressEvent
{
int key_code;
}
class InputHandler
{
public:
InputHandler()
{
EventManager::listen<KeyPressEvent>(this, &InputHandler::on_key_press);
}
private:
void on_key_press(KeyPressEvent* event)
{
// ...
}
}
int main()
{
InputHandler input_handler;
EventManager::fire<KeyPressEvent>(key_w);
}
A ListenerHandle
is returned to the user upon registering an event callback, and each callback function is uniquely associated with its given handle.
#include "EventManager.h"
const int key_w = 87;
struct KeyPressEvent
{
int key_code;
}
void on_key_press(KeyPressEvent* event)
{
// ...
}
int main()
{
ListenerHandle handle = EventManager::listen<KeyPressEvent>(&on_key_press);
EventManager::remove_listener(handle);
}
Event Manager is a single-header library so no build is required. You can either clone or directly download the repository and place EventManager.h
in your include folder.
From here it is as simple as #include "EventManager.h"
and you're ready to go.