1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
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.
52 #ifndef Patternist_SequenceMappingIterator_H
53 #define Patternist_SequenceMappingIterator_H
55 #include <private/qabstractxmlforwarditerator_p.h>
56 #include <private/qdynamiccontext_p.h>
65 * @short Proxies another QAbstractXmlForwardIterator, and for each item, returns the
66 * Sequence returned from a mapping function.
68 * ItemMappingIterator is practical when the items in an QAbstractXmlForwardIterator needs to
69 * be translated to another sequence, while still doing it in a pipe-lined
70 * fashion. In contrast to ItemMappingIterator, SequenceMappingIterator maps
71 * each item into another QAbstractXmlForwardIterator, and where the SequenceMappingIterator's own
72 * result is the concatenation of all those Iterators. Hence, while ItemMappingIterator
73 * is better tailored for one-to-one or one-to-zero conversion, SequenceMappingIterator
74 * is more suitable for one-to-many conversion.
76 * This is achieved by that SequenceMappingIterator's constructor takes
77 * an instance of a class, that must have the following member:
80 * QAbstractXmlForwardIterator<TResult>::Ptr mapToSequence(const TSource::Ptr &item,
81 * const DynamicContext::Ptr &context) const;
84 * @author Frans Englich <frans.englich@nokia.com>
85 * @see ItemMappingIterator
86 * @ingroup Patternist_iterators
88 template<typename TResult, typename TSource, typename TMapper>
89 class SequenceMappingIterator : public QAbstractXmlForwardIterator<TResult>
93 * Constructs a SequenceMappingIterator.
95 * @param mapper the object that has the mapToItem() sequence.
96 * @param sourceIterator the QAbstractXmlForwardIterator whose items should be mapped.
97 * @param context the DynamicContext that will be passed to the map function.
100 SequenceMappingIterator(const TMapper &mapper,
101 const typename QAbstractXmlForwardIterator<TSource>::Ptr &sourceIterator,
102 const DynamicContext::Ptr &context);
104 virtual TResult next();
105 virtual xsInteger count();
106 virtual TResult current() const;
107 virtual xsInteger position() const;
110 * The reason the implementation is placed in line here, is due to a bug
111 * in MSVC-2005 version 14.00.50727.762. Note that it works with version 14.00.50727.42.
113 virtual typename QAbstractXmlForwardIterator<TResult>::Ptr copy() const
115 return typename QAbstractXmlForwardIterator<TResult>::Ptr
116 (new SequenceMappingIterator<TResult, TSource, TMapper>(m_mapper,
117 m_mainIterator->copy(),
122 xsInteger m_position;
124 typename QAbstractXmlForwardIterator<TSource>::Ptr m_mainIterator;
125 typename QAbstractXmlForwardIterator<TResult>::Ptr m_currentIterator;
126 const typename DynamicContext::Ptr m_context;
127 const TMapper m_mapper;
130 template<typename TResult, typename TSource, typename TMapper>
131 SequenceMappingIterator<TResult, TSource, TMapper>::SequenceMappingIterator(
132 const TMapper &mapper,
133 const typename QAbstractXmlForwardIterator<TSource>::Ptr &iterator,
134 const DynamicContext::Ptr &context)
136 m_mainIterator(iterator),
144 template<typename TResult, typename TSource, typename TMapper>
145 TResult SequenceMappingIterator<TResult, TSource, TMapper>::next()
147 /* This was once implemented with a recursive function, but the stack
148 * got blown for some inputs by that approach. */
151 while(!m_currentIterator)
153 const TSource mainItem(m_mainIterator->next());
155 if(qIsForwardIteratorEnd(mainItem)) /* We've reached the very end. */
158 m_current = TResult();
162 m_currentIterator = m_mapper->mapToSequence(mainItem, m_context);
165 m_current = m_currentIterator->next();
167 if(qIsForwardIteratorEnd(m_current))
169 m_currentIterator.reset();
180 template<typename TResult, typename TSource, typename TMapper>
181 xsInteger SequenceMappingIterator<TResult, TSource, TMapper>::count()
183 TSource unit(m_mainIterator->next());
186 while(!qIsForwardIteratorEnd(unit))
188 const typename QAbstractXmlForwardIterator<TResult>::Ptr sit(m_mapper->mapToSequence(unit, m_context));
190 unit = m_mainIterator->next();
196 template<typename TResult, typename TSource, typename TMapper>
197 TResult SequenceMappingIterator<TResult, TSource, TMapper>::current() const
202 template<typename TResult, typename TSource, typename TMapper>
203 xsInteger SequenceMappingIterator<TResult, TSource, TMapper>::position() const
210 * @short An object generator for SequenceMappingIterator.
212 * makeSequenceMappingIterator() is a convenience function for avoiding specifying
213 * the full template instantiation for SequenceMappingIterator. Conceptually, it
214 * is identical to Qt's qMakePair().
216 * @returns a SequenceMappingIterator wrapped in a smart pointer, that has been
217 * passed the constructor arguments @p mapper, @p source, and @p context.
218 * @see makeMappingCallbackPtr()
219 * @relates QAbstractXmlForwardIterator
221 template<typename TResult, typename TSource, typename TMapper>
223 typename QAbstractXmlForwardIterator<TResult>::Ptr
224 makeSequenceMappingIterator(const TMapper &mapper,
225 const QExplicitlySharedDataPointer<QAbstractXmlForwardIterator<TSource> > &source,
226 const DynamicContext::Ptr &context)
228 return typename QAbstractXmlForwardIterator<TResult>::Ptr
229 (new SequenceMappingIterator<TResult, TSource, TMapper>(mapper, source, context));