| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QITERATOR_H |
| 5 | #define QITERATOR_H |
| 6 | |
| 7 | #include <QtCore/qglobal.h> |
| 8 | #include <QtCore/qcontainertools_impl.h> |
| 9 | |
| 10 | QT_BEGIN_NAMESPACE |
| 11 | |
| 12 | #if !defined(QT_NO_JAVA_STYLE_ITERATORS) |
| 13 | |
| 14 | #ifdef Q_QDOC |
| 15 | #define Q_DISABLE_BACKWARD_ITERATOR |
| 16 | #else |
| 17 | #define Q_DISABLE_BACKWARD_ITERATOR \ |
| 18 | template<typename It = decltype(i), QtPrivate::IfIteratorCanMoveBackwards<It> = true> |
| 19 | #endif |
| 20 | |
| 21 | #define Q_DECLARE_SEQUENTIAL_ITERATOR(C) \ |
| 22 | \ |
| 23 | template <class T> \ |
| 24 | class Q##C##Iterator \ |
| 25 | { \ |
| 26 | typedef typename Q##C<T>::const_iterator const_iterator; \ |
| 27 | Q##C<T> c; \ |
| 28 | const_iterator i; \ |
| 29 | public: \ |
| 30 | inline Q##C##Iterator(const Q##C<T> &container) \ |
| 31 | : c(container), i(c.constBegin()) {} \ |
| 32 | inline Q##C##Iterator &operator=(const Q##C<T> &container) \ |
| 33 | { c = container; i = c.constBegin(); return *this; } \ |
| 34 | inline void toFront() { i = c.constBegin(); } \ |
| 35 | inline void toBack() { i = c.constEnd(); } \ |
| 36 | inline bool hasNext() const { return i != c.constEnd(); } \ |
| 37 | inline const T &next() { return *i++; } \ |
| 38 | inline const T &peekNext() const { return *i; } \ |
| 39 | Q_DISABLE_BACKWARD_ITERATOR \ |
| 40 | inline bool hasPrevious() const { return i != c.constBegin(); } \ |
| 41 | Q_DISABLE_BACKWARD_ITERATOR \ |
| 42 | inline const T &previous() { return *--i; } \ |
| 43 | Q_DISABLE_BACKWARD_ITERATOR \ |
| 44 | inline const T &peekPrevious() const { const_iterator p = i; return *--p; } \ |
| 45 | inline bool findNext(const T &t) \ |
| 46 | { while (i != c.constEnd()) if (*i++ == t) return true; return false; } \ |
| 47 | Q_DISABLE_BACKWARD_ITERATOR \ |
| 48 | inline bool findPrevious(const T &t) \ |
| 49 | { while (i != c.constBegin()) if (*(--i) == t) return true; \ |
| 50 | return false; } \ |
| 51 | }; |
| 52 | |
| 53 | #define Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(C) \ |
| 54 | \ |
| 55 | template <class T> \ |
| 56 | class QMutable##C##Iterator \ |
| 57 | { \ |
| 58 | typedef typename Q##C<T>::iterator iterator; \ |
| 59 | typedef typename Q##C<T>::const_iterator const_iterator; \ |
| 60 | Q##C<T> *c; \ |
| 61 | iterator i, n; \ |
| 62 | inline bool item_exists() const { return const_iterator(n) != c->constEnd(); } \ |
| 63 | public: \ |
| 64 | inline QMutable##C##Iterator(Q##C<T> &container) \ |
| 65 | : c(&container) \ |
| 66 | { i = c->begin(); n = c->end(); } \ |
| 67 | inline QMutable##C##Iterator &operator=(Q##C<T> &container) \ |
| 68 | { c = &container; i = c->begin(); n = c->end(); return *this; } \ |
| 69 | inline void toFront() { i = c->begin(); n = c->end(); } \ |
| 70 | inline void toBack() { i = c->end(); n = i; } \ |
| 71 | inline bool hasNext() const { return c->constEnd() != const_iterator(i); } \ |
| 72 | inline T &next() { n = i++; return *n; } \ |
| 73 | inline T &peekNext() const { return *i; } \ |
| 74 | Q_DISABLE_BACKWARD_ITERATOR \ |
| 75 | inline bool hasPrevious() const { return c->constBegin() != const_iterator(i); } \ |
| 76 | Q_DISABLE_BACKWARD_ITERATOR \ |
| 77 | inline T &previous() { n = --i; return *n; } \ |
| 78 | Q_DISABLE_BACKWARD_ITERATOR \ |
| 79 | inline T &peekPrevious() const { iterator p = i; return *--p; } \ |
| 80 | inline void remove() \ |
| 81 | { if (c->constEnd() != const_iterator(n)) { i = c->erase(n); n = c->end(); } } \ |
| 82 | inline void setValue(const T &t) const { if (c->constEnd() != const_iterator(n)) *n = t; } \ |
| 83 | inline T &value() { Q_ASSERT(item_exists()); return *n; } \ |
| 84 | inline const T &value() const { Q_ASSERT(item_exists()); return *n; } \ |
| 85 | inline void insert(const T &t) { n = i = c->insert(i, t); ++i; } \ |
| 86 | inline bool findNext(const T &t) \ |
| 87 | { while (c->constEnd() != const_iterator(n = i)) if (*i++ == t) return true; return false; } \ |
| 88 | Q_DISABLE_BACKWARD_ITERATOR \ |
| 89 | inline bool findPrevious(const T &t) \ |
| 90 | { while (c->constBegin() != const_iterator(i)) if (*(n = --i) == t) return true; \ |
| 91 | n = c->end(); return false; } \ |
| 92 | }; |
| 93 | |
| 94 | #define Q_DECLARE_ASSOCIATIVE_ITERATOR(C) \ |
| 95 | \ |
| 96 | template <class Key, class T> \ |
| 97 | class Q##C##Iterator \ |
| 98 | { \ |
| 99 | typedef typename Q##C<Key,T>::const_iterator const_iterator; \ |
| 100 | Q##C<Key,T> c; \ |
| 101 | const_iterator i, n; \ |
| 102 | inline bool item_exists() const { return n != c.constEnd(); } \ |
| 103 | public: \ |
| 104 | typedef const_iterator Item; \ |
| 105 | inline Q##C##Iterator(const Q##C<Key,T> &container) \ |
| 106 | : c(container), i(c.constBegin()), n(c.constEnd()) {} \ |
| 107 | inline Q##C##Iterator &operator=(const Q##C<Key,T> &container) \ |
| 108 | { c = container; i = c.constBegin(); n = c.constEnd(); return *this; } \ |
| 109 | inline void toFront() { i = c.constBegin(); n = c.constEnd(); } \ |
| 110 | inline void toBack() { i = c.constEnd(); n = c.constEnd(); } \ |
| 111 | inline bool hasNext() const { return i != c.constEnd(); } \ |
| 112 | inline Item next() { n = i++; return n; } \ |
| 113 | inline Item peekNext() const { return i; } \ |
| 114 | Q_DISABLE_BACKWARD_ITERATOR \ |
| 115 | inline bool hasPrevious() const { return i != c.constBegin(); } \ |
| 116 | Q_DISABLE_BACKWARD_ITERATOR \ |
| 117 | inline Item previous() { n = --i; return n; } \ |
| 118 | Q_DISABLE_BACKWARD_ITERATOR \ |
| 119 | inline Item peekPrevious() const { const_iterator p = i; return --p; } \ |
| 120 | inline const T &value() const { Q_ASSERT(item_exists()); return *n; } \ |
| 121 | inline const Key &key() const { Q_ASSERT(item_exists()); return n.key(); } \ |
| 122 | inline bool findNext(const T &t) \ |
| 123 | { while ((n = i) != c.constEnd()) if (*i++ == t) return true; return false; } \ |
| 124 | Q_DISABLE_BACKWARD_ITERATOR \ |
| 125 | inline bool findPrevious(const T &t) \ |
| 126 | { while (i != c.constBegin()) if (*(n = --i) == t) return true; \ |
| 127 | n = c.constEnd(); return false; } \ |
| 128 | }; |
| 129 | |
| 130 | #define Q_DECLARE_MUTABLE_ASSOCIATIVE_ITERATOR(C) \ |
| 131 | \ |
| 132 | template <class Key, class T> \ |
| 133 | class QMutable##C##Iterator \ |
| 134 | { \ |
| 135 | typedef typename Q##C<Key,T>::iterator iterator; \ |
| 136 | typedef typename Q##C<Key,T>::const_iterator const_iterator; \ |
| 137 | Q##C<Key,T> *c; \ |
| 138 | iterator i, n; \ |
| 139 | inline bool item_exists() const { return const_iterator(n) != c->constEnd(); } \ |
| 140 | public: \ |
| 141 | typedef iterator Item; \ |
| 142 | inline QMutable##C##Iterator(Q##C<Key,T> &container) \ |
| 143 | : c(&container) \ |
| 144 | { i = c->begin(); n = c->end(); } \ |
| 145 | inline QMutable##C##Iterator &operator=(Q##C<Key,T> &container) \ |
| 146 | { c = &container; i = c->begin(); n = c->end(); return *this; } \ |
| 147 | inline void toFront() { i = c->begin(); n = c->end(); } \ |
| 148 | inline void toBack() { i = c->end(); n = c->end(); } \ |
| 149 | inline bool hasNext() const { return const_iterator(i) != c->constEnd(); } \ |
| 150 | inline Item next() { n = i++; return n; } \ |
| 151 | inline Item peekNext() const { return i; } \ |
| 152 | Q_DISABLE_BACKWARD_ITERATOR \ |
| 153 | inline bool hasPrevious() const { return const_iterator(i) != c->constBegin(); } \ |
| 154 | Q_DISABLE_BACKWARD_ITERATOR \ |
| 155 | inline Item previous() { n = --i; return n; } \ |
| 156 | Q_DISABLE_BACKWARD_ITERATOR \ |
| 157 | inline Item peekPrevious() const { iterator p = i; return --p; } \ |
| 158 | inline void remove() \ |
| 159 | { if (const_iterator(n) != c->constEnd()) { i = c->erase(n); n = c->end(); } } \ |
| 160 | inline void setValue(const T &t) { if (const_iterator(n) != c->constEnd()) *n = t; } \ |
| 161 | inline T &value() { Q_ASSERT(item_exists()); return *n; } \ |
| 162 | inline const T &value() const { Q_ASSERT(item_exists()); return *n; } \ |
| 163 | inline const Key &key() const { Q_ASSERT(item_exists()); return n.key(); } \ |
| 164 | inline bool findNext(const T &t) \ |
| 165 | { while (const_iterator(n = i) != c->constEnd()) if (*i++ == t) return true; return false; } \ |
| 166 | Q_DISABLE_BACKWARD_ITERATOR \ |
| 167 | inline bool findPrevious(const T &t) \ |
| 168 | { while (const_iterator(i) != c->constBegin()) if (*(n = --i) == t) return true; \ |
| 169 | n = c->end(); return false; } \ |
| 170 | }; |
| 171 | |
| 172 | #define Q_DECLARE_ASSOCIATIVE_FORWARD_ITERATOR(C) \ |
| 173 | \ |
| 174 | template <class Key, class T> \ |
| 175 | class Q##C##Iterator \ |
| 176 | { \ |
| 177 | typedef typename Q##C<Key,T>::const_iterator const_iterator; \ |
| 178 | Q##C<Key,T> c; \ |
| 179 | const_iterator i, n; \ |
| 180 | inline bool item_exists() const { return n != c.constEnd(); } \ |
| 181 | public: \ |
| 182 | typedef const_iterator Item; \ |
| 183 | inline Q##C##Iterator(const Q##C<Key,T> &container) \ |
| 184 | : c(container), i(c.constBegin()), n(c.constEnd()) {} \ |
| 185 | inline Q##C##Iterator &operator=(const Q##C<Key,T> &container) \ |
| 186 | { c = container; i = c.constBegin(); n = c.constEnd(); return *this; } \ |
| 187 | inline void toFront() { i = c.constBegin(); n = c.constEnd(); } \ |
| 188 | inline void toBack() { i = c.constEnd(); n = c.constEnd(); } \ |
| 189 | inline bool hasNext() const { return i != c.constEnd(); } \ |
| 190 | inline Item next() { n = i++; return n; } \ |
| 191 | inline Item peekNext() const { return i; } \ |
| 192 | inline const T &value() const { Q_ASSERT(item_exists()); return *n; } \ |
| 193 | inline const Key &key() const { Q_ASSERT(item_exists()); return n.key(); } \ |
| 194 | inline bool findNext(const T &t) \ |
| 195 | { while ((n = i) != c.constEnd()) if (*i++ == t) return true; return false; } \ |
| 196 | }; |
| 197 | |
| 198 | #define Q_DECLARE_MUTABLE_ASSOCIATIVE_FORWARD_ITERATOR(C) \ |
| 199 | \ |
| 200 | template <class Key, class T> \ |
| 201 | class QMutable##C##Iterator \ |
| 202 | { \ |
| 203 | typedef typename Q##C<Key,T>::iterator iterator; \ |
| 204 | typedef typename Q##C<Key,T>::const_iterator const_iterator; \ |
| 205 | Q##C<Key,T> *c; \ |
| 206 | iterator i, n; \ |
| 207 | inline bool item_exists() const { return const_iterator(n) != c->constEnd(); } \ |
| 208 | public: \ |
| 209 | typedef iterator Item; \ |
| 210 | inline QMutable##C##Iterator(Q##C<Key,T> &container) \ |
| 211 | : c(&container) \ |
| 212 | { i = c->begin(); n = c->end(); } \ |
| 213 | inline QMutable##C##Iterator &operator=(Q##C<Key,T> &container) \ |
| 214 | { c = &container; i = c->begin(); n = c->end(); return *this; } \ |
| 215 | inline void toFront() { i = c->begin(); n = c->end(); } \ |
| 216 | inline void toBack() { i = c->end(); n = c->end(); } \ |
| 217 | inline bool hasNext() const { return const_iterator(i) != c->constEnd(); } \ |
| 218 | inline Item next() { n = i++; return n; } \ |
| 219 | inline Item peekNext() const { return i; } \ |
| 220 | inline void remove() \ |
| 221 | { if (const_iterator(n) != c->constEnd()) { i = c->erase(n); n = c->end(); } } \ |
| 222 | inline void setValue(const T &t) { if (const_iterator(n) != c->constEnd()) *n = t; } \ |
| 223 | inline T &value() { Q_ASSERT(item_exists()); return *n; } \ |
| 224 | inline const T &value() const { Q_ASSERT(item_exists()); return *n; } \ |
| 225 | inline const Key &key() const { Q_ASSERT(item_exists()); return n.key(); } \ |
| 226 | inline bool findNext(const T &t) \ |
| 227 | { while (const_iterator(n = i) != c->constEnd()) if (*i++ == t) return true; return false; } \ |
| 228 | }; |
| 229 | |
| 230 | |
| 231 | #else // QT_NO_JAVA_STYLE_ITERATORS |
| 232 | #define Q_DECLARE_SEQUENTIAL_ITERATOR(C) |
| 233 | #define Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(C) |
| 234 | #define Q_DECLARE_ASSOCIATIVE_ITERATOR(C) |
| 235 | #define Q_DECLARE_MUTABLE_ASSOCIATIVE_ITERATOR(C) |
| 236 | #define Q_DECLARE_ASSOCIATIVE_FORWARD_ITERATOR(C) |
| 237 | #define Q_DECLARE_MUTABLE_ASSOCIATIVE_FORWARD_ITERATOR(C) |
| 238 | #endif // QT_NO_JAVA_STYLE_ITERATORS |
| 239 | |
| 240 | template<typename Key, typename T, class Iterator> |
| 241 | class QKeyValueIterator |
| 242 | { |
| 243 | public: |
| 244 | typedef typename Iterator::iterator_category iterator_category; |
| 245 | typedef typename Iterator::difference_type difference_type; |
| 246 | typedef std::pair<Key, T> value_type; |
| 247 | typedef const value_type &reference; |
| 248 | |
| 249 | QKeyValueIterator() = default; |
| 250 | constexpr explicit QKeyValueIterator(Iterator o) noexcept(std::is_nothrow_move_constructible<Iterator>::value) |
| 251 | : i(std::move(o)) {} |
| 252 | |
| 253 | std::pair<Key, T> operator*() const { |
| 254 | return std::pair<Key, T>(i.key(), i.value()); |
| 255 | } |
| 256 | |
| 257 | using pointer = QtPrivate::ArrowProxy<value_type>; |
| 258 | |
| 259 | pointer operator->() const { |
| 260 | return pointer{std::pair<Key, T>(i.key(), i.value())}; |
| 261 | } |
| 262 | |
| 263 | friend bool operator==(QKeyValueIterator lhs, QKeyValueIterator rhs) noexcept { return lhs.i == rhs.i; } |
| 264 | friend bool operator!=(QKeyValueIterator lhs, QKeyValueIterator rhs) noexcept { return lhs.i != rhs.i; } |
| 265 | |
| 266 | inline QKeyValueIterator &operator++() { ++i; return *this; } |
| 267 | inline QKeyValueIterator operator++(int) { return QKeyValueIterator(i++);} |
| 268 | inline QKeyValueIterator &operator--() { --i; return *this; } |
| 269 | inline QKeyValueIterator operator--(int) { return QKeyValueIterator(i--); } |
| 270 | Iterator base() const { return i; } |
| 271 | |
| 272 | private: |
| 273 | Iterator i; |
| 274 | }; |
| 275 | |
| 276 | namespace QtPrivate { |
| 277 | |
| 278 | template <typename Map> |
| 279 | class QKeyValueRangeStorage |
| 280 | { |
| 281 | protected: |
| 282 | Map m_map; |
| 283 | public: |
| 284 | explicit QKeyValueRangeStorage(const Map &map) : m_map(map) {} |
| 285 | explicit QKeyValueRangeStorage(Map &&map) : m_map(std::move(map)) {} |
| 286 | }; |
| 287 | |
| 288 | template <typename Map> |
| 289 | class QKeyValueRangeStorage<Map &> |
| 290 | { |
| 291 | protected: |
| 292 | Map &m_map; |
| 293 | public: |
| 294 | explicit QKeyValueRangeStorage(Map &map) : m_map(map) {} |
| 295 | }; |
| 296 | |
| 297 | template <typename Map> |
| 298 | class QKeyValueRange : public QKeyValueRangeStorage<Map> |
| 299 | { |
| 300 | public: |
| 301 | using QKeyValueRangeStorage<Map>::QKeyValueRangeStorage; |
| 302 | auto begin() { return this->m_map.keyValueBegin(); } |
| 303 | auto begin() const { return this->m_map.keyValueBegin(); } |
| 304 | auto end() { return this->m_map.keyValueEnd(); } |
| 305 | auto end() const { return this->m_map.keyValueEnd(); } |
| 306 | }; |
| 307 | |
| 308 | template <typename Map> |
| 309 | QKeyValueRange(Map &) -> QKeyValueRange<Map &>; |
| 310 | |
| 311 | template <typename Map, std::enable_if_t<!std::is_reference_v<Map>, bool> = false> |
| 312 | QKeyValueRange(Map &&) -> QKeyValueRange<std::remove_const_t<Map>>; |
| 313 | |
| 314 | } // namespace QtPrivate |
| 315 | |
| 316 | |
| 317 | QT_END_NAMESPACE |
| 318 | |
| 319 | #endif // QITERATOR_H |
| 320 | |