diff options
author | Stephen Kelly <stephen.kelly@kdab.com> | 2012-06-09 22:57:35 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-06-20 15:45:07 +0200 |
commit | 668efc29fd85bbae2395a4eca8d0ad71ad6ee3d1 (patch) | |
tree | ffccf50b396428156cafb3c6a51ddb87d2359f28 /tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp | |
parent | 534bcc96676ac02fc327f9e43d3785c7424b0750 (diff) |
Add some internal API for extracting a QSharedPointer<T> 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<T> can be inserted into a QVariant, and
where T derives from QObject, a QSharedPointer<QObject> can be
extracted from the QVariant, and its properties are then accessible.
Change-Id: I68d6d89aceceb019267bd7301baa2047f9c09b90
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp')
-rw-r--r-- | tests/auto/corelib/tools/qsharedpointer/tst_qsharedpointer.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
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<QFile> sp = QSharedPointer<QFile>::create(); + sp->setObjectName("A test name"); + QVariant v = QVariant::fromValue(sp); + + { + QSharedPointer<QObject> other = qSharedPointerFromVariant<QObject>(v); + QCOMPARE(other->objectName(), QString::fromLatin1("A test name")); + } + { + QSharedPointer<QIODevice> other = qSharedPointerFromVariant<QIODevice>(v); + QCOMPARE(other->objectName(), QString::fromLatin1("A test name")); + } + { + QSharedPointer<QFile> other = qSharedPointerFromVariant<QFile>(v); + QCOMPARE(other->objectName(), QString::fromLatin1("A test name")); + } + { + QSharedPointer<QThread> other = qSharedPointerFromVariant<QThread>(v); + QVERIFY(!other); + } + // Intentionally does not compile. +// QSharedPointer<int> sop = qSharedPointerFromVariant<int>(v); + + v = QVariant::fromValue(sp.toWeakRef()); + + { + QWeakPointer<QObject> other = qWeakPointerFromVariant<QObject>(v); + QCOMPARE(other.data()->objectName(), QString::fromLatin1("A test name")); + } + { + QWeakPointer<QIODevice> other = qWeakPointerFromVariant<QIODevice>(v); + QCOMPARE(other.data()->objectName(), QString::fromLatin1("A test name")); + } + { + QWeakPointer<QFile> other = qWeakPointerFromVariant<QFile>(v); + QCOMPARE(other.data()->objectName(), QString::fromLatin1("A test name")); + } + { + QWeakPointer<QThread> other = qWeakPointerFromVariant<QThread>(v); + QVERIFY(!other); + } + + // Intentionally does not compile. +// QWeakPointer<int> sop = qWeakPointerFromVariant<int>(v); + + QWeakPointer<QFile> tracking = new QFile; + tracking.data()->setObjectName("A test name"); + v = QVariant::fromValue(tracking); + + { + QWeakPointer<QObject> other = qWeakPointerFromVariant<QObject>(v); + QCOMPARE(other.data()->objectName(), QString::fromLatin1("A test name")); + } + { + QWeakPointer<QIODevice> other = qWeakPointerFromVariant<QIODevice>(v); + QCOMPARE(other.data()->objectName(), QString::fromLatin1("A test name")); + } + { + QWeakPointer<QFile> other = qWeakPointerFromVariant<QFile>(v); + QCOMPARE(other.data()->objectName(), QString::fromLatin1("A test name")); + } + { + QWeakPointer<QThread> other = qWeakPointerFromVariant<QThread>(v); + QVERIFY(!other); + } +} + namespace ReentrancyWhileDestructing { struct IB { |