C# Style Events in C++
- Include
Event.h
in your project. It's all template based so that's it as far as integrating it. - Declare an event inside the class which will own the event (only the owner class can instantiate and raise the event).
- Use
+=
and-=
to add / remove delegates (removal takes a key -- part of the event type). - From the owner class scope, use the call operator to raise the event.
#include <iostream>
#include "Event.h"
class A {
public:
Event<A, std::string, void(int)> onEvent;
void clearEvent() {
onEvent.bindings.clear();
}
void raiseEvent() {
onEvent(2);
}
};
class B {
public:
static void staticHandler(int i) {
std::cout << "\nstatic handler: " << i;
}
void instanceHandler(int i) {
std::cout << "\ninstance handler: " << i;
}
};
void functionHandler(int i) {
std::cout << "\nfunction handler: " << i;
}
int main() {
A a;
B b;
a.onEvent += {"function", &functionHandler};
a.onEvent += {"static", &B::staticHandler};
a.onEvent += {"instance", &b, &B::instanceHandler};
a.onEvent += {"lambda", [](int i) {
std::cout << "\nlambda handler: " << i;
}};
return 0;
}
There are also multievents. These basically group related events so they can easily be indexed by an arbitrary key:
class Input {
public:
enum Key {A,W,S,D};
MultiEvent<Input, Key, std::string, void()> onKeyPressed;
};
int main() {
Input input;
input.onKeyPressed[Input::Key::A] += {"left key handler", []() {
// move player left
}};
// ...
}
Copyright (c) 2016 Federico Saldarini