From 7d42408e404619465e93da76d9d605f46f48dd9b Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 13 May 2024 12:52:09 +0200 Subject: [PATCH] qpropertytesthelper_p.h: port from QScopedPointer to std::optional It's more efficient (doesn't need to allocate the QSignalSpies on the heap) and makes the header fit for the upcoming QT_NO_SCOPED_POINTER. VxWorks seems to have a problem with its std::optional implementation, though: QWARN : tst_QAbstractAnimation::stateBinding() Trying to construct an instance of an invalid type, type id: -2125884168 QWARN : tst_QAbstractAnimation::stateBinding() Trying to construct an instance of an invalid type, type id: -2125884172 QWARN : tst_QAbstractAnimation::stateBinding() Trying to construct an instance of an invalid type, type id: -2125884024 Received signal 11 (SIGSEGV), code 2, for address 0x0000000d So write a small optional-like wrapper around std::unique_ptr for that platform. Amends 930e59b798d9e3d08e17440980d33a08fb411cbe. Pick-to: 6.5 Task-number: QTBUG-132213 Change-Id: Icf85678d616bc96c7d74b982d0b919ea3f13265a Reviewed-by: Ahmad Samir (cherry picked from commit 7bd47fb70881e7240c027cd2844866c99f8f096d) Reviewed-by: Qt Cherry-pick Bot (cherry picked from commit ffb442db62888db5b5cdf82c03655c09e58373b5) --- src/testlib/qpropertytesthelper_p.h | 39 ++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/testlib/qpropertytesthelper_p.h b/src/testlib/qpropertytesthelper_p.h index 54398278997..6bdd1feda12 100644 --- a/src/testlib/qpropertytesthelper_p.h +++ b/src/testlib/qpropertytesthelper_p.h @@ -22,11 +22,38 @@ #include #include +#include +#include QT_BEGIN_NAMESPACE namespace QTestPrivate { +#ifdef Q_OS_VXWORKS +template +class OptionalWrapper : private std::unique_ptr +{ + using Base = std::unique_ptr; + + Base &as_base() { return *this; } + const Base &as_base() const { return *this; } +public: + Q_IMPLICIT OptionalWrapper(std::nullopt_t) : Base() {} + + using Base::operator->; + using Base::operator*; + using Base::operator bool; + + template + T &emplace(Args&&...args) + { as_base() = std::make_unique(std::forward(args)...); return **this; } +}; +#else +template +using OptionalWrapper = std::optional; +#endif // Q_OS_VXWORKS + + /*! \internal @@ -128,9 +155,9 @@ void testReadWritePropertyBasics( QVERIFY2(metaProperty.isBindable() && metaProperty.isWritable(), "Preconditions not met for " + QByteArray(propertyName)); - QScopedPointer spy(nullptr); + QTestPrivate::OptionalWrapper spy = std::nullopt; if (metaProperty.hasNotifySignal()) - spy.reset(new QSignalSpy(&instance, metaProperty.notifySignal())); + spy.emplace(&instance, metaProperty.notifySignal()); testedObj.setProperty(propertyName, QVariant::fromValue(initial)); QPROPERTY_TEST_COMPARISON_HELPER( @@ -321,9 +348,9 @@ void testWriteOncePropertyBasics( QUntypedBindable bindable = metaProperty.bindable(&instance); - QScopedPointer spy(nullptr); + QTestPrivate::OptionalWrapper spy = std::nullopt; if (metaProperty.hasNotifySignal()) - spy.reset(new QSignalSpy(&instance, metaProperty.notifySignal())); + spy.emplace(&instance, metaProperty.notifySignal()); QPROPERTY_TEST_COMPARISON_HELPER( testedObj.property(propertyName).template value(), prior, comparator, @@ -455,9 +482,9 @@ void testReadOnlyPropertyBasics( QUntypedBindable bindable = metaProperty.bindable(&instance); - QScopedPointer spy(nullptr); + QTestPrivate::OptionalWrapper spy = std::nullopt; if (metaProperty.hasNotifySignal()) - spy.reset(new QSignalSpy(&instance, metaProperty.notifySignal())); + spy.emplace(&instance, metaProperty.notifySignal()); QPROPERTY_TEST_COMPARISON_HELPER( testedObj.property(propertyName).template value(), initial, comparator,