1/****************************************************************************
2**
3** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QITERATOR_H
43#define QITERATOR_H
44
45#include <QtCore/qglobal.h>
46
47QT_BEGIN_HEADER
48
49#ifdef QT_NO_STL
50# include <new> // No-op, indirectly include additional configuration headers.
51# if defined(_LIBCPP_VERSION)
52// libc++ may declare these structs in an inline namespace. Forward-declare
53// these iterators in the same namespace so that we do not shadow the original
54// declarations.
55
56// Tell clang not to warn about the use of inline namespaces when not building
57// in C++11 mode.
58# if defined(Q_CC_CLANG)
59# pragma GCC diagnostic push
60# pragma GCC diagnostic ignored "-Wc++11-extensions"
61# endif
62
63_LIBCPP_BEGIN_NAMESPACE_STD
64 struct bidirectional_iterator_tag;
65 struct random_access_iterator_tag;
66_LIBCPP_END_NAMESPACE_STD
67
68# if defined(Q_CC_CLANG)
69# pragma GCC diagnostic pop
70# endif
71# else
72namespace std {
73 struct bidirectional_iterator_tag;
74 struct random_access_iterator_tag;
75}
76# endif
77#endif
78
79QT_BEGIN_NAMESPACE
80
81QT_MODULE(Core)
82
83#define Q_DECLARE_SEQUENTIAL_ITERATOR(C) \
84\
85template <class T> \
86class Q##C##Iterator \
87{ \
88 typedef typename Q##C<T>::const_iterator const_iterator; \
89 Q##C<T> c; \
90 const_iterator i; \
91public: \
92 inline Q##C##Iterator(const Q##C<T> &container) \
93 : c(container), i(c.constBegin()) {} \
94 inline Q##C##Iterator &operator=(const Q##C<T> &container) \
95 { c = container; i = c.constBegin(); return *this; } \
96 inline void toFront() { i = c.constBegin(); } \
97 inline void toBack() { i = c.constEnd(); } \
98 inline bool hasNext() const { return i != c.constEnd(); } \
99 inline const T &next() { return *i++; } \
100 inline const T &peekNext() const { return *i; } \
101 inline bool hasPrevious() const { return i != c.constBegin(); } \
102 inline const T &previous() { return *--i; } \
103 inline const T &peekPrevious() const { const_iterator p = i; return *--p; } \
104 inline bool findNext(const T &t) \
105 { while (i != c.constEnd()) if (*i++ == t) return true; return false; } \
106 inline bool findPrevious(const T &t) \
107 { while (i != c.constBegin()) if (*(--i) == t) return true; \
108 return false; } \
109};
110
111#define Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(C) \
112\
113template <class T> \
114class QMutable##C##Iterator \
115{ \
116 typedef typename Q##C<T>::iterator iterator; \
117 typedef typename Q##C<T>::const_iterator const_iterator; \
118 Q##C<T> *c; \
119 iterator i, n; \
120 inline bool item_exists() const { return const_iterator(n) != c->constEnd(); } \
121public: \
122 inline QMutable##C##Iterator(Q##C<T> &container) \
123 : c(&container) \
124 { c->setSharable(false); i = c->begin(); n = c->end(); } \
125 inline ~QMutable##C##Iterator() \
126 { c->setSharable(true); } \
127 inline QMutable##C##Iterator &operator=(Q##C<T> &container) \
128 { c->setSharable(true); c = &container; c->setSharable(false); \
129 i = c->begin(); n = c->end(); return *this; } \
130 inline void toFront() { i = c->begin(); n = c->end(); } \
131 inline void toBack() { i = c->end(); n = i; } \
132 inline bool hasNext() const { return c->constEnd() != const_iterator(i); } \
133 inline T &next() { n = i++; return *n; } \
134 inline T &peekNext() const { return *i; } \
135 inline bool hasPrevious() const { return c->constBegin() != const_iterator(i); } \
136 inline T &previous() { n = --i; return *n; } \
137 inline T &peekPrevious() const { iterator p = i; return *--p; } \
138 inline void remove() \
139 { if (c->constEnd() != const_iterator(n)) { i = c->erase(n); n = c->end(); } } \
140 inline void setValue(const T &t) const { if (c->constEnd() != const_iterator(n)) *n = t; } \
141 inline T &value() { Q_ASSERT(item_exists()); return *n; } \
142 inline const T &value() const { Q_ASSERT(item_exists()); return *n; } \
143 inline void insert(const T &t) { n = i = c->insert(i, t); ++i; } \
144 inline bool findNext(const T &t) \
145 { while (c->constEnd() != const_iterator(n = i)) if (*i++ == t) return true; return false; } \
146 inline bool findPrevious(const T &t) \
147 { while (c->constBegin() != const_iterator(i)) if (*(n = --i) == t) return true; \
148 n = c->end(); return false; } \
149};
150
151#define Q_DECLARE_ASSOCIATIVE_ITERATOR(C) \
152\
153template <class Key, class T> \
154class Q##C##Iterator \
155{ \
156 typedef typename Q##C<Key,T>::const_iterator const_iterator; \
157 typedef const_iterator Item; \
158 Q##C<Key,T> c; \
159 const_iterator i, n; \
160 inline bool item_exists() const { return n != c.constEnd(); } \
161public: \
162 inline Q##C##Iterator(const Q##C<Key,T> &container) \
163 : c(container), i(c.constBegin()), n(c.constEnd()) {} \
164 inline Q##C##Iterator &operator=(const Q##C<Key,T> &container) \
165 { c = container; i = c.constBegin(); n = c.constEnd(); return *this; } \
166 inline void toFront() { i = c.constBegin(); n = c.constEnd(); } \
167 inline void toBack() { i = c.constEnd(); n = c.constEnd(); } \
168 inline bool hasNext() const { return i != c.constEnd(); } \
169 inline Item next() { n = i++; return n; } \
170 inline Item peekNext() const { return i; } \
171 inline bool hasPrevious() const { return i != c.constBegin(); } \
172 inline Item previous() { n = --i; return n; } \
173 inline Item peekPrevious() const { const_iterator p = i; return --p; } \
174 inline const T &value() const { Q_ASSERT(item_exists()); return *n; } \
175 inline const Key &key() const { Q_ASSERT(item_exists()); return n.key(); } \
176 inline bool findNext(const T &t) \
177 { while ((n = i) != c.constEnd()) if (*i++ == t) return true; return false; } \
178 inline bool findPrevious(const T &t) \
179 { while (i != c.constBegin()) if (*(n = --i) == t) return true; \
180 n = c.constEnd(); return false; } \
181};
182
183#define Q_DECLARE_MUTABLE_ASSOCIATIVE_ITERATOR(C) \
184\
185template <class Key, class T> \
186class QMutable##C##Iterator \
187{ \
188 typedef typename Q##C<Key,T>::iterator iterator; \
189 typedef typename Q##C<Key,T>::const_iterator const_iterator; \
190 typedef iterator Item; \
191 Q##C<Key,T> *c; \
192 iterator i, n; \
193 inline bool item_exists() const { return const_iterator(n) != c->constEnd(); } \
194public: \
195 inline QMutable##C##Iterator(Q##C<Key,T> &container) \
196 : c(&container) \
197 { c->setSharable(false); i = c->begin(); n = c->end(); } \
198 inline ~QMutable##C##Iterator() \
199 { c->setSharable(true); } \
200 inline QMutable##C##Iterator &operator=(Q##C<Key,T> &container) \
201 { c->setSharable(true); c = &container; c->setSharable(false); i = c->begin(); n = c->end(); return *this; } \
202 inline void toFront() { i = c->begin(); n = c->end(); } \
203 inline void toBack() { i = c->end(); n = c->end(); } \
204 inline bool hasNext() const { return const_iterator(i) != c->constEnd(); } \
205 inline Item next() { n = i++; return n; } \
206 inline Item peekNext() const { return i; } \
207 inline bool hasPrevious() const { return const_iterator(i) != c->constBegin(); } \
208 inline Item previous() { n = --i; return n; } \
209 inline Item peekPrevious() const { iterator p = i; return --p; } \
210 inline void remove() \
211 { if (const_iterator(n) != c->constEnd()) { i = c->erase(n); n = c->end(); } } \
212 inline void setValue(const T &t) { if (const_iterator(n) != c->constEnd()) *n = t; } \
213 inline T &value() { Q_ASSERT(item_exists()); return *n; } \
214 inline const T &value() const { Q_ASSERT(item_exists()); return *n; } \
215 inline const Key &key() const { Q_ASSERT(item_exists()); return n.key(); } \
216 inline bool findNext(const T &t) \
217 { while (const_iterator(n = i) != c->constEnd()) if (*i++ == t) return true; return false; } \
218 inline bool findPrevious(const T &t) \
219 { while (const_iterator(i) != c->constBegin()) if (*(n = --i) == t) return true; \
220 n = c->end(); return false; } \
221};
222
223QT_END_NAMESPACE
224
225QT_END_HEADER
226
227#endif // QITERATOR_H
228