From 668efc29fd85bbae2395a4eca8d0ad71ad6ee3d1 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Sat, 9 Jun 2012 22:57:35 +0200 Subject: Add some internal API for extracting a QSharedPointer from QVariant. The T must be derived from QObject, or it will fail to compile. This will allow scripting or other 'wrapping' and runtime environments like QtDeclarative to handle QSharedPointers to types derived from QObject properly. A QSharedPointer can be inserted into a QVariant, and where T derives from QObject, a QSharedPointer can be extracted from the QVariant, and its properties are then accessible. Change-Id: I68d6d89aceceb019267bd7301baa2047f9c09b90 Reviewed-by: Thiago Macieira --- .../tools/qsharedpointer/tst_qsharedpointer.cpp | 71 ++++++++++++++++++++++ 1 file changed, 71 insertions(+) (limited to 'tests/auto/corelib/tools') diff --git a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp index 70caad856b..6896b328d0 100644 --- a/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp +++ b/tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp @@ -105,6 +105,8 @@ private slots: void invalidConstructs_data(); void invalidConstructs(); + void qvariantCast(); + public slots: void cleanup() { safetyCheck(); } @@ -1853,6 +1855,75 @@ void tst_QSharedPointer::invalidConstructs() } } +void tst_QSharedPointer::qvariantCast() +{ + QSharedPointer sp = QSharedPointer::create(); + sp->setObjectName("A test name"); + QVariant v = QVariant::fromValue(sp); + + { + QSharedPointer other = qSharedPointerFromVariant(v); + QCOMPARE(other->objectName(), QString::fromLatin1("A test name")); + } + { + QSharedPointer other = qSharedPointerFromVariant(v); + QCOMPARE(other->objectName(), QString::fromLatin1("A test name")); + } + { + QSharedPointer other = qSharedPointerFromVariant(v); + QCOMPARE(other->objectName(), QString::fromLatin1("A test name")); + } + { + QSharedPointer other = qSharedPointerFromVariant(v); + QVERIFY(!other); + } + // Intentionally does not compile. +// QSharedPointer sop = qSharedPointerFromVariant(v); + + v = QVariant::fromValue(sp.toWeakRef()); + + { + QWeakPointer other = qWeakPointerFromVariant(v); + QCOMPARE(other.data()->objectName(), QString::fromLatin1("A test name")); + } + { + QWeakPointer other = qWeakPointerFromVariant(v); + QCOMPARE(other.data()->objectName(), QString::fromLatin1("A test name")); + } + { + QWeakPointer other = qWeakPointerFromVariant(v); + QCOMPARE(other.data()->objectName(), QString::fromLatin1("A test name")); + } + { + QWeakPointer other = qWeakPointerFromVariant(v); + QVERIFY(!other); + } + + // Intentionally does not compile. +// QWeakPointer sop = qWeakPointerFromVariant(v); + + QWeakPointer tracking = new QFile; + tracking.data()->setObjectName("A test name"); + v = QVariant::fromValue(tracking); + + { + QWeakPointer other = qWeakPointerFromVariant(v); + QCOMPARE(other.data()->objectName(), QString::fromLatin1("A test name")); + } + { + QWeakPointer other = qWeakPointerFromVariant(v); + QCOMPARE(other.data()->objectName(), QString::fromLatin1("A test name")); + } + { + QWeakPointer other = qWeakPointerFromVariant(v); + QCOMPARE(other.data()->objectName(), QString::fromLatin1("A test name")); + } + { + QWeakPointer other = qWeakPointerFromVariant(v); + QVERIFY(!other); + } +} + namespace ReentrancyWhileDestructing { struct IB { -- cgit v1.2.3