QVarLengthArray: Extract Method QtPrivate::q_rotate()

It seems like we'll need this in lots of other places, too.

Pick-to: 6.5 6.4 6.2 5.15
Change-Id: I767495c2eb02a2fc85b6f835ad9003fa89315c7f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Marc Mutz 2023-02-16 09:04:11 +01:00
parent bb2ff8a69f
commit 147dd6e82f
2 changed files with 21 additions and 6 deletions

View File

@ -86,6 +86,26 @@ void q_uninitialized_relocate_n(T* first, N n, T* out)
QT_WARNING_POP QT_WARNING_POP
/*!
\internal
A wrapper around std::rotate(), with an optimization for
Q_RELOCATABLE_TYPEs. We omit the return value, as it would be more work to
compute in the Q_RELOCATABLE_TYPE case and, unlike std::rotate on
ForwardIterators, callers can compute the result in constant time
themselves.
*/
template <typename T>
void q_rotate(T *first, T *mid, T *last)
{
if constexpr (QTypeInfo<T>::isRelocatable) {
const auto cast = [](T *p) { return reinterpret_cast<uchar*>(p); };
std::rotate(cast(first), cast(mid), cast(last));
} else {
std::rotate(first, mid, last);
}
}
/*! /*!
\internal \internal
Copies all elements, except the ones for which \a pred returns \c true, from Copies all elements, except the ones for which \a pred returns \c true, from

View File

@ -916,12 +916,7 @@ Q_OUTOFLINE_TEMPLATE auto QVLABase<T>::emplace_impl(qsizetype prealloc, void *ar
emplace_back_impl(prealloc, array, std::forward<Args>(args)...); emplace_back_impl(prealloc, array, std::forward<Args>(args)...);
const auto b = begin() + offset; const auto b = begin() + offset;
const auto e = end(); const auto e = end();
if constexpr (QTypeInfo<T>::isRelocatable) { QtPrivate::q_rotate(b, e - 1, e);
auto cast = [](T *p) { return reinterpret_cast<uchar*>(p); };
std::rotate(cast(b), cast(e - 1), cast(e));
} else {
std::rotate(b, e - 1, e);
}
return b; return b;
} }