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

A thread-safe 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< FreeArgs >::ParamType... args) const
 
void emit (typename Cgu::Param< FreeArgs >::ParamType... args) const
 
bool test_emit (typename Cgu::Param< FreeArgs >::ParamType... args) const
 
Callback::SafeFunctorArg< FreeArgs... > connect (const Callback::SafeFunctorArg< FreeArgs... > &f)
 
Callback::SafeFunctorArg< FreeArgs... > connect (const Callback::SafeFunctorArg< FreeArgs... > &f, Releaser &r)
 
void disconnect (const Callback::SafeFunctorArg< FreeArgs... > &f)
 
void block (const Callback::SafeFunctorArg< FreeArgs... > &f)
 
void unblock (const Callback::SafeFunctorArg< FreeArgs... > &f)
 
 SafeEmitterArg ()=default
 
 SafeEmitterArg (const SafeEmitterArg &)=delete
 
SafeEmitterArgoperator= (const SafeEmitterArg &)=delete
 
 ~SafeEmitterArg ()
 

Friends

class Releaser
 

Detailed Description

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

See also
EmitterArg Releaser
emitter.h
Callback namespace

This is a thread-safe version of the EmitterArg class. Callback::SafeFunctorArg objects may be connected to SafeEmitter classes, and will be executed when SafeEmitterArg::emit() or SafeEmitterArg::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 (or calling into) any of its methods to be disconnected automatically from the SafeEmitterArg 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 types are the types of the unbound arguments, if any. SafeEmitterArg<> is typedef'ed to SafeEmitter.

Usage

These are examples:

using namespace Cgu;
se1.connect(Callback::lambda<>([] () {std::cout << "Hello world\n";}));
se1();
int res;
se2.connect(Callback::lambda<int, int&>([] (int i, int& j) {j = 10 * i;}));
se2(2, res);
std::cout << "10 times 2 is " << res << '\n';

For further background, including about thread-safety and exception safety and other matters, read this: emitter.h, or for more information about bound and unbound arguments, read this: Cgu::Callback.

Constructor & Destructor Documentation

◆ SafeEmitterArg() [1/2]

Cgu::SafeEmitterArg::SafeEmitterArg ( )
default
Exceptions
std::bad_allocThe constructor might throw std::bad_alloc if memory is exhausted and the system throws in that case.
Thread::MutexErrorThe constructor might throw Thread::MutexError if initialisation of the contained mutex fails. (It is often not worth checking for this, as it means either memory is exhausted or pthread has run out of other resources to create new mutexes.)

◆ SafeEmitterArg() [2/2]

Cgu::SafeEmitterArg::SafeEmitterArg ( const SafeEmitterArg )
delete

This class cannot be copied. The copy constructor is deleted.

◆ ~SafeEmitterArg()

Cgu::SafeEmitterArg::~SafeEmitterArg ( )

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). It is thread-safe as regards the dropping of any connected functors and of any relevant Releaser objects.

Member Function Documentation

◆ block()

void Cgu::SafeEmitterArg::block ( const Callback::SafeFunctorArg< FreeArgs... > &  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). It is thread safe.

Parameters
fThe functor to block.
Note
If the same functor has been connected more than once to the same SafeEmitterArg object, this call will block all of them.

◆ connect() [1/2]

Callback::SafeFunctorArg<FreeArgs...> Cgu::SafeEmitterArg::connect ( const Callback::SafeFunctorArg< FreeArgs... > &  f)

Connects a functor. It is thread safe.

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.

◆ connect() [2/2]

Callback::SafeFunctorArg<FreeArgs...> Cgu::SafeEmitterArg::connect ( const Callback::SafeFunctorArg< FreeArgs... > &  f,
Releaser r 
)

Connects a functor. It is thread safe.

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.

◆ disconnect()

void Cgu::SafeEmitterArg::disconnect ( const Callback::SafeFunctorArg< FreeArgs... > &  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). It is thread safe.

Parameters
fThe functor to disconnect.
Note
If the same functor has been connected more than once to the same SafeEmitterArg object, this call will disconnect all of them.

◆ emit()

void Cgu::SafeEmitterArg::emit ( typename Cgu::Param< FreeArgs >::ParamType...  args) const

This will execute the connected functors. It is thread safe if the functions or class methods referenced by the connected functors are thread safe.

Parameters
argsThe unbound arguments 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).

◆ operator()()

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

This will execute the connected functors. It is thread safe if the functions or class methods referenced by the connected functors are thread safe.

Parameters
argsThe unbound arguments 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).

◆ operator=()

SafeEmitterArg& Cgu::SafeEmitterArg::operator= ( const SafeEmitterArg )
delete

This class cannot be copied. The assignment operator is deleted.

◆ test_emit()

bool Cgu::SafeEmitterArg::test_emit ( typename Cgu::Param< FreeArgs >::ParamType...  args) const

This will execute the connected functors, but it also reports whether in fact there were any connected functors to execute. It is thread safe if the functions or class methods referenced by the connected functors are thread safe. (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
argsThe unbound arguments to be passed to the referenced function or class method, 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).

◆ unblock()

void Cgu::SafeEmitterArg::unblock ( const Callback::SafeFunctorArg< FreeArgs... > &  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). It is thread safe.

Parameters
fThe functor to unblock.
Note
If the same functor has been connected more than once to the same SafeEmitterArg object, this call will unblock all of them.

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:44
Cgu::SafeEmitterArg::connect
Callback::SafeFunctorArg< FreeArgs... > connect(const Callback::SafeFunctorArg< FreeArgs... > &f)
Cgu::SafeEmitterArg
A thread-safe class to execute callbacks connected to it, with provision for automatic disconnection.
Definition: emitter.h:308