From f437fb2934e56c293039dc3b00410c53596f9c3e Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sun, 21 May 2017 20:18:34 +0200 Subject: Qt containers: use std::move in take*() methods Move the objects out of the data structure to avoid needless copies. Change-Id: I1a69fccc431e040b229d6ea9ded0e041c208c861 Reviewed-by: Olivier Goffart (Woboq GmbH) --- src/corelib/tools/qhash.h | 2 +- src/corelib/tools/qlinkedlist.h | 4 ++-- src/corelib/tools/qlist.h | 6 +++--- src/corelib/tools/qmap.h | 2 +- src/corelib/tools/qvector.h | 6 +++--- 5 files changed, 10 insertions(+), 10 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index 8689243a98..d7378df746 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -825,7 +825,7 @@ Q_OUTOFLINE_TEMPLATE T QHash::take(const Key &akey) Node **node = findNode(akey); if (*node != e) { - T t = (*node)->value; + T t = std::move((*node)->value); Node *next = (*node)->next; deleteNode(*node); *node = next; diff --git a/src/corelib/tools/qlinkedlist.h b/src/corelib/tools/qlinkedlist.h index 249f76dafd..9f54ba7825 100644 --- a/src/corelib/tools/qlinkedlist.h +++ b/src/corelib/tools/qlinkedlist.h @@ -448,7 +448,7 @@ bool QLinkedList::removeOne(const T &_t) template inline T QLinkedList::takeFirst() { - T t = first(); + T t = std::move(first()); removeFirst(); return t; } @@ -456,7 +456,7 @@ inline T QLinkedList::takeFirst() template inline T QLinkedList::takeLast() { - T t = last(); + T t = std::move(last()); removeLast(); return t; } diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index e2706de9ee..1042c29460 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -555,14 +555,14 @@ inline void QList::removeAt(int i) template inline T QList::takeAt(int i) { Q_ASSERT_X(i >= 0 && i < p.size(), "QList::take", "index out of range"); - detach(); Node *n = reinterpret_cast(p.at(i)); T t = n->t(); node_destruct(n); + detach(); Node *n = reinterpret_cast(p.at(i)); T t = std::move(n->t()); node_destruct(n); p.remove(i); return t; } template inline T QList::takeFirst() -{ T t = first(); removeFirst(); return t; } +{ T t = std::move(first()); removeFirst(); return t; } template inline T QList::takeLast() -{ T t = last(); removeLast(); return t; } +{ T t = std::move(last()); removeLast(); return t; } template Q_OUTOFLINE_TEMPLATE void QList::reserve(int alloc) diff --git a/src/corelib/tools/qmap.h b/src/corelib/tools/qmap.h index 2c19ae969f..9a663c2c20 100644 --- a/src/corelib/tools/qmap.h +++ b/src/corelib/tools/qmap.h @@ -956,7 +956,7 @@ Q_OUTOFLINE_TEMPLATE T QMap::take(const Key &akey) Node *node = d->findNode(akey); if (node) { - T t = node->value; + T t = std::move(node->value); d->deleteNode(node); return t; } diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index 121187083b..2d12a95b4b 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -147,8 +147,8 @@ public: void remove(int i, int n); inline void removeFirst() { Q_ASSERT(!isEmpty()); erase(d->begin()); } inline void removeLast(); - inline T takeFirst() { Q_ASSERT(!isEmpty()); T r = first(); removeFirst(); return r; } - inline T takeLast() { Q_ASSERT(!isEmpty()); T r = last(); removeLast(); return r; } + T takeFirst() { Q_ASSERT(!isEmpty()); T r = std::move(first()); removeFirst(); return r; } + T takeLast() { Q_ASSERT(!isEmpty()); T r = std::move(last()); removeLast(); return r; } QVector &fill(const T &t, int size = -1); @@ -180,7 +180,7 @@ public: return true; } int length() const { return size(); } - T takeAt(int i) { T t = at(i); remove(i); return t; } + T takeAt(int i) { T t = std::move((*this)[i]); remove(i); return t; } void move(int from, int to) { Q_ASSERT_X(from >= 0 && from < size(), "QVector::move(int,int)", "'from' is out-of-range"); -- cgit v1.2.3