diff options
Diffstat (limited to 'src/corelib/tools/qlist.h')
-rw-r--r-- | src/corelib/tools/qlist.h | 101 |
1 files changed, 65 insertions, 36 deletions
diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index cc65039941..1cced5acc2 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -10,12 +10,16 @@ #include <QtCore/qhashfunctions.h> #include <QtCore/qiterator.h> #include <QtCore/qcontainertools_impl.h> +#include <QtCore/qnamespace.h> +#include <QtCore/qttypetraits.h> #include <functional> #include <limits> #include <initializer_list> #include <type_traits> +class tst_QList; + QT_BEGIN_NAMESPACE namespace QtPrivate { @@ -75,10 +79,15 @@ class QList using DataPointer = QArrayDataPointer<T>; class DisableRValueRefs {}; + friend class ::tst_QList; + DataPointer d; template <typename V, typename U> friend qsizetype QtPrivate::indexOf(const QList<V> &list, const U &u, qsizetype from) noexcept; template <typename V, typename U> friend qsizetype QtPrivate::lastIndexOf(const QList<V> &list, const U &u, qsizetype from) noexcept; + // This alias prevents the QtPrivate namespace from being exposed into the docs. + template <typename InputIterator> + using if_input_iterator = QtPrivate::IfIsInputIterator<InputIterator>; public: using Type = T; @@ -109,8 +118,7 @@ public: public: using difference_type = qsizetype; using value_type = T; - // libstdc++ shipped with gcc < 11 does not have a fix for defect LWG 3346 -#if __cplusplus >= 202002L && (!defined(_GLIBCXX_RELEASE) || _GLIBCXX_RELEASE >= 11) +#ifdef QT_COMPILER_HAS_LWG3346 using iterator_concept = std::contiguous_iterator_tag; using element_type = value_type; #endif @@ -180,8 +188,7 @@ public: public: using difference_type = qsizetype; using value_type = T; - // libstdc++ shipped with gcc < 11 does not have a fix for defect LWG 3346 -#if __cplusplus >= 202002L && (!defined(_GLIBCXX_RELEASE) || _GLIBCXX_RELEASE >= 11) +#ifdef QT_COMPILER_HAS_LWG3346 using iterator_concept = std::contiguous_iterator_tag; using element_type = const value_type; #endif @@ -252,6 +259,14 @@ private: const std::less<const T*> less = {}; return !less(d->end(), i.i) && !less(i.i, d->begin()); } + + void verify([[maybe_unused]] qsizetype pos = 0, [[maybe_unused]] qsizetype n = 1) const + { + Q_ASSERT(pos >= 0); + Q_ASSERT(pos <= size()); + Q_ASSERT(n >= 0); + Q_ASSERT(n <= size() - pos); + } public: QList(DataPointer dd) noexcept : d(dd) @@ -261,20 +276,20 @@ public: public: QList() = default; explicit QList(qsizetype size) - : d(Data::allocate(size)) + : d(size) { if (size) d->appendInitialize(size); } QList(qsizetype size, parameter_type t) - : d(Data::allocate(size)) + : d(size) { if (size) d->copyAppend(size, t); } inline QList(std::initializer_list<T> args) - : d(Data::allocate(args.size())) + : d(qsizetype(args.size())) { if (args.size()) d->copyAppend(args.begin(), args.end()); @@ -282,12 +297,10 @@ public: QList<T> &operator=(std::initializer_list<T> args) { - d = DataPointer(Data::allocate(args.size())); - if (args.size()) - d->copyAppend(args.begin(), args.end()); - return *this; + return assign(args); } - template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator> = true> + + template <typename InputIterator, if_input_iterator<InputIterator> = true> QList(InputIterator i1, InputIterator i2) { if constexpr (!std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::forward_iterator_tag>) { @@ -295,7 +308,7 @@ public: } else { const auto distance = std::distance(i1, i2); if (distance) { - d = DataPointer(Data::allocate(distance)); + d = DataPointer(qsizetype(distance)); // appendIteratorRange can deal with contiguous iterators on its own, // this is an optimization for C++17 code. if constexpr (std::is_same_v<std::decay_t<InputIterator>, iterator> || @@ -313,6 +326,13 @@ public: inline explicit QList(const String &str) { append(str); } + QList(qsizetype size, Qt::Initialization) + : d(size) + { + if (size) + d->appendUninitialized(size); + } + // compiler-generated special member functions are fine! void swap(QList &other) noexcept { d.swap(other.d); } @@ -393,6 +413,12 @@ public: if (size > this->size()) d->copyAppend(size - this->size(), c); } + void resizeForOverwrite(qsizetype size) + { + resize_internal(size); + if (size > this->size()) + d->appendUninitialized(size); + } inline qsizetype capacity() const { return qsizetype(d->constAllocatedCapacity()); } void reserve(qsizetype size); @@ -411,7 +437,7 @@ public: return; if (d->needsDetach()) { // must allocate memory - DataPointer detached(Data::allocate(d.allocatedCapacity())); + DataPointer detached(d.allocatedCapacity()); d.swap(detached); } else { d->truncate(0); @@ -488,6 +514,19 @@ public: } } + QList &assign(qsizetype n, parameter_type t) + { + Q_ASSERT(n >= 0); + return fill(t, n); + } + + template <typename InputIterator, if_input_iterator<InputIterator> = true> + QList &assign(InputIterator first, InputIterator last) + { d.assign(first, last); return *this; } + + QList &assign(std::initializer_list<T> l) + { return assign(l.begin(), l.end()); } + template <typename ...Args> iterator emplace(const_iterator before, Args&&... args) { @@ -615,27 +654,13 @@ public: QList<T> mid(qsizetype pos, qsizetype len = -1) const; QList<T> first(qsizetype n) const - { - Q_ASSERT(size_t(n) <= size_t(size())); - return QList<T>(begin(), begin() + n); - } + { verify(0, n); return QList<T>(begin(), begin() + n); } QList<T> last(qsizetype n) const - { - Q_ASSERT(size_t(n) <= size_t(size())); - return QList<T>(end() - n, end()); - } + { verify(0, n); return QList<T>(end() - n, end()); } QList<T> sliced(qsizetype pos) const - { - Q_ASSERT(size_t(pos) <= size_t(size())); - return QList<T>(begin() + pos, end()); - } + { verify(pos, 0); return QList<T>(begin() + pos, end()); } QList<T> sliced(qsizetype pos, qsizetype n) const - { - Q_ASSERT(size_t(pos) <= size_t(size())); - Q_ASSERT(n >= 0); - Q_ASSERT(pos + n <= size()); - return QList<T>(begin() + pos, begin() + pos + n); - } + { verify(pos, n); return QList<T>(begin() + pos, begin() + pos + n); } T value(qsizetype i) const { return value(i, T()); } T value(qsizetype i, parameter_type defaultValue) const; @@ -665,6 +690,10 @@ public: inline reference back() { return last(); } inline const_reference back() const noexcept { return last(); } void shrink_to_fit() { squeeze(); } + static qsizetype max_size() noexcept + { + return Data::max_size(); + } // comfort QList<T> &operator+=(const QList<T> &l) { append(l); return *this; } @@ -735,7 +764,7 @@ void QList<T>::reserve(qsizetype asize) } } - DataPointer detached(Data::allocate(qMax(asize, size()))); + DataPointer detached(qMax(asize, size())); detached->copyAppend(d->begin(), d->end()); if (detached.d_ptr()) detached->setFlag(Data::CapacityReserved); @@ -749,7 +778,7 @@ inline void QList<T>::squeeze() return; if (d->needsDetach() || size() < capacity()) { // must allocate memory - DataPointer detached(Data::allocate(size())); + DataPointer detached(size()); if (size()) { if (d.needsDetach()) detached->copyAppend(d.data(), d.data() + d.size); @@ -878,7 +907,7 @@ inline QList<T> &QList<T>::fill(parameter_type t, qsizetype newSize) newSize = size(); if (d->needsDetach() || newSize > capacity()) { // must allocate memory - DataPointer detached(Data::allocate(d->detachCapacity(newSize))); + DataPointer detached(d->detachCapacity(newSize)); detached->copyAppend(newSize, t); d.swap(detached); } else { @@ -960,7 +989,7 @@ inline QList<T> QList<T>::mid(qsizetype pos, qsizetype len) const } // Allocate memory - DataPointer copied(Data::allocate(l)); + DataPointer copied(l); copied->copyAppend(data() + p, data() + p + l); return copied; } |