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

A class to execute callbacks connected to it, with provision for automatic disconnection. More...

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

Public Member Functions

void operator() (typename Cgu::Param< FreeArg >::ParamType arg) const
 
void emit (typename Cgu::Param< FreeArg >::ParamType arg) const
 
bool test_emit (typename Cgu::Param< FreeArg >::ParamType arg) const
 
Callback::FunctorArg< FreeArg > connect (const Callback::FunctorArg< FreeArg > &f)
 
Callback::FunctorArg< FreeArg > connect (const Callback::FunctorArg< FreeArg > &f, Releaser &r)
 
void disconnect (const Callback::FunctorArg< FreeArg > &f)
 
void block (const Callback::FunctorArg< FreeArg > &f)
 
void unblock (const Callback::FunctorArg< FreeArg > &f)
 
 EmitterArg ()
 
 ~EmitterArg ()
 

Friends

class Releaser
 

Detailed Description

A class to execute callbacks connected to it, with provision for automatic disconnection.

See also
SafeEmitterArg Releaser
emitter.h
Callback namespace

Callback::FunctorArg objects may be connected to Emitter classes, and will be executed when EmitterArg<>::emit() or EmitterArg<>::operator()() are called.

One version of the connect() method takes a Releaser object as an argument. Such a Releaser object should be a public member of any target class which wants functors representing any of its methods to be disconnected automatically from the EmitterArg object when the target class object is destroyed.

A connection may be explicitly disconnected by calling the disconnect() method, and may also be temporarily blocked and subsequently unblocked with the block() and unblock() methods.

The template type is the type of the unbound argument (or type container of unbound arguments), if any. EmitterArg<void> is typedef'ed to Emitter.

Usage

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

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

Or for a partially bound functor and emitter:

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

For further background, including about thread-safety, exception safety, the use of the Cgu::TypeTuple struct for calls involving two or three unbound arguments and other matters, read this: emitter.h, or for more information about bound and unbound arguments, read this: Cgu::Callback.

Constructor & Destructor Documentation

◆ EmitterArg()

Cgu::EmitterArg::EmitterArg ( )
inline
Exceptions
std::bad_allocThe constructor might throw std::bad_alloc if memory is exhausted and the system throws in that case.

◆ ~EmitterArg()

Cgu::EmitterArg::~EmitterArg ( )

The destructor does not throw provided that the destructors of any bound arguments do not throw (as they should not do), and assuming that merely iterating through a list does not throw (as it would not on any sane implementation).

Member Function Documentation

◆ block()

void Cgu::EmitterArg::block ( const Callback::FunctorArg< FreeArg > &  f)

Blocks a connected functor from executing when emit() or operator()() is called until unblock() is called. This method does not throw (assuming that merely iterating through a list does not throw, as it would not on any sane implementation).

Parameters
fThe functor to block.
Note
1. If the same functor has been connected more than once to the same EmitterArg object, this call will block all of them.
2. This function is specialised for Emitter (ie EmitterArg<void>) to take a Callback::Functor argument, and for EmitterArg<TypeTuple<T1, T2> > and EmitterArg<TypeTuple<T1, T2, T3>

to take a Callback::FunctorArg<TypeTuple<T1, T2> > and

Callback::FunctorArg<TypeTuple<T1, T2, T3> > argument respectively.

◆ connect() [1/2]

Callback::FunctorArg<FreeArg> Cgu::EmitterArg::connect ( const Callback::FunctorArg< FreeArg > &  f)

Connects a functor.

Parameters
fThe functor to connect.
Returns
The functor connected.
Exceptions
std::bad_allocThe method might throw std::bad_alloc if memory is exhausted and the system throws in that case.
Note
This function is specialised for Emitter (ie EmitterArg<void>) to take and return a Callback::Functor argument, and for EmitterArg<TypeTuple<T1, T2> > and EmitterArg<TypeTuple<T1, T2, T3> > to take and return a Callback::FunctorArg<TypeTuple<T1, T2> > and Callback::FunctorArg<TypeTuple<T1, T2, T3> > argument respectively.

◆ connect() [2/2]

Callback::FunctorArg<FreeArg> Cgu::EmitterArg::connect ( const Callback::FunctorArg< FreeArg > &  f,
Releaser r 
)

Connects a functor.

Parameters
fThe functor to connect.
rA Releaser object for automatic disconnection of the functor if the object whose method it represents is destroyed.
Returns
The functor connected.
Exceptions
std::bad_allocThe method might throw std::bad_alloc if memory is exhausted and the system throws in that case.
Note
This function is specialised for Emitter (ie EmitterArg<void>) to take and return a Callback::Functor argument, and for EmitterArg<TypeTuple<T1, T2> > and EmitterArg<TypeTuple<T1, T2, T3> > to take and return a Callback::FunctorArg<TypeTuple<T1, T2> > and Callback::FunctorArg<TypeTuple<T1, T2, T3> > argument respectively.

◆ disconnect()

void Cgu::EmitterArg::disconnect ( const Callback::FunctorArg< FreeArg > &  f)

Disconnects a functor previously connected. This does not throw provided that the destructors of any bound arguments do not throw (as they should not do), and assuming that merely iterating through a list does not throw (as it would not on any sane implementation).

Parameters
fThe functor to disconnect.
Note
1. If the same functor has been connected more than once to the same EmitterArg object, this call will disconnect all of them.
2. This function is specialised for Emitter (ie EmitterArg<void>) to take a Callback::Functor argument, and for EmitterArg<TypeTuple<T1, T2> > and EmitterArg<TypeTuple<T1, T2, T3>

to take a Callback::FunctorArg<TypeTuple<T1, T2> > and

Callback::FunctorArg<TypeTuple<T1, T2, T3> > argument respectively.

◆ emit()

void Cgu::EmitterArg::emit ( typename Cgu::Param< FreeArg >::ParamType  arg) const

This will execute the connected functors.

Parameters
argThe argument to be passed to the referenced function or class method, if any.
Exceptions
std::bad_allocThe method might throw std::bad_alloc if memory is exhausted and the system throws in that case. In addition, it will throw if the functions or class methods referenced by the functors throw (or if the copy constructor of a free or bound argument throws and it is not a reference argument).
Note
This function is specialised for Emitter (ie EmitterArg<void>) to take no argument, and for EmitterArg<TypeTuple<T1, T2> > and EmitterArg<TypeTuple<T1, T2, T3>

to take two and three arguments respectively.

◆ operator()()

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

This will execute the connected functors.

Parameters
argThe argument to be passed to the referenced function or class method, if any.
Exceptions
std::bad_allocThe method might throw std::bad_alloc if memory is exhausted and the system throws in that case. In addition, it will throw if the functions or class methods referenced by the functors throw (or if the copy constructor of a free or bound argument throws and it is not a reference argument).
Note
This function is specialised for Emitter (ie EmitterArg<void>) to take no argument, and for EmitterArg<TypeTuple<T1, T2> > and EmitterArg<TypeTuple<T1, T2, T3>

to take two and three arguments respectively.

◆ test_emit()

bool Cgu::EmitterArg::test_emit ( typename Cgu::Param< FreeArg >::ParamType  arg) const

This will execute the connected functors, but it also reports whether in fact there were any connected functors to execute. (It is not necessary to use this function just because it is not known whether a functor is connected - if the standard emit() function is called when no functor is connected, nothing will happen. The feature of this method is that it will report the outcome.)

Parameters
argThe argument to be passed to the connected functions or class methods, if any.
Returns
Returns false if there were no functors to execute, or true if functors have been executed.
Exceptions
std::bad_allocThe method might throw std::bad_alloc if memory is exhausted and the system throws in that case. In addition, it will throw if the functions or class methods referenced by the functors throw (or if the copy constructor of a free or bound argument throws and it is not a reference argument).
Note
This function is specialised for Emitter (ie EmitterArg<void>) to take no argument, and for EmitterArg<TypeTuple<T1, T2> > and EmitterArg<TypeTuple<T1, T2, T3>

to take two and three arguments respectively.

◆ unblock()

void Cgu::EmitterArg::unblock ( const Callback::FunctorArg< FreeArg > &  f)

Unblocks a previously blocked functor. This method does not throw (assuming that merely iterating through a list does not throw, as it would not on any sane implementation).

Parameters
fThe functor to unblock.
Note
1. If the same functor has been connected more than once to the same EmitterArg object, this call will unblock all of them.
2. This function is specialised for Emitter (ie EmitterArg<void>) to take a Callback::Functor argument, and for EmitterArg<TypeTuple<T1, T2> > and EmitterArg<TypeTuple<T1, T2, T3>

to take a Callback::FunctorArg<TypeTuple<T1, T2> > and

Callback::FunctorArg<TypeTuple<T1, T2, T3> > argument respectively.

Friends And Related Function Documentation

◆ Releaser

friend class Releaser
friend

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::EmitterArg::connect
Callback::FunctorArg< FreeArg > connect(const Callback::FunctorArg< FreeArg > &f)
Cgu::EmitterArg< void >