Qt 4.8
qsequencemappingiterator_p.h
Go to the documentation of this file.
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 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 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 //
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 Patternist_SequenceMappingIterator_H
53 #define Patternist_SequenceMappingIterator_H
54 
56 #include "qdynamiccontext_p.h"
57 
59 
61 
62 namespace QPatternist
63 {
88  template<typename TResult, typename TSource, typename TMapper>
89  class SequenceMappingIterator : public QAbstractXmlForwardIterator<TResult>
90  {
91  public:
100  SequenceMappingIterator(const TMapper &mapper,
101  const typename QAbstractXmlForwardIterator<TSource>::Ptr &sourceIterator,
102  const DynamicContext::Ptr &context);
103 
104  virtual TResult next();
105  virtual xsInteger count();
106  virtual TResult current() const;
107  virtual xsInteger position() const;
108 
114  {
117  m_mainIterator->copy(),
118  m_context));
119  }
120 
121  private:
123  TResult m_current;
127  const TMapper m_mapper;
128  };
129 
130  template<typename TResult, typename TSource, typename TMapper>
132  const TMapper &mapper,
133  const typename QAbstractXmlForwardIterator<TSource>::Ptr &iterator,
134  const DynamicContext::Ptr &context)
135  : m_position(0),
136  m_mainIterator(iterator),
137  m_context(context),
138  m_mapper(mapper)
139  {
140  Q_ASSERT(mapper);
141  Q_ASSERT(iterator);
142  }
143 
144  template<typename TResult, typename TSource, typename TMapper>
146  {
147  /* This was once implemented with a recursive function, but the stack
148  * got blown for some inputs by that approach. */
149  while(true)
150  {
151  while(!m_currentIterator)
152  {
153  const TSource mainItem(m_mainIterator->next());
154 
155  if(qIsForwardIteratorEnd(mainItem)) /* We've reached the very end. */
156  {
157  m_position = -1;
158  m_current = TResult();
159  return TResult();
160  }
161  else
162  m_currentIterator = m_mapper->mapToSequence(mainItem, m_context);
163  }
164 
166 
168  {
169  m_currentIterator.reset();
170  continue;
171  }
172  else
173  {
174  ++m_position;
175  return m_current;
176  }
177  }
178  }
179 
180  template<typename TResult, typename TSource, typename TMapper>
182  {
183  TSource unit(m_mainIterator->next());
184  xsInteger c = 0;
185 
186  while(!qIsForwardIteratorEnd(unit))
187  {
188  const typename QAbstractXmlForwardIterator<TResult>::Ptr sit(m_mapper->mapToSequence(unit, m_context));
189  c += sit->count();
190  unit = m_mainIterator->next();
191  }
192 
193  return c;
194  }
195 
196  template<typename TResult, typename TSource, typename TMapper>
198  {
199  return m_current;
200  }
201 
202  template<typename TResult, typename TSource, typename TMapper>
204  {
205  return m_position;
206  }
207 
208 
221  template<typename TResult, typename TSource, typename TMapper>
222  static inline
224  makeSequenceMappingIterator(const TMapper &mapper,
226  const DynamicContext::Ptr &context)
227  {
229  (new SequenceMappingIterator<TResult, TSource, TMapper>(mapper, source, context));
230  }
231 }
232 
234 
236 
237 #endif
QAbstractXmlForwardIterator< TResult >::Ptr m_currentIterator
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
qint64 xsInteger
virtual xsInteger position() const
Returns the current position in the sequence represented by this.
virtual QAbstractXmlForwardIterator< TResult >::Ptr copy() const
virtual xsInteger count()
Determines the number of items this QAbstractXmlForwardIterator represents.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
SequenceMappingIterator(const TMapper &mapper, const typename QAbstractXmlForwardIterator< TSource >::Ptr &sourceIterator, const DynamicContext::Ptr &context)
QExplicitlySharedDataPointer< QAbstractXmlForwardIterator< T > > Ptr
A smart pointer wrapping an instance of a QAbstractXmlForwardIterator subclass.
QAbstractXmlForwardIterator< TSource >::Ptr m_mainIterator
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual TResult next()
Returns the next item in the sequence, or a null object if the end has been reached.
The QAbstractXmlForwardIterator class is a base class for forward iterators.
The namespace for the internal API of QtXmlPatterns.
virtual qint64 count()
Determines the number of items this QAbstractXmlForwardIterator represents.
Proxies another QAbstractXmlForwardIterator, and for each item, returns the Sequence returned from a ...
virtual QAbstractXmlForwardIterator< T >::Ptr copy() const
Copies this QAbstractXmlForwardIterator and returns the copy.
bool qIsForwardIteratorEnd(const TResult &unit)
The Callback QAbstractXmlForwardIterator uses for determining whether unit is the end of a sequence...
virtual TResult current() const
Returns the current item in the sequence.
QExplicitlySharedDataPointer< DynamicContext > Ptr
static QAbstractXmlForwardIterator< TResult >::Ptr makeSequenceMappingIterator(const TMapper &mapper, const QExplicitlySharedDataPointer< QAbstractXmlForwardIterator< TSource > > &source, const DynamicContext::Ptr &context)
An object generator for SequenceMappingIterator.
#define QT_END_HEADER
Definition: qglobal.h:137
virtual T next()=0
Returns the next item in the sequence, or a null object if the end has been reached.