diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2013-07-27 00:58:27 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-07-28 23:09:10 +0200 |
commit | 5b9006bbdba7dcab01b8e640554a7d7a4b64f76b (patch) | |
tree | 9fac766b361f86514184382ac05e13be95291f5f /tests/auto/corelib/tools/qscopedpointer/tst_qscopedpointer.cpp | |
parent | 69b31f7b657a7ca611ad980c2974597de160598c (diff) |
Implement move-ctor and move-assignment-op for QScopedPointer
It makes sense for a QScopedPointer to be movable, for instance
for allowing instances to be returned from a function.
Ownwership of the managed pointer is still tied to one (and one only)
QScopedPointer instance.
Moreover, a move assignment operator makes sense as well, as it
implementing the equivalent of
this->reset(other.take());
only when other is a rvalue and not a lvalue (so either it's a temporary
or it's getting explicitly moved in with std::move).
This makes QScopedPointer API's a bit closer to std::unique_ptr's one.
Task-number: QTBUG-29754
Change-Id: If1ac0c688327a67af4ad5b7ad45b439b022ed1c6
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/tools/qscopedpointer/tst_qscopedpointer.cpp')
-rw-r--r-- | tests/auto/corelib/tools/qscopedpointer/tst_qscopedpointer.cpp | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qscopedpointer/tst_qscopedpointer.cpp b/tests/auto/corelib/tools/qscopedpointer/tst_qscopedpointer.cpp index 66ea3e940c..cb0382eb66 100644 --- a/tests/auto/corelib/tools/qscopedpointer/tst_qscopedpointer.cpp +++ b/tests/auto/corelib/tools/qscopedpointer/tst_qscopedpointer.cpp @@ -42,6 +42,8 @@ #include <QtTest/QtTest> #include <QtCore/QScopedPointer> +#include <utility> + /*! \class tst_QScopedPointer \internal @@ -73,6 +75,7 @@ private Q_SLOTS: void objectSize(); void comparison(); void array(); + void move(); // TODO instanciate on const object }; @@ -458,6 +461,158 @@ void tst_QScopedPointer::array() QCOMPARE(instCount, RefCounted::instanceCount.load()); } +#ifdef Q_COMPILER_RVALUE_REFS +struct CountedInteger +{ + CountedInteger(int i) : i(i) + { + ++instanceCount; + } + ~CountedInteger() + { + --instanceCount; + } + CountedInteger(const CountedInteger &c) + : i(c.i) + { + ++instanceCount; + } + + static int instanceCount; + int i; +}; + +int CountedInteger::instanceCount = 0; + +QScopedPointer<CountedInteger> returnScopedPointer(int i) +{ + return QScopedPointer<CountedInteger>(new CountedInteger(i)); +} +#endif + +void tst_QScopedPointer::move() +{ +#ifndef Q_COMPILER_RVALUE_REFS + QSKIP("This test requires rvalues refs"); +#else + QCOMPARE(CountedInteger::instanceCount, 0); + + { + QScopedPointer<CountedInteger> p = returnScopedPointer(42); + QVERIFY(!p.isNull()); + QCOMPARE(p->i, 42); + QCOMPARE(CountedInteger::instanceCount, 1); + + QScopedPointer<CountedInteger> q = returnScopedPointer(51); + QVERIFY(!q.isNull()); + QCOMPARE(q->i, 51); + QCOMPARE(CountedInteger::instanceCount, 2); + + q = returnScopedPointer(123); + QVERIFY(!q.isNull()); + QCOMPARE(q->i, 123); + QCOMPARE(CountedInteger::instanceCount, 2); + + p = std::move(q); + QVERIFY(!p.isNull()); + QCOMPARE(p->i, 123); + QVERIFY(q.isNull()); + QCOMPARE(CountedInteger::instanceCount, 1); + + p = std::move(q); + QVERIFY(p.isNull()); + QVERIFY(q.isNull()); + QCOMPARE(CountedInteger::instanceCount, 0); + } + + QCOMPARE(CountedInteger::instanceCount, 0); + + { + QScopedPointer<CountedInteger> p = returnScopedPointer(1024); + QVERIFY(!p.isNull()); + QCOMPARE(p->i, 1024); + QCOMPARE(CountedInteger::instanceCount, 1); + + p.reset(); + QVERIFY(p.isNull()); + QCOMPARE(CountedInteger::instanceCount, 0); + + p = returnScopedPointer(1024); + const CountedInteger * const rawPtr = p.data(); + p = std::move(p); + // now p is in a "valid, but unspecified state". so the test must not crash. + // we do actually know that this move should've been a noop. + QVERIFY(!p.isNull()); + QCOMPARE(p.data(), rawPtr); + QCOMPARE(p->i, 1024); + QCOMPARE(CountedInteger::instanceCount, 1); + + p.reset(); + QVERIFY(p.isNull()); + QCOMPARE(CountedInteger::instanceCount, 0); + } + + QCOMPARE(CountedInteger::instanceCount, 0); + + { + QScopedPointer<CountedInteger> p = returnScopedPointer(-1); + QVERIFY(!p.isNull()); + QCOMPARE(p->i, -1); + QCOMPARE(CountedInteger::instanceCount, 1); + } + + QCOMPARE(CountedInteger::instanceCount, 0); + + { + QScopedPointer<CountedInteger> p = returnScopedPointer(0); + QVERIFY(!p.isNull()); + QCOMPARE(p->i, 0); + QCOMPARE(CountedInteger::instanceCount, 1); + + QScopedPointer<CountedInteger> q; + QVERIFY(q.isNull()); + + p = std::move(q); + QVERIFY(p.isNull()); + QVERIFY(q.isNull()); + QCOMPARE(CountedInteger::instanceCount, 0); + } + + QCOMPARE(CountedInteger::instanceCount, 0); + + { + QScopedPointer<CountedInteger> p; + QVERIFY(p.isNull()); + QCOMPARE(CountedInteger::instanceCount, 0); + + QScopedPointer<CountedInteger> q = returnScopedPointer(123); + QVERIFY(!q.isNull()); + QCOMPARE(q->i, 123); + QCOMPARE(CountedInteger::instanceCount, 1); + + p = std::move(q); + QVERIFY(!p.isNull()); + QCOMPARE(p->i, 123); + QVERIFY(q.isNull()); + QCOMPARE(CountedInteger::instanceCount, 1); + } + + QCOMPARE(CountedInteger::instanceCount, 0); + + { + QScopedPointer<CountedInteger> p; + QVERIFY(p.isNull()); + QCOMPARE(CountedInteger::instanceCount, 0); + + p = returnScopedPointer(2001); + QVERIFY(!p.isNull()); + QCOMPARE(p->i, 2001); + QCOMPARE(CountedInteger::instanceCount, 1); + } + + QCOMPARE(CountedInteger::instanceCount, 0); +#endif +} QTEST_MAIN(tst_QScopedPointer) #include "tst_qscopedpointer.moc" |