Delegate< R, As > Class Template Reference

#include <Pt/Delegate.h>

Delegates an action to a slot. More...

Inherits DelegateBase.

Public Member Functions

 Delegate ()
 Default Constructor.
 
 Delegate (const Delegate &rhs)
 Deeply copies the other Delegate.
 
Connection connect (const BasicSlot< R, As... > &slot)
 Connects this object to the given slot and returns that Connection.
 
void disconnect ()
 Disconnects from current target.
 
void disconnect (const BasicSlot< R, As... > &slot)
 Disconnects from the target.
 
call (As... args)
 Calls the slot connected to the Delegate. More...
 
void invoke (As... args)
 Invoke the slot connected to the Delegate. More...
 
operator() (As... args)
 Same as call()
 
bool isConnected () const
 Returns true if connected to a target.
 

Related Functions

template<typename R , typename... As>
Connection operator+= (Delegate< R, As... > &delegate, const BasicSlot< R, As... > &slot)
 Connect a Delegate to a slot.
 

Detailed Description

template<typename R, typename... As>
class Pt::Delegate< R, As >

The Pt::Delegate is an alternative to the Pt::Signal and differs from it in two ways. Firstly, delegates forward the return value of the slot and secondly, delegates can only be connected to one slot at a time. The same types of slots can be used for signals and delegates. The template parameter list of the Delegate determines its signature, where the first parameter represents the return type:

Pt::Delegate<int> del0; // Delegate only returns int
Pt::Delegate<int, int> del1; // Delegate with one argument
Pt::Delegate<int, int, int> del2; // Delegate with two arguments

The syntax for connecting delegates is identical to how signals are connected to slots. However, since a delegate forwards the return value of its slot, not only the arguments passed to the slot must be compatible, but also the return value. Furthermore, when an already connected delegate is connected again, the current connection will be closed and the delegate is connected to its new target.

int slotA()
{ return 5; }
int slotB()
{ return 6; }
int main()
{
delegate += Pt::slot(slotA);
delegate += Pt::slot(slotB); // disconnects from slotA
return 0;
}

The example above constructs a delegate which can be connected to any slot that returns an int. It is first connected to a slot of the function slotA. When it is connected for the second time to a slot of the function slotB, the old connection will be closed and only slotB is called when the delegate is called.

There are two possibilities how a delegate can call its slot. The member function call() will return the return value of the slot. If the delegate is not connected to a slot, an exception is thrown. The second method is through invoke(), where the return value is ignored, but if the delegate is not connected, no exception will be thrown.

int slot()
{ return 5; }
int main()
{
try
{
Pt::Connection connection = delegate += Pt::slot(slot);
int i = delegate.call(); // i is 5 now
connection.close();
delegate.invoke() // does not throw
delegate.call(); // will throw because not connected
}
catch(const std::logic_error& ex)
{
std::cerr << "could not call delegate" << std::endl;
}
return 0;
}

The program above connects a delegate to a slot and then calls it. After the connection was closed, the delegate is invoked, which has no effect. Finally, when the delegate is called again, an exception is thrown and catched.

Member Function Documentation

◆ call()

R call ( As...  args)

Passes on all arguments to the connected slot and returns the return value of that slot. If no slot is connect then an exception is thrown.

◆ invoke()

void invoke ( As...  args)

Passes on all arguments to the connected slot and ignores the return value. If No slot is connected, the call is silently ignored.

void invoke(As... args)
Invoke the slot connected to the Delegate.
Definition: Delegate.h:251
void close()
Closes the connection.
R call(As... args)
Calls the slot connected to the Delegate.
Definition: Delegate.h:236
Delegates an action to a slot.
Definition: Delegate.h:194
Represents a connection between a Signal/Delegate and a slot.
Definition: Connection.h:96