Implement the move constructor for containers.

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 <jedrzej.nowacki@nokia.com>
This commit is contained in:
Olivier Goffart 2012-04-22 22:04:46 +02:00 committed by Qt by Nokia
parent 7226361aed
commit f92fdd190d
8 changed files with 28 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -287,6 +287,7 @@ public:
QHash<Key, T> &operator=(const QHash<Key, T> &other);
#ifdef Q_COMPILER_RVALUE_REFS
inline QHash(QHash<Key, T> &&other) : d(other.d) { other.d = const_cast<QHashData *>(&QHashData::shared_null); }
inline QHash<Key, T> &operator=(QHash<Key, T> &&other)
{ qSwap(d, other.d); return *this; }
#endif

View File

@ -83,6 +83,7 @@ public:
~QLinkedList();
QLinkedList<T> &operator=(const QLinkedList<T> &);
#ifdef Q_COMPILER_RVALUE_REFS
inline QLinkedList(QLinkedList<T> &&other) : d(other.d) { other.d = const_cast<QLinkedListData *>(&QLinkedListData::shared_null); }
inline QLinkedList<T> &operator=(QLinkedList<T> &&other)
{ qSwap(d, other.d); return *this; }
#endif

View File

@ -115,7 +115,8 @@ public:
~QList();
QList<T> &operator=(const QList<T> &l);
#ifdef Q_COMPILER_RVALUE_REFS
inline QList &operator=(QList &&other)
inline QList(QList<T> &&other) : d(other.d) { other.d = const_cast<QListData::Data *>(&QListData::shared_null); }
inline QList &operator=(QList<T> &&other)
{ qSwap(d, other.d); return *this; }
#endif
inline void swap(QList<T> &other) { qSwap(d, other.d); }

View File

@ -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

View File

@ -115,6 +115,7 @@ public:
inline ~QVector() { if (!d->ref.deref()) free(d); }
QVector<T> &operator=(const QVector<T> &v);
#ifdef Q_COMPILER_RVALUE_REFS
inline QVector(QVector<T> &&other) : d(other.d) { other.d = Data::sharedNull(); }
inline QVector<T> operator=(QVector<T> &&other)
{ qSwap(d, other.d); return *this; }
#endif

View File

@ -2419,6 +2419,14 @@ bool isSharable(const Container &container)
return !container.isDetached();
}
template <class Container> Container newInstance() {
Container container;
populate(container);
if (!container.isEmpty())
return container;
return Container();
}
template <class Container, class ContainerMutableIterator>
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<Container>());
QVERIFY(c1.size() == 4);
QVERIFY(c1 == newInstance<Container>());
c1 = newInstance<Container>();
QVERIFY(c1.size() == 4);
QVERIFY(c1 == newInstance<Container>());
}
}
#define TEST_SEQUENTIAL_CONTAINER(Container) \
testContainer<Q##Container<int>, QMutable##Container##Iterator<int> >()
testContainer<Q##Container<int>, QMutable##Container##Iterator<int> >() \
#define TEST_ASSOCIATIVE_CONTAINER(Container) \
testContainer<Q##Container<int, int>, QMutable##Container##Iterator<int, int> >()