ClanLib includes a complete signalling library – a signal library is a C++ template library that allow you to use type-safe C++ templates to setup function callbacks.
The library currently supports two different types of callback templates:
A signal is an object that, when emitted, invokes one or more slot functions. This setup allows a one-way messaging system that informs a number of functions whenever a certain event occours. A simple example with one signal and two slot functions:
The 'v2' part of clan::Signal_v2 indicates that the slot functions returns void and take 2 parameters. The types of the parameters are then defined as int, int. When the clan::Slot handle object returned by clan::Signal_vX::connect is destroyed, the slot function is disconnected from the signal. If you plan to connect a lot of slots to signals, the clan::SlotContainer class may come in handy:
Just like with the clan::Thread::start and clan::string_format functions, the slot callback function can be placed in a class and have additional fixed parameters passed along:
The second type of callback template classes available is clan::Callback_X. It simply calls one callback function when invoked, just like a standard C style function pointer would do.
Callbacks are used in many ClanLib classes, like the func_expired callback in clan::Timer :