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 | |
49 | QT_BEGIN_HEADER |
50 | QT_BEGIN_NAMESPACE |
51 | |
52 | QT_MODULE(Core) |
53 | |
54 | #ifndef qdoc |
55 | |
56 | namespace QtConcurrent { |
57 | |
58 | template <typename T> |
59 | class FunctionWrapper0 |
60 | { |
61 | public: |
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 | } |
71 | private: |
72 | FunctionPointerType functionPointer; |
73 | }; |
74 | |
75 | template <typename T, typename U> |
76 | class FunctionWrapper1 |
77 | { |
78 | public: |
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 | |
89 | private: |
90 | FunctionPointerType functionPointer; |
91 | }; |
92 | |
93 | template <typename T, typename U, typename V> |
94 | class FunctionWrapper2 |
95 | { |
96 | public: |
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 | } |
106 | private: |
107 | FunctionPointerType functionPointer; |
108 | }; |
109 | |
110 | template <typename T, typename C> |
111 | class MemberFunctionWrapper |
112 | { |
113 | public: |
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 | } |
123 | private: |
124 | FunctionPointerType functionPointer; |
125 | }; |
126 | |
127 | template <typename T, typename C, typename U> |
128 | class MemberFunctionWrapper1 |
129 | { |
130 | public: |
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 | |
143 | private: |
144 | FunctionPointerType functionPointer; |
145 | }; |
146 | |
147 | template <typename T, typename C> |
148 | class ConstMemberFunctionWrapper |
149 | { |
150 | public: |
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 | } |
160 | private: |
161 | FunctionPointerType functionPointer; |
162 | }; |
163 | |
164 | } // namespace QtConcurrent. |
165 | |
166 | namespace QtPrivate { |
167 | |
168 | template <typename T> |
169 | const T& createFunctionWrapper(const T& t) |
170 | { |
171 | return t; |
172 | } |
173 | |
174 | template <typename T, typename U> |
175 | QtConcurrent::FunctionWrapper1<T, U> createFunctionWrapper(T (*func)(U)) |
176 | { |
177 | return QtConcurrent::FunctionWrapper1<T, U>(func); |
178 | } |
179 | |
180 | template <typename T, typename C> |
181 | QtConcurrent::MemberFunctionWrapper<T, C> createFunctionWrapper(T (C::*func)()) |
182 | { |
183 | return QtConcurrent::MemberFunctionWrapper<T, C>(func); |
184 | } |
185 | |
186 | template <typename T, typename C, typename U> |
187 | QtConcurrent::MemberFunctionWrapper1<T, C, U> createFunctionWrapper(T (C::*func)(U)) |
188 | { |
189 | return QtConcurrent::MemberFunctionWrapper1<T, C, U>(func); |
190 | } |
191 | |
192 | template <typename T, typename C> |
193 | QtConcurrent::ConstMemberFunctionWrapper<T, C> createFunctionWrapper(T (C::*func)() const) |
194 | { |
195 | return QtConcurrent::ConstMemberFunctionWrapper<T, C>(func); |
196 | } |
197 | |
198 | struct 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 | |
217 | template <typename Functor, bool foo = HasResultType<Functor>::Value> |
218 | struct LazyResultType { typedef typename Functor::result_type Type; }; |
219 | template <typename Functor> |
220 | struct LazyResultType<Functor, false> { typedef void Type; }; |
221 | |
222 | template <class T> |
223 | struct ReduceResultType; |
224 | |
225 | template <class U, class V> |
226 | struct ReduceResultType<void(*)(U&,V)> |
227 | { |
228 | typedef U ResultType; |
229 | }; |
230 | |
231 | template <class T, class C, class U> |
232 | struct ReduceResultType<T(C::*)(U)> |
233 | { |
234 | typedef C ResultType; |
235 | }; |
236 | |
237 | template <class InputSequence, class MapFunctor> |
238 | struct MapResultType |
239 | { |
240 | typedef typename LazyResultType<MapFunctor>::Type ResultType; |
241 | }; |
242 | |
243 | template <class U, class V> |
244 | struct MapResultType<void, U (*)(V)> |
245 | { |
246 | typedef U ResultType; |
247 | }; |
248 | |
249 | template <class T, class C> |
250 | struct MapResultType<void, T(C::*)() const> |
251 | { |
252 | typedef T ResultType; |
253 | }; |
254 | |
255 | #ifndef QT_NO_TEMPLATE_TEMPLATE_PARAMETERS |
256 | |
257 | template <template <typename> class InputSequence, typename MapFunctor, typename T> |
258 | struct MapResultType<InputSequence<T>, MapFunctor> |
259 | { |
260 | typedef InputSequence<typename LazyResultType<MapFunctor>::Type> ResultType; |
261 | }; |
262 | |
263 | template <template <typename> class InputSequence, class T, class U, class V> |
264 | struct MapResultType<InputSequence<T>, U (*)(V)> |
265 | { |
266 | typedef InputSequence<U> ResultType; |
267 | }; |
268 | |
269 | template <template <typename> class InputSequence, class T, class U, class C> |
270 | struct MapResultType<InputSequence<T>, U(C::*)() const> |
271 | { |
272 | typedef InputSequence<U> ResultType; |
273 | }; |
274 | |
275 | #endif // QT_NO_TEMPLATE_TEMPLATE_PARAMETER |
276 | |
277 | template <class MapFunctor> |
278 | struct MapResultType<QStringList, MapFunctor> |
279 | { |
280 | typedef QList<typename LazyResultType<MapFunctor>::Type> ResultType; |
281 | }; |
282 | |
283 | template <class U, class V> |
284 | struct MapResultType<QStringList, U (*)(V)> |
285 | { |
286 | typedef QList<U> ResultType; |
287 | }; |
288 | |
289 | template <class U, class C> |
290 | struct MapResultType<QStringList, U(C::*)() const> |
291 | { |
292 | typedef QList<U> ResultType; |
293 | }; |
294 | |
295 | } // namespace QtPrivate. |
296 | |
297 | #endif //qdoc |
298 | |
299 | QT_END_NAMESPACE |
300 | QT_END_HEADER |
301 | |
302 | #endif // QT_NO_CONCURRENT |
303 | |
304 | #endif |
305 | |