diff options
author | Fabian Kosmale <fabian.kosmale@qt.io> | 2020-07-15 14:12:58 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-09-02 22:44:27 +0200 |
commit | 733d890430542e907b9014a2cf73d63edf931245 (patch) | |
tree | 470dd4321b748c0de57b182617950321b7f22731 /tests | |
parent | 52bbb19fa4dc7647a0cad35a69dcf09437386080 (diff) |
Add operator-> and operator*() to QProperty
Enable the arrow operator for all types that could have members, so
that one can e.g. write myStringProperty->size() instead of having to
use the less convenient myStringProperty.value().size().
Also cleaned up the rvalue ref overloads to be
disabled for basic types. For those we now also
return by value, for more complex types we
return a const reference.
Change-Id: If6a75898dc0a097f57052488f0af0cd7166b3393
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp index 74a81cc93b..20565080ff 100644 --- a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp +++ b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp @@ -69,6 +69,7 @@ private slots: void setBindingFunctor(); void multipleObservers(); void propertyAlias(); + void arrowAndStarOperator(); void notifiedProperty(); void notifiedPropertyWithOldValueCallback(); void notifiedPropertyWithGuard(); @@ -753,6 +754,34 @@ void tst_QProperty::propertyAlias() QCOMPARE(value2, 22); } +void tst_QProperty::arrowAndStarOperator() +{ + QString str("Hello"); + QProperty<QString *> prop(&str); + + QCOMPARE(prop->size(), str.size()); + QCOMPARE(**prop, str); + + struct Dereferenceable { + QString x; + QString *operator->() { return &x; } + const QString *operator->() const { return &x; } + }; + static_assert(QTypeTraits::is_dereferenceable_v<Dereferenceable>); + + QProperty<Dereferenceable> prop2(Dereferenceable{str}); + QCOMPARE(prop2->size(), str.size()); + QCOMPARE(**prop, str); + + QObject *object = new QObject; + object->setObjectName("Hello"); + QProperty<QSharedPointer<QObject>> prop3(QSharedPointer<QObject>{object}); + + QCOMPARE(prop3->objectName(), str); + QCOMPARE(*prop3, object); + +} + struct ClassWithNotifiedProperty { QList<int> recordedValues; |