Wt  4.0.2
Public Member Functions | List of all members
Wt::Signal< A > Class Template Reference

A signal that propagates events to listeners. More...

#include <Wt/WSignal.h>

Inheritance diagram for Wt::Signal< A >:
Inheritance graph
[legend]

Public Member Functions

 Signal ()
 Creates a signal.
 
virtual Wt::Signals::connection connect (WObject *target, WObject::Method method) override
 Connects to a slot. More...
 
template<class F >
Wt::Signals::connection connect (const F &function)
 Connects to a function. More...
 
template<class T , class V , class... B>
Wt::Signals::connection connect (T *target, void(V::*method)(B...))
 Connects a slot. More...
 
void emit (A...args) const
 Emits the signal. More...
 
void operator() (A...args) const
 Emits the signal. More...
 
virtual bool isConnected () const override
 Returns whether this signal is connected. More...
 
- Public Member Functions inherited from Wt::SignalBase
template<class T , class V >
Wt::Signals::connection connect (T *target, void(V::*method)())
 Connects to a slot. More...
 

Detailed Description

template<class... A>
class Wt::Signal< A >

A signal that propagates events to listeners.

Use Signal/slots to let one object (A) listen to events caused by another object (B). In this scenario, object B provides in its public interface access to a signal, to which object A connects one of its member function (which act as slot). Object A can then signal an event (which triggers the connected callback functions), by emitting the signal. Signal/slot is a generalization of the popular observer pattern used in GUIs.

A signal can provide details of the event, using up to 6 parameters. A slot must have a compatible signature to connect to a signal, based on its parameters. A compatible signature provides the same parameters in the member function, or less (leaving out parameters at the end).

The signal automatically disconnects from the slot when the target is deleted. In addition, the signal may be deleted at any time, in particular also while it is being emitted.

class MyWidget : public Wt::WContainerWidget
{
public:
MyWidget()
: Wt::WContainerWidget()
done_(this)
{
...
Wt::WPushButton *button = addWidget(std::make_unique<Wt::WPushButton>("Okay"));
button->clicked().connect(this, &MyWidget::process);
}
// provide an accessor for the signal
Wt::Signal<int, std::string>& done() { return done_; }
private:
void process() {
...
done_.emit(42, "Totally done"); // emit the signal
}
};

This widget could then be used from another class:

class GUIClass : public Wt::WContainerWidget
{
...
private:
void init() {
MyWidget *widget = addWidget(std::make_unique<MyWidget>());
widget->done().connect(this, &GUIClass::whenDone);
}
void whenDone(int result, const std::string& description) {
...
}
};

Member Function Documentation

template<class... A>
Wt::Signals::connection Wt::Signal< A >::connect ( WObject target,
WObject::Method  method 
)
overridevirtual

Connects to a slot.

Every signal can be connected to a slot which does not take any arguments (and may thus ignore signal arguments).

Implements Wt::SignalBase.

template<class... A>
template<class F >
Wt::Signals::connection Wt::Signal< A >::connect ( const F function)

Connects to a function.

This variant of the overloaded connect() method supports a template function object (which supports operator ()).

When the receiver function is an object method, the signal will automatically be disconnected when the object is deleted, as long as the object inherits from WObject (or Wt::Core::observable).

The function may leave up to N parameters unbound (e.g. using std::bind placeholders _1 to _N) that may be bound to values passed by the signal.

template<class... A>
template<class T , class V , class... B>
Wt::Signals::connection Wt::Signal< A >::connect ( T target,
void(V::*)(B...)  method 
)

Connects a slot.

The slot is as a method of an object target of class T, which equals class V, or is a base class of class V. Thus, the following statement must return a non-null pointer:

V *v = dynamic_cast<V *>(target);

In practice, to facilitate automatic disconnects on deletion of the target, class T must be also be a descendant of WObject, but this is not enforced by the interface.

template<class... A>
void Wt::Signal< A >::emit ( A...  args) const

Emits the signal.

The arguments must exactly match the arguments of the target function.

This will cause all connected slots to be triggered, with the given arguments.

template<class... A>
bool Wt::Signal< A >::isConnected ( ) const
overridevirtual

Returns whether this signal is connected.

Returns true when the signal was connected to to at least one slot.

Implements Wt::SignalBase.

template<class... A>
void Wt::Signal< A >::operator() ( A...  args) const

Emits the signal.

This is equivalent to emit().

See also
emit

Generated on Fri Nov 24 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.11