Partially revert e84e86dc.
[profile/ivi/qtbase.git] / src / concurrent / qtconcurrentfilter.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtCore 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 #ifndef QTCONCURRENT_FILTER_H
43 #define QTCONCURRENT_FILTER_H
44
45 #include <QtConcurrent/qtconcurrent_global.h>
46
47 #ifndef QT_NO_CONCURRENT
48
49 #include <QtConcurrent/qtconcurrentfilterkernel.h>
50 #include <QtConcurrent/qtconcurrentfunctionwrappers.h>
51
52 QT_BEGIN_HEADER
53 QT_BEGIN_NAMESPACE
54
55
56 #ifdef qdoc
57
58 namespace QtConcurrent {
59
60     QFuture<void> filter(Sequence &sequence, FilterFunction filterFunction);
61
62     template <typename T>
63     QFuture<T> filtered(const Sequence &sequence, FilterFunction filterFunction);
64     template <typename T>
65     QFuture<T> filtered(ConstIterator begin, ConstIterator end, FilterFunction filterFunction);
66
67     template <typename T>
68     QFuture<T> filteredReduced(const Sequence &sequence,
69                                FilterFunction filterFunction,
70                                ReduceFunction reduceFunction,
71                                QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce);
72     template <typename T>
73     QFuture<T> filteredReduced(ConstIterator begin,
74                                ConstIterator end,
75                                FilterFunction filterFunction,
76                                ReduceFunction reduceFunction,
77                                QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce);
78
79     void blockingFilter(Sequence &sequence, FilterFunction filterFunction);
80
81     template <typename Sequence>
82     Sequence blockingFiltered(const Sequence &sequence, FilterFunction filterFunction);
83     template <typename Sequence>
84     Sequence blockingFiltered(ConstIterator begin, ConstIterator end, FilterFunction filterFunction);
85
86     template <typename T>
87     T blockingFilteredReduced(const Sequence &sequence,
88                               FilterFunction filterFunction,
89                               ReduceFunction reduceFunction,
90                               QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce);
91     template <typename T>
92     T blockingFilteredReduced(ConstIterator begin,
93                               ConstIterator end,
94                               FilterFunction filterFunction,
95                               ReduceFunction reduceFunction,
96                               QtConcurrent::ReduceOptions reduceOptions = UnorderedReduce | SequentialReduce);
97
98 } // namespace QtConcurrent
99
100 #else
101
102 namespace QtConcurrent {
103
104 template <typename Sequence, typename KeepFunctor, typename ReduceFunctor>
105 ThreadEngineStarter<void> filterInternal(Sequence &sequence, KeepFunctor keep, ReduceFunctor reduce)
106 {
107     typedef FilterKernel<Sequence, KeepFunctor, ReduceFunctor> KernelType;
108     return startThreadEngine(new KernelType(sequence, keep, reduce));
109 }
110
111 // filter() on sequences
112 template <typename Sequence, typename KeepFunctor>
113 QFuture<void> filter(Sequence &sequence, KeepFunctor keep)
114 {
115     return filterInternal(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::PushBackWrapper());
116 }
117
118 // filteredReduced() on sequences
119 template <typename ResultType, typename Sequence, typename KeepFunctor, typename ReduceFunctor>
120 QFuture<ResultType> filteredReduced(const Sequence &sequence,
121                                     KeepFunctor keep,
122                                     ReduceFunctor reduce,
123                                     ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
124 {
125     return startFilteredReduced<ResultType>(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::createFunctionWrapper(reduce), options);
126 }
127
128 template <typename Sequence, typename KeepFunctor, typename ReduceFunctor>
129 QFuture<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> filteredReduced(const Sequence &sequence,
130                                     KeepFunctor keep,
131                                     ReduceFunctor reduce,
132                                     ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
133 {
134     return startFilteredReduced<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
135             (sequence,
136              QtPrivate::createFunctionWrapper(keep),
137              QtPrivate::createFunctionWrapper(reduce),
138              options);
139 }
140
141 // filteredReduced() on iterators
142 template <typename ResultType, typename Iterator, typename KeepFunctor, typename ReduceFunctor>
143 QFuture<ResultType> filteredReduced(Iterator begin,
144                                     Iterator end,
145                                     KeepFunctor keep,
146                                     ReduceFunctor reduce,
147                                     ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
148 {
149    return startFilteredReduced<ResultType>(begin, end, QtPrivate::createFunctionWrapper(keep), QtPrivate::createFunctionWrapper(reduce), options);
150 }
151
152 template <typename Iterator, typename KeepFunctor, typename ReduceFunctor>
153 QFuture<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType> filteredReduced(Iterator begin,
154                                     Iterator end,
155                                     KeepFunctor keep,
156                                     ReduceFunctor reduce,
157                                     ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
158 {
159    return startFilteredReduced<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
160            (begin, end,
161             QtPrivate::createFunctionWrapper(keep),
162             QtPrivate::createFunctionWrapper(reduce),
163             options);
164 }
165
166 // filtered() on sequences
167 template <typename Sequence, typename KeepFunctor>
168 QFuture<typename Sequence::value_type> filtered(const Sequence &sequence, KeepFunctor keep)
169 {
170     return startFiltered(sequence, QtPrivate::createFunctionWrapper(keep));
171 }
172
173 // filtered() on iterators
174 template <typename Iterator, typename KeepFunctor>
175 QFuture<typename qValueType<Iterator>::value_type> filtered(Iterator begin, Iterator end, KeepFunctor keep)
176 {
177     return startFiltered(begin, end, QtPrivate::createFunctionWrapper(keep));
178 }
179
180 // blocking filter() on sequences
181 template <typename Sequence, typename KeepFunctor>
182 void blockingFilter(Sequence &sequence, KeepFunctor keep)
183 {
184     filterInternal(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::PushBackWrapper()).startBlocking();
185 }
186
187 // blocking filteredReduced() on sequences
188 template <typename ResultType, typename Sequence, typename KeepFunctor, typename ReduceFunctor>
189 ResultType blockingFilteredReduced(const Sequence &sequence,
190                                    KeepFunctor keep,
191                                    ReduceFunctor reduce,
192                                    ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
193 {
194     return startFilteredReduced<ResultType>(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::createFunctionWrapper(reduce), options)
195         .startBlocking();
196 }
197
198 template <typename Sequence, typename KeepFunctor, typename ReduceFunctor>
199 typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType blockingFilteredReduced(const Sequence &sequence,
200                                    KeepFunctor keep,
201                                    ReduceFunctor reduce,
202                                    ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
203 {
204     return blockingFilteredReduced<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
205         (sequence,
206          QtPrivate::createFunctionWrapper(keep),
207          QtPrivate::createFunctionWrapper(reduce),
208          options);
209 }
210
211 // blocking filteredReduced() on iterators
212 template <typename ResultType, typename Iterator, typename KeepFunctor, typename ReduceFunctor>
213 ResultType blockingFilteredReduced(Iterator begin,
214                                    Iterator end,
215                                    KeepFunctor keep,
216                                    ReduceFunctor reduce,
217                                    ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
218 {
219     return startFilteredReduced<ResultType>
220         (begin, end,
221          QtPrivate::createFunctionWrapper(keep),
222          QtPrivate::createFunctionWrapper(reduce),
223          options)
224         .startBlocking();
225 }
226
227 template <typename Iterator, typename KeepFunctor, typename ReduceFunctor>
228 typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType blockingFilteredReduced(Iterator begin,
229                                    Iterator end,
230                                    KeepFunctor keep,
231                                    ReduceFunctor reduce,
232                                    ReduceOptions options = ReduceOptions(UnorderedReduce | SequentialReduce))
233 {
234     return startFilteredReduced<typename QtPrivate::ReduceResultType<ReduceFunctor>::ResultType>
235         (begin, end,
236          QtPrivate::createFunctionWrapper(keep),
237          QtPrivate::createFunctionWrapper(reduce),
238          options)
239         .startBlocking();
240 }
241
242 // blocking filtered() on sequences
243 template <typename Sequence, typename KeepFunctor>
244 Sequence blockingFiltered(const Sequence &sequence, KeepFunctor keep)
245 {
246     return startFilteredReduced<Sequence>(sequence, QtPrivate::createFunctionWrapper(keep), QtPrivate::PushBackWrapper(), OrderedReduce).startBlocking();
247 }
248
249 // blocking filtered() on iterators
250 template <typename OutputSequence, typename Iterator, typename KeepFunctor>
251 OutputSequence blockingFiltered(Iterator begin, Iterator end, KeepFunctor keep)
252 {
253     return startFilteredReduced<OutputSequence>(begin, end,
254         QtPrivate::createFunctionWrapper(keep),
255         QtPrivate::PushBackWrapper(),
256         OrderedReduce).startBlocking();
257 }
258
259 } // namespace QtConcurrent
260
261 #endif // qdoc
262
263 QT_END_NAMESPACE
264 QT_END_HEADER
265
266 #endif // QT_NO_CONCURRENT
267
268 #endif