QArrayDataPointer: add a C++20 ranges-style optional projection to assign()

This will be useful for implementing QString::assign(), which
otherwise has the problem that it's d_ptr is based on char16_t, but
it's assign() is supposed to be able to deal with iterators whose
value_type returns QChar.

Task-number: QTBUG-106198
Change-Id: I87882bf749b4e21b7b32391167962d3e6bae9983
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Marc Mutz 2023-05-10 18:29:02 +02:00
parent bbbe5f45c4
commit 7ca633d9a8

View File

@ -7,6 +7,8 @@
#include <QtCore/qarraydataops.h> #include <QtCore/qarraydataops.h>
#include <QtCore/qcontainertools_impl.h> #include <QtCore/qcontainertools_impl.h>
#include <QtCore/q20functional.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
template <class T> template <class T>
@ -305,8 +307,8 @@ public:
this->ptr = res; this->ptr = res;
} }
template <typename InputIterator> template <typename InputIterator, typename Projection = q20::identity>
void assign(InputIterator first, InputIterator last) void assign(InputIterator first, InputIterator last, Projection proj = {})
{ {
// This function only provides the basic exception guarantee. // This function only provides the basic exception guarantee.
constexpr bool IsFwdIt = std::is_convertible_v< constexpr bool IsFwdIt = std::is_convertible_v<
@ -340,12 +342,12 @@ public:
break; break;
} else { } else {
do { do {
(*this)->emplace(size, *first); (*this)->emplace(size, proj(*first));
} while (++first != last); } while (++first != last);
return; // size() is already correct (and dst invalidated)! return; // size() is already correct (and dst invalidated)!
} }
} }
*dst = *first; // overwrite existing element *dst = proj(*first); // overwrite existing element
++dst; ++dst;
++first; ++first;
} }