From f92fdd190d056fe929f723e3f2ce9e0c0b141cec Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Sun, 22 Apr 2012 22:04:46 +0200 Subject: Implement the move constructor for containers. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This changes all the containers that uses QtPrivate::RefCount (QMap already had one), and QVariant In Qt 4.8, it was pointless to have the move constructor because we did not have quick way to re-initialize a null container. (shared_null still needed to be refcounted) But now that we have RefCount, and that the shared_null do not have reference count, we can implement a fast move constructor that do not generate code to increment the reference count. Change-Id: I2bc3c6ae96983f08aa7b1c7cb98d44a89255160b Reviewed-by: Jędrzej Nowacki --- src/corelib/kernel/qvariant.h | 2 ++ src/corelib/tools/qbytearray.h | 1 + src/corelib/tools/qhash.h | 1 + src/corelib/tools/qlinkedlist.h | 1 + src/corelib/tools/qlist.h | 3 ++- src/corelib/tools/qstring.h | 1 + src/corelib/tools/qvector.h | 1 + tests/auto/other/collections/tst_collections.cpp | 20 +++++++++++++++++++- 8 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index 67cec6875a..8ba27d2354 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -251,6 +251,8 @@ class Q_CORE_EXPORT QVariant QVariant& operator=(const QVariant &other); #ifdef Q_COMPILER_RVALUE_REFS + inline QVariant(QVariant &&other) : d(other.d) + { other.d = Private(); } inline QVariant &operator=(QVariant &&other) { qSwap(d, other.d); return *this; } #endif diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h index 79a7377c40..1e2a93d9f0 100644 --- a/src/corelib/tools/qbytearray.h +++ b/src/corelib/tools/qbytearray.h @@ -201,6 +201,7 @@ public: QByteArray &operator=(const QByteArray &); QByteArray &operator=(const char *str); #ifdef Q_COMPILER_RVALUE_REFS + inline QByteArray(QByteArray && other) : d(other.d) { other.d = Data::sharedNull(); } inline QByteArray &operator=(QByteArray &&other) { qSwap(d, other.d); return *this; } #endif diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index d196e6e376..3936e36efb 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -287,6 +287,7 @@ public: QHash &operator=(const QHash &other); #ifdef Q_COMPILER_RVALUE_REFS + inline QHash(QHash &&other) : d(other.d) { other.d = const_cast(&QHashData::shared_null); } inline QHash &operator=(QHash &&other) { qSwap(d, other.d); return *this; } #endif diff --git a/src/corelib/tools/qlinkedlist.h b/src/corelib/tools/qlinkedlist.h index a8a97b308a..1caf21f456 100644 --- a/src/corelib/tools/qlinkedlist.h +++ b/src/corelib/tools/qlinkedlist.h @@ -83,6 +83,7 @@ public: ~QLinkedList(); QLinkedList &operator=(const QLinkedList &); #ifdef Q_COMPILER_RVALUE_REFS + inline QLinkedList(QLinkedList &&other) : d(other.d) { other.d = const_cast(&QLinkedListData::shared_null); } inline QLinkedList &operator=(QLinkedList &&other) { qSwap(d, other.d); return *this; } #endif diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 0d5b109f2f..ad94781b6f 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -115,7 +115,8 @@ public: ~QList(); QList &operator=(const QList &l); #ifdef Q_COMPILER_RVALUE_REFS - inline QList &operator=(QList &&other) + inline QList(QList &&other) : d(other.d) { other.d = const_cast(&QListData::shared_null); } + inline QList &operator=(QList &&other) { qSwap(d, other.d); return *this; } #endif inline void swap(QList &other) { qSwap(d, other.d); } diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 61b7264296..7b6d7842cc 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -198,6 +198,7 @@ public: QString &operator=(const QString &); inline QString &operator=(const QLatin1String &); #ifdef Q_COMPILER_RVALUE_REFS + inline QString(QString && other) : d(other.d) { other.d = Data::sharedNull(); } inline QString &operator=(QString &&other) { qSwap(d, other.d); return *this; } #endif diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index b36b832c26..60cf12e60d 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -115,6 +115,7 @@ public: inline ~QVector() { if (!d->ref.deref()) free(d); } QVector &operator=(const QVector &v); #ifdef Q_COMPILER_RVALUE_REFS + inline QVector(QVector &&other) : d(other.d) { other.d = Data::sharedNull(); } inline QVector operator=(QVector &&other) { qSwap(d, other.d); return *this; } #endif diff --git a/tests/auto/other/collections/tst_collections.cpp b/tests/auto/other/collections/tst_collections.cpp index 472dd5020e..26e3ccfce4 100644 --- a/tests/auto/other/collections/tst_collections.cpp +++ b/tests/auto/other/collections/tst_collections.cpp @@ -2419,6 +2419,14 @@ bool isSharable(const Container &container) return !container.isDetached(); } +template Container newInstance() { + Container container; + populate(container); + if (!container.isEmpty()) + return container; + return Container(); +} + template void testContainer() { @@ -2539,10 +2547,20 @@ void testContainer() QVERIFY(!c2.isDetached()); QVERIFY(!c3.isDetached()); } + + /* test that the move operators work properly */ + { + Container c1 = Container(newInstance()); + QVERIFY(c1.size() == 4); + QVERIFY(c1 == newInstance()); + c1 = newInstance(); + QVERIFY(c1.size() == 4); + QVERIFY(c1 == newInstance()); + } } #define TEST_SEQUENTIAL_CONTAINER(Container) \ - testContainer, QMutable##Container##Iterator >() + testContainer, QMutable##Container##Iterator >() \ #define TEST_ASSOCIATIVE_CONTAINER(Container) \ testContainer, QMutable##Container##Iterator >() -- cgit v1.2.3