Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-18 08:32:27

0001 // Copyright (C) 2016 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 
0004 #ifndef QITERATOR_H
0005 #define QITERATOR_H
0006 
0007 #include <QtCore/qglobal.h>
0008 #include <QtCore/qcontainertools_impl.h>
0009 
0010 #ifdef __cpp_lib_ranges
0011 #include <ranges>
0012 #endif
0013 
0014 QT_BEGIN_NAMESPACE
0015 
0016 #if !defined(QT_NO_JAVA_STYLE_ITERATORS)
0017 
0018 #ifdef Q_QDOC
0019 #define Q_DISABLE_BACKWARD_ITERATOR
0020 #else
0021 #define Q_DISABLE_BACKWARD_ITERATOR \
0022         template<typename It = decltype(i), QtPrivate::IfIteratorCanMoveBackwards<It> = true>
0023 #endif
0024 
0025 #define Q_DECLARE_SEQUENTIAL_ITERATOR(C) \
0026 \
0027 template <class T> \
0028 class Q##C##Iterator \
0029 { \
0030     typedef typename Q##C<T>::const_iterator const_iterator; \
0031     Q##C<T> c; \
0032     const_iterator i; \
0033 public: \
0034     inline Q##C##Iterator(const Q##C<T> &container) \
0035         : c(container), i(c.constBegin()) {} \
0036     inline Q##C##Iterator &operator=(const Q##C<T> &container) \
0037     { c = container; i = c.constBegin(); return *this; } \
0038     inline void toFront() { i = c.constBegin(); } \
0039     inline void toBack() { i = c.constEnd(); } \
0040     inline bool hasNext() const { return i != c.constEnd(); } \
0041     inline const T &next() { return *i++; } \
0042     inline const T &peekNext() const { return *i; } \
0043     Q_DISABLE_BACKWARD_ITERATOR \
0044     inline bool hasPrevious() const { return i != c.constBegin(); } \
0045     Q_DISABLE_BACKWARD_ITERATOR \
0046     inline const T &previous() { return *--i; } \
0047     Q_DISABLE_BACKWARD_ITERATOR \
0048     inline const T &peekPrevious() const { const_iterator p = i; return *--p; } \
0049     inline bool findNext(const T &t) \
0050     { while (i != c.constEnd()) if (*i++ == t) return true; return false; } \
0051     Q_DISABLE_BACKWARD_ITERATOR \
0052     inline bool findPrevious(const T &t) \
0053     { while (i != c.constBegin()) if (*(--i) == t) return true; \
0054       return false;  } \
0055 };
0056 
0057 #define Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(C) \
0058 \
0059 template <class T> \
0060 class QMutable##C##Iterator \
0061 { \
0062     typedef typename Q##C<T>::iterator iterator; \
0063     typedef typename Q##C<T>::const_iterator const_iterator; \
0064     Q##C<T> *c; \
0065     iterator i, n; \
0066     inline bool item_exists() const { return const_iterator(n) != c->constEnd(); } \
0067 public: \
0068     inline QMutable##C##Iterator(Q##C<T> &container) \
0069         : c(&container) \
0070     { i = c->begin(); n = c->end(); } \
0071     inline QMutable##C##Iterator &operator=(Q##C<T> &container) \
0072     { c = &container; i = c->begin(); n = c->end(); return *this; } \
0073     inline void toFront() { i = c->begin(); n = c->end(); } \
0074     inline void toBack() { i = c->end(); n = i; } \
0075     inline bool hasNext() const { return c->constEnd() != const_iterator(i); } \
0076     inline T &next() { n = i++; return *n; } \
0077     inline T &peekNext() const { return *i; } \
0078     Q_DISABLE_BACKWARD_ITERATOR \
0079     inline bool hasPrevious() const { return c->constBegin() != const_iterator(i); } \
0080     Q_DISABLE_BACKWARD_ITERATOR \
0081     inline T &previous() { n = --i; return *n; } \
0082     Q_DISABLE_BACKWARD_ITERATOR \
0083     inline T &peekPrevious() const { iterator p = i; return *--p; } \
0084     inline void remove() \
0085     { if (c->constEnd() != const_iterator(n)) { i = c->erase(n); n = c->end(); } } \
0086     inline void setValue(const T &t) const { if (c->constEnd() != const_iterator(n)) *n = t; } \
0087     inline T &value() { Q_ASSERT(item_exists()); return *n; } \
0088     inline const T &value() const { Q_ASSERT(item_exists()); return *n; } \
0089     inline void insert(const T &t) { n = i = c->insert(i, t); ++i; } \
0090     inline bool findNext(const T &t) \
0091     { while (c->constEnd() != const_iterator(n = i)) if (*i++ == t) return true; return false; } \
0092     Q_DISABLE_BACKWARD_ITERATOR \
0093     inline bool findPrevious(const T &t) \
0094     { while (c->constBegin() != const_iterator(i)) if (*(n = --i) == t) return true; \
0095       n = c->end(); return false;  } \
0096 };
0097 
0098 #define Q_DECLARE_ASSOCIATIVE_ITERATOR(C) \
0099 \
0100 template <class Key, class T> \
0101 class Q##C##Iterator \
0102 { \
0103     typedef typename Q##C<Key,T>::const_iterator const_iterator; \
0104     Q##C<Key,T> c; \
0105     const_iterator i, n; \
0106     inline bool item_exists() const { return n != c.constEnd(); } \
0107 public: \
0108     typedef const_iterator Item; \
0109     inline Q##C##Iterator(const Q##C<Key,T> &container) \
0110         : c(container), i(c.constBegin()), n(c.constEnd()) {} \
0111     inline Q##C##Iterator &operator=(const Q##C<Key,T> &container) \
0112     { c = container; i = c.constBegin(); n = c.constEnd(); return *this; } \
0113     inline void toFront() { i = c.constBegin(); n = c.constEnd(); } \
0114     inline void toBack() { i = c.constEnd(); n = c.constEnd(); } \
0115     inline bool hasNext() const { return i != c.constEnd(); } \
0116     inline Item next() { n = i++; return n; } \
0117     inline Item peekNext() const { return i; } \
0118     Q_DISABLE_BACKWARD_ITERATOR \
0119     inline bool hasPrevious() const { return i != c.constBegin(); } \
0120     Q_DISABLE_BACKWARD_ITERATOR \
0121     inline Item previous() { n = --i; return n; } \
0122     Q_DISABLE_BACKWARD_ITERATOR \
0123     inline Item peekPrevious() const { const_iterator p = i; return --p; } \
0124     inline const T &value() const { Q_ASSERT(item_exists()); return *n; } \
0125     inline const Key &key() const { Q_ASSERT(item_exists()); return n.key(); } \
0126     inline bool findNext(const T &t) \
0127     { while ((n = i) != c.constEnd()) if (*i++ == t) return true; return false; } \
0128     Q_DISABLE_BACKWARD_ITERATOR \
0129     inline bool findPrevious(const T &t) \
0130     { while (i != c.constBegin()) if (*(n = --i) == t) return true; \
0131       n = c.constEnd(); return false; } \
0132 };
0133 
0134 #define Q_DECLARE_MUTABLE_ASSOCIATIVE_ITERATOR(C) \
0135 \
0136 template <class Key, class T> \
0137 class QMutable##C##Iterator \
0138 { \
0139     typedef typename Q##C<Key,T>::iterator iterator; \
0140     typedef typename Q##C<Key,T>::const_iterator const_iterator; \
0141     Q##C<Key,T> *c; \
0142     iterator i, n; \
0143     inline bool item_exists() const { return const_iterator(n) != c->constEnd(); } \
0144 public: \
0145     typedef iterator Item; \
0146     inline QMutable##C##Iterator(Q##C<Key,T> &container) \
0147         : c(&container) \
0148     { i = c->begin(); n = c->end(); } \
0149     inline QMutable##C##Iterator &operator=(Q##C<Key,T> &container) \
0150     { c = &container; i = c->begin(); n = c->end(); return *this; } \
0151     inline void toFront() { i = c->begin(); n = c->end(); } \
0152     inline void toBack() { i = c->end(); n = c->end(); } \
0153     inline bool hasNext() const { return const_iterator(i) != c->constEnd(); } \
0154     inline Item next() { n = i++; return n; } \
0155     inline Item peekNext() const { return i; } \
0156     Q_DISABLE_BACKWARD_ITERATOR \
0157     inline bool hasPrevious() const { return const_iterator(i) != c->constBegin(); } \
0158     Q_DISABLE_BACKWARD_ITERATOR \
0159     inline Item previous() { n = --i; return n; } \
0160     Q_DISABLE_BACKWARD_ITERATOR \
0161     inline Item peekPrevious() const { iterator p = i; return --p; } \
0162     inline void remove() \
0163     { if (const_iterator(n) != c->constEnd()) { i = c->erase(n); n = c->end(); } } \
0164     inline void setValue(const T &t) { if (const_iterator(n) != c->constEnd()) *n = t; } \
0165     inline T &value() { Q_ASSERT(item_exists()); return *n; } \
0166     inline const T &value() const { Q_ASSERT(item_exists()); return *n; } \
0167     inline const Key &key() const { Q_ASSERT(item_exists()); return n.key(); } \
0168     inline bool findNext(const T &t) \
0169     { while (const_iterator(n = i) != c->constEnd()) if (*i++ == t) return true; return false; } \
0170     Q_DISABLE_BACKWARD_ITERATOR \
0171     inline bool findPrevious(const T &t) \
0172     { while (const_iterator(i) != c->constBegin()) if (*(n = --i) == t) return true; \
0173       n = c->end(); return false; } \
0174 };
0175 
0176 #define Q_DECLARE_ASSOCIATIVE_FORWARD_ITERATOR(C) \
0177 \
0178 template <class Key, class T> \
0179 class Q##C##Iterator \
0180 { \
0181     typedef typename Q##C<Key,T>::const_iterator const_iterator; \
0182     Q##C<Key,T> c; \
0183     const_iterator i, n; \
0184     inline bool item_exists() const { return n != c.constEnd(); } \
0185 public: \
0186     typedef const_iterator Item; \
0187     inline Q##C##Iterator(const Q##C<Key,T> &container) \
0188         : c(container), i(c.constBegin()), n(c.constEnd()) {} \
0189     inline Q##C##Iterator &operator=(const Q##C<Key,T> &container) \
0190     { c = container; i = c.constBegin(); n = c.constEnd(); return *this; } \
0191     inline void toFront() { i = c.constBegin(); n = c.constEnd(); } \
0192     inline void toBack() { i = c.constEnd(); n = c.constEnd(); } \
0193     inline bool hasNext() const { return i != c.constEnd(); } \
0194     inline Item next() { n = i++; return n; } \
0195     inline Item peekNext() const { return i; } \
0196     inline const T &value() const { Q_ASSERT(item_exists()); return *n; } \
0197     inline const Key &key() const { Q_ASSERT(item_exists()); return n.key(); } \
0198     inline bool findNext(const T &t) \
0199     { while ((n = i) != c.constEnd()) if (*i++ == t) return true; return false; } \
0200 };
0201 
0202 #define Q_DECLARE_MUTABLE_ASSOCIATIVE_FORWARD_ITERATOR(C) \
0203 \
0204 template <class Key, class T> \
0205 class QMutable##C##Iterator \
0206 { \
0207     typedef typename Q##C<Key,T>::iterator iterator; \
0208     typedef typename Q##C<Key,T>::const_iterator const_iterator; \
0209     Q##C<Key,T> *c; \
0210     iterator i, n; \
0211     inline bool item_exists() const { return const_iterator(n) != c->constEnd(); } \
0212 public: \
0213     typedef iterator Item; \
0214     inline QMutable##C##Iterator(Q##C<Key,T> &container) \
0215         : c(&container) \
0216     { i = c->begin(); n = c->end(); } \
0217     inline QMutable##C##Iterator &operator=(Q##C<Key,T> &container) \
0218     { c = &container; i = c->begin(); n = c->end(); return *this; } \
0219     inline void toFront() { i = c->begin(); n = c->end(); } \
0220     inline void toBack() { i = c->end(); n = c->end(); } \
0221     inline bool hasNext() const { return const_iterator(i) != c->constEnd(); } \
0222     inline Item next() { n = i++; return n; } \
0223     inline Item peekNext() const { return i; } \
0224     inline void remove() \
0225     { if (const_iterator(n) != c->constEnd()) { i = c->erase(n); n = c->end(); } } \
0226     inline void setValue(const T &t) { if (const_iterator(n) != c->constEnd()) *n = t; } \
0227     inline T &value() { Q_ASSERT(item_exists()); return *n; } \
0228     inline const T &value() const { Q_ASSERT(item_exists()); return *n; } \
0229     inline const Key &key() const { Q_ASSERT(item_exists()); return n.key(); } \
0230     inline bool findNext(const T &t) \
0231     { while (const_iterator(n = i) != c->constEnd()) if (*i++ == t) return true; return false; } \
0232 };
0233 
0234 
0235 #else // QT_NO_JAVA_STYLE_ITERATORS
0236 #define Q_DECLARE_SEQUENTIAL_ITERATOR(C)
0237 #define Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(C)
0238 #define Q_DECLARE_ASSOCIATIVE_ITERATOR(C)
0239 #define Q_DECLARE_MUTABLE_ASSOCIATIVE_ITERATOR(C)
0240 #define Q_DECLARE_ASSOCIATIVE_FORWARD_ITERATOR(C)
0241 #define Q_DECLARE_MUTABLE_ASSOCIATIVE_FORWARD_ITERATOR(C)
0242 #endif // QT_NO_JAVA_STYLE_ITERATORS
0243 
0244 namespace QtPrivate {
0245 
0246 template <typename Key, typename T, typename Iterator>
0247 struct QDefaultKeyValues
0248 {
0249     static Key key(const Iterator &it) { return it.key(); }
0250     static Key key(Iterator &it) { return it.key(); }
0251     static T value(const Iterator &it) { return it.value(); }
0252     static T value(Iterator &it) { return it.value(); }
0253 };
0254 
0255 } // namespace QtPrivate
0256 
0257 template <typename Key, typename T, class Iterator,
0258           class Traits = QtPrivate::QDefaultKeyValues<Key, T, Iterator>>
0259 class QKeyValueIterator
0260 {
0261 public:
0262     typedef typename Iterator::iterator_category iterator_category;
0263     typedef typename Iterator::difference_type difference_type;
0264     typedef std::pair<Key, T> value_type;
0265     typedef const value_type &reference;
0266 
0267     QKeyValueIterator() = default;
0268     constexpr explicit QKeyValueIterator(Iterator o) noexcept(std::is_nothrow_move_constructible<Iterator>::value)
0269         : i(std::move(o)) {}
0270 
0271     std::pair<Key, T> operator*() const {
0272         return std::pair<Key, T>(Traits::key(i), Traits::value(i));
0273     }
0274 
0275     using pointer = QtPrivate::ArrowProxy<value_type>;
0276 
0277     pointer operator->() const {
0278         return pointer{ std::pair<Key, T>(Traits::key(i), Traits::value(i)) };
0279     }
0280 
0281     friend bool operator==(QKeyValueIterator lhs, QKeyValueIterator rhs) noexcept { return lhs.i == rhs.i; }
0282     friend bool operator!=(QKeyValueIterator lhs, QKeyValueIterator rhs) noexcept { return lhs.i != rhs.i; }
0283 
0284     inline QKeyValueIterator &operator++() { ++i; return *this; }
0285     inline QKeyValueIterator operator++(int) { return QKeyValueIterator(i++);}
0286     inline QKeyValueIterator &operator--() { --i; return *this; }
0287     inline QKeyValueIterator operator--(int) { return QKeyValueIterator(i--); }
0288     Iterator base() const { return i; }
0289 
0290 private:
0291     Iterator i;
0292 };
0293 
0294 namespace QtPrivate {
0295 
0296 template <typename Map>
0297 class QKeyValueRangeStorage
0298 {
0299 protected:
0300     Map m_map;
0301     Map &map() { return m_map; }
0302     const Map &map() const { return m_map; }
0303 public:
0304     explicit QKeyValueRangeStorage(const Map &map) : m_map(map) {}
0305     explicit QKeyValueRangeStorage(Map &&map) : m_map(std::move(map)) {}
0306 };
0307 
0308 template <typename Map>
0309 class QKeyValueRangeStorage<Map &>
0310 #ifdef __cpp_lib_ranges
0311     : public std::ranges::view_base
0312 #endif
0313 {
0314 protected:
0315     Map *m_map;
0316     Map &map() { return *m_map; }
0317     const Map &map() const { return *m_map; }
0318 public:
0319     explicit QKeyValueRangeStorage(Map &map) : m_map(&map) {}
0320 };
0321 
0322 template <typename Map>
0323 class QKeyValueRange : public QKeyValueRangeStorage<Map>
0324 {
0325 public:
0326     using QKeyValueRangeStorage<Map>::QKeyValueRangeStorage;
0327     auto begin() { return this->map().keyValueBegin(); }
0328     auto begin() const { return this->map().keyValueBegin(); }
0329     auto end() { return this->map().keyValueEnd(); }
0330     auto end() const { return this->map().keyValueEnd(); }
0331 };
0332 
0333 } // namespace QtPrivate
0334 
0335 
0336 QT_END_NAMESPACE
0337 
0338 #endif // QITERATOR_H