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 <a.samirh78@gmail.com>
(cherry picked from commit 7bd47fb70881e7240c027cd2844866c99f8f096d)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit ffb442db62888db5b5cdf82c03655c09e58373b5)
This commit is contained in:
Marc Mutz 2024-05-13 12:52:09 +02:00 committed by Qt Cherry-pick Bot
parent fefaf8227c
commit 7d42408e40

View File

@ -22,11 +22,38 @@
#include <private/qglobal_p.h>
#include <cstdio>
#include <memory>
#include <optional>
QT_BEGIN_NAMESPACE
namespace QTestPrivate {
#ifdef Q_OS_VXWORKS
template <typename T>
class OptionalWrapper : private std::unique_ptr<T>
{
using Base = std::unique_ptr<T>;
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 <typename...Args>
T &emplace(Args&&...args)
{ as_base() = std::make_unique<T>(std::forward<Args>(args)...); return **this; }
};
#else
template <typename T>
using OptionalWrapper = std::optional<T>;
#endif // Q_OS_VXWORKS
/*!
\internal
@ -128,9 +155,9 @@ void testReadWritePropertyBasics(
QVERIFY2(metaProperty.isBindable() && metaProperty.isWritable(),
"Preconditions not met for " + QByteArray(propertyName));
QScopedPointer<QSignalSpy> spy(nullptr);
QTestPrivate::OptionalWrapper<QSignalSpy> 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<QSignalSpy> spy(nullptr);
QTestPrivate::OptionalWrapper<QSignalSpy> 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<PropertyType>(), prior, comparator,
@ -455,9 +482,9 @@ void testReadOnlyPropertyBasics(
QUntypedBindable bindable = metaProperty.bindable(&instance);
QScopedPointer<QSignalSpy> spy(nullptr);
QTestPrivate::OptionalWrapper<QSignalSpy> 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<PropertyType>(), initial, comparator,