Cgu::Callback::CallbackArg< FreeArgs > Class Template Referenceabstract

The callback interface class. More...

`#include <c++-gtk-utils/callback.h>`

Inheritance diagram for Cgu::Callback::CallbackArg< FreeArgs >:

## Public Member Functions

virtual void | dispatch (typename Cgu::Param< FreeArgs >::ParamType...args) const =0
| CallbackArg ()
virtual | ~CallbackArg ()

## Detailed Description

### template<class... FreeArgs>
class Cgu::Callback::CallbackArg< FreeArgs >

The callback interface class.

See Also
  Callback namespace
  FunctorArg SafeFunctorArg

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 arguments (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:

using namespace Cgu;
// here cb1 is of type Callback::Callback*
auto cb1 = Callback::lambda<>([] () {std::cout << "Hello world\n";});
cb1->dispatch();
delete cb1;
// here cb2 is of type Callback::CallbackArg<int, int&>*
auto cb2 = Callback::lambda<int, int&>([] (int i, int& j) {j = 10 * i;});
int res;
cb2->dispatch(2, res);
std::cout << "10 times 2 is " << res << '\n';
delete cb2;

For further background, including about the Callback::make(), Callback::make_ref() functions, read this: Callback

## Constructor & Destructor Documentation

template<class... FreeArgs>

Cgu::Callback::CallbackArg< FreeArgs >::CallbackArg | ( | ) |  |

inline

The constructor will not throw unless the copy constructor of an argument bound to the derived implementation class throws.

template<class... FreeArgs>

virtual Cgu::Callback::CallbackArg< FreeArgs >::~CallbackArg | ( | ) |  |

inlinevirtual

The destructor will not throw unless the destructor of an argument bound to the derived implementation class throws.

## Member Function Documentation

template<class... FreeArgs>

virtual void Cgu::Callback::CallbackArg< FreeArgs >::dispatch | ( | typename Cgu::Param< FreeArgs >::ParamType... | args) |  | const

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.

Parameters

  args | The unbound arguments to be passed to the referenced function, callable object or class method, if any.

Note
  We use dispatch() to execute the callback, because the callback would normally be invoked through a base class pointer. To invoke it through operator()(), use the FunctorArg or SafeFunctorArg wrapper class.

Implemented in Cgu::Callback::Callback_lambda< Lambda, 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 >.

The documentation for this class was generated from the following file:

- callback.h