From ad32ac5b4f05c9eed1fb7a93ee7947050d840a19 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Sat, 22 Aug 2020 17:21:36 +0200 Subject: Make bindings introspectable through moc Add a new BINDABLE declaration to the Q_PROPERTY() macro that tells moc where to find the QBindable for the property. Add a QUntypedBindable base class to QBindable that gives access to generic functionality and checks argument compatibility at runtime. QBindable will still do static checking at compile time. Add QMetaProperty::isBindable() and QMetaProperty::bindable() to be able to dynamically access the binding functionality. Change-Id: Ic7b08ae2cde83fd43e627d813a886e1de01fa3dc Reviewed-by: Fabian Kosmale --- src/corelib/kernel/qmetaobject.cpp | 29 ++++++++++--- src/corelib/kernel/qmetaobject.h | 6 ++- src/corelib/kernel/qmetaobject_p.h | 2 +- src/corelib/kernel/qobjectdefs.h | 3 +- src/corelib/kernel/qproperty.h | 87 +++++++++++++++++++++++++++++--------- 5 files changed, 97 insertions(+), 30 deletions(-) (limited to 'src/corelib/kernel') diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp index f0cef22b6f..ef042317c1 100644 --- a/src/corelib/kernel/qmetaobject.cpp +++ b/src/corelib/kernel/qmetaobject.cpp @@ -3251,6 +3251,23 @@ bool QMetaProperty::reset(QObject *object) const QMetaObject::metacall(object, QMetaObject::ResetProperty, data.index(mobj) + mobj->propertyOffset(), argv); return true; } + +/*! + \since 6.0 + Returns the bindable interface for the property on a given \a object. + + If the property doesn't support bindings, the returned interface will be + invalid. + + \sa QUntypedBindable, QProperty, isBindable() +*/ +QUntypedBindable QMetaProperty::bindable(QObject *object) const +{ + QUntypedBindable bindable; + void * argv[1] { &bindable }; + mobj->metacall(object, QMetaObject::BindableProperty, data.index(mobj) + mobj->propertyOffset(), argv); + return bindable; +} /*! \since 5.5 @@ -3509,16 +3526,18 @@ bool QMetaProperty::isRequired() const /*! \since 6.0 - Returns \c true if the property is implemented using a QProperty member; otherwise returns \c false. + Returns \c true if the \c{Q_PROPERTY()} exposes binding functionality; otherwise returns false. + + This implies that you can create bindings that use this property as a dependency or install QPropertyObserver + objects on this property. Unless the property is readonly, you can also set a binding on this property. - This can be used to detect the availability of QProperty related meta-call types ahead of - performing the call itself. + \sa QProperty, isReadOnly(), bindable() */ -bool QMetaProperty::isQProperty() const +bool QMetaProperty::isBindable() const { if (!mobj) return false; - return data.flags() & IsQProperty; + return (data.flags() & Bindable); } /*! diff --git a/src/corelib/kernel/qmetaobject.h b/src/corelib/kernel/qmetaobject.h index 8136e04dc9..08c57d5f28 100644 --- a/src/corelib/kernel/qmetaobject.h +++ b/src/corelib/kernel/qmetaobject.h @@ -46,6 +46,8 @@ QT_BEGIN_NAMESPACE +class QUntypedBindable; + #define Q_METAMETHOD_INVOKE_MAX_ARGS 10 class Q_CORE_EXPORT QMetaMethod @@ -294,7 +296,7 @@ public: bool isConstant() const; bool isFinal() const; bool isRequired() const; - bool isQProperty() const; + bool isBindable() const; bool isFlagType() const; bool isEnumType() const; @@ -310,6 +312,8 @@ public: bool write(QObject *obj, const QVariant &value) const; bool reset(QObject *obj) const; + QUntypedBindable bindable(QObject *object) const; + QVariant readOnGadget(const void *gadget) const; bool writeOnGadget(void *gadget, const QVariant &value) const; bool resetOnGadget(void *gadget) const; diff --git a/src/corelib/kernel/qmetaobject_p.h b/src/corelib/kernel/qmetaobject_p.h index 0d6cd7396d..d205034d77 100644 --- a/src/corelib/kernel/qmetaobject_p.h +++ b/src/corelib/kernel/qmetaobject_p.h @@ -81,7 +81,7 @@ enum PropertyFlags { Stored = 0x00010000, User = 0x00100000, Required = 0x01000000, - IsQProperty = 0x02000000 + Bindable = 0x02000000 }; enum MethodFlags { diff --git a/src/corelib/kernel/qobjectdefs.h b/src/corelib/kernel/qobjectdefs.h index 2013af1695..6c6e4e5a8e 100644 --- a/src/corelib/kernel/qobjectdefs.h +++ b/src/corelib/kernel/qobjectdefs.h @@ -386,8 +386,7 @@ struct Q_CORE_EXPORT QMetaObject IndexOfMethod, RegisterPropertyMetaType, RegisterMethodArgumentMetaType, - RegisterQPropertyObserver, - SetQPropertyBinding + BindableProperty }; int static_metacall(Call, int, void **) const; diff --git a/src/corelib/kernel/qproperty.h b/src/corelib/kernel/qproperty.h index b3f4644031..27ad20d51a 100644 --- a/src/corelib/kernel/qproperty.h +++ b/src/corelib/kernel/qproperty.h @@ -615,10 +615,12 @@ struct QBindableInterface using BindingSetter = QUntypedPropertyBinding (*)(QUntypedPropertyData *d, const QUntypedPropertyBinding &binding); using MakeBinding = QUntypedPropertyBinding (*)(const QUntypedPropertyData *d, const QPropertyBindingSourceLocation &location); using SetObserver = void (*)(const QUntypedPropertyData *d, QPropertyObserver *observer); + using GetMetaType = QMetaType (*)(); BindingGetter getBinding; BindingSetter setBinding; MakeBinding makeBinding; SetObserver setObserver; + GetMetaType metaType; }; template @@ -634,7 +636,8 @@ public: [](const QUntypedPropertyData *d, const QPropertyBindingSourceLocation &location) -> QUntypedPropertyBinding { return Qt::makePropertyBinding([d]() -> T { return static_cast(d)->value(); }, location); }, [](const QUntypedPropertyData *d, QPropertyObserver *observer) -> void - { observer->setSource(static_cast(d)->bindingData()); } + { observer->setSource(static_cast(d)->bindingData()); }, + []() { return QMetaType::fromType(); } }; }; @@ -651,36 +654,45 @@ public: [](const QUntypedPropertyData *d, const QPropertyBindingSourceLocation &location) -> QUntypedPropertyBinding { return Qt::makePropertyBinding([d]() -> T { return static_cast(d)->value(); }, location); }, [](const QUntypedPropertyData *d, QPropertyObserver *observer) -> void - { observer->setSource(static_cast(d)->bindingData()); } + { observer->setSource(static_cast(d)->bindingData()); }, + []() { return QMetaType::fromType(); } }; }; } -template -class QBindable +class QUntypedBindable { protected: - QUntypedPropertyData *data; - const QtPrivate::QBindableInterface *iface; + QUntypedPropertyData *data = nullptr; + const QtPrivate::QBindableInterface *iface = nullptr; public: + constexpr QUntypedBindable() = default; template - QBindable(Property *p) + QUntypedBindable(Property *p) : data(const_cast *>(p)), iface(&QtPrivate::QBindableInterfaceForProperty::iface) - {} + { Q_ASSERT(data && iface); } - QPropertyBinding makeBinding(const QPropertyBindingSourceLocation &location = QT_PROPERTY_DEFAULT_BINDING_LOCATION) + bool isValid() const { return data != nullptr; } + bool isBindable() const { return iface && iface->getBinding; } + + QUntypedPropertyBinding makeBinding(const QPropertyBindingSourceLocation &location = QT_PROPERTY_DEFAULT_BINDING_LOCATION) + { + return iface ? iface->makeBinding(data, location) : QUntypedPropertyBinding(); + } + void observe(QPropertyObserver *observer) { - return static_cast &&>(iface->makeBinding(data, location)); + if (iface) + iface->setObserver(data, observer); } template QPropertyChangeHandler onValueChanged(Functor f) { QPropertyChangeHandler handler(f); - iface->setObserver(data, &handler); + observe(&handler); return handler; } @@ -691,17 +703,54 @@ public: return onValueChanged(f); } - QPropertyBinding binding() const + QUntypedPropertyBinding binding() const { if (!iface->getBinding) - return QPropertyBinding(); - return static_cast &&>(iface->getBinding(data)); + return QUntypedPropertyBinding(); + return iface->getBinding(data); } - QPropertyBinding setBinding(const QPropertyBinding &binding) + bool setBinding(const QUntypedPropertyBinding &binding) { if (!iface->setBinding) - return QPropertyBinding(); - return static_cast &&>(iface->setBinding(data, binding)); + return false; + if (!binding.isNull() && binding.valueMetaType() != iface->metaType()) + return false; + iface->setBinding(data, binding); + return true; + } + bool hasBinding() const + { + return !binding().isNull(); + } + +}; + +template +class QBindable : public QUntypedBindable +{ +public: + using QUntypedBindable::QUntypedBindable; + explicit QBindable(const QUntypedBindable &b) : QUntypedBindable(b) + { + if (iface && iface->metaType() != QMetaType::fromType()) { + data = nullptr; + iface = nullptr; + } + } + + QPropertyBinding makeBinding(const QPropertyBindingSourceLocation &location = QT_PROPERTY_DEFAULT_BINDING_LOCATION) + { + return static_cast &&>(QUntypedBindable::makeBinding(location)); + } + QPropertyBinding binding() const + { + return static_cast &&>(QUntypedBindable::binding()); + } + using QUntypedBindable::setBinding; + QPropertyBinding setBinding(const QPropertyBinding &binding) + { + Q_ASSERT(!iface || binding.isNull() || binding.valueMetaType() == iface->metaType()); + return iface ? static_cast &&>(iface->setBinding(data, binding)) : QPropertyBinding(); } #ifndef Q_CLANG_QDOC template @@ -715,10 +764,6 @@ public: template QPropertyBinding setBinding(Functor f); #endif - bool hasBinding() const - { - return !binding().isNull(); - } }; struct QBindingStatus; -- cgit v1.2.3