c++-gtk-utils: Cgu::Callback::FunctorArg< FreeArgs > Class Template Reference

c++-gtk-utils

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | Friends | List of all members
Cgu::Callback::FunctorArg< FreeArgs > Class Template Reference

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

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

## Public Member Functions
void | operator() (typename Cgu::Param< FreeArgs >::ParamType... args) const
FunctorArg & | operator= (const FunctorArg &f)
FunctorArg & | operator= (FunctorArg &&f)
| FunctorArg (const CallbackArg< FreeArgs... > *cb)
| FunctorArg (const FunctorArg &f)
| FunctorArg (FunctorArg &&f)
| FunctorArg ()

## Friends
struct | std::hash< FunctorArg >
bool | operator== (const FunctorArg &, const FunctorArg &) noexcept
bool | operator< (const FunctorArg &, const FunctorArg &)

## Detailed Description

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

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 types are the types of the unbound arguments, if any. Callback::FunctorArg<> is typedef'ed to Callback::Functor.

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

Functor/FunctorArg 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:

using namespace Cgu;
// here f1 is directly initialized using the type conversion constructor
Callback::Functor f1{Callback::lambda<>([] () {std::cout << "Hello world\n";})};
f1();
// here Callback::to_functor() is used to enable use of the auto keyword.
// f2 is of type Callback::Functor
auto f2 = Callback::to_functor(Callback::lambda<>([] () {std::cout << "Hello world\n";}));
f2();
// here f3 is of type Callback::FunctorArg<int, int&>
auto f3 = Callback::to_functor(Callback::lambda<int, int&>([] (int i, int& j) {j = 10 * i;}));
int res;
f3(2, res);
std::cout << "10 times 2 is " << res << '\n';

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

## Constructor & Destructor Documentation

## ◆ FunctorArg() [1/4]

template<class... FreeArgs>

Cgu::Callback::FunctorArg< FreeArgs >::FunctorArg | ( | const CallbackArg< FreeArgs... > * | cb | ) |
| inline

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

Parameters

  cb | The CallbackArg object which the functor is to manage.

Exceptions

  std::bad_alloc | This 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
  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.

## ◆ FunctorArg() [2/4]

template<class... FreeArgs>

Cgu::Callback::FunctorArg< FreeArgs >::FunctorArg | ( | const FunctorArg< FreeArgs > & | f | ) |
| inline

The copy constructor does not throw.

Parameters

  f | The assignor

## ◆ FunctorArg() [3/4]

template<class... FreeArgs>

Cgu::Callback::FunctorArg< FreeArgs >::FunctorArg | ( | FunctorArg< FreeArgs > && | f | ) |
| inline

The move constructor does not throw.

Parameters

  f | The functor to be moved.

## ◆ FunctorArg() [4/4]

template<class... FreeArgs>

Cgu::Callback::FunctorArg< FreeArgs >::FunctorArg | ( |  | ) |
| inline

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

## Member Function Documentation

## ◆ operator()()

template<class... FreeArgs>

void Cgu::Callback::FunctorArg< FreeArgs >::operator() | ( | typename Cgu::Param< FreeArgs >::ParamType... | args | ) | const
| inline

This will execute the function, callable object or class method represented 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, 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.

## ◆ operator=() [1/2]

template<class... FreeArgs>

FunctorArg& Cgu::Callback::FunctorArg< FreeArgs >::operator= | ( | const FunctorArg< FreeArgs > & | f | ) |
| inline

This function does not throw.

Parameters

  f | The assignor.

Returns
  The functor object after assignment.

## ◆ operator=() [2/2]

template<class... FreeArgs>

FunctorArg& Cgu::Callback::FunctorArg< FreeArgs >::operator= | ( | FunctorArg< FreeArgs > && | f | ) |
| inline

This function does not throw.

Parameters

  f | The functor to be moved.

Returns
  The functor object after the move operation.

## Friends And Related Function Documentation

## ◆ operator<

template<class... FreeArgs>

bool operator< | ( | const FunctorArg< FreeArgs > & | ,
|  | const FunctorArg< FreeArgs > & |
| ) |  |
| friend

One FunctorArg object is less than another if the address of the CallbackArg object contained by the first is regarded by std::less as less than the address of the CallbackArg object contained by the other. This comparison operator does not throw unless std::less applied to pointer types throws (which it would not do with any sane implementation).

## ◆ operator==

template<class... FreeArgs>

bool operator== | ( | const FunctorArg< FreeArgs > & | ,
|  | const FunctorArg< FreeArgs > & |
| ) |  |
| friend

Two FunctorArg objects compare equal if the addresses of the CallbackArg objects they contain are the same. This comparison operator does not throw.

## ◆ std::hash< FunctorArg >

template<class... FreeArgs>

friend struct std::hash< FunctorArg >
| friend

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

- callback.h