diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2015-07-19 10:37:49 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2015-07-19 18:09:27 +0000 |
commit | 4ea3c0ba80e55e93ed0e03650325a046a676f73d (patch) | |
tree | 821771a7558aa210e2123e46a640677c755297c1 /src/corelib/tools/qvector.h | |
parent | 8d1d12a32896cca3f9aaae31ae2a3ee28dc8f276 (diff) |
QVector: add an rvalue overload of push_back/append
This is low-hanging fruit, for two reasons:
1. The implementation is dead-simple (unlike, say, in QList).
2. It's completely transparent to the QVector user (unlike,
say, emplace_back, which can only be used inside an ifdef).
Change-Id: Iaf750100cf61ced77aa452f0e4e3c4ec36b29639
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Diffstat (limited to 'src/corelib/tools/qvector.h')
-rw-r--r-- | src/corelib/tools/qvector.h | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index 1a7692c0f4..890dbd317d 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -129,6 +129,9 @@ public: T &operator[](int i); const T &operator[](int i) const; void append(const T &t); +#ifdef Q_COMPILER_RVALUE_REFS + void append(T &&t); +#endif inline void append(const QVector<T> &l) { *this += l; } void prepend(const T &t); void insert(int i, const T &t); @@ -247,6 +250,9 @@ public: typedef const_iterator ConstIterator; typedef int size_type; inline void push_back(const T &t) { append(t); } +#ifdef Q_COMPILER_RVALUE_REFS + void push_back(T &&t) { append(std::move(t)); } +#endif inline void push_front(const T &t) { prepend(t); } void pop_back() { removeLast(); } void pop_front() { removeFirst(); } @@ -643,6 +649,22 @@ void QVector<T>::append(const T &t) ++d->size; } +#ifdef Q_COMPILER_RVALUE_REFS +template <typename T> +void QVector<T>::append(T &&t) +{ + const bool isTooSmall = uint(d->size + 1) > d->alloc; + if (!isDetached() || isTooSmall) { + QArrayData::AllocationOptions opt(isTooSmall ? QArrayData::Grow : QArrayData::Default); + reallocData(d->size, isTooSmall ? d->size + 1 : d->alloc, opt); + } + + new (d->end()) T(std::move(t)); + + ++d->size; +} +#endif + template <typename T> void QVector<T>::removeLast() { |