Sapphire Suite's C++ Event and Function library.
Links to the official documentation.
#include <SA/Collections/Event>
Add the subdirectory to the build tree and link the library to your taget.
add_subdirectory(Event)
target_link_libraries(<target> <link> SA_Event)
Create your event of type Event<R(Args...)> with R as return type and Args... as argument types:
Event<void(int)> ev;
Then subscribe/unsubscribe functions to the event:
struct MyStruct
{
void Bar(int _i);
static void Foo(int _i);
};
// Static function.
ev += &MyStruct::Foo;
// Member function.
MyStruct mem;
ev.Add(&mem, &MyStruct::Bar);
// Lambda function.
auto lambda = [](int _i) {};
ev += lambda;
// Unsubscribe.
ev.Remove(&mem, &MyStruct::Bar);
ev -= lambda;
To raise the event, simply use the operator() or the Execute method:
ev(5);
ev.Execute(6);
For event with return types, you can pass a std::vector to get the output results:
Event<float(int)> rev;
std::vector<float> results;
rev.Execute(results, 7);
For more examples, see Event Unit Tests.
OwnedEvent is a specialized event type that can only be raised by a owner class.
Create your owned-event of type OwnedEvent<OwnerT, R(Args...)> with OwnerT as owner class type, R as return type and Args... as argument types:
class MyClass
{
public:
OwnedEvent<MyClass, void(int)> ev;
void Do(int _param)
{
// Only accessible through MyClass.
ev.Execute(_param);
}
}
MyClass mClass;
mClass.ev += &MyStruct::Foo;
mClass.ev -= &MyStruct::Foo;
// Not accessible outside of MyClass.
// mClass.ev(4);
For more examples, see OwnedEvent Unit Tests.
Function is used to easliy store a pointer to function. Create your function of type Function<R(Args...)> with R as return type and Args... as argument types:
Function<void(int)> func;
Then assign a function:
struct MyStruct
{
void Bar(int _i);
static void Foo(int _i);
};
// Static function.
func = &MyStruct::Foo;
// Member function.
MyStruct mem;
func.Set(&mem, &MyStruct::Bar);
// Lambda function.
auto lambda = [](int _i) {};
func = lambda;
// Clear.
func.Clear();
func = nullptr;
To raise the function, simply use the operator() or the Execute method:
func(5);
func.Execute(6);
For function with return types, the value is normally returned:
Function<float(int)> rfunc;
float result = rfunc.Execute(7);
For more examples, see Function Unit Tests.
PackedFunction is used to easliy store a pointer to function with packed arguments. Create your packed function of type PackedFunction<R(Args...)> with R as return type and Args... as argument types:
PackedFunction<void(int)> pfunc;
Then assign a function:
struct MyStruct
{
void Bar(int _i);
static void Foo(int _i);
};
// Static function.
pfunc.Set(&MyStruct::Foo, 5);
// Member function.
MyStruct mem;
pfunc.Set(&mem, &MyStruct::Bar, 5);
// Lambda function.
auto lambda = [](int _i) {};
pfunc.Set(lambda, 5);
// Clear.
pfunc.Clear();
pfunc = nullptr;
To raise the packed function, simply use the operator() or the Execute method:
pfunc();
pfunc.Execute();
For function with return types, the value is normally returned:
PackedFunction<float(int)> rpfunc;
float result = rpfunc.Execute();
For more examples, see Packed Unit Tests.
Maxime "mrouffet" ROUFFET - main developer (maximerouffet@gmail.com)
Clément "cfazilleau" FAZILLEAU - OwnedEvent implementation request.