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 QtCore 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 ****************************************************************************/
42 #ifndef QTCONCURRENT_FUNCTIONWRAPPERS_H
43 #define QTCONCURRENT_FUNCTIONWRAPPERS_H
45 #include <QtConcurrent/qtconcurrentcompilertest.h>
47 #ifndef QT_NO_CONCURRENT
55 namespace QtConcurrent {
58 class FunctionWrapper0
61 typedef T (*FunctionPointerType)();
62 typedef T result_type;
63 inline FunctionWrapper0(FunctionPointerType _functionPointer)
64 :functionPointer(_functionPointer) { }
68 return functionPointer();
71 FunctionPointerType functionPointer;
74 template <typename T, typename U>
75 class FunctionWrapper1
78 typedef T (*FunctionPointerType)(U u);
79 typedef T result_type;
80 inline FunctionWrapper1(FunctionPointerType _functionPointer)
81 :functionPointer(_functionPointer) { }
83 inline T operator()(U u)
85 return functionPointer(u);
89 FunctionPointerType functionPointer;
92 template <typename T, typename U, typename V>
93 class FunctionWrapper2
96 typedef T (*FunctionPointerType)(U u, V v);
97 typedef T result_type;
98 inline FunctionWrapper2(FunctionPointerType _functionPointer)
99 :functionPointer(_functionPointer) { }
101 inline T operator()(U u, V v)
103 return functionPointer(u, v);
106 FunctionPointerType functionPointer;
109 template <typename T, typename C>
110 class MemberFunctionWrapper
113 typedef T (C::*FunctionPointerType)();
114 typedef T result_type;
115 inline MemberFunctionWrapper(FunctionPointerType _functionPointer)
116 :functionPointer(_functionPointer) { }
118 inline T operator()(C &c)
120 return (c.*functionPointer)();
123 FunctionPointerType functionPointer;
126 template <typename T, typename C, typename U>
127 class MemberFunctionWrapper1
130 typedef T (C::*FunctionPointerType)(U);
131 typedef T result_type;
133 inline MemberFunctionWrapper1(FunctionPointerType _functionPointer)
134 : functionPointer(_functionPointer)
137 inline T operator()(C &c, U u)
139 return (c.*functionPointer)(u);
143 FunctionPointerType functionPointer;
146 template <typename T, typename C>
147 class ConstMemberFunctionWrapper
150 typedef T (C::*FunctionPointerType)() const;
151 typedef T result_type;
152 inline ConstMemberFunctionWrapper(FunctionPointerType _functionPointer)
153 :functionPointer(_functionPointer) { }
155 inline T operator()(const C &c) const
157 return (c.*functionPointer)();
160 FunctionPointerType functionPointer;
163 } // namespace QtConcurrent.
165 namespace QtPrivate {
167 template <typename T>
168 const T& createFunctionWrapper(const T& t)
173 template <typename T, typename U>
174 QtConcurrent::FunctionWrapper1<T, U> createFunctionWrapper(T (*func)(U))
176 return QtConcurrent::FunctionWrapper1<T, U>(func);
179 template <typename T, typename C>
180 QtConcurrent::MemberFunctionWrapper<T, C> createFunctionWrapper(T (C::*func)())
182 return QtConcurrent::MemberFunctionWrapper<T, C>(func);
185 template <typename T, typename C, typename U>
186 QtConcurrent::MemberFunctionWrapper1<T, C, U> createFunctionWrapper(T (C::*func)(U))
188 return QtConcurrent::MemberFunctionWrapper1<T, C, U>(func);
191 template <typename T, typename C>
192 QtConcurrent::ConstMemberFunctionWrapper<T, C> createFunctionWrapper(T (C::*func)() const)
194 return QtConcurrent::ConstMemberFunctionWrapper<T, C>(func);
197 struct PushBackWrapper
199 typedef void result_type;
201 template <class C, class U>
202 inline void operator()(C &c, const U &u) const
204 return c.push_back(u);
207 #ifdef Q_COMPILER_RVALUE_REFS
208 template <class C, class U>
209 inline void operator()(C &c, U &&u) const
211 return c.push_back(u);
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; };
222 struct ReduceResultType;
224 template <class U, class V>
225 struct ReduceResultType<void(*)(U&,V)>
227 typedef U ResultType;
230 template <class T, class C, class U>
231 struct ReduceResultType<T(C::*)(U)>
233 typedef C ResultType;
236 template <class InputSequence, class MapFunctor>
239 typedef typename LazyResultType<MapFunctor>::Type ResultType;
242 template <class U, class V>
243 struct MapResultType<void, U (*)(V)>
245 typedef U ResultType;
248 template <class T, class C>
249 struct MapResultType<void, T(C::*)() const>
251 typedef T ResultType;
254 #ifndef QT_NO_TEMPLATE_TEMPLATE_PARAMETERS
256 template <template <typename> class InputSequence, typename MapFunctor, typename T>
257 struct MapResultType<InputSequence<T>, MapFunctor>
259 typedef InputSequence<typename LazyResultType<MapFunctor>::Type> ResultType;
262 template <template <typename> class InputSequence, class T, class U, class V>
263 struct MapResultType<InputSequence<T>, U (*)(V)>
265 typedef InputSequence<U> ResultType;
268 template <template <typename> class InputSequence, class T, class U, class C>
269 struct MapResultType<InputSequence<T>, U(C::*)() const>
271 typedef InputSequence<U> ResultType;
274 #endif // QT_NO_TEMPLATE_TEMPLATE_PARAMETER
276 template <class MapFunctor>
277 struct MapResultType<QStringList, MapFunctor>
279 typedef QList<typename LazyResultType<MapFunctor>::Type> ResultType;
282 template <class U, class V>
283 struct MapResultType<QStringList, U (*)(V)>
285 typedef QList<U> ResultType;
288 template <class U, class C>
289 struct MapResultType<QStringList, U(C::*)() const>
291 typedef QList<U> ResultType;
294 } // namespace QtPrivate.
301 #endif // QT_NO_CONCURRENT