From 6cd37869ca724b0207dd4d784c0a7ac559220f08 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 16 Feb 2023 09:04:11 +0100 Subject: [PATCH] QVarLengthArray: Extract Method QtPrivate::q_rotate() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It seems like we'll need this in lots of other places, too. Change-Id: I767495c2eb02a2fc85b6f835ad9003fa89315c7f Reviewed-by: Thiago Macieira Reviewed-by: MÃ¥rten Nordheim (cherry picked from commit 147dd6e82f41778060bdadf9b2a792bd11e1bc1e) Reviewed-by: Volker Hilsheimer --- src/corelib/tools/qcontainertools_impl.h | 20 ++++++++++++++++++++ src/corelib/tools/qvarlengtharray.h | 7 +------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/corelib/tools/qcontainertools_impl.h b/src/corelib/tools/qcontainertools_impl.h index adaa254a04a..d380421170c 100644 --- a/src/corelib/tools/qcontainertools_impl.h +++ b/src/corelib/tools/qcontainertools_impl.h @@ -86,6 +86,26 @@ void q_uninitialized_relocate_n(T* first, N n, T* out) 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 +void q_rotate(T *first, T *mid, T *last) +{ + if constexpr (QTypeInfo::isRelocatable) { + const auto cast = [](T *p) { return reinterpret_cast(p); }; + std::rotate(cast(first), cast(mid), cast(last)); + } else { + std::rotate(first, mid, last); + } +} + template void q_relocate_overlap_n_left_move(iterator first, N n, iterator d_first) { diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index 618e04b41cf..6428c359c0e 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -853,12 +853,7 @@ Q_OUTOFLINE_TEMPLATE auto QVLABase::emplace_impl(qsizetype prealloc, void *ar emplace_back_impl(prealloc, array, std::forward(args)...); const auto b = begin() + offset; const auto e = end(); - if constexpr (QTypeInfo::isRelocatable) { - auto cast = [](T *p) { return reinterpret_cast(p); }; - std::rotate(cast(b), cast(e - 1), cast(e)); - } else { - std::rotate(b, e - 1, e); - } + QtPrivate::q_rotate(b, e - 1, e); return b; }