From 733d890430542e907b9014a2cf73d63edf931245 Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Wed, 15 Jul 2020 14:12:58 +0200 Subject: [PATCH] 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 --- src/corelib/global/qtypeinfo.h | 10 +++++ src/corelib/kernel/qproperty.h | 42 +++++++++++++------ src/corelib/kernel/qpropertyprivate.h | 2 +- .../kernel/qproperty/tst_qproperty.cpp | 29 +++++++++++++ 4 files changed, 70 insertions(+), 13 deletions(-) diff --git a/src/corelib/global/qtypeinfo.h b/src/corelib/global/qtypeinfo.h index 30db5c4487..c0617a41f2 100644 --- a/src/corelib/global/qtypeinfo.h +++ b/src/corelib/global/qtypeinfo.h @@ -324,6 +324,16 @@ struct expand_operator_less_than_tuple> : expand_operator_les } +template +struct is_dereferenceable : std::false_type {}; + +template +struct is_dereferenceable().operator->())> > + : std::true_type {}; + +template +constexpr bool is_dereferenceable_v = is_dereferenceable::value; + template struct has_operator_equal : detail::expand_operator_equal {}; template diff --git a/src/corelib/kernel/qproperty.h b/src/corelib/kernel/qproperty.h index acaf74f9af..ff61596fb6 100644 --- a/src/corelib/kernel/qproperty.h +++ b/src/corelib/kernel/qproperty.h @@ -192,8 +192,15 @@ struct QPropertyBasePointer; template class QProperty { + class DisableRValueRefs {}; + static constexpr bool UseReferences = !(std::is_arithmetic_v || std::is_enum_v || std::is_pointer_v); + public: using value_type = T; + using parameter_type = std::conditional_t; + using rvalue_ref = typename std::conditional_t; + using arrow_operator_result = std::conditional_t, const T &, + std::conditional_t, const T &, void>>; QProperty() = default; explicit QProperty(const T &initialValue) : d(initialValue) {} @@ -218,7 +225,7 @@ public: #endif ~QProperty() = default; - T value() const + parameter_type value() const { if (d.priv.hasBinding()) d.priv.evaluateIfDirty(); @@ -226,32 +233,49 @@ public: return d.getValue(); } - operator T() const + arrow_operator_result operator->() const + { + if constexpr (QTypeTraits::is_dereferenceable_v) { + return value(); + } else if constexpr (std::is_pointer_v) { + value(); + return this->val; + } else { + return; + } + } + + parameter_type operator*() const { return value(); } - void setValue(T &&newValue) + operator parameter_type() const + { + return value(); + } + + void setValue(rvalue_ref newValue) { d.priv.removeBinding(); if (d.setValueAndReturnTrueIfChanged(std::move(newValue))) notify(); } - void setValue(const T &newValue) + void setValue(parameter_type newValue) { d.priv.removeBinding(); if (d.setValueAndReturnTrueIfChanged(newValue)) notify(); } - QProperty &operator=(T &&newValue) + QProperty &operator=(rvalue_ref newValue) { setValue(std::move(newValue)); return *this; } - QProperty &operator=(const T &newValue) + QProperty &operator=(parameter_type newValue) { setValue(newValue); return *this; @@ -263,12 +287,6 @@ public: return *this; } - QProperty &operator=(QPropertyBinding &&newBinding) - { - setBinding(std::move(newBinding)); - return *this; - } - QPropertyBinding setBinding(const QPropertyBinding &newBinding) { QPropertyBinding oldBinding(d.priv.setBinding(newBinding, &d)); diff --git a/src/corelib/kernel/qpropertyprivate.h b/src/corelib/kernel/qpropertyprivate.h index d5ee9f29c2..4d8a457e32 100644 --- a/src/corelib/kernel/qpropertyprivate.h +++ b/src/corelib/kernel/qpropertyprivate.h @@ -136,7 +136,7 @@ public: QPropertyValueStorage(QPropertyValueStorage &&other) : value(std::move(other.value)), priv(std::move(other.priv), this) {} QPropertyValueStorage &operator=(QPropertyValueStorage &&other) { value = std::move(other.value); priv.moveAssign(std::move(other.priv), &value); return *this; } - T getValue() const { return value; } + T const& getValue() const { return value; } bool setValueAndReturnTrueIfChanged(T &&v) { if constexpr (QTypeTraits::has_operator_equal_v) { 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 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); + + QProperty prop2(Dereferenceable{str}); + QCOMPARE(prop2->size(), str.size()); + QCOMPARE(**prop, str); + + QObject *object = new QObject; + object->setObjectName("Hello"); + QProperty> prop3(QSharedPointer{object}); + + QCOMPARE(prop3->objectName(), str); + QCOMPARE(*prop3, object); + +} + struct ClassWithNotifiedProperty { QList recordedValues;