diff options
author | Ahmad Samir <a.samirh78@gmail.com> | 2023-07-06 15:05:08 +0300 |
---|---|---|
committer | Ahmad Samir <a.samirh78@gmail.com> | 2023-09-28 23:45:24 +0300 |
commit | 2fd9735e7aee9f6c6c014c3e4d33b420f7ef5375 (patch) | |
tree | c6dd34ead9a2a9ee8d5520c28e09fccf92d4e660 /src/corelib/tools/qlist.h | |
parent | 016addc201b29696077b8d397c988c3817eaa429 (diff) |
Add a verify() method to all sequential containers
A helper method encasuplating the asserts related to index into the
container and length, modelled after the QVLA::verify().
`pos <= size` is OK because if pos == size, the e.g. sliced()'ed
container is just going to be empty.
Normalize how verify() is used, the first arg is an index and the second
a length.
This method is constexpr even in QString/QByteArray merely for
consistency with similar methods in other string classes (this
necessitates using `d.size` in verify() in QString/QBA because size()
isn't constexpr).
Change-Id: I90e3c56d76c802259297a06d11d46ee342a1daf2
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qlist.h')
-rw-r--r-- | src/corelib/tools/qlist.h | 30 |
1 files changed, 12 insertions, 18 deletions
diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index a5512ddf0d..eafdfce140 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -259,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) @@ -633,27 +641,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; |