A typesafe callback system for standard C++0x - a bit like libsigc++
Libsigc++ has much more features, such as non-void return types, marshallers and trackable base class for automatic disconnection. Sig0x has less than 300loc, is probably faster to parse and compile, and mostly just an experiment :)
sig0x::Slot<int, float> event;
void func(int a, float b);
event.bind(&func);
void func(int a, float b);
static void static_func(int a, float b);
event.bind<&func>();
event.bind<&static_func>(); // This won't work
event.bind([](int a, float b){
std::cout << "recieved two numbers: " << a << "," << b << std::endl; });
struct Object {
void func(int a, float b);
};
Object o;
event.bind(o, &Object::func);
// Or the 'fast' variant
event.with(o).bind<&Object::func>();
event.fire(42, 3.14f);
// Or the simple variant
event(42, 3.14f);
auto link = event.bind(func);
link.release();
{
sig0x::Slot<int, float>::AutoLink autolink = event.bind(func);
} // link released here
This one is particularly useful if used as a member of a class to disconnect event recievers when 'this' is deleted.
struct Object {
sig0x::Slot<int, float>::AutoLink autolink;
void func(int a, float b);
};
sig0x::Slot<int, float> event;
{
std::unique_ptr<Object> o(new Object());
o->autolink = event.with(o).bind(&Object::func);
} // o is deleted
// it's safe, no dangling link between event and the deleted o
event.fire(42, 3.14f);