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).
The 2.2 series of the library generally dispenses with the need to create objects explicitly using Callback::lambda() when calling into the library itself: all the library interfaces which take a Callback/CallbackArg object also now provide an overload which takes any callable object as a template type, and the implementation calls Callback::lambda() internally for you.
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_fun_tuple< Func, Tuple, FreeArgs >, Cgu::Callback::Callback_memfun< T, MemFunc, FreeArgs >, Cgu::Callback::Callback_memfun_tuple< T, MemFunc, Tuple, FreeArgs >, and Cgu::Callback::Callback_lambda< Lambda, FreeArgs >.