Merge remote-tracking branch 'origin/master' into api_changes
[profile/ivi/qtbase.git] / src / corelib / tools / qiterator.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QITERATOR_H
43 #define QITERATOR_H
44
45 #include <QtCore/qglobal.h>
46
47 QT_BEGIN_HEADER
48
49 namespace std {
50     struct bidirectional_iterator_tag;
51     struct random_access_iterator_tag;
52 }
53
54 QT_BEGIN_NAMESPACE
55
56
57 #define Q_DECLARE_SEQUENTIAL_ITERATOR(C) \
58 \
59 template <class T> \
60 class Q##C##Iterator \
61 { \
62     typedef typename Q##C<T>::const_iterator const_iterator; \
63     Q##C<T> c; \
64     const_iterator i; \
65 public: \
66     inline Q##C##Iterator(const Q##C<T> &container) \
67         : c(container), i(c.constBegin()) {} \
68     inline Q##C##Iterator &operator=(const Q##C<T> &container) \
69     { c = container; i = c.constBegin(); return *this; } \
70     inline void toFront() { i = c.constBegin(); } \
71     inline void toBack() { i = c.constEnd(); } \
72     inline bool hasNext() const { return i != c.constEnd(); } \
73     inline const T &next() { return *i++; } \
74     inline const T &peekNext() const { return *i; } \
75     inline bool hasPrevious() const { return i != c.constBegin(); } \
76     inline const T &previous() { return *--i; } \
77     inline const T &peekPrevious() const { const_iterator p = i; return *--p; } \
78     inline bool findNext(const T &t) \
79     { while (i != c.constEnd()) if (*i++ == t) return true; return false; } \
80     inline bool findPrevious(const T &t) \
81     { while (i != c.constBegin()) if (*(--i) == t) return true; \
82       return false;  } \
83 };
84
85 #define Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(C) \
86 \
87 template <class T> \
88 class QMutable##C##Iterator \
89 { \
90     typedef typename Q##C<T>::iterator iterator; \
91     typedef typename Q##C<T>::const_iterator const_iterator; \
92     Q##C<T> *c; \
93     iterator i, n; \
94     inline bool item_exists() const { return const_iterator(n) != c->constEnd(); } \
95 public: \
96     inline QMutable##C##Iterator(Q##C<T> &container) \
97         : c(&container) \
98     { c->setSharable(false); i = c->begin(); n = c->end(); } \
99     inline ~QMutable##C##Iterator() \
100     { c->setSharable(true); } \
101     inline QMutable##C##Iterator &operator=(Q##C<T> &container) \
102     { c->setSharable(true); c = &container; c->setSharable(false); \
103       i = c->begin(); n = c->end(); return *this; } \
104     inline void toFront() { i = c->begin(); n = c->end(); } \
105     inline void toBack() { i = c->end(); n = i; } \
106     inline bool hasNext() const { return c->constEnd() != const_iterator(i); } \
107     inline T &next() { n = i++; return *n; } \
108     inline T &peekNext() const { return *i; } \
109     inline bool hasPrevious() const { return c->constBegin() != const_iterator(i); } \
110     inline T &previous() { n = --i; return *n; } \
111     inline T &peekPrevious() const { iterator p = i; return *--p; } \
112     inline void remove() \
113     { if (c->constEnd() != const_iterator(n)) { i = c->erase(n); n = c->end(); } } \
114     inline void setValue(const T &t) const { if (c->constEnd() != const_iterator(n)) *n = t; } \
115     inline T &value() { Q_ASSERT(item_exists()); return *n; } \
116     inline const T &value() const { Q_ASSERT(item_exists()); return *n; } \
117     inline void insert(const T &t) { n = i = c->insert(i, t); ++i; } \
118     inline bool findNext(const T &t) \
119     { while (c->constEnd() != const_iterator(n = i)) if (*i++ == t) return true; return false; } \
120     inline bool findPrevious(const T &t) \
121     { while (c->constBegin() != const_iterator(i)) if (*(n = --i) == t) return true; \
122       n = c->end(); return false;  } \
123 };
124
125 #define Q_DECLARE_ASSOCIATIVE_ITERATOR(C) \
126 \
127 template <class Key, class T> \
128 class Q##C##Iterator \
129 { \
130     typedef typename Q##C<Key,T>::const_iterator const_iterator; \
131     typedef const_iterator Item; \
132     Q##C<Key,T> c; \
133     const_iterator i, n; \
134     inline bool item_exists() const { return n != c.constEnd(); } \
135 public: \
136     inline Q##C##Iterator(const Q##C<Key,T> &container) \
137         : c(container), i(c.constBegin()), n(c.constEnd()) {} \
138     inline Q##C##Iterator &operator=(const Q##C<Key,T> &container) \
139     { c = container; i = c.constBegin(); n = c.constEnd(); return *this; } \
140     inline void toFront() { i = c.constBegin(); n = c.constEnd(); } \
141     inline void toBack() { i = c.constEnd(); n = c.constEnd(); } \
142     inline bool hasNext() const { return i != c.constEnd(); } \
143     inline Item next() { n = i++; return n; } \
144     inline Item peekNext() const { return i; } \
145     inline bool hasPrevious() const { return i != c.constBegin(); } \
146     inline Item previous() { n = --i; return n; } \
147     inline Item peekPrevious() const { const_iterator p = i; return --p; } \
148     inline const T &value() const { Q_ASSERT(item_exists()); return *n; } \
149     inline const Key &key() const { Q_ASSERT(item_exists()); return n.key(); } \
150     inline bool findNext(const T &t) \
151     { while ((n = i) != c.constEnd()) if (*i++ == t) return true; return false; } \
152     inline bool findPrevious(const T &t) \
153     { while (i != c.constBegin()) if (*(n = --i) == t) return true; \
154       n = c.constEnd(); return false; } \
155 };
156
157 #define Q_DECLARE_MUTABLE_ASSOCIATIVE_ITERATOR(C) \
158 \
159 template <class Key, class T> \
160 class QMutable##C##Iterator \
161 { \
162     typedef typename Q##C<Key,T>::iterator iterator; \
163     typedef typename Q##C<Key,T>::const_iterator const_iterator; \
164     typedef iterator Item; \
165     Q##C<Key,T> *c; \
166     iterator i, n; \
167     inline bool item_exists() const { return const_iterator(n) != c->constEnd(); } \
168 public: \
169     inline QMutable##C##Iterator(Q##C<Key,T> &container) \
170         : c(&container) \
171     { c->setSharable(false); i = c->begin(); n = c->end(); } \
172     inline ~QMutable##C##Iterator() \
173     { c->setSharable(true); } \
174     inline QMutable##C##Iterator &operator=(Q##C<Key,T> &container) \
175     { c->setSharable(true); c = &container; c->setSharable(false); i = c->begin(); n = c->end(); return *this; } \
176     inline void toFront() { i = c->begin(); n = c->end(); } \
177     inline void toBack() { i = c->end(); n = c->end(); } \
178     inline bool hasNext() const { return const_iterator(i) != c->constEnd(); } \
179     inline Item next() { n = i++; return n; } \
180     inline Item peekNext() const { return i; } \
181     inline bool hasPrevious() const { return const_iterator(i) != c->constBegin(); } \
182     inline Item previous() { n = --i; return n; } \
183     inline Item peekPrevious() const { iterator p = i; return --p; } \
184     inline void remove() \
185     { if (const_iterator(n) != c->constEnd()) { i = c->erase(n); n = c->end(); } } \
186     inline void setValue(const T &t) { if (const_iterator(n) != c->constEnd()) *n = t; } \
187     inline T &value() { Q_ASSERT(item_exists()); return *n; } \
188     inline const T &value() const { Q_ASSERT(item_exists()); return *n; } \
189     inline const Key &key() const { Q_ASSERT(item_exists()); return n.key(); } \
190     inline bool findNext(const T &t) \
191     { while (const_iterator(n = i) != c->constEnd()) if (*i++ == t) return true; return false; } \
192     inline bool findPrevious(const T &t) \
193     { while (const_iterator(i) != c->constBegin()) if (*(n = --i) == t) return true; \
194       n = c->end(); return false; } \
195 };
196
197 QT_END_NAMESPACE
198
199 QT_END_HEADER
200
201 #endif // QITERATOR_H