From 311f8896322bcd39d33369c8311a8c89ccdad449 Mon Sep 17 00:00:00 2001 From: Po-Hao Su Date: Thu, 9 Nov 2023 20:32:55 +0800 Subject: [PATCH] QProperty: clean up unnecessary sentinel class Instead of introducing the nested class InheritsQUntypedPropertyData as a sentinel class for inheritance check, we can use BinaryTypeTrait to handle the check. By doing this, we no longer need to maintain the nested class. Change-Id: Ie3aae976015d5fae6b6d072cad6ee52cd30b769d Reviewed-by: Fabian Kosmale --- src/corelib/kernel/qproperty.h | 12 +++-- src/corelib/kernel/qpropertyprivate.h | 7 ++- .../kernel/qproperty/tst_qproperty.cpp | 54 +++++++++++++++++++ 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/src/corelib/kernel/qproperty.h b/src/corelib/kernel/qproperty.h index 32d7a445067..54df12fbfea 100644 --- a/src/corelib/kernel/qproperty.h +++ b/src/corelib/kernel/qproperty.h @@ -263,7 +263,8 @@ public: QPropertyObserver &operator=(QPropertyObserver &&other) noexcept; ~QPropertyObserver(); - template + template >> void setSource(const Property &property) { setSource(property.bindingData()); } void setSource(const QtPrivate::QPropertyBindingData &property); @@ -302,7 +303,8 @@ public: { } - template + template >> Q_NODISCARD_CTOR QPropertyChangeHandler(const Property &property, Functor handler) : QPropertyObserver([](QPropertyObserver *self, QUntypedPropertyData *) { @@ -332,7 +334,8 @@ public: { } - template + template >> Q_NODISCARD_CTOR QPropertyNotifier(const Property &property, Functor handler) : QPropertyObserver([](QPropertyObserver *self, QUntypedPropertyData *) { @@ -906,7 +909,8 @@ public: iface->setObserver(aliasedProperty(), this); } - template + template >> QPropertyAlias(Property *property) : QPropertyObserver(property), iface(&QtPrivate::QBindableInterfaceForProperty::iface) diff --git a/src/corelib/kernel/qpropertyprivate.h b/src/corelib/kernel/qpropertyprivate.h index aca6d14c968..fa8fe04d5d0 100644 --- a/src/corelib/kernel/qpropertyprivate.h +++ b/src/corelib/kernel/qpropertyprivate.h @@ -125,8 +125,13 @@ struct QPropertyObserverPointer; class QUntypedPropertyData { public: +#if QT_DEPRECATED_SINCE(6, 8) // sentinel to check whether a class inherits QUntypedPropertyData - struct InheritsQUntypedPropertyData {}; + struct QT_DEPRECATED_VERSION_X_6_8("Use std::is_base_of instead.") + InheritsQUntypedPropertyData + { + }; +#endif }; template diff --git a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp index 28d9a6eea44..abbe05b27c7 100644 --- a/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp +++ b/tests/auto/corelib/kernel/qproperty/tst_qproperty.cpp @@ -29,6 +29,7 @@ class tst_QProperty : public QObject { Q_OBJECT private slots: + void inheritQUntypedPropertyData(); void functorBinding(); void basicDependencies(); void multipleDependencies(); @@ -109,6 +110,59 @@ private slots: void propertyUpdateViaSignaledProperty(); }; +namespace { +template +constexpr auto isDerivedFromQUntypedPropertyData = std::is_base_of_v; + +template +constexpr auto isDerivedFromQUntypedPropertyDataFunc(const Property &property) +{ + Q_UNUSED(property); + return isDerivedFromQUntypedPropertyData; +} + +template +constexpr auto isDerivedFromQUntypedPropertyDataFunc(Property *property) +{ + Q_UNUSED(property); + return isDerivedFromQUntypedPropertyData; +} +} // namespace + +void tst_QProperty::inheritQUntypedPropertyData() +{ + class propertyPublic : public QUntypedPropertyData + { + }; + class propertyPrivate : private QUntypedPropertyData + { + }; + + // Compile time test + static_assert(isDerivedFromQUntypedPropertyData); + static_assert(isDerivedFromQUntypedPropertyData); + static_assert(isDerivedFromQUntypedPropertyData>); + static_assert(isDerivedFromQUntypedPropertyData>); + + // Run time test + propertyPublic _propertyPublic; + propertyPrivate _propertyPrivate; + QPropertyData qpropertyData; + QProperty qproperty; + std::unique_ptr _propertyPublicPtr{ new propertyPublic }; + std::unique_ptr _propertyPrivatePtr{ new propertyPrivate }; + std::unique_ptr> qpropertyDataPtr{ new QPropertyData }; + std::unique_ptr> qpropertyPtr{ new QProperty }; + QVERIFY(isDerivedFromQUntypedPropertyDataFunc(_propertyPublic)); + QVERIFY(isDerivedFromQUntypedPropertyDataFunc(_propertyPrivate)); + QVERIFY(isDerivedFromQUntypedPropertyDataFunc(qpropertyData)); + QVERIFY(isDerivedFromQUntypedPropertyDataFunc(qproperty)); + QVERIFY(isDerivedFromQUntypedPropertyDataFunc(_propertyPublicPtr.get())); + QVERIFY(isDerivedFromQUntypedPropertyDataFunc(_propertyPrivatePtr.get())); + QVERIFY(isDerivedFromQUntypedPropertyDataFunc(qpropertyDataPtr.get())); + QVERIFY(isDerivedFromQUntypedPropertyDataFunc(qpropertyPtr.get())); +} + void tst_QProperty::functorBinding() { QProperty property([]() { return 42; });