Wt  3.3.9
Public Member Functions | List of all members
Wt::WSignalMapper< T, A1 > Class Template Reference

A utility class to connect multiple senders to a single slot. More...

#include <Wt/WSignalMapper>

Inheritance diagram for Wt::WSignalMapper< T, A1 >:
Inheritance graph
[legend]

Public Member Functions

 WSignalMapper (WObject *parent=0)
 Creates a new WSignalMapper.
 
void setMapping (WObject *sender, const T &data)
 Associates data with a sender. More...
 
Wt::Signals::connection mapConnect (SignalBase &signal, const T &data)
 Maps a signal without arguments. More...
 
template<typename S >
Wt::Signals::connection mapConnect1 (S &signal, const T &data)
 Maps a signal with one argument. More...
 
Signal< T, A1 > & mapped ()
 Signal emitted in response to a signal sent to map() or map1(). More...
 
void map ()
 Slot to which to connect the source signal. More...
 
void map1 (A1 a)
 Slot to which to connect the source signal, passing the argument to the receiver. More...
 
void removeMapping (WObject *sender)
 Removes the mapping of an object. More...
 
- Public Member Functions inherited from Wt::WObject
 WObject (WObject *parent=0)
 Create a WObject with a given parent object. More...
 
virtual ~WObject ()
 Destructor. More...
 
virtual const std::string id () const
 Returns the (unique) identifier for this object. More...
 
virtual void setObjectName (const std::string &name)
 Sets an object name. More...
 
virtual std::string objectName () const
 Returns the object name. More...
 
void resetLearnedSlots ()
 Resets learned stateless slot implementations. More...
 
template<class T >
void resetLearnedSlot (void(T::*method)())
 Resets a learned stateless slot implementation. More...
 
template<class T >
WStatelessSlot * implementStateless (void(T::*method)())
 Declares a slot to be stateless and learn client-side behaviour on first invocation. More...
 
template<class T >
WStatelessSlot * implementStateless (void(T::*method)(), void(T::*undoMethod)())
 Declares a slot to be stateless and learn client-side behaviour in advance. More...
 
void isNotStateless ()
 Marks the current function as not stateless. More...
 
template<class T >
WStatelessSlot * implementJavaScript (void(T::*method)(), const std::string &jsCode)
 Provides a JavaScript implementation for a method. More...
 
void addChild (WObject *child)
 Adds a child object. More...
 
virtual void removeChild (WObject *child)
 Removes a child object. More...
 
const std::vector< WObject * > & children () const
 Returns the children.
 
WObjectparent () const
 Returns the parent object.
 

Additional Inherited Members

- Public Types inherited from Wt::WObject
typedef void(WObject::* Method) ()
 Typedef for a WObject method without arguments.
 
- Protected Member Functions inherited from Wt::WObject
virtual WStatelessSlot * getStateless (Method method)
 On-demand stateless slot implementation. More...
 
- Static Protected Member Functions inherited from Wt::WObject
static WObjectsender ()
 Returns the sender of the current slot call. More...
 

Detailed Description

template<typename T, typename A1 = NoClass>
class Wt::WSignalMapper< T, A1 >

A utility class to connect multiple senders to a single slot.

This class is useful if you would like to respond to signals of many objects or widgets within a single slot, but need to identify the particular sender through some property.

Usage example:

void Test::createWidgets()
{
// connect mapped() to our target slot
MyMap->mapped().connect(this, &Test::onClick);
// connect and map every source signal to the mapper
MyMap->mapConnect(text1->clicked(), text1);
MyMap->mapConnect(text2->clicked(), text2);
MyMap->mapConnect(text3->clicked(), text3);
}
void Test::onClick(WText* source)
{
// source is where it is coming from
// ...
}

The type T may be any type that has proper copy semantics and a default constructor. The mapper may pass one extra argument (type A1) from the original signal to the mapped() signal. In that case, you must connect the original signal to the map1() slot, or use mapConnect1().

The mapper uses signal.sender() to attribute ownership of a signal to a sender.

You may want to consider to boost::bind() (or std::bind()) instead, which is simpler and achieves the same in a more direct way:

text1->clicked().connect(boost::bind(&Test::onClick, this, text1));
text2->clicked().connect(boost::bind(&Test::onClick, this, text2));
text3->clicked().connect(boost::bind(&Test::onClick, this, text3));

Member Function Documentation

template<typename T , typename A1 >
void Wt::WSignalMapper< T, A1 >::map ( )

Slot to which to connect the source signal.

When a signal triggers the slot, the sender is identified and used to find corresponding data set with setMapping(), which is then use to propagate further in the mapped() signal.

template<typename T , typename A1>
void Wt::WSignalMapper< T, A1 >::map1 ( A1  a)

Slot to which to connect the source signal, passing the argument to the receiver.

When a signal triggers the slot, the sender is identified and used to find corresponding data set with setMapping(), which is then use to propagate further in the mapped() signal. The additional argument a is passed as the second argument to the mapped() signal.

template<typename T, typename A1 >
Wt::Signals::connection Wt::WSignalMapper< T, A1 >::mapConnect ( SignalBase signal,
const T &  data 
)

Maps a signal without arguments.

Connect the given signal with the slot, and associate the data when it is triggered.

Wt::WSignalMapper<T> *mapper = ...
mapper->mapConnect(widget->clicked(), data);

is equivalent to:

Wt::WSignalMapper<T> *mapper = ...
widget->clicked().connect(mapper, &Wt::WSignalMapper<T>::map);
mapper->setMapping(widget, data);
template<typename T, typename A1 >
template<typename S >
Wt::Signals::connection Wt::WSignalMapper< T, A1 >::mapConnect1 ( S &  signal,
const T &  data 
)

Maps a signal with one argument.

Connect the given signal with the slot, and associate the data when it is triggered. The signal argument will be passed to the mapped() signal.

mapper->mapConnect(widget->clicked(), data);

is equivalent to:

widget->clicked().connect(mapper, &Wt::WSignalMapper<T, Wt::WMouseEvent>::map1);
mapper->setMapping(widget, data);
template<typename T, typename A1 = NoClass>
Signal<T, A1>& Wt::WSignalMapper< T, A1 >::mapped ( )

Signal emitted in response to a signal sent to map() or map1().

The first argument propagated is the data that is associated with the specific sender, set in setMapping() or mapConnect(). The second argument is an argument passed from the originating signal.

template<typename T , typename A1 >
void Wt::WSignalMapper< T, A1 >::removeMapping ( WObject sender)

Removes the mapping of an object.

This method does not disconnect any signals; that is the responsability of the user of WSignalMapper. If no mapping of for an object exits, the mapper will ignore the signal, and not emit the mapped signal.

template<typename T, typename A1 >
void Wt::WSignalMapper< T, A1 >::setMapping ( WObject sender,
const T &  data 
)

Associates data with a sender.

Associate data with a sender, which wel emitted by the mapped() signal, when the corresponding sender signal triggers map() or map1().


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