Qx v0.6
Qt Extensions Library
Loading...
Searching...
No Matches
Qx::Bindable< T > Class Template Reference

Bindable is a wrapper class around binding-enabled properties that provides uniform access, regardless of the specific type. More...

#include <qx/core/qx-property.h>

Public Member Functions

 Bindable ()
 
 Bindable (AbstractBindableProperty< T > &bp)
 
 Bindable (const AbstractBindableProperty< T > &bp)
 
 Bindable (QObject *obj, const char *property)
 
 Bindable (QObject *obj, const QMetaProperty &property)
 
template<std::invocable Functor>
void addLifetimeNotifier (Functor &&f) const
 
template<std::invocable Functor>
PropertyNotifier addNotifier (Functor &&f) const
 
PropertyBinding< T > binding () const
 
bool hasBinding () const
 
bool isReadOnly () const
 
bool isValid () const
 
 operator const T & () const
 
const T & operator* () const
 
decltype(auto) operator-> () const
 
Bindableoperator= (const T &newValue) noexcept
 
Bindableoperator= (T &&newValue) noexcept
 
void removeBinding ()
 
PropertyBinding< T > setBinding (const PropertyBinding< T > &binding)
 
template<std::invocable Functor>
PropertyBinding< T > setBinding (Functor &&f)
 
void setValue (const T &newValue)
 
void setValue (T &&newValue)
 
void setValueBypassingBindings (const T &v)
 
void setValueBypassingBindings (T &&v)
 
template<std::invocable Functor>
PropertyNotifier subscribe (Functor &&f) const
 
template<std::invocable Functor>
void subscribeLifetime (Functor &&f) const
 
PropertyBinding< T > takeBinding ()
 
const T & value () const
 
const T & valueBypassingBindings () const
 

Detailed Description

template<typename T>
class Qx::Bindable< T >

Bindable acts as a convenience type for sharing access to any concrete bindable property without the need to use interface pointers directly, via a thin, cheap to copy wrapper.

The methods of this class essentially mirror those of AbstractBindableProperty and an instance of this class can be used in an identical fashion as the property it shadows.

Additionally, the constructors that take a QObject pointer can be used to wrap a property of a classes based on it (i.e. those created using Q_PROPERTY()), which are not already bindable.

Due to the abstraction that this class provides, instances may be "read-only", meaning that any attempt to use a non-const method will fail and a warning will be emitted. The contexts in which this is the case are noted in the documentation for the constructors of this class. Use isReadOnly() to check if a particular instance cannot mutate the property it wraps.

Note

Since the Qx Bindable Properties System does not directly interact with the native Qt equivalent, the aforementioned constructor of QBindable will need to be used to bind to the property of any any QObject derived typed that does not have accessor methods to underlying Qx bindable properties (e.g. Qx::Property), even if the property in question is already bindable using Qt's system.

The best way to create a QObject-based class that natively uses Qx properties is to add instances of Property as class members and provide an accessor method to them that returns a Qx::Bindable, like so:

class MyObject : public QObject
{
Q_OBJECT
public:
Qx::Bindable<int> bindableData() { return data; }
};
Bindable is a wrapper class around binding-enabled properties that provides uniform access,...
Definition qx-property.h:474
The Property class is a template class that enables automatic property bindings.
Definition qx-property.h:688
See also
Property and Qx Bindable Properties System

Constructor & Destructor Documentation

◆ Bindable() [1/5]

template<typename T >
Qx::Bindable< T >::Bindable ( )
inline

Constructs an invalid Bindable.

◆ Bindable() [2/5]

template<typename T >
Qx::Bindable< T >::Bindable ( AbstractBindableProperty< T > & bp)
inline

Constructs a Bindable wrapper for the bindable property bp.

◆ Bindable() [3/5]

template<typename T >
Qx::Bindable< T >::Bindable ( const AbstractBindableProperty< T > & bp)
inline

Constructs a Bindable wrapper for the bindable property bp.

Note
The Bindable will be read-only since bp is const.

◆ Bindable() [4/5]

template<typename T >
Qx::Bindable< T >::Bindable ( QObject * obj,
const QMetaProperty & property )
inline

Constructs a Bindable wrapper for property property of obj.

The property must have a notify signal, and you must access the property through the created Bindable (e.g. via value(), etc.) instead of the normal property READ function (or MEMBER) to enable dependency tracking.

When binding using a lambda, you may prefer to capture the QBindable by value to avoid the cost of calling this constructor in the binding expression.

Note
The Bindable will be read-only if property is itself read-only.

◆ Bindable() [5/5]

template<typename T >
Qx::Bindable< T >::Bindable ( QObject * obj,
const char * property )
inline

Constructs a Bindable wrapper for the property named property of obj.

See Bindable(QObject*, const QMetaProperty&).

Note
The Bindable will be read-only if property is read-only itself.

Member Function Documentation

◆ addLifetimeNotifier()

template<typename T >
template<std::invocable Functor>
void Qx::Bindable< T >::addLifetimeNotifier ( Functor && f) const
inline

Same as addNotifier(), but the lifetime of the subscription is tied to the lifetime of the property so no change handler object is returned.

Warning
Be sure that any data referenced in f lives as long as the property itself.
See also
addNotifier() and subscribeLifetime().

◆ addNotifier()

template<typename T >
template<std::invocable Functor>
PropertyNotifier Qx::Bindable< T >::addNotifier ( Functor && f) const
inline

Subscribes the given functor f as a callback that is called whenever the value of the property changes.

The callback f is expected to be a type that has a plain call operator() without any parameters. This means that you can provide a C++ lambda expression, a std::function or even a custom struct with a call operator.

The returned property change handler object keeps track of the subscription. When it goes out of scope, the callback is unsubscribed.

See also
addLifetimeNotifier() and subscribe().

◆ binding()

template<typename T >
PropertyBinding< T > Qx::Bindable< T >::binding ( ) const
inline

Returns the binding expression that is associated with this property. A default constructed PropertyBinding will be returned if no such association exists.

◆ hasBinding()

template<typename T >
bool Qx::Bindable< T >::hasBinding ( ) const
inline

Returns true if the property has a binding associated with it; otherwise, returns false.

◆ isReadOnly()

template<typename T >
bool Qx::Bindable< T >::isReadOnly ( ) const
inline

Returns true if the Bindable wraps a read-only property; otherwise, returns false.

A Bindable may be read-only depending on how it was constructed.

See also
Bindable(QObject* obj, const QMetaProperty&) and Bindable(const AbstractBindableProperty<T>&)

◆ isValid()

template<typename T >
bool Qx::Bindable< T >::isValid ( ) const
inline

Returns true if the Bindable is valid; otherwise, returns false.

A binding is invalid if there was an issue the arguments passed to its constructor, like a null pointer or a property being specified that does not actually belong to the preceding object, or if the default constructor was used.

◆ operator const T &()

template<typename T >
Qx::Bindable< T >::operator const T & ( ) const
inline

Type-conversion operator for a const reference to the underlying type.

◆ operator*()

template<typename T >
const T & Qx::Bindable< T >::operator* ( ) const
inline

Same as value().

◆ operator->()

template<typename T >
const T * Qx::Bindable< T >::operator-> ( ) const
inline

Returns a pointer to the underlying property data (bindings are still respected).

See also
value().

◆ operator=() [1/2]

template<typename T >
Bindable & Qx::Bindable< T >::operator= ( const T & newValue)
inlinenoexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ operator=() [2/2]

template<typename T >
Bindable & Qx::Bindable< T >::operator= ( T && newValue)
inlinenoexcept

Assigns newValue to the property and returns a reference to this bindable.

◆ removeBinding()

template<typename T >
void Qx::Bindable< T >::removeBinding ( )
inline

Disassociates the binding expression from this property. After calling this function, the value of the property will only change if you assign a new value to it, or when a new binding is set.

See also
takeBinding() and setBinding().

◆ setBinding() [1/2]

template<typename T >
PropertyBinding< T > Qx::Bindable< T >::setBinding ( const PropertyBinding< T > & binding)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Associates the value of this property with the provided binding expression and returns the previously associated binding. The property's value is set to the result of evaluating the new binding. Whenever a dependency of the binding changes, the binding will be re-evaluated, and the property's value gets updated accordingly.

◆ setBinding() [2/2]

template<typename T >
template<std::invocable Functor>
PropertyBinding< T > Qx::Bindable< T >::setBinding ( Functor && f)
inline

Associates the value of this property with the provided functor f and returns the previously associated binding. The property's value is set to the result of evaluating the new binding. Whenever a dependency of the binding changes, the binding will be re-evaluated, and the property's value gets updated accordingly.

◆ setValue() [1/2]

template<typename T >
void Qx::Bindable< T >::setValue ( const T & newValue)
inline

Assigns newValue to this property and removes the property's associated binding, if present.

See also
binding() and beginPropertyUpdateGroup().

◆ setValue() [2/2]

template<typename T >
void Qx::Bindable< T >::setValue ( T && newValue)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ setValueBypassingBindings() [1/2]

template<typename T >
void Qx::Bindable< T >::setValueBypassingBindings ( const T & v)
inline

Directly sets the value of the property to v.

This is generally only used by a derived class to control how the underlying data is written when a copy of T is provided, but there are some cases (like maintaining class invariants) where it is useful externally.

Note
Using this method will bypass any potential binding registered for this property.

◆ setValueBypassingBindings() [2/2]

template<typename T >
void Qx::Bindable< T >::setValueBypassingBindings ( T && v)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Directly sets the value of this property to v.

This is generally only used by a derived class to control how the underlying data is written when an rvalue of T is provided, but there are some cases (like maintaining class invariants) where it is useful externally.

Note
Using this method will bypass any potential binding registered for this property.

◆ subscribe()

template<typename T >
template<std::invocable Functor>
PropertyNotifier Qx::Bindable< T >::subscribe ( Functor && f) const
inline

Same as invoking f (e.g. f()), followed by addNotifier(f).

That is, the callback functor is called immediately before it's registered.

See also
subscribeLifetime() and addNotifier().

◆ subscribeLifetime()

template<typename T >
template<std::invocable Functor>
void Qx::Bindable< T >::subscribeLifetime ( Functor && f) const
inline

Same as invoking f (e.g. f()), followed by addLifetimeNotifier(f).

That is, the callback functor is called immediately before it's registered.

See also
subscribe() and addLifetimeNotifier().

◆ takeBinding()

template<typename T >
PropertyBinding< T > Qx::Bindable< T >::takeBinding ( )
inline

Disassociates the binding expression from this property and returns it. After calling this function, the value of the property will only change if you assign a new value to it, or when a new binding is set.

See also
removeBinding() and setBinding().

◆ value()

template<typename T >
const T & Qx::Bindable< T >::value ( ) const
inline

Returns the value of the property. This may evaluate a binding expression that is tied to this property, before returning the value.

See also
value().

◆ valueBypassingBindings()

template<typename T >
const T & Qx::Bindable< T >::valueBypassingBindings ( ) const
inline

Returns the direct value of the property.

This is generally only used by a derived class to control how the underlying data is read when requested, but there are some cases where it is useful externally.

Note
Using this method will not register the property access with any currently executing binding.

The documentation for this class was generated from the following files: