diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-05-26 13:30:04 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-07-06 21:30:54 +0200 |
commit | ded37aedc91f8b9460da62baaa4d0e651f19eada (patch) | |
tree | 05b0a28fa6593806d5012962c92916718d14277d /src/corelib | |
parent | 0341bf2e979264b9f7606bd57e594a41c27a38cc (diff) | |
download | qtbase-ded37aedc91f8b9460da62baaa4d0e651f19eada.tar.gz |
Implement a proper iterator for QTypedArrayData
This avoids ambiguities in our API when someone e.g. writes
vector.insert(0, ...).
It requires a slight workaround in qlalr, where std::search()
for libc++ doesn't like that our difference_type is qsizetype.
Change-Id: I40aa1040781ffbdd12d04410078207969b3bde53
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/tools/qarraydata.h | 68 |
1 files changed, 66 insertions, 2 deletions
diff --git a/src/corelib/tools/qarraydata.h b/src/corelib/tools/qarraydata.h index b66a5add04..c1f9656707 100644 --- a/src/corelib/tools/qarraydata.h +++ b/src/corelib/tools/qarraydata.h @@ -133,8 +133,72 @@ template <class T> struct QTypedArrayData : QArrayData { - typedef T* iterator; - typedef const T* const_iterator; + class iterator { + T *i = nullptr; + public: + typedef std::random_access_iterator_tag iterator_category; + typedef qsizetype difference_type; + typedef T value_type; + typedef T *pointer; + typedef T &reference; + + inline constexpr iterator() = default; + inline iterator(T *n) : i(n) {} + inline T &operator*() const { return *i; } + inline T *operator->() const { return i; } + inline T &operator[](qsizetype j) const { return *(i + j); } + inline constexpr bool operator==(iterator o) const { return i == o.i; } + inline constexpr bool operator!=(iterator o) const { return i != o.i; } + inline constexpr bool operator<(iterator other) const { return i < other.i; } + inline constexpr bool operator<=(iterator other) const { return i <= other.i; } + inline constexpr bool operator>(iterator other) const { return i > other.i; } + inline constexpr bool operator>=(iterator other) const { return i >= other.i; } + inline iterator &operator++() { ++i; return *this; } + inline iterator operator++(int) { T *n = i; ++i; return n; } + inline iterator &operator--() { i--; return *this; } + inline iterator operator--(int) { T *n = i; i--; return n; } + inline iterator &operator+=(qsizetype j) { i+=j; return *this; } + inline iterator &operator-=(qsizetype j) { i-=j; return *this; } + inline iterator operator+(qsizetype j) const { return iterator(i+j); } + inline iterator operator-(qsizetype j) const { return iterator(i-j); } + friend inline iterator operator+(qsizetype j, iterator k) { return k + j; } + inline qsizetype operator-(iterator j) const { return i - j.i; } + inline operator T*() const { return i; } + }; + + class const_iterator { + const T *i = nullptr; + public: + typedef std::random_access_iterator_tag iterator_category; + typedef qsizetype difference_type; + typedef T value_type; + typedef const T *pointer; + typedef const T &reference; + + inline constexpr const_iterator() = default; + inline const_iterator(const T *n) : i(n) {} + inline constexpr const_iterator(iterator o): i(o) {} + inline const T &operator*() const { return *i; } + inline const T *operator->() const { return i; } + inline const T &operator[](qsizetype j) const { return *(i + j); } + inline bool operator==(const_iterator o) const { return i == o.i; } + inline bool operator!=(const_iterator o) const { return i != o.i; } + inline bool operator<(const_iterator other) const { return i < other.i; } + inline bool operator<=(const_iterator other) const { return i <= other.i; } + inline bool operator>(const_iterator other) const { return i > other.i; } + inline bool operator>=(const_iterator other) const { return i >= other.i; } + inline const_iterator &operator++() { ++i; return *this; } + inline const_iterator operator++(int) { const T *n = i; ++i; return n; } + inline const_iterator &operator--() { i--; return *this; } + inline const_iterator operator--(int) { const T *n = i; i--; return n; } + inline const_iterator &operator+=(qsizetype j) { i+=j; return *this; } + inline const_iterator &operator-=(qsizetype j) { i-=j; return *this; } + inline const_iterator operator+(qsizetype j) const { return const_iterator(i+j); } + inline const_iterator operator-(qsizetype j) const { return const_iterator(i-j); } + friend inline const_iterator operator+(qsizetype j, const_iterator k) { return k + j; } + inline qsizetype operator-(const_iterator j) const { return i - j.i; } + inline operator const T*() const { return i; } + }; class AlignmentDummy { QArrayData header; T data; }; |