Delegates - saezon/jsolibrary GitHub Wiki
This is a single header library for an event system or delegates taking inspiration in Epic's Unreal Engine 4 delegate system.
You can just import it to your project and use with without dependencies, defines or anything.
The file is in jsolibrary/src/Events.h
#include "Events.h"
DECLARE_DELEGATE_NO_PARAM(FNoParamDelegate);
DECLARE_DELEGATE_ONE_PARAM(FOneParamDelegate, TYPE);
DECLARE_DELEGATE_TWO_PARAM(FTwoParamDelegate, TYPE1, TYPE2);
DECLARE_DELEGATE_THREE_PARAM(FThreeParamDelegate, TYPE1, TYPE2, TYPE3);
Right now events up to 3 parameters are supported, the number will be increasing over time, so stay tuned!
FOneParamDelegate delegate;
void Callback(int value)
{
cout << "Callback: " << value << endl;
}
class Foo
{
public:
void callback(int value)
{
cout << value << endl;
}
};
int main() {
FOneParam delegate;
Foo foo;
delegate.addListener<Foo, &Foo::callback>(&foo);
delegate.addListener<&Callback>();
}
delegate.broadcast(0);
0
Callback: 0
1. Remove all bindings of the event
delegate.removeAll();
2. Remove a specific listener binding from delegate
delegate.removeListener<&Callback>();
delegate.removeListener<Foo, &Foo::callback>(&foo);
- Add events with more parameters
- Add remove binding option for an specific listener instead of having to delete them all
- Add remove binding of all listeners of a given instance
- Improve the performance of the events by getting rid of the vector holding all the listeners