diff options
author | Dennis Oberst <dennis.oberst@qt.io> | 2023-04-03 17:16:18 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2023-05-16 20:09:39 +0200 |
commit | bbbe5f45c4d354ef977d4e09459ef5ae4d6db0da (patch) | |
tree | 510a79687d3b5201465b65a1129d4238147f9775 /src/corelib/tools/qlist.h | |
parent | d8bdb66e82b0a248d4bbc9f3898f3d8b61620dbd (diff) |
QList: add STL-style assign()
Implemented assign() methods for QList to align with the criteria of
std::vector, addressing the previously missing functionality.
Reference:
https://en.cppreference.com/w/cpp/container/vector/assign
[ChangeLog][QtCore][QList] Added assign().
Fixes: QTBUG-106196
Change-Id: I5df8689c020dafde68d2cd7d09c769744fa8f137
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/corelib/tools/qlist.h')
-rw-r--r-- | src/corelib/tools/qlist.h | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index d61b90503a..5f5dad583e 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -16,6 +16,8 @@ #include <initializer_list> #include <type_traits> +class tst_QList; + QT_BEGIN_NAMESPACE namespace QtPrivate { @@ -75,10 +77,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; @@ -287,7 +294,8 @@ public: d->copyAppend(args.begin(), args.end()); return *this; } - 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>) { @@ -488,6 +496,19 @@ public: } } + void assign(qsizetype n, parameter_type t) + { + Q_ASSERT(n >= 0); + fill(t, n); + } + + template <typename InputIterator, if_input_iterator<InputIterator> = true> + void assign(InputIterator first, InputIterator last) + { d.assign(first, last); } + + void assign(std::initializer_list<T> l) + { assign(l.begin(), l.end()); } + template <typename ...Args> iterator emplace(const_iterator before, Args&&... args) { |