#include <Pt/Delegate.h>
Delegates an action to a slot. More...
Inherits Connectable.
Public Member Functions | |
Delegate () | |
Default Constructor. | |
Delegate (const Delegate &rhs) | |
Deeply copies the other Delegate. | |
R | call (ARGUMENTS) const |
Calls the slot connected to the Delegate. More... | |
Connection | connect (const BasicSlot< R, ARGUMENTS > &slot) |
Connects this object to the given slot and returns that Connection. | |
void | disconnect () |
Disconnects from current target. | |
void | disconnect (const BasicSlot< R, ARGUMENTS > &slot) |
Disconnects from the target. | |
void | invoke (ARGUMENTS) const |
Invoke the slot connected to the Delegate. More... | |
bool | isConnected () const |
Returns true if connected to a target. | |
R | operator() (ARGUMENTS) const |
Same as call() | |
Related Functions | |
template<typename R , typename ARGS > | |
Connection | operator+= (Delegate< R, ARGS > &delegate, const BasicSlot< R, ARGS > &slot) |
Connect a Delegate to a slot. | |
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:
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.
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.
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.
R call | ( | ARGUMENTS | ) | const |
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.
void invoke | ( | ARGUMENTS | ) | const |
Passes on all arguments to the connected slot and ignores the return value. If No slot is connected, the call is silently ignored.