c++-gtk-utils
Public Member Functions | Friends | List of all members
Cgu::Callback::FunctorArg Class Reference

Functor class holding a Callback::CallbackArg object. More...

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

Public Member Functions

void operator() (typename Cgu::Param< FreeArg >::ParamType arg) const
 
 FunctorArg (const CallbackArg< FreeArg > *cb)
 
 FunctorArg (const FunctorArg &f)
 
 FunctorArg ()
 

Friends

bool operator== (const FunctorArg &, const FunctorArg &)
 
bool operator< (const FunctorArg &, const FunctorArg &)
 

Detailed Description

Functor class holding a Callback::CallbackArg object.

See also
SafeFunctorArg
Callback namespace

This class wraps a CallbackArg object. The callback object is kept by SharedPtr so the functor can be copied and offers automatic lifetime management of the wrapped callback object, as well as providing an operator()() function. Ownership is taken of the CallbackArg object passed to the constructor taking a CallbackArg pointer, so that constructor should be treated like a shared pointer constructor - only pass a newly allocated object to it (or copy construct it or assign to it from another existing FunctorArg object.). The template type is the type of the unbound argument or container of unbound argument types, if any. Callback::FunctorArg<void> is typedef'ed to Callback::Functor.

The constructor taking a Callback::CallbackArg pointer is not marked explicit, so the results of Callback::make() can be passed directly to a function taking a Callback::FunctorArg argument, and implicit conversion will take place.

Usage

For a class object my_obj of type MyClass, with a method void MyClass::my_method(int, const char*), usage for a fully bound functor would be:

using namespace Cgu;
int arg = 1;
Callback::Functor f(Callback::make(my_obj, &MyClass::my_method, arg, "Hello\n"));
f();

Or for a partially bound functor:

using namespace Cgu;
int arg = 1;
Callback::FunctorArg<const char*> f(Callback::make(my_obj, &MyClass::my_method, arg));
f("Hello\n");

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).

For further background, including about the Callback::make() and Callback::make_ref() functions, and the use of the Cgu::TypeTuple struct for calls with two or three unbound arguments, read this: Callback

Constructor & Destructor Documentation

◆ FunctorArg() [1/3]

Cgu::Callback::FunctorArg::FunctorArg ( const CallbackArg< FreeArg > *  cb)
inline

Constructor of first FunctorArg holding the referenced callback. As it is not marked explicit, it is also a type conversion constructor.

Parameters
cbThe CallbackArg object which the functor is to manage.
Exceptions
std::bad_allocThis might throw std::bad_alloc if memory is exhausted and the system throws in that case. Note that if such an exception is thrown, then this constructor will clean itself up and also delete the callback object passed to it.
Note
1. std::bad_alloc will not be thrown if the library has been installed using the --with-glib-memory-slices-no-compat configuration option: instead glib will terminate the program if it is unable to obtain memory from the operating system.
2. This function is specialised for Callback::Functor (ie Callback::FunctorArg<void>) to take a Callback::Callback argument, and for Callback::FunctorArg<TypeTuple<T1, T2> > and Callback::FunctorArg<TypeTuple<T1, T2, T3> > to take a Callback::CallbackArg<TypeTuple<T1, T2> > and Callback::CallbackArg<TypeTuple<T1, T2, T3> > argument respectively.

◆ FunctorArg() [2/3]

Cgu::Callback::FunctorArg::FunctorArg ( const FunctorArg f)
inline

The copy constructor does not throw.

Parameters
fThe assignor.

◆ FunctorArg() [3/3]

Cgu::Callback::FunctorArg::FunctorArg ( )
inline

Default constructor, where a Callback::CallbackArg object is to be assigned later (via the type conversion constructor and/or the implicit assignment operator). This constructor does not throw.

Member Function Documentation

◆ operator()()

void Cgu::Callback::FunctorArg::operator() ( typename Cgu::Param< FreeArg >::ParamType  arg) const
inline

This will execute the function or class method referenced by the callback encapsulated by this object, or do nothing if this object has not been initialized with a callback. It will only throw if the executed function 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
argThe argument to be passed to the referenced function or class method, if any.
Note
This function is specialised for Callback::Functor (ie Callback::FunctorArg<void>) to take no argument, and for Callback::FunctorArg<TypeTuple<T1, T2> > and Callback::FunctorArg<TypeTuple<T1, T2, T3> > to take two and three arguments respectively.

Friends And Related Function Documentation

◆ operator<

bool operator< ( const FunctorArg ,
const FunctorArg  
)
friend

This comparison operator does not throw.

◆ operator==

bool operator== ( const FunctorArg ,
const FunctorArg  
)
friend

This comparison operator does not throw (this friend function is also called by operator!=<>() from version 1.2.5).


The documentation for this class was generated from the following file:
Cgu
Definition: application.h:45
Cgu::Callback::make
Callback * make(T &t, void(T::*func)())
Definition: callback.h:2376
Cgu::Callback::FunctorArg
Functor class holding a Callback::CallbackArg object.
Definition: callback.h:1077