Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qiterator.h
Go to the documentation of this file.
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
10QT_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) template
22
23 <class T> class
24 Q##C##Iterator \
25{
26 typedef typename Q##C<T>::const_iterator const_iterator;
27 Q##C<T> c;
28 const_iterator i; public
29 :
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; }
40 inline bool hasPrevious() const { return i != c.constBegin(); }
42 inline const T &previous() { return *--i; }
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; }
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) template
54
55 <class T> class
56 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(); } public
63 :
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; }
75 inline bool hasPrevious() const { return c->constBegin() != const_iterator(i); }
77 inline T &previous() { n = --i; return *n; }
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; }
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) template
95
96 <class Key, class T> class
97 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(); } public
103 :
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; }
115 inline bool hasPrevious() const { return i != c.constBegin(); }
117 inline Item previous() { n = --i; return n; }
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; }
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) template
131
132 <class Key, class T> class
133 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(); } public
140 :
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; }
153 inline bool hasPrevious() const { return const_iterator(i) != c->constBegin(); }
155 inline Item previous() { n = --i; return n; }
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; }
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) template
173
174 <class Key, class T> class
175 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(); } public
181 :
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) template
199
200 <class Key, class T> class
201 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(); } public
208 :
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
240template<typename Key, typename T, class Iterator>
241class QKeyValueIterator
242{
243public:
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
272private:
273 Iterator i;
274};
275
276namespace QtPrivate {
277
278template <typename Map>
280{
281protected:
282 Map m_map;
283public:
284 explicit QKeyValueRangeStorage(const Map &map) : m_map(map) {}
285 explicit QKeyValueRangeStorage(Map &&map) : m_map(std::move(map)) {}
286};
287
288template <typename Map>
290{
291protected:
292 Map &m_map;
293public:
294 explicit QKeyValueRangeStorage(Map &map) : m_map(map) {}
295};
296
297template <typename Map>
299{
300public:
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
308template <typename Map>
310
311template <typename Map, std::enable_if_t<!std::is_reference_v<Map>, bool> = false>
313
314} // namespace QtPrivate
315
316
317QT_END_NAMESPACE
318
319#endif // QITERATOR_H
QKeyValueRangeStorage(const Map &map)
Definition qiterator.h:284
\macro QT_NO_KEYWORDS >
Definition qcompare.h:24
QKeyValueRange(Map &) -> QKeyValueRange< Map & >
#define Q_DISABLE_BACKWARD_ITERATOR
Definition qiterator.h:17