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 --- tests/auto/tools/moc/allmocs_baseline_in.json | 20 +-- tests/auto/tools/moc/tst_moc.cpp | 222 ++++++++++++-------------- 2 files changed, 101 insertions(+), 141 deletions(-) (limited to 'tests/auto/tools') diff --git a/tests/auto/tools/moc/allmocs_baseline_in.json b/tests/auto/tools/moc/allmocs_baseline_in.json index 85751845c4..b477aa01f1 100644 --- a/tests/auto/tools/moc/allmocs_baseline_in.json +++ b/tests/auto/tools/moc/allmocs_baseline_in.json @@ -1063,7 +1063,6 @@ "constant": false, "designable": true, "final": false, - "isQProperty": false, "name": "prop1", "read": "getProp1", "required": false, @@ -1077,7 +1076,6 @@ "constant": false, "designable": true, "final": false, - "isQProperty": false, "name": "prop2", "read": "getProp2", "required": false, @@ -1091,7 +1089,6 @@ "constant": false, "designable": true, "final": false, - "isQProperty": false, "name": "prop3", "read": "getProp3", "required": false, @@ -1204,7 +1201,6 @@ "constant": false, "designable": true, "final": false, - "isQProperty": false, "name": "flags", "read": "flags", "required": false, @@ -1231,7 +1227,6 @@ "constant": false, "designable": true, "final": false, - "isQProperty": false, "name": "flags", "read": "flags", "required": false, @@ -1245,7 +1240,6 @@ "constant": false, "designable": true, "final": false, - "isQProperty": false, "name": "flagsList", "read": "flagsList", "required": false, @@ -1593,8 +1587,8 @@ "outputRevision": 68 }, { - "classes": [ - { + "classes": [ + { "className": "TestPointeeCanBeIncomplete", "object": true, "qualifiedClassName": "TestPointeeCanBeIncomplete", @@ -1787,7 +1781,6 @@ "constant": false, "designable": true, "final": false, - "isQProperty": false, "name": "blah", "read": "blah", "required": false, @@ -1840,7 +1833,6 @@ "constant": false, "designable": true, "final": false, - "isQProperty": false, "name": "blah", "read": "blah", "required": false, @@ -2016,7 +2008,6 @@ "constant": false, "designable": true, "final": false, - "isQProperty": false, "name": "gadgetPoperty", "read": "gadgetPoperty", "required": false, @@ -2029,7 +2020,6 @@ "constant": false, "designable": true, "final": false, - "isQProperty": false, "name": "objectPoperty", "read": "objectPoperty", "required": false, @@ -2055,7 +2045,6 @@ "constant": false, "designable": true, "final": false, - "isQProperty": false, "name": "nestedGadgetPoperty", "read": "nestedGadgetPoperty", "required": false, @@ -2081,7 +2070,6 @@ "constant": false, "designable": true, "final": false, - "isQProperty": false, "name": "nestedObjectPoperty", "read": "nestedObjectPoperty", "required": false, @@ -2215,7 +2203,6 @@ "constant": false, "designable": true, "final": false, - "isQProperty": false, "name": "gadgetPoperty", "read": "gadgetPoperty", "required": false, @@ -2228,7 +2215,6 @@ "constant": false, "designable": true, "final": false, - "isQProperty": false, "name": "objectPoperty", "read": "objectPoperty", "required": false, @@ -2254,7 +2240,6 @@ "constant": false, "designable": true, "final": false, - "isQProperty": false, "name": "nestedGadgetPoperty", "read": "nestedGadgetPoperty", "required": false, @@ -2280,7 +2265,6 @@ "constant": false, "designable": true, "final": false, - "isQProperty": false, "name": "nestedObjectPoperty", "read": "nestedObjectPoperty", "required": false, diff --git a/tests/auto/tools/moc/tst_moc.cpp b/tests/auto/tools/moc/tst_moc.cpp index e17e2283fa..8d405d997e 100644 --- a/tests/auto/tools/moc/tst_moc.cpp +++ b/tests/auto/tools/moc/tst_moc.cpp @@ -4071,15 +4071,16 @@ void tst_Moc::requiredProperties() class ClassWithQPropertyMembers : public QObject { Q_OBJECT - Q_PROPERTY(int publicProperty NOTIFY publicPropertyChanged) - Q_PROPERTY(int privateExposedProperty) + Q_PROPERTY(int publicProperty MEMBER publicProperty BINDABLE bindablePublicProperty NOTIFY publicPropertyChanged) + Q_PROPERTY(int privateExposedProperty MEMBER privateExposedProperty) public: signals: void publicPropertyChanged(); public: -// QNotifiedProperty publicProperty; + QBindable bindablePublicProperty() { return QBindable(&publicProperty); } + Q_OBJECT_BINDABLE_PROPERTY(ClassWithQPropertyMembers, int, publicProperty, &ClassWithQPropertyMembers::publicPropertyChanged); QProperty notExposed; @@ -4093,188 +4094,163 @@ private: void tst_Moc::qpropertyMembers() { -// const auto metaObject = &ClassWithQPropertyMembers::staticMetaObject; + const auto metaObject = &ClassWithQPropertyMembers::staticMetaObject; -// QCOMPARE(metaObject->propertyCount() - metaObject->superClass()->propertyCount(), 2); + QCOMPARE(metaObject->propertyCount() - metaObject->superClass()->propertyCount(), 2); -// QCOMPARE(metaObject->indexOfProperty("notExposed"), -1); + QCOMPARE(metaObject->indexOfProperty("notExposed"), -1); -// QMetaProperty prop = metaObject->property(metaObject->indexOfProperty("publicProperty")); -// QVERIFY(prop.isValid()); + QMetaProperty prop = metaObject->property(metaObject->indexOfProperty("publicProperty")); + QVERIFY(prop.isValid()); -// QVERIFY(metaObject->property(metaObject->indexOfProperty("privateExposedProperty")).isValid()); + QVERIFY(metaObject->property(metaObject->indexOfProperty("privateExposedProperty")).isValid()); -// ClassWithQPropertyMembers instance; + ClassWithQPropertyMembers instance; -// prop.write(&instance, 42); -// QCOMPARE(instance.publicProperty.value(), 42); + prop.write(&instance, 42); + QCOMPARE(instance.publicProperty.value(), 42); -// QSignalSpy publicPropertySpy(&instance, SIGNAL(publicPropertyChanged())); + QSignalSpy publicPropertySpy(&instance, SIGNAL(publicPropertyChanged())); -// instance.publicProperty.setValue(&instance, 100); -// QCOMPARE(prop.read(&instance).toInt(), 100); -// QCOMPARE(publicPropertySpy.count(), 1); + instance.publicProperty.setValue(100); + QCOMPARE(prop.read(&instance).toInt(), 100); + QCOMPARE(publicPropertySpy.count(), 1); -// QCOMPARE(prop.metaType(), QMetaType(QMetaType::Int)); + QCOMPARE(prop.metaType(), QMetaType(QMetaType::Int)); -// QVERIFY(prop.notifySignal().isValid()); + QVERIFY(prop.notifySignal().isValid()); } void tst_Moc::observerMetaCall() { -// const auto metaObject = &ClassWithQPropertyMembers::staticMetaObject; -// QMetaProperty prop = metaObject->property(metaObject->indexOfProperty("publicProperty")); -// QVERIFY(prop.isValid()); + const auto metaObject = &ClassWithQPropertyMembers::staticMetaObject; + QMetaProperty prop = metaObject->property(metaObject->indexOfProperty("publicProperty")); + QVERIFY(prop.isValid()); -// ClassWithQPropertyMembers instance; + ClassWithQPropertyMembers instance; -// int observerCallCount = 0; + int observerCallCount = 0; -// auto handler = QPropertyChangeHandler([&observerCallCount]() { -// ++observerCallCount; -// }); + auto observer = [&observerCallCount]() { + ++observerCallCount; + }; + + auto bindable = prop.bindable(&instance); + QVERIFY(bindable.isBindable()); -// { -// void *argv[] = { &handler }; -// instance.qt_metacall(QMetaObject::RegisterQPropertyObserver, prop.propertyIndex(), argv); -// } + auto handler = bindable.onValueChanged(observer); -// QCOMPARE(observerCallCount, 0); -// instance.publicProperty.setValue(100); -// QCOMPARE(observerCallCount, 1); -// instance.publicProperty.setValue(&instance, 101); -// QCOMPARE(observerCallCount, 2); + QCOMPARE(observerCallCount, 0); + instance.publicProperty.setValue(100); + QCOMPARE(observerCallCount, 1); + instance.publicProperty.setValue(101); + QCOMPARE(observerCallCount, 2); } void tst_Moc::setQPRopertyBinding() { -// const auto metaObject = &ClassWithQPropertyMembers::staticMetaObject; -// QMetaProperty prop = metaObject->property(metaObject->indexOfProperty("publicProperty")); -// QVERIFY(prop.isValid()); + const auto metaObject = &ClassWithQPropertyMembers::staticMetaObject; + QMetaProperty prop = metaObject->property(metaObject->indexOfProperty("publicProperty")); + QVERIFY(prop.isValid()); -// ClassWithQPropertyMembers instance; + ClassWithQPropertyMembers instance; -// bool bindingCalled = false; -// auto binding = Qt::makePropertyBinding([&bindingCalled]() { -// bindingCalled = true; -// return 42; -// }); + bool bindingCalled = false; + auto binding = Qt::makePropertyBinding([&bindingCalled]() { + bindingCalled = true; + return 42; + }); -// { -// void *argv[] = { &binding }; -// instance.qt_metacall(QMetaObject::SetQPropertyBinding, prop.propertyIndex(), argv); -// } + auto bindable = prop.bindable(&instance); + QVERIFY(bindable.isBindable()); + bindable.setBinding(binding); -// QCOMPARE(instance.publicProperty.value(), 42); -// QVERIFY(bindingCalled); // but now it should've been called :) + QCOMPARE(instance.publicProperty.value(), 42); + QVERIFY(bindingCalled); // but now it should've been called :) } -#if 0 class ClassWithPrivateQPropertyShim :public QObject { Q_OBJECT public: - Q_PRIVATE_QPROPERTY(d_func(), int, testProperty, setTestProperty, NOTIFY testPropertyChanged) - Q_PRIVATE_QPROPERTY(d_func(), int, testProperty2, setTestProperty2, NOTIFY false) - Q_PRIVATE_QPROPERTY(d_func(), int, lazyTestProperty, setLazyTestProperty, - NOTIFY lazyTestPropertyChanged STORED false) - - Q_PRIVATE_QPROPERTIES_BEGIN - Q_PRIVATE_QPROPERTY_IMPL(testProperty) - Q_PRIVATE_QPROPERTY_IMPL(testProperty2) - Q_PRIVATE_QPROPERTY_IMPL(lazyTestProperty) - Q_PRIVATE_QPROPERTIES_END + Q_PROPERTY(int testProperty READ testProperty WRITE setTestProperty BINDABLE bindableTestProperty NOTIFY testPropertyChanged) + Q_PROPERTY(int testProperty2 READ testProperty2 WRITE setTestProperty2 BINDABLE bindableTestProperty2) + //Q_PROPERTY(d_func(), int, lazyTestProperty, setLazyTestProperty, NOTIFY lazyTestPropertyChanged) signals: void testPropertyChanged(); void lazyTestPropertyChanged(); public: + int testProperty() const { return priv.testProperty; } + void setTestProperty(int val) { priv.testProperty = val; } + int testProperty2() const { return priv.testProperty2; } + void setTestProperty2(int val) { priv.testProperty2 = val; } + + QBindable bindableTestProperty() { return QBindable(&priv.testProperty); } + QBindable bindableTestProperty2() { return QBindable(&priv.testProperty2); } + struct Private { Private(ClassWithPrivateQPropertyShim *pub) : q(pub) {} + QBindingStorage bindingStorage; + ClassWithPrivateQPropertyShim *q = nullptr; void onTestPropertyChanged() { q->testPropertyChanged(); } - QNotifiedProperty testProperty; + Q_OBJECT_BINDABLE_PROPERTY(Private, int, testProperty, &Private::onTestPropertyChanged); QProperty testProperty2; - - void onLazyTestPropertyChanged() { q->lazyTestPropertyChanged(); } - - const QNotifiedProperty *lazyTestProperty() const { - // Mind that this prevents the property read from being recorded. - // For real-world use cases some more logic is necessary here. - return lazyTestPropertyStorage.data(); - } - - QNotifiedProperty *lazyTestProperty() { - if (!lazyTestPropertyStorage) - lazyTestPropertyStorage.reset(new QNotifiedProperty); - return lazyTestPropertyStorage.data(); - } - - QScopedPointer> lazyTestPropertyStorage; }; Private priv{this}; Private *d_func() { return &priv; } const Private *d_func() const { return &priv; } }; -#endif + +inline const QBindingStorage *qGetBindingStorage(const ClassWithPrivateQPropertyShim::Private *o) +{ + return &o->bindingStorage; +} +inline QBindingStorage *qGetBindingStorage(ClassWithPrivateQPropertyShim::Private *o) +{ + return &o->bindingStorage; +} void tst_Moc::privateQPropertyShim() { -// ClassWithPrivateQPropertyShim testObject; - -// { -// auto metaObject = &ClassWithPrivateQPropertyShim::staticMetaObject; -// QMetaProperty prop = metaObject->property(metaObject->indexOfProperty("testProperty")); -// QVERIFY(prop.isValid()); -// QVERIFY(prop.notifySignal().isValid()); -// } - -// testObject.priv.testProperty.setValue(&testObject.priv, 42); -// QCOMPARE(testObject.property("testProperty").toInt(), 42); - -// // Behave like a QProperty -// QVERIFY(!testObject.testProperty.hasBinding()); -// testObject.testProperty.setBinding([]() { return 100; }); -// QCOMPARE(testObject.testProperty.value(), 100); -// QVERIFY(testObject.testProperty.hasBinding()); - -// // Old style setter getters -// testObject.setTestProperty(400); -// QVERIFY(!testObject.testProperty.hasBinding()); -// QCOMPARE(testObject.testProperty(), 400); - -// // Created and default-initialized, without nullptr access -// QCOMPARE(testObject.lazyTestProperty(), 0); - -// // Explicitly set to something -// testObject.priv.lazyTestProperty()->setValue(&testObject.priv, 42); -// QCOMPARE(testObject.property("lazyTestProperty").toInt(), 42); - -// // Behave like a QProperty -// QVERIFY(!testObject.lazyTestProperty.hasBinding()); -// testObject.lazyTestProperty.setBinding([]() { return 100; }); -// QCOMPARE(testObject.lazyTestProperty.value(), 100); -// QVERIFY(testObject.lazyTestProperty.hasBinding()); - -// // Old style setter getters -// testObject.setLazyTestProperty(400); -// QVERIFY(!testObject.lazyTestProperty.hasBinding()); -// QCOMPARE(testObject.lazyTestProperty(), 400); - -// // mo generates correct code for plain QProperty in PIMPL -// testObject.testProperty2.setValue(42); -// QCOMPARE(testObject.testProperty2.value(), 42); + ClassWithPrivateQPropertyShim testObject; + + { + auto metaObject = &ClassWithPrivateQPropertyShim::staticMetaObject; + QMetaProperty prop = metaObject->property(metaObject->indexOfProperty("testProperty")); + QVERIFY(prop.isValid()); + QVERIFY(prop.notifySignal().isValid()); + } + + testObject.priv.testProperty.setValue(42); + QCOMPARE(testObject.property("testProperty").toInt(), 42); + + // Behave like a QProperty + QVERIFY(!testObject.bindableTestProperty().hasBinding()); + testObject.bindableTestProperty().setBinding([]() { return 100; }); + QCOMPARE(testObject.testProperty(), 100); + QVERIFY(testObject.bindableTestProperty().hasBinding()); + + // Old style setter getters + testObject.setTestProperty(400); + QVERIFY(!testObject.bindableTestProperty().hasBinding()); + QCOMPARE(testObject.testProperty(), 400); + + // moc generates correct code for plain QProperty in PIMPL + testObject.setTestProperty2(42); + QCOMPARE(testObject.priv.testProperty2.value(), 42); } QTEST_MAIN(tst_Moc) -- cgit v1.2.3