summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2022-04-04 15:20:51 +0200
committerMarc Mutz <marc.mutz@qt.io>2022-04-06 10:44:40 +0000
commita00a1d8806cfbf17e04b88d1b4ff4a9cf5b6294a (patch)
tree393f227915629c65ca4d63230b0b6f45f2acf341 /src/corelib/tools
parent2fb7c94f63ce783c7f36149791fe72e053933ece (diff)
QByteArray/QVarLengthArray: add missing resize(n, v) overloads
QList and QString had them, so add them to QByteArray and QVarLengthArray, too. In the QVLA case, we need to jump though a hoop or two to avoid having to duplicate all the reallocation logic. Nothing a few template tricks cannot solve. [ChangeLog][QtCore][QByteArray] Added resize(n, ch) overload. [ChangeLog][QtCore][QVarLengthArray] Added resize(n, v) overload. Fixes: QTBUG-102270 Change-Id: I0d281ae5b574f440f682e4a62427b434dcf5b687 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools')
-rw-r--r--src/corelib/tools/qvarlengtharray.h27
-rw-r--r--src/corelib/tools/qvarlengtharray.qdoc13
2 files changed, 35 insertions, 5 deletions
diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h
index fd3d4ffbf2..2c9d0dd975 100644
--- a/src/corelib/tools/qvarlengtharray.h
+++ b/src/corelib/tools/qvarlengtharray.h
@@ -237,9 +237,9 @@ protected:
}
void append_impl(qsizetype prealloc, void *array, const T *buf, qsizetype n);
- void reallocate_impl(qsizetype prealloc, void *array, qsizetype size, qsizetype alloc);
- void resize_impl(qsizetype prealloc, void *array, qsizetype sz)
- { reallocate_impl(prealloc, array, sz, qMax(sz, capacity())); }
+ void reallocate_impl(qsizetype prealloc, void *array, qsizetype size, qsizetype alloc, const T *v = nullptr);
+ void resize_impl(qsizetype prealloc, void *array, qsizetype sz, const T *v = nullptr)
+ { reallocate_impl(prealloc, array, sz, qMax(sz, capacity()), v); }
bool isValidIterator(const const_iterator &i) const
{
@@ -393,6 +393,14 @@ public:
}
bool isEmpty() const { return empty(); }
void resize(qsizetype sz) { Base::resize_impl(Prealloc, this->array, sz); }
+#ifdef Q_QDOC
+ void
+#else
+ template <typename U = T>
+ std::enable_if_t<std::is_copy_constructible_v<U>>
+#endif
+ resize(qsizetype sz, const T &v)
+ { Base::resize_impl(Prealloc, this->array, sz, &v); }
inline void clear() { resize(0); }
void squeeze() { reallocate(size(), size()); }
@@ -720,7 +728,7 @@ Q_OUTOFLINE_TEMPLATE void QVLABase<T>::append_impl(qsizetype prealloc, void *arr
}
template <class T>
-Q_OUTOFLINE_TEMPLATE void QVLABase<T>::reallocate_impl(qsizetype prealloc, void *array, qsizetype asize, qsizetype aalloc)
+Q_OUTOFLINE_TEMPLATE void QVLABase<T>::reallocate_impl(qsizetype prealloc, void *array, qsizetype asize, qsizetype aalloc, const T *v)
{
Q_ASSERT(aalloc >= asize);
Q_ASSERT(data());
@@ -765,7 +773,16 @@ Q_OUTOFLINE_TEMPLATE void QVLABase<T>::reallocate_impl(qsizetype prealloc, void
if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != data())
free(oldPtr);
- if constexpr (QTypeInfo<T>::isComplex) {
+ if (v) {
+ if constexpr (std::is_copy_constructible_v<T>) {
+ while (size() < asize) {
+ new (data() + size()) T(*v);
+ ++s;
+ }
+ } else {
+ Q_UNREACHABLE();
+ }
+ } else if constexpr (QTypeInfo<T>::isComplex) {
// call default constructor for new objects (which can throw)
while (size() < asize) {
new (data() + size()) T;
diff --git a/src/corelib/tools/qvarlengtharray.qdoc b/src/corelib/tools/qvarlengtharray.qdoc
index de57daf4e4..12789b49b9 100644
--- a/src/corelib/tools/qvarlengtharray.qdoc
+++ b/src/corelib/tools/qvarlengtharray.qdoc
@@ -245,6 +245,19 @@
\sa size(), squeeze()
*/
+/*!
+ \fn template<class T, qsizetype Prealloc> void QVarLengthArray<T, Prealloc>::resize(qsizetype size, const T &v)
+ \since 6.4
+
+ Sets the size of the array to \a size. If \a size is greater than
+ the current size, copies of \a v are added to the end. If \a size is
+ less than the current size, elements are removed from the end.
+
+ \note This function is only available when \c T is copy-constructible.
+
+ \sa size(), squeeze()
+*/
+
/*! \fn template<class T, qsizetype Prealloc> qsizetype QVarLengthArray<T, Prealloc>::capacity() const
Returns the maximum number of elements that can be stored in the