From bf7debf658ce65ada062c9a129320c7356becfd1 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Wed, 5 Feb 2020 15:02:27 +0100 Subject: Add move semantics to QContiguousCache MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Provide move semantics for QContiguousCache. Do further cleanup of the code and avoid special casing for complex typeinfo where it won't make a difference anyway. Change-Id: I9bd261aec21be7a050e04f5b3a1f0b8e1d94c064 Reviewed-by: Thiago Macieira Reviewed-by: MÃ¥rten Nordheim --- src/corelib/tools/qcontiguouscache.h | 94 +++++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 34 deletions(-) (limited to 'src/corelib/tools') diff --git a/src/corelib/tools/qcontiguouscache.h b/src/corelib/tools/qcontiguouscache.h index fb487b60a1..eb3194c752 100644 --- a/src/corelib/tools/qcontiguouscache.h +++ b/src/corelib/tools/qcontiguouscache.h @@ -115,10 +115,14 @@ public: T &operator[](int i); const T &operator[](int i) const; + void append(T &&value); void append(const T &value); + void prepend(T &&value); void prepend(const T &value); + void insert(int pos, T &&value); void insert(int pos, const T &value); + inline bool containsIndex(int pos) const { return pos >= d->offset && pos - d->offset < d->count; } inline int firstIndex() const { return d->offset; } inline int lastIndex() const { return d->offset + d->count - 1; } @@ -162,11 +166,7 @@ void QContiguousCache::detach_helper() T *src = d->array + d->start; int oldcount = x->count; while (oldcount--) { - if (QTypeInfo::isComplex) { - new (dest) T(*src); - } else { - *dest = *src; - } + new (dest) T(*src); dest++; if (dest == x->array + x->alloc) dest = x->array; @@ -203,11 +203,7 @@ void QContiguousCache::setCapacity(int asize) T *dest = x->array + (x->start + x->count-1) % x->alloc; T *src = d->array + (d->start + d->count-1) % d->alloc; while (oldcount--) { - if (QTypeInfo::isComplex) { - new (dest) T(*src); - } else { - *dest = *src; - } + new (dest) T(*src); if (dest == x->array) dest = x->array + x->alloc; dest--; @@ -307,18 +303,33 @@ void QContiguousCache::freeData(Data *x) Data::freeData(x); } template -void QContiguousCache::append(const T &value) +void QContiguousCache::append(T &&value) { if (!d->alloc) return; // zero capacity detach(); - if (QTypeInfo::isComplex) { - if (d->count == d->alloc) - (d->array + (d->start+d->count) % d->alloc)->~T(); - new (d->array + (d->start+d->count) % d->alloc) T(value); + if (d->count == d->alloc) + (d->array + (d->start+d->count) % d->alloc)->~T(); + new (d->array + (d->start+d->count) % d->alloc) T(std::move(value)); + + if (d->count == d->alloc) { + d->start++; + d->start %= d->alloc; + d->offset++; } else { - d->array[(d->start+d->count) % d->alloc] = value; + d->count++; } +} + +template +void QContiguousCache::append(const T &value) +{ + if (!d->alloc) + return; // zero capacity + detach(); + if (d->count == d->alloc) + (d->array + (d->start+d->count) % d->alloc)->~T(); + new (d->array + (d->start+d->count) % d->alloc) T(value); if (d->count == d->alloc) { d->start++; @@ -330,7 +341,7 @@ void QContiguousCache::append(const T &value) } template -void QContiguousCache::prepend(const T &value) +void QContiguousCache::prepend(T &&value) { if (!d->alloc) return; // zero capacity @@ -347,26 +358,39 @@ void QContiguousCache::prepend(const T &value) if (d->count == d->alloc) (d->array + d->start)->~T(); - if (QTypeInfo::isComplex) - new (d->array + d->start) T(value); + new (d->array + d->start) T(std::move(value)); +} + +template +void QContiguousCache::prepend(const T &value) +{ + if (!d->alloc) + return; // zero capacity + detach(); + if (d->start) + d->start--; else - d->array[d->start] = value; + d->start = d->alloc-1; + d->offset--; + + if (d->count != d->alloc) + d->count++; + else + if (d->count == d->alloc) + (d->array + d->start)->~T(); + + new (d->array + d->start) T(value); } template -void QContiguousCache::insert(int pos, const T &value) +void QContiguousCache::insert(int pos, T &&value) { Q_ASSERT_X(pos >= 0 && pos < INT_MAX, "QContiguousCache::insert", "index out of range"); if (!d->alloc) return; // zero capacity detach(); if (containsIndex(pos)) { - if (QTypeInfo::isComplex) { - (d->array + pos % d->alloc)->~T(); - new (d->array + pos % d->alloc) T(value); - } else { - d->array[pos % d->alloc] = value; - } + d->array[pos % d->alloc] = std::move(value); } else if (pos == d->offset-1) prepend(value); else if (pos == d->offset+d->count) @@ -377,19 +401,21 @@ void QContiguousCache::insert(int pos, const T &value) d->offset = pos; d->start = pos % d->alloc; d->count = 1; - if (QTypeInfo::isComplex) - new (d->array + d->start) T(value); - else - d->array[d->start] = value; + new (d->array + d->start) T(std::move(value)); } } +template +void QContiguousCache::insert(int pos, const T &value) +{ + return insert(pos, T(value)); +} template inline const T &QContiguousCache::at(int pos) const { Q_ASSERT_X(pos >= d->offset && pos - d->offset < d->count, "QContiguousCache::at", "index out of range"); return d->array[pos % d->alloc]; } template inline const T &QContiguousCache::operator[](int pos) const -{ Q_ASSERT_X(pos >= d->offset && pos - d->offset < d->count, "QContiguousCache::at", "index out of range"); return d->array[pos % d->alloc]; } +{ return at(pos); } template inline T &QContiguousCache::operator[](int pos) @@ -424,11 +450,11 @@ inline void QContiguousCache::removeLast() template inline T QContiguousCache::takeFirst() -{ T t = first(); removeFirst(); return t; } +{ T t = std::move(first()); removeFirst(); return t; } template inline T QContiguousCache::takeLast() -{ T t = last(); removeLast(); return t; } +{ T t = std::move(last()); removeLast(); return t; } QT_END_NAMESPACE -- cgit v1.2.3