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