diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/kernel/qmetaobject.cpp | 29 | ||||
-rw-r--r-- | src/corelib/kernel/qmetaobject.h | 6 | ||||
-rw-r--r-- | src/corelib/kernel/qmetaobject_p.h | 2 | ||||
-rw-r--r-- | src/corelib/kernel/qobjectdefs.h | 3 | ||||
-rw-r--r-- | src/corelib/kernel/qproperty.h | 87 | ||||
-rw-r--r-- | src/tools/moc/generator.cpp | 59 | ||||
-rw-r--r-- | src/tools/moc/moc.cpp | 29 | ||||
-rw-r--r-- | src/tools/moc/moc.h | 3 |
8 files changed, 120 insertions, 98 deletions
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<typename Property, typename = void> @@ -634,7 +636,8 @@ public: [](const QUntypedPropertyData *d, const QPropertyBindingSourceLocation &location) -> QUntypedPropertyBinding { return Qt::makePropertyBinding([d]() -> T { return static_cast<const Property *>(d)->value(); }, location); }, [](const QUntypedPropertyData *d, QPropertyObserver *observer) -> void - { observer->setSource(static_cast<const Property *>(d)->bindingData()); } + { observer->setSource(static_cast<const Property *>(d)->bindingData()); }, + []() { return QMetaType::fromType<T>(); } }; }; @@ -651,36 +654,45 @@ public: [](const QUntypedPropertyData *d, const QPropertyBindingSourceLocation &location) -> QUntypedPropertyBinding { return Qt::makePropertyBinding([d]() -> T { return static_cast<const Property *>(d)->value(); }, location); }, [](const QUntypedPropertyData *d, QPropertyObserver *observer) -> void - { observer->setSource(static_cast<const Property *>(d)->bindingData()); } + { observer->setSource(static_cast<const Property *>(d)->bindingData()); }, + []() { return QMetaType::fromType<T>(); } }; }; } -template<typename T> -class QBindable +class QUntypedBindable { protected: - QUntypedPropertyData *data; - const QtPrivate::QBindableInterface *iface; + QUntypedPropertyData *data = nullptr; + const QtPrivate::QBindableInterface *iface = nullptr; public: + constexpr QUntypedBindable() = default; template<typename Property> - QBindable(Property *p) + QUntypedBindable(Property *p) : data(const_cast<std::remove_cv_t<Property> *>(p)), iface(&QtPrivate::QBindableInterfaceForProperty<Property>::iface) - {} + { Q_ASSERT(data && iface); } - QPropertyBinding<T> 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<QPropertyBinding<T> &&>(iface->makeBinding(data, location)); + if (iface) + iface->setObserver(data, observer); } template<typename Functor> QPropertyChangeHandler<Functor> onValueChanged(Functor f) { QPropertyChangeHandler<Functor> handler(f); - iface->setObserver(data, &handler); + observe(&handler); return handler; } @@ -691,17 +703,54 @@ public: return onValueChanged(f); } - QPropertyBinding<T> binding() const + QUntypedPropertyBinding binding() const { if (!iface->getBinding) - return QPropertyBinding<T>(); - return static_cast<QPropertyBinding<T> &&>(iface->getBinding(data)); + return QUntypedPropertyBinding(); + return iface->getBinding(data); } - QPropertyBinding<T> setBinding(const QPropertyBinding<T> &binding) + bool setBinding(const QUntypedPropertyBinding &binding) { if (!iface->setBinding) - return QPropertyBinding<T>(); - return static_cast<QPropertyBinding<T> &&>(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<typename T> +class QBindable : public QUntypedBindable +{ +public: + using QUntypedBindable::QUntypedBindable; + explicit QBindable(const QUntypedBindable &b) : QUntypedBindable(b) + { + if (iface && iface->metaType() != QMetaType::fromType<T>()) { + data = nullptr; + iface = nullptr; + } + } + + QPropertyBinding<T> makeBinding(const QPropertyBindingSourceLocation &location = QT_PROPERTY_DEFAULT_BINDING_LOCATION) + { + return static_cast<QPropertyBinding<T> &&>(QUntypedBindable::makeBinding(location)); + } + QPropertyBinding<T> binding() const + { + return static_cast<QPropertyBinding<T> &&>(QUntypedBindable::binding()); + } + using QUntypedBindable::setBinding; + QPropertyBinding<T> setBinding(const QPropertyBinding<T> &binding) + { + Q_ASSERT(!iface || binding.isNull() || binding.valueMetaType() == iface->metaType()); + return iface ? static_cast<QPropertyBinding<T> &&>(iface->setBinding(data, binding)) : QPropertyBinding<T>(); } #ifndef Q_CLANG_QDOC template <typename Functor> @@ -715,10 +764,6 @@ public: template <typename Functor> QPropertyBinding<T> setBinding(Functor f); #endif - bool hasBinding() const - { - return !binding().isNull(); - } }; struct QBindingStatus; diff --git a/src/tools/moc/generator.cpp b/src/tools/moc/generator.cpp index 1d2fa5d1d7..155a98209d 100644 --- a/src/tools/moc/generator.cpp +++ b/src/tools/moc/generator.cpp @@ -906,8 +906,8 @@ void Generator::generateProperties() if (p.required) flags |= Required; - if (p.isQProperty) - flags |= IsQProperty; + if (!p.bind.isEmpty()) + flags |= Bindable; fprintf(out, " %4d, ", stridx(p.name)); generateTypeInfo(p.type); @@ -1025,9 +1025,8 @@ void Generator::generateMetacall() fprintf(out, "else "); fprintf(out, "if (_c == QMetaObject::ReadProperty || _c == QMetaObject::WriteProperty\n" - " || _c == QMetaObject::ResetProperty || _c == QMetaObject::RegisterPropertyMetaType\n" - " || _c == QMetaObject::RegisterQPropertyObserver\n" - " || _c == QMetaObject::SetQPropertyBinding) {\n" + " || _c == QMetaObject::ResetProperty || _c == QMetaObject::BindableProperty\n" + " || _c == QMetaObject::RegisterPropertyMetaType) {\n" " qt_static_metacall(this, _c, _id, _a);\n" " _id -= %d;\n }", int(cdef->propertyList.count())); fprintf(out, "\n#endif // QT_NO_PROPERTIES"); @@ -1268,7 +1267,7 @@ void Generator::generateStaticMetacall() bool needTempVarForGet = false; bool needSet = false; bool needReset = false; - bool haveQProperties = false; + bool hasBindableProperties = false; for (int i = 0; i < cdef->propertyList.size(); ++i) { const PropertyDef &p = cdef->propertyList.at(i); needGet |= !p.read.isEmpty() || !p.member.isEmpty(); @@ -1278,7 +1277,7 @@ void Generator::generateStaticMetacall() needSet |= !p.write.isEmpty() || (!p.member.isEmpty() && !p.constant); needReset |= !p.reset.isEmpty(); - haveQProperties |= p.isQProperty; + hasBindableProperties |= !p.bind.isEmpty(); } fprintf(out, "\n#ifndef QT_NO_PROPERTIES\n "); @@ -1404,59 +1403,21 @@ void Generator::generateStaticMetacall() } fprintf(out, " }"); -#if 0 fprintf(out, " else "); - fprintf(out, "if (_c == QMetaObject::RegisterQPropertyObserver) {\n"); - if (haveQProperties) { + fprintf(out, "if (_c == QMetaObject::BindableProperty) {\n"); + if (hasBindableProperties) { setupMemberAccess(); - fprintf(out, " QPropertyObserver *observer = reinterpret_cast<QPropertyObserver *>(_a[0]);\n"); fprintf(out, " switch (_id) {\n"); for (int propindex = 0; propindex < cdef->propertyList.size(); ++propindex) { const PropertyDef &p = cdef->propertyList.at(propindex); - if (!p.isQProperty) + if (p.bind.isEmpty()) continue; - QByteArray prefix = "_t->"; - if (p.qpropertyname.isEmpty() || p.stored == "true") { - fprintf(out, " case %d: observer->setSource(%s%s); break;\n", - propindex, prefix.constData(), p.bindingAccessor.constData()); - } else { - fprintf(out, " case %d: if (auto *source = %s%s) observer->setSource(*source); break; \n", - propindex, prefix.constData(), p.bindingAccessor.constData()); - } + fprintf(out, " case %d: *static_cast<QUntypedBindable *>(_a[0]) = _t->%s(); break;\n", propindex, p.bind.constData()); } fprintf(out, " default: break;\n"); fprintf(out, " }\n"); } fprintf(out, " }"); - - fprintf(out, " else "); - fprintf(out, "if (_c == QMetaObject::SetQPropertyBinding) {\n"); - if (haveQProperties) { - setupMemberAccess(); - fprintf(out, " switch (_id) {\n"); - for (int propindex = 0; propindex < cdef->propertyList.size(); ++propindex) { - const PropertyDef &p = cdef->propertyList.at(propindex); - if (!p.isQProperty) - continue; - QByteArray prefix = "_t->"; - - if (p.qpropertyname.isEmpty() || p.stored == "true") { - fprintf(out, " case %d: %s%s.setBinding(*reinterpret_cast<QPropertyBinding<%s> *>(_a[0])); break;\n", - propindex, prefix.constData(), - p.bindingAccessor.constData(), - p.type.constData()); - } else { - fprintf(out, " case %d: if (auto *source = %s%s) source->setBinding(*reinterpret_cast<QPropertyBinding<%s> *>(_a[0])); break;\n", - propindex, prefix.constData(), p.bindingAccessor.constData(), - p.type.constData()); - } - - } - fprintf(out, " default: break;\n"); - fprintf(out, " }\n"); - } - fprintf(out, " }"); -#endif fprintf(out, "\n#endif // QT_NO_PROPERTIES"); needElse = true; } diff --git a/src/tools/moc/moc.cpp b/src/tools/moc/moc.cpp index 1a20d32d7c..516b231cd5 100644 --- a/src/tools/moc/moc.cpp +++ b/src/tools/moc/moc.cpp @@ -1022,7 +1022,7 @@ static QByteArrayList requiredQtContainers(const QList<ClassDef> &classes) for (const auto &c : classes) { for (const auto &p : c.propertyList) - needsQProperty |= p.isQProperty; + needsQProperty |= !p.bind.isEmpty(); if (any_type_contains(c.propertyList, pattern) || any_arg_contains(c.slotList, pattern) || any_arg_contains(c.signalList, pattern) || @@ -1219,23 +1219,9 @@ void Moc::createPropertyDef(PropertyDef &propDef) { propDef.location = index; - const bool isPrivateProperty = !propDef.inPrivateClass.isEmpty(); - bool typeWrappedInQProperty = false; - if (isPrivateProperty) { - const int rewind = index; - if (test(IDENTIFIER) && lexem() == "QProperty" && test(LANGLE)) { - typeWrappedInQProperty = true; - propDef.isQProperty = true; - } else { - index = rewind; - } - } - QByteArray type = parseType().name; if (type.isEmpty()) error(); - if (typeWrappedInQProperty) - next(RANGLE); propDef.designable = propDef.scriptable = propDef.stored = "true"; propDef.user = "false"; /* @@ -1346,6 +1332,9 @@ void Moc::parsePropertyAttributes(PropertyDef &propDef) case 'W': if (l != "WRITE") error(2); propDef.write = v; break; + case 'B': if (l != "BINDABLE") error(2); + propDef.bind = v; + break; case 'D': if (l != "DESIGNABLE") error(2); propDef.designable = v + v2; checkIsFunction(propDef.designable, "DESIGNABLE"); @@ -1373,6 +1362,12 @@ void Moc::parsePropertyAttributes(PropertyDef &propDef) propDef.constant = false; warning(msg.constData()); } + if (propDef.constant && !propDef.bind.isNull()) { + const QByteArray msg = "Property declaration " + propDef.name + + " is both BINDable and CONSTANT. CONSTANT will be ignored."; + propDef.constant = false; + warning(msg.constData()); + } } void Moc::parseProperty(ClassDef *def) @@ -1808,7 +1803,7 @@ void Moc::checkProperties(ClassDef *cdef) warning(msg.constData()); } - if (p.read.isEmpty() && p.member.isEmpty() && !p.isQProperty) { + if (p.read.isEmpty() && p.member.isEmpty() && p.bind.isEmpty()) { const int rewind = index; if (p.location >= 0) index = p.location; @@ -2012,6 +2007,7 @@ QJsonObject PropertyDef::toJson() const jsonify("member", member); jsonify("read", read); jsonify("write", write); + jsonify("bindable", bind); jsonify("reset", reset); jsonify("notify", notify); jsonify("privateClass", inPrivateClass); @@ -2035,7 +2031,6 @@ QJsonObject PropertyDef::toJson() const prop[QLatin1String("constant")] = constant; prop[QLatin1String("final")] = final; prop[QLatin1String("required")] = required; - prop[QLatin1String("isQProperty")] = isQProperty; if (revision > 0) prop[QLatin1String("revision")] = revision; diff --git a/src/tools/moc/moc.h b/src/tools/moc/moc.h index def5a8e82d..7f63402aca 100644 --- a/src/tools/moc/moc.h +++ b/src/tools/moc/moc.h @@ -132,7 +132,7 @@ struct PropertyDef return (s == write); } - QByteArray name, type, member, read, write, reset, designable, scriptable, stored, user, notify, inPrivateClass; + QByteArray name, type, member, read, write, bind, reset, designable, scriptable, stored, user, notify, inPrivateClass; int notifyId = -1; // -1 means no notifyId, >= 0 means signal defined in this class, < -1 means signal not defined in this class enum Specification { ValueSpec, ReferenceSpec, PointerSpec }; Specification gspec = ValueSpec; @@ -140,7 +140,6 @@ struct PropertyDef bool constant = false; bool final = false; bool required = false; - bool isQProperty = false; int location = -1; // token index, used for error reporting |