1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtXmlPatterns 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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40//
41// W A R N I N G
42// -------------
43//
44// This file is not part of the Qt API. It exists purely as an
45// implementation detail. This header file may change from version to
46// version without notice, or even be removed.
47//
48// We mean it.
49
50#ifndef Patternist_SingletonIterator_H
51#define Patternist_SingletonIterator_H
52
53#include <QtXmlPatterns/private/qabstractxmlforwarditerator_p.h>
54
55#include <QtXmlPatterns/private/qprimitives_p.h>
56
57QT_BEGIN_NAMESPACE
58
59namespace QPatternist
60{
61 /**
62 * @short An QAbstractXmlForwardIterator over exactly one item.
63 *
64 * SingletonIterator's constructor takes one value which is
65 * the item it forms an QAbstractXmlForwardIterator over. Other QAbstractXmlForwardIterator instances can
66 * also form an QAbstractXmlForwardIterator with one in length, but by that SingletonIterator
67 * has this as it only task, it means it is efficient at it.
68 *
69 * Having to represent single items in Iterators is relatively common.
70 *
71 * @author Frans Englich <frans.englich@nokia.com>
72 * @ingroup Patternist_iterators
73 */
74 template<typename T>
75 class SingletonIterator : public QAbstractXmlForwardIterator<T>
76 {
77 public:
78 /**
79 * Creates an iterator over @p item.
80 *
81 * @note item may not be @c null. Use the EmptyIterator for
82 * the empty sequence
83 */
84 SingletonIterator(const T &item) : m_item(item),
85 m_position(0)
86 {
87 Q_ASSERT(!qIsForwardIteratorEnd(item));
88 }
89
90 virtual T next()
91 {
92 switch(m_position)
93 {
94 case 0:
95 {
96 ++m_position;
97 return m_item;
98 }
99 case 1:
100 {
101 m_position = -1;
102 return T();
103 }
104 default:
105 {
106 Q_ASSERT(m_position == -1);
107 return T();
108 }
109 }
110 }
111
112 virtual T current() const
113 {
114 if(m_position == 1)
115 return m_item;
116 else
117 return T();
118 }
119
120 virtual xsInteger position() const
121 {
122 return m_position;
123 }
124
125 /**
126 * @returns a copy of this instance, rewinded to the beginning.
127 */
128 virtual typename QAbstractXmlForwardIterator<T>::Ptr toReversed()
129 {
130 return typename QAbstractXmlForwardIterator<T>::Ptr(new SingletonIterator<T>(m_item));
131 }
132
133 /**
134 * @returns always 1
135 */
136 virtual xsInteger count()
137 {
138 return 1;
139 }
140
141 virtual typename QAbstractXmlForwardIterator<T>::Ptr copy() const
142 {
143 return typename QAbstractXmlForwardIterator<T>::Ptr(new SingletonIterator(m_item));
144 }
145
146 private:
147 const T m_item;
148 qint8 m_position;
149 };
150
151 /**
152 * @short An object generator for SingletonIterator.
153 *
154 * makeSingletonIterator() is a convenience function for avoiding specifying
155 * the full template instantiation for SingletonIterator. Conceptually, it
156 * is identical to Qt's qMakePair().
157 *
158 * @relates SingletonIterator
159 */
160 template<typename T>
161 inline
162 typename SingletonIterator<T>::Ptr
163 makeSingletonIterator(const T &item)
164 {
165 return typename SingletonIterator<T>::Ptr(new SingletonIterator<T>(item));
166 }
167}
168
169QT_END_NAMESPACE
170
171#endif
172

source code of qtxmlpatterns/src/xmlpatterns/iterators/qsingletoniterator_p.h