63241e64eafa4419df4e1637214758ac0f81d343
[profile/ivi/qtxmlpatterns.git] / src / xmlpatterns / api / qabstractxmlforwarditerator_p.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 QtXmlPatterns 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 //
43 //  W A R N I N G
44 //  -------------
45 //
46 // This file is not part of the Qt API.  It exists purely as an
47 // implementation detail.  This header file may change from version to
48 // version without notice, or even be removed.
49 //
50 // We mean it.
51
52 #ifndef QABSTRACTXMLFORWARDITERATOR_H
53 #define QABSTRACTXMLFORWARDITERATOR_H
54
55 #include <QtCore/QList>
56 #include <QtCore/QVector>
57 #include <QtCore/QSharedData>
58 #include <QtCore/QString>
59
60 QT_BEGIN_HEADER
61
62 QT_BEGIN_NAMESPACE
63
64
65 template<typename T> class QVector;
66
67 /* In this file we in some cases do not use QAbstractXmlForwardIterator's Ptr typedef.
68  * This is a compiler workaround for MS VS 6.0. */
69
70 template<typename T>
71 inline bool qIsForwardIteratorEnd(const T &unit)
72 {
73     return !unit;
74 }
75
76 /**
77  * @short Helper class for StringSplitter
78  *
79  * Needed by the QAbstractXmlForwardIterator sub-class.
80  *
81  * @relates StringSplitter
82  */
83 template<>
84 inline bool qIsForwardIteratorEnd(const QString &unit)
85 {
86     return unit.isNull();
87 }
88
89 template<typename T> class QAbstractXmlForwardIterator;
90
91 class QAbstractXmlForwardIteratorPrivate;
92
93 template<typename T>
94 class QAbstractXmlForwardIterator : public QSharedData
95 {
96 public:
97     typedef QExplicitlySharedDataPointer<QAbstractXmlForwardIterator<T> > Ptr;
98     typedef QList<QExplicitlySharedDataPointer<QAbstractXmlForwardIterator<T> > > List;
99     typedef QVector<QExplicitlySharedDataPointer<QAbstractXmlForwardIterator<T> > > Vector;
100
101     inline QAbstractXmlForwardIterator() : d_ptr(0) {}
102     virtual ~QAbstractXmlForwardIterator() {}
103
104     virtual T next() = 0;
105     virtual T current() const = 0;
106
107     virtual qint64 position() const = 0;
108
109     virtual typename QAbstractXmlForwardIterator<T>::Ptr toReversed();
110     virtual QList<T> toList();
111     virtual typename QAbstractXmlForwardIterator<T>::Ptr copy() const;
112     virtual T last();
113     virtual bool isEmpty();
114     virtual qint64 count();
115     virtual qint64 sizeHint() const;
116
117 private:
118     Q_DISABLE_COPY(QAbstractXmlForwardIterator<T>)
119
120     QAbstractXmlForwardIteratorPrivate *d_ptr; /* Currently not used. */
121 };
122
123 /* The namespace QPatternist and its members are internal, not part of the public API, and
124  * unsupported. Using them leads to undefined behavior. */
125 namespace QPatternist
126 {
127     class DeduplicateIterator;
128
129     template<typename InputType,
130              typename OutputType,
131              typename Derived,
132              typename ListType = QList<InputType> >
133     class ListIteratorPlatform : public QAbstractXmlForwardIterator<OutputType>
134     {
135         /* This declaration is a workaround for a set of GCC versions on OS X,
136          * amongst others powerpc-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1. In
137          * DeduplicateIterator, it fails to see the protected inheritance. */
138         friend class DeduplicateIterator;
139
140     public:
141         virtual OutputType next()
142         {
143             if(m_position == -1)
144                 return OutputType();
145
146             if(m_position == m_list.count())
147             {
148                 m_position = -1;
149                 m_current = OutputType();
150                 return OutputType();
151             }
152
153             m_current = static_cast<const Derived *>(this)->inputToOutputItem(m_list.at(m_position));
154             ++m_position;
155             return m_current;
156         }
157
158         virtual OutputType current() const
159         {
160             return m_current;
161         }
162
163         virtual qint64 position() const
164         {
165             return m_position;
166         }
167
168         virtual qint64 count()
169         {
170             return m_list.count();
171         }
172
173         virtual typename QAbstractXmlForwardIterator<OutputType>::Ptr copy() const
174         {
175             return QExplicitlySharedDataPointer<QAbstractXmlForwardIterator<OutputType> >(new ListIteratorPlatform<InputType, OutputType, Derived, ListType>(m_list));
176         }
177
178     protected:
179         inline ListIteratorPlatform(const ListType &list) : m_list(list)
180                                                            , m_position(0)
181         {
182         }
183
184         const ListType  m_list;
185         qint64          m_position;
186         OutputType      m_current;
187     };
188
189     template<typename T,
190              typename ListType = QList<T> >
191     class ListIterator : public ListIteratorPlatform<T, T, ListIterator<T, ListType>, ListType>
192     {
193         /*
194          * This declaration is needed for MSVC 2005, 14.00.50727.42 for 80x86.
195          */
196         friend class IteratorVector;
197
198         using ListIteratorPlatform<T, T, ListIterator<T, ListType>, ListType>::m_list;
199
200         static inline QVector<T> toVector(const QVector<T> &vector)
201         {
202             return vector;
203         }
204
205         static inline QVector<T> toVector(const QList<T> &list)
206         {
207             return list.toVector();
208         }
209
210         static inline QList<T> toList(const QVector<T> &vector)
211         {
212             return vector.toList();
213         }
214
215         static inline QList<T> toList(const QList<T> &list)
216         {
217             return list;
218         }
219
220     public:
221         inline ListIterator(const ListType &list) : ListIteratorPlatform<T, T, ListIterator<T, ListType>, ListType>(list)
222         {
223         }
224
225         virtual QList<T> toList()
226         {
227             return toList(m_list);
228         }
229
230         virtual QVector<T> toVector()
231         {
232             return toVector(m_list);
233         }
234
235     private:
236         inline const T &inputToOutputItem(const T &inputType) const
237         {
238             return inputType;
239         }
240         friend class ListIteratorPlatform<T, T, ListIterator<T, ListType>, ListType>;
241
242         // needed for MSVC 2005
243         friend class DeduplicateIterator;
244     };
245
246     template<typename T>
247     inline
248     typename QAbstractXmlForwardIterator<T>::Ptr
249     makeListIterator(const QList<T> &list)
250     {
251         return typename ListIterator<T>::Ptr(new ListIterator<T>(list));
252     }
253
254     template<typename T>
255     inline
256     typename QAbstractXmlForwardIterator<T>::Ptr
257     makeVectorIterator(const QVector<T> &vector)
258     {
259         return typename ListIterator<T, QVector<T> >::Ptr(new ListIterator<T, QVector<T> >(vector));
260     }
261 }
262
263 template<typename T>
264 QList<T> QAbstractXmlForwardIterator<T>::toList()
265 {
266     QList<T> result;
267     T item(next());
268
269     while(!qIsForwardIteratorEnd(item))
270     {
271         result.append(item);
272         item = next();
273     }
274
275     return result;
276 }
277
278 template<typename T>
279 qint64 QAbstractXmlForwardIterator<T>::count()
280 {
281     qint64 retval = 0;
282
283     while(!qIsForwardIteratorEnd(next()))
284         ++retval;
285
286     return retval;
287 }
288
289 template<typename T>
290 typename QAbstractXmlForwardIterator<T>::Ptr QAbstractXmlForwardIterator<T>::toReversed()
291 {
292     T item(next());
293     QList<T> result;
294
295     while(!qIsForwardIteratorEnd(item))
296     {
297         result.prepend(item);
298         item = next();
299     }
300
301     return QExplicitlySharedDataPointer<QAbstractXmlForwardIterator<T> >(new QPatternist::ListIterator<T>(result));
302 }
303
304 template<typename T>
305 T QAbstractXmlForwardIterator<T>::last()
306 {
307     T item(next());
308
309     while(!qIsForwardIteratorEnd(item))
310         item = next();
311
312     return item;
313 }
314
315 template<typename T>
316 typename QAbstractXmlForwardIterator<T>::Ptr QAbstractXmlForwardIterator<T>::copy() const
317 {
318     Q_ASSERT_X(false, Q_FUNC_INFO,
319                "This function is internal, unsupported, and should never be called.");
320     return typename QAbstractXmlForwardIterator<T>::Ptr();
321 }
322
323 template<typename T>
324 bool QAbstractXmlForwardIterator<T>::isEmpty()
325 {
326     return qIsForwardIteratorEnd(next());
327 }
328
329 template<typename T>
330 qint64 QAbstractXmlForwardIterator<T>::sizeHint() const
331 {
332     Q_ASSERT_X(false, Q_FUNC_INFO, "This function is currently not expected to be used.");
333     return -1;
334 }
335
336 QT_END_NAMESPACE
337
338 QT_END_HEADER
339
340 #endif