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>
This commit is contained in:
Giuseppe D'Angelo 2020-11-24 16:20:10 +01:00
parent e6e67f31c1
commit b475c3e67e

View File

@ -108,11 +108,8 @@ public:
inline ~QVarLengthArray() inline ~QVarLengthArray()
{ {
if (QTypeInfo<T>::isComplex) { if constexpr (QTypeInfo<T>::isComplex)
T *i = ptr + s; std::destroy_n(ptr, s);
while (i-- != ptr)
i->~T();
}
if (ptr != reinterpret_cast<T *>(array)) if (ptr != reinterpret_cast<T *>(array))
free(ptr); free(ptr);
} }
@ -157,7 +154,7 @@ public:
inline void removeLast() inline void removeLast()
{ {
Q_ASSERT(s > 0); Q_ASSERT(s > 0);
if (QTypeInfo<T>::isComplex) if constexpr (QTypeInfo<T>::isComplex)
ptr[s - 1].~T(); ptr[s - 1].~T();
--s; --s;
} }
@ -473,15 +470,13 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::append(const T *abuf, qs
if (asize >= a) if (asize >= a)
reallocate(s, qMax(s * 2, asize)); reallocate(s, qMax(s * 2, asize));
if (QTypeInfo<T>::isComplex) { if constexpr (QTypeInfo<T>::isComplex)
// call constructor for new objects (which can throw) std::uninitialized_copy_n(abuf, increment, ptr + s);
while (s < asize) else
new (ptr+(s++)) T(*abuf++);
} else {
memcpy(static_cast<void *>(&ptr[s]), static_cast<const void *>(abuf), increment * sizeof(T)); memcpy(static_cast<void *>(&ptr[s]), static_cast<const void *>(abuf), increment * sizeof(T));
s = asize; s = asize;
} }
}
template <class T, qsizetype Prealloc> template <class T, qsizetype Prealloc>
Q_INLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::squeeze() Q_INLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::squeeze()
@ -531,10 +526,10 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::reallocate(qsizetype asi
} }
s = copySize; s = copySize;
if (QTypeInfo<T>::isComplex) {
// destroy remaining old objects // destroy remaining old objects
while (osize > asize) if constexpr (QTypeInfo<T>::isComplex) {
(oldPtr+(--osize))->~T(); if (osize > asize)
std::destroy(oldPtr + asize, oldPtr + osize);
} }
if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr) 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 f = qsizetype(abegin - ptr);
qsizetype l = qsizetype(aend - ptr); qsizetype l = qsizetype(aend - ptr);
qsizetype n = l - f; qsizetype n = l - f;
if (QTypeInfo<T>::isComplex) {
std::copy(ptr + l, ptr + s, QT_MAKE_CHECKED_ARRAY_ITERATOR(ptr + f, s - f)); if constexpr (QTypeInfo<T>::isComplex) {
T *i = ptr + s; std::move(ptr + l, ptr + s, QT_MAKE_CHECKED_ARRAY_ITERATOR(ptr + f, s - f));
T *b = ptr + s - n; std::destroy(ptr + s - n, ptr + s);
while (i != b) {
--i;
i->~T();
}
} else { } else {
memmove(static_cast<void *>(ptr + f), static_cast<const void *>(ptr + l), (s - l) * sizeof(T)); memmove(static_cast<void *>(ptr + f), static_cast<const void *>(ptr + l), (s - l) * sizeof(T));
} }