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