1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
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 "qabstractxmlforwarditerator_p.h"
56 #include "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));