Wt  3.7.1
Public Types | Public Member Functions | Protected Member Functions | Static Protected Member Functions | List of all members
Wt::WObject Class Reference

A base class for objects that participate in the signal/slot system. More...

#include <Wt/WObject>

Inheritance diagram for Wt::WObject:
Inheritance graph
[legend]

Public Types

typedef void(WObject::* Method) ()
 Typedef for a WObject method without arguments.
 

Public Member Functions

 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.
 

Protected Member Functions

virtual WStatelessSlot * getStateless (Method method)
 On-demand stateless slot implementation. More...
 

Static Protected Member Functions

static WObjectsender ()
 Returns the sender of the current slot call. More...
 

Detailed Description

A base class for objects that participate in the signal/slot system.

The main feature offered by WObject is automated object life-time tracking when involved in signal/slot connections. Connections between signals and slots of WObject instances implement a type-safe event callback system. For example, one can simply connect() the WInteractWidget::clicked() signal of a WPushButton to the WApplication::quit() method, to exit the application when the button is clicked:

Wt's signals may also propagate arguments to slots. For example, the same clicked() signal provides event details in a WMouseEvent details class, and these details may be received in the slot:

class MyClass : public Wt::WContainerWidget
{
public:
MyClass(Wt::WContainerWidget *parent = 0)
: Wt::WContainerWidget(parent)
{
Wt::WText *text = Wt::WText("Click here", this);
text->clicked().connect(this, &MyClass::handleClick);
...
}
private:
void handleClick(const Wt::WMouseEvent& event) {
if (event.modifiers() & Wt::ShiftModifier) {
...
}
}
};

As the example illustrates, slots are ordinary WObject methods.

A second feature of WObject is that they allow ownership organization in ownership object trees. When an object is created with another object as parent, it's ownership is transferred to the parent. If not deleted explicitly, the child object will be deleted together with the parent. Child objects may also be deleted manually: they will remove themselves from their parent in the process.

In conjunction with EventSignal, WObject also facilitates learning of client-side event handling (in JavaScript) through invocation of the slot method. This is only possible when the slot behaviour is stateless, i.e. independent of any application state, and can be specified using the implementStateless() methods.

See also
Signal, EventSignal

Constructor & Destructor Documentation

◆ WObject()

Wt::WObject::WObject ( WObject parent = 0)

Create a WObject with a given parent object.

If the optional parent is specified, the parent object will destroy all child objects. Set parent to 0 to create an object with no parent.

See also
addChild()

◆ ~WObject()

Wt::WObject::~WObject ( )
virtual

Destructor.

This automatically:

  • deletes all child objects
  • invalidates this object as sender or receiver in signals and slots

Member Function Documentation

◆ addChild()

void Wt::WObject::addChild ( WObject child)

Adds a child object.

Take responsibility of deleting the child object, together with this object.

See also
removeChild()

◆ getStateless()

WStatelessSlot * Wt::WObject::getStateless ( Method  method)
protectedvirtual

On-demand stateless slot implementation.

This method returns a stateless slot implementation for the given method. To avoid the cost of declaring methods to be stateless when they are not used, you may reimplement this method to provide a stateless implementation for a method only when the method is involved in a slot connection.

Use implementStateless() to provide a stateless implementation of the given method, or return the base class implementation otherwise.

Reimplemented in Wt::WWidget, Wt::WWebWidget, and Wt::WAbstractToggleButton.

◆ id()

const std::string Wt::WObject::id ( ) const
virtual

Returns the (unique) identifier for this object.

For a WWidget, this corresponds to the id of the DOM element that represents the widget. This is not entirely unique, since a composite widget shares the same id as its implementation.

By default, the id is auto-generated, unless a custom id is set for a widget using WWidget::setId(). The auto-generated id is created by concatenating objectName() with a unique number.

See also
WWidget::jsRef()

Reimplemented in Wt::WWebWidget, Wt::WTableRow, Wt::WButtonGroup, Wt::WTableColumn, and Wt::WCompositeWidget.

◆ implementJavaScript()

template<class T >
WStatelessSlot * Wt::WObject::implementJavaScript ( void(T::*)()  method,
const std::string &  jsCode 
)

Provides a JavaScript implementation for a method.

This method sets the JavaScript implementation for a method. As a result, if JavaScript is available, the JavaScript version will be used on the client side and the visual effect of the C++ implementation will be ignored.

This is very similar to an auto-learned stateless slot, but here the learning is avoided by directly setting the JavaScript implementation.

The jsCode should be one or more valid JavaScript statements.

See also
implementStateless(void (T::*method)())

◆ implementStateless() [1/2]

template<class T >
WStatelessSlot * Wt::WObject::implementStateless ( void(T::*)()  method)

Declares a slot to be stateless and learn client-side behaviour on first invocation.

Indicate that the given slot is stateless, and meets the requirement that the slot's code does not depend on any state of the object, but performs the same visual effect regardless of any state, or at least until resetLearnedSlot() is called.

When this slot is connected to an EventSignal (such as those exposed by WInteractWidget and WFormWidget), the Wt library may decide to cache the visual effect of this slot in JavaScript code at client-side: this effect will be learned automatically at the first invocation. This has no consequences for the normal event handling, since the slot implementation is still executed in response to any event notification. Therefore, it is merely an optimization of the latency for the visual effect, but it does not change the behaviour of the application.

When for some reason the visual effect does change, one may use resetLearnedSlot() or resetLearnedSlots() to flush the existing cached visual effect, forcing the library to relearn it.

It is crucial that this function be applied first to a slot that is intended to be stateless before any EventSignal connects to that slot. Otherwise, the connecting EventSignal cannot find the stateless slot implementation for the intended slot, and the statement will have no effect for that connection.

See also
resetLearnedSlot(), EventSignal

◆ implementStateless() [2/2]

template<class T >
WStatelessSlot * Wt::WObject::implementStateless ( void(T::*)()  method,
void(T::*)()  undoMethod 
)

Declares a slot to be stateless and learn client-side behaviour in advance.

This method has the same effect as implementStateless(void (T::*method)()), but learns the visual effect of the slot before the first invocation of the event.

To learn the visual effect, the library will simulate the event and record the visual effect. To restore the application state, it will call the undoMethod which must restore the effect of method.

See also
implementStateless(void (T::*method)())

◆ isNotStateless()

void Wt::WObject::isNotStateless ( )

Marks the current function as not stateless.

This may be useful if your current function is manipulating the UI in a way that is not stateless (i.e. does depend on some state), but which happens to be called from within a function that is marked stateless (such as WWidget::setHidden()). This will reject stateless slot pre-learning in this case, reverting to plain server-side dynamic UI updates.

◆ objectName()

std::string Wt::WObject::objectName ( ) const
virtual

Returns the object name.

See also
setObjectName()

Reimplemented in Wt::WCompositeWidget.

◆ removeChild()

void Wt::WObject::removeChild ( WObject child)
virtual

Removes a child object.

The child must have been previously added.

See also
addChild()

Reimplemented in Wt::WWidget.

◆ resetLearnedSlot()

template<class T >
void Wt::WObject::resetLearnedSlot ( void(T::*)()  method)

Resets a learned stateless slot implementation.

Clears the stateless implementation for the given slot that was declared to be implemented with a stateless implementation.

When something has changed that breaks the contract of a stateless slot to always have the same effect, you may call this method to force the application to discard the current implementation.

See also
implementStateless()

◆ resetLearnedSlots()

void Wt::WObject::resetLearnedSlots ( )

Resets learned stateless slot implementations.

Clears the stateless implementation for all slots declared to be implemented with a stateless implementation.

See also
resetLearnedSlot(), implementStateless()

◆ sender()

WObject * Wt::WObject::sender ( )
staticprotected

Returns the sender of the current slot call.

Use this function to know who emitted the signal that triggered this slot call. It may be 0 if the signal has no owner information, or if there is no signal triggering the current slot, but instead the slot method is called directly.

◆ setObjectName()

void Wt::WObject::setObjectName ( const std::string &  name)
virtual

Sets an object name.

The object name can be used to easily identify a type of object in the DOM, and does not need to be unique. It will usually reflect the widget type or role. The object name is prepended to the auto-generated object id().

The default object name is empty.

Note
Only letters ([A-Za-z]), digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".") are allowed in the id.
See also
id()

Reimplemented in Wt::WWidget, Wt::WAbstractItemView, Wt::WTreeView, and Wt::WCompositeWidget.


Generated on Tue Dec 15 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.13