diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2020-11-24 16:20:10 +0100 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2020-11-26 14:43:20 +0100 |
commit | b475c3e67e79a1b2231f8139c4956de960d1cb37 (patch) | |
tree | 99bbc678bba0600c87db0fe2a7661fc464797004 /src/corelib | |
parent | e6e67f31c10da900ec6ecb06c2e827b17283be6a (diff) |
QVLA: modernize some code
Use if constexpr instead of plain if; use C++17 algorithms
instead of hand-rolled loops.
Change-Id: Ifa092f892199b9b21bad04b2d72d5e3117a1b377
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/tools/qvarlengtharray.h | 41 |
1 files changed, 16 insertions, 25 deletions
diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index 6a99602153..06a4b66125 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -108,11 +108,8 @@ public: inline ~QVarLengthArray() { - if (QTypeInfo<T>::isComplex) { - T *i = ptr + s; - while (i-- != ptr) - i->~T(); - } + if constexpr (QTypeInfo<T>::isComplex) + std::destroy_n(ptr, s); if (ptr != reinterpret_cast<T *>(array)) free(ptr); } @@ -157,7 +154,7 @@ public: inline void removeLast() { Q_ASSERT(s > 0); - if (QTypeInfo<T>::isComplex) + if constexpr (QTypeInfo<T>::isComplex) ptr[s - 1].~T(); --s; } @@ -473,14 +470,12 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::append(const T *abuf, qs if (asize >= a) reallocate(s, qMax(s * 2, asize)); - if (QTypeInfo<T>::isComplex) { - // call constructor for new objects (which can throw) - while (s < asize) - new (ptr+(s++)) T(*abuf++); - } else { + if constexpr (QTypeInfo<T>::isComplex) + std::uninitialized_copy_n(abuf, increment, ptr + s); + else memcpy(static_cast<void *>(&ptr[s]), static_cast<const void *>(abuf), increment * sizeof(T)); - s = asize; - } + + s = asize; } template <class T, qsizetype Prealloc> @@ -531,10 +526,10 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::reallocate(qsizetype asi } s = copySize; - if (QTypeInfo<T>::isComplex) { - // destroy remaining old objects - while (osize > asize) - (oldPtr+(--osize))->~T(); + // destroy remaining old objects + if constexpr (QTypeInfo<T>::isComplex) { + if (osize > asize) + std::destroy(oldPtr + asize, oldPtr + osize); } if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr) @@ -664,14 +659,10 @@ Q_OUTOFLINE_TEMPLATE typename QVarLengthArray<T, Prealloc>::iterator QVarLengthA qsizetype f = qsizetype(abegin - ptr); qsizetype l = qsizetype(aend - ptr); qsizetype n = l - f; - if (QTypeInfo<T>::isComplex) { - std::copy(ptr + l, ptr + s, QT_MAKE_CHECKED_ARRAY_ITERATOR(ptr + f, s - f)); - T *i = ptr + s; - T *b = ptr + s - n; - while (i != b) { - --i; - i->~T(); - } + + if constexpr (QTypeInfo<T>::isComplex) { + std::move(ptr + l, ptr + s, QT_MAKE_CHECKED_ARRAY_ITERATOR(ptr + f, s - f)); + std::destroy(ptr + s - n, ptr + s); } else { memmove(static_cast<void *>(ptr + f), static_cast<const void *>(ptr + l), (s - l) * sizeof(T)); } |