c++-gtk-utils
|
The callback interface class. More...
#include <c++-gtk-utils/callback.h>
Public Member Functions | |
virtual void | dispatch (typename Cgu::Param< FreeArgs >::ParamType... args) const =0 |
CallbackArg () | |
virtual | ~CallbackArg () |
The callback interface class.
This class provides type erasure for callable objects. The CallbackArg type is constructed on free store and can wrap any callable object, such as a lambda expression or the return value of std::bind.
The class is particularly relevant where a callable object with bound values needs to be handed safely between threads, or in other cases where a callback object has to be passed by pointer (which will happen at some stage with glib or pthreads). They are therefore useful for general event passing when used together with the Callback::post() functions or as the continuation for GIO async operations, and are more efficient than constructing std::function objects on free store and passing them by pointer (they avoid one level of indirection and only require one rather than two allocations).
The classes are also used in the Emitter/EmitterArg and SafeEmitter/SafeEmitterArg classes in emitter.h, which enable callable objects to be connected to an emitter and provide for automatic disconnection where a class object whose member a callback represents or calls into ceases to exist. They are also used internally to implement the Thread::Future and Thread::TaskManager classes.
The template types are the types of the unbound arguments, if any. Callback::CallbackArg<> is typedef'ed to Callback::Callback. The main method of constructing a Callback/CallbackArg object is with the Callback::lambda() factory function. When using Callback::lambda(), the unbound argument types (if any) must be passed as explicit template parameters.
Callback/CallbackArg classes do not provide for a return value. If a result is wanted, users should pass an unbound argument by reference or pointer (or pointer to pointer).
Usage
These are examples:
For further background, including about the Callback::make(), Callback::make_ref() and Callback::to_unique() functions, read this: Callback
|
inline |
The constructor will not throw unless the copy constructor of an argument bound to the derived implementation class throws.
|
inlinevirtual |
The destructor will not throw unless the destructor of an argument bound to the derived implementation class throws.
|
pure virtual |
This will execute the referenced function, callable object or class method encapsulated by this class. It will only throw if the dispatched function, callable object or class method throws, or if the copy constructor of a free or bound argument throws and it is not a reference argument. It is thread safe if the referenced function or class method is thread safe.
args | The unbound arguments to be passed to the referenced function, callable object or class method, if any. |
Implemented in Cgu::Callback::Callback_lambda< Lambda, FreeArgs >, Cgu::Callback::Callback_function< FreeArgs >, Cgu::Callback::Callback5_static< unref, BoundArg1, BoundArg2, BoundArg3, BoundArg4, BoundArg5, FreeArgs >, Cgu::Callback::Callback4_static< unref, BoundArg1, BoundArg2, BoundArg3, BoundArg4, FreeArgs >, Cgu::Callback::Callback3_static< unref, BoundArg1, BoundArg2, BoundArg3, FreeArgs >, Cgu::Callback::Callback2_static< unref, BoundArg1, BoundArg2, FreeArgs >, Cgu::Callback::Callback1_static< unref, BoundArg, FreeArgs >, Cgu::Callback::Callback0_static< FreeArgs >, Cgu::Callback::Callback5_const< unref, T, BoundArg1, BoundArg2, BoundArg3, BoundArg4, BoundArg5, FreeArgs >, Cgu::Callback::Callback4_const< unref, T, BoundArg1, BoundArg2, BoundArg3, BoundArg4, FreeArgs >, Cgu::Callback::Callback3_const< unref, T, BoundArg1, BoundArg2, BoundArg3, FreeArgs >, Cgu::Callback::Callback2_const< unref, T, BoundArg1, BoundArg2, FreeArgs >, Cgu::Callback::Callback1_const< unref, T, BoundArg, FreeArgs >, Cgu::Callback::Callback0_const< T, FreeArgs >, Cgu::Callback::Callback5< unref, T, BoundArg1, BoundArg2, BoundArg3, BoundArg4, BoundArg5, FreeArgs >, Cgu::Callback::Callback4< unref, T, BoundArg1, BoundArg2, BoundArg3, BoundArg4, FreeArgs >, Cgu::Callback::Callback3< unref, T, BoundArg1, BoundArg2, BoundArg3, FreeArgs >, Cgu::Callback::Callback2< unref, T, BoundArg1, BoundArg2, FreeArgs >, Cgu::Callback::Callback1< unref, T, BoundArg, FreeArgs >, and Cgu::Callback::Callback0< T, FreeArgs >.