Partially revert e84e86dc.
[profile/ivi/qtbase.git] / src / concurrent / qtconcurrentfunctionwrappers.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_FUNCTIONWRAPPERS_H
43 #define QTCONCURRENT_FUNCTIONWRAPPERS_H
44
45 #include <QtConcurrent/qtconcurrentcompilertest.h>
46
47 #ifndef QT_NO_CONCURRENT
48
49 QT_BEGIN_HEADER
50 QT_BEGIN_NAMESPACE
51
52
53 #ifndef qdoc
54
55 namespace QtConcurrent {
56
57 template <typename T>
58 class FunctionWrapper0
59 {
60 public:
61     typedef T (*FunctionPointerType)();
62     typedef T result_type;
63     inline FunctionWrapper0(FunctionPointerType _functionPointer)
64     :functionPointer(_functionPointer) { }
65
66     inline T operator()()
67     {
68         return functionPointer();
69     }
70 private:
71     FunctionPointerType functionPointer;
72 };
73
74 template <typename T, typename U>
75 class FunctionWrapper1
76 {
77 public:
78     typedef T (*FunctionPointerType)(U u);
79     typedef T result_type;
80     inline FunctionWrapper1(FunctionPointerType _functionPointer)
81     :functionPointer(_functionPointer) { }
82
83     inline T operator()(U u)
84     {
85         return functionPointer(u);
86     }
87
88 private:
89     FunctionPointerType functionPointer;
90 };
91
92 template <typename T, typename U, typename V>
93 class FunctionWrapper2
94 {
95 public:
96     typedef T (*FunctionPointerType)(U u, V v);
97     typedef T result_type;
98     inline FunctionWrapper2(FunctionPointerType _functionPointer)
99     :functionPointer(_functionPointer) { }
100
101     inline T operator()(U u, V v)
102     {
103         return functionPointer(u, v);
104     }
105 private:
106     FunctionPointerType functionPointer;
107 };
108
109 template <typename T, typename C>
110 class MemberFunctionWrapper
111 {
112 public:
113     typedef T (C::*FunctionPointerType)();
114     typedef T result_type;
115     inline MemberFunctionWrapper(FunctionPointerType _functionPointer)
116     :functionPointer(_functionPointer) { }
117
118     inline T operator()(C &c)
119     {
120         return (c.*functionPointer)();
121     }
122 private:
123     FunctionPointerType functionPointer;
124 };
125
126 template <typename T, typename C, typename U>
127 class MemberFunctionWrapper1
128 {
129 public:
130     typedef T (C::*FunctionPointerType)(U);
131     typedef T result_type;
132
133     inline MemberFunctionWrapper1(FunctionPointerType _functionPointer)
134         : functionPointer(_functionPointer)
135     { }
136
137     inline T operator()(C &c, U u)
138     {
139         return (c.*functionPointer)(u);
140     }
141
142 private:
143     FunctionPointerType functionPointer;
144 };
145
146 template <typename T, typename C>
147 class ConstMemberFunctionWrapper
148 {
149 public:
150     typedef T (C::*FunctionPointerType)() const;
151     typedef T result_type;
152     inline ConstMemberFunctionWrapper(FunctionPointerType _functionPointer)
153     :functionPointer(_functionPointer) { }
154
155     inline T operator()(const C &c) const
156     {
157         return (c.*functionPointer)();
158     }
159 private:
160     FunctionPointerType functionPointer;
161 };
162
163 } // namespace QtConcurrent.
164
165 namespace QtPrivate {
166
167 template <typename T>
168 const T& createFunctionWrapper(const T& t)
169 {
170     return t;
171 }
172
173 template <typename T, typename U>
174 QtConcurrent::FunctionWrapper1<T, U> createFunctionWrapper(T (*func)(U))
175 {
176     return QtConcurrent::FunctionWrapper1<T, U>(func);
177 }
178
179 template <typename T, typename C>
180 QtConcurrent::MemberFunctionWrapper<T, C> createFunctionWrapper(T (C::*func)())
181 {
182     return QtConcurrent::MemberFunctionWrapper<T, C>(func);
183 }
184
185 template <typename T, typename C, typename U>
186 QtConcurrent::MemberFunctionWrapper1<T, C, U> createFunctionWrapper(T (C::*func)(U))
187 {
188     return QtConcurrent::MemberFunctionWrapper1<T, C, U>(func);
189 }
190
191 template <typename T, typename C>
192 QtConcurrent::ConstMemberFunctionWrapper<T, C> createFunctionWrapper(T (C::*func)() const)
193 {
194     return QtConcurrent::ConstMemberFunctionWrapper<T, C>(func);
195 }
196
197 struct PushBackWrapper
198 {
199     typedef void result_type;
200
201     template <class C, class U>
202     inline void operator()(C &c, const U &u) const
203     {
204         return c.push_back(u);
205     }
206
207 #ifdef Q_COMPILER_RVALUE_REFS
208     template <class C, class U>
209     inline void operator()(C &c, U &&u) const
210     {
211         return c.push_back(u);
212     }
213 #endif
214 };
215
216 template <typename Functor, bool foo = HasResultType<Functor>::Value>
217 struct LazyResultType { typedef typename Functor::result_type Type; };
218 template <typename Functor>
219 struct LazyResultType<Functor, false> { typedef void Type; };
220
221 template <class T>
222 struct ReduceResultType;
223
224 template <class U, class V>
225 struct ReduceResultType<void(*)(U&,V)>
226 {
227     typedef U ResultType;
228 };
229
230 template <class T, class C, class U>
231 struct ReduceResultType<T(C::*)(U)>
232 {
233     typedef C ResultType;
234 };
235
236 template <class InputSequence, class MapFunctor>
237 struct MapResultType
238 {
239     typedef typename LazyResultType<MapFunctor>::Type ResultType;
240 };
241
242 template <class U, class V>
243 struct MapResultType<void, U (*)(V)>
244 {
245     typedef U ResultType;
246 };
247
248 template <class T, class C>
249 struct MapResultType<void, T(C::*)() const>
250 {
251     typedef T ResultType;
252 };
253
254 #ifndef QT_NO_TEMPLATE_TEMPLATE_PARAMETERS
255
256 template <template <typename> class InputSequence, typename MapFunctor, typename T>
257 struct MapResultType<InputSequence<T>, MapFunctor>
258 {
259     typedef InputSequence<typename LazyResultType<MapFunctor>::Type> ResultType;
260 };
261
262 template <template <typename> class InputSequence, class T, class U, class V>
263 struct MapResultType<InputSequence<T>, U (*)(V)>
264 {
265     typedef InputSequence<U> ResultType;
266 };
267
268 template <template <typename> class InputSequence, class T, class U, class C>
269 struct MapResultType<InputSequence<T>, U(C::*)() const>
270 {
271     typedef InputSequence<U> ResultType;
272 };
273
274 #endif // QT_NO_TEMPLATE_TEMPLATE_PARAMETER
275
276 template <class MapFunctor>
277 struct MapResultType<QStringList, MapFunctor>
278 {
279     typedef QList<typename LazyResultType<MapFunctor>::Type> ResultType;
280 };
281
282 template <class U, class V>
283 struct MapResultType<QStringList, U (*)(V)>
284 {
285     typedef QList<U> ResultType;
286 };
287
288 template <class U, class C>
289 struct MapResultType<QStringList, U(C::*)() const>
290 {
291     typedef QList<U> ResultType;
292 };
293
294 } // namespace QtPrivate.
295
296 #endif //qdoc
297
298 QT_END_NAMESPACE
299 QT_END_HEADER
300
301 #endif // QT_NO_CONCURRENT
302
303 #endif