1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtConcurrent 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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QTCONCURRENT_FUNCTIONWRAPPERS_H
41#define QTCONCURRENT_FUNCTIONWRAPPERS_H
42
43#include <QtConcurrent/qtconcurrentcompilertest.h>
44#include <QtCore/QStringList>
45
46#if !defined(QT_NO_CONCURRENT) || defined(Q_CLANG_QDOC)
47
48QT_BEGIN_NAMESPACE
49
50namespace QtConcurrent {
51
52template <typename T>
53class FunctionWrapper0
54{
55public:
56 typedef T (*FunctionPointerType)();
57 typedef T result_type;
58 inline FunctionWrapper0(FunctionPointerType _functionPointer)
59 :functionPointer(_functionPointer) { }
60
61 inline T operator()()
62 {
63 return functionPointer();
64 }
65private:
66 FunctionPointerType functionPointer;
67};
68
69template <typename T, typename U>
70class FunctionWrapper1
71{
72public:
73 typedef T (*FunctionPointerType)(U u);
74 typedef T result_type;
75 inline FunctionWrapper1(FunctionPointerType _functionPointer)
76 :functionPointer(_functionPointer) { }
77
78 inline T operator()(U u)
79 {
80 return functionPointer(u);
81 }
82
83private:
84 FunctionPointerType functionPointer;
85};
86
87template <typename T, typename U, typename V>
88class FunctionWrapper2
89{
90public:
91 typedef T (*FunctionPointerType)(U u, V v);
92 typedef T result_type;
93 inline FunctionWrapper2(FunctionPointerType _functionPointer)
94 :functionPointer(_functionPointer) { }
95
96 inline T operator()(U u, V v)
97 {
98 return functionPointer(u, v);
99 }
100private:
101 FunctionPointerType functionPointer;
102};
103
104template <typename T, typename C>
105class MemberFunctionWrapper
106{
107public:
108 typedef T (C::*FunctionPointerType)();
109 typedef T result_type;
110 inline MemberFunctionWrapper(FunctionPointerType _functionPointer)
111 :functionPointer(_functionPointer) { }
112
113 inline T operator()(C &c)
114 {
115 return (c.*functionPointer)();
116 }
117private:
118 FunctionPointerType functionPointer;
119};
120
121template <typename T, typename C, typename U>
122class MemberFunctionWrapper1
123{
124public:
125 typedef T (C::*FunctionPointerType)(U);
126 typedef T result_type;
127
128 inline MemberFunctionWrapper1(FunctionPointerType _functionPointer)
129 : functionPointer(_functionPointer)
130 { }
131
132 inline T operator()(C &c, U u)
133 {
134 return (c.*functionPointer)(u);
135 }
136
137private:
138 FunctionPointerType functionPointer;
139};
140
141template <typename T, typename C>
142class ConstMemberFunctionWrapper
143{
144public:
145 typedef T (C::*FunctionPointerType)() const;
146 typedef T result_type;
147 inline ConstMemberFunctionWrapper(FunctionPointerType _functionPointer)
148 :functionPointer(_functionPointer) { }
149
150 inline T operator()(const C &c) const
151 {
152 return (c.*functionPointer)();
153 }
154private:
155 FunctionPointerType functionPointer;
156};
157
158} // namespace QtConcurrent.
159
160namespace QtPrivate {
161
162template <typename T>
163const T& createFunctionWrapper(const T& t)
164{
165 return t;
166}
167
168template <typename T, typename U>
169QtConcurrent::FunctionWrapper1<T, U> createFunctionWrapper(T (*func)(U))
170{
171 return QtConcurrent::FunctionWrapper1<T, U>(func);
172}
173
174template <typename T, typename C>
175QtConcurrent::MemberFunctionWrapper<T, C> createFunctionWrapper(T (C::*func)())
176{
177 return QtConcurrent::MemberFunctionWrapper<T, C>(func);
178}
179
180template <typename T, typename C, typename U>
181QtConcurrent::MemberFunctionWrapper1<T, C, U> createFunctionWrapper(T (C::*func)(U))
182{
183 return QtConcurrent::MemberFunctionWrapper1<T, C, U>(func);
184}
185
186template <typename T, typename C>
187QtConcurrent::ConstMemberFunctionWrapper<T, C> createFunctionWrapper(T (C::*func)() const)
188{
189 return QtConcurrent::ConstMemberFunctionWrapper<T, C>(func);
190}
191
192#if defined(__cpp_noexcept_function_type) && __cpp_noexcept_function_type >= 201510
193template <typename T, typename U>
194QtConcurrent::FunctionWrapper1<T, U> createFunctionWrapper(T (*func)(U) noexcept)
195{
196 return QtConcurrent::FunctionWrapper1<T, U>(func);
197}
198
199template <typename T, typename C>
200QtConcurrent::MemberFunctionWrapper<T, C> createFunctionWrapper(T (C::*func)() noexcept)
201{
202 return QtConcurrent::MemberFunctionWrapper<T, C>(func);
203}
204
205template <typename T, typename C, typename U>
206QtConcurrent::MemberFunctionWrapper1<T, C, U> createFunctionWrapper(T (C::*func)(U) noexcept)
207{
208 return QtConcurrent::MemberFunctionWrapper1<T, C, U>(func);
209}
210
211template <typename T, typename C>
212QtConcurrent::ConstMemberFunctionWrapper<T, C> createFunctionWrapper(T (C::*func)() const noexcept)
213{
214 return QtConcurrent::ConstMemberFunctionWrapper<T, C>(func);
215}
216#endif
217
218struct PushBackWrapper
219{
220 typedef void result_type;
221
222 template <class C, class U>
223 inline void operator()(C &c, const U &u) const
224 {
225 return c.push_back(u);
226 }
227
228 template <class C, class U>
229 inline void operator()(C &c, U &&u) const
230 {
231 return c.push_back(u);
232 }
233};
234
235template <typename Functor, bool foo = HasResultType<Functor>::Value>
236struct LazyResultType { typedef typename Functor::result_type Type; };
237template <typename Functor>
238struct LazyResultType<Functor, false> { typedef void Type; };
239
240template <class T>
241struct ReduceResultType;
242
243template <class U, class V>
244struct ReduceResultType<void(*)(U&,V)>
245{
246 typedef U ResultType;
247};
248
249template <class T, class C, class U>
250struct ReduceResultType<T(C::*)(U)>
251{
252 typedef C ResultType;
253};
254
255#if defined(__cpp_noexcept_function_type) && __cpp_noexcept_function_type >= 201510
256template <class U, class V>
257struct ReduceResultType<void(*)(U&,V) noexcept>
258{
259 typedef U ResultType;
260};
261
262template <class T, class C, class U>
263struct ReduceResultType<T(C::*)(U) noexcept>
264{
265 typedef C ResultType;
266};
267#endif
268
269template <class InputSequence, class MapFunctor>
270struct MapResultType
271{
272 typedef typename LazyResultType<MapFunctor>::Type ResultType;
273};
274
275template <class U, class V>
276struct MapResultType<void, U (*)(V)>
277{
278 typedef U ResultType;
279};
280
281template <class T, class C>
282struct MapResultType<void, T(C::*)() const>
283{
284 typedef T ResultType;
285};
286
287#if defined(__cpp_noexcept_function_type) && __cpp_noexcept_function_type >= 201510
288template <class U, class V>
289struct MapResultType<void, U (*)(V) noexcept>
290{
291 typedef U ResultType;
292};
293
294template <class T, class C>
295struct MapResultType<void, T(C::*)() const noexcept>
296{
297 typedef T ResultType;
298};
299#endif
300
301#ifndef QT_NO_TEMPLATE_TEMPLATE_PARAMETERS
302
303template <template <typename> class InputSequence, typename MapFunctor, typename T>
304struct MapResultType<InputSequence<T>, MapFunctor>
305{
306 typedef InputSequence<typename LazyResultType<MapFunctor>::Type> ResultType;
307};
308
309template <template <typename> class InputSequence, class T, class U, class V>
310struct MapResultType<InputSequence<T>, U (*)(V)>
311{
312 typedef InputSequence<U> ResultType;
313};
314
315template <template <typename> class InputSequence, class T, class U, class C>
316struct MapResultType<InputSequence<T>, U(C::*)() const>
317{
318 typedef InputSequence<U> ResultType;
319};
320
321#if defined(__cpp_noexcept_function_type) && __cpp_noexcept_function_type >= 201510
322
323template <template <typename> class InputSequence, class T, class U, class V>
324struct MapResultType<InputSequence<T>, U (*)(V) noexcept>
325{
326 typedef InputSequence<U> ResultType;
327};
328
329template <template <typename> class InputSequence, class T, class U, class C>
330struct MapResultType<InputSequence<T>, U(C::*)() const noexcept>
331{
332 typedef InputSequence<U> ResultType;
333};
334#endif
335
336#endif // QT_NO_TEMPLATE_TEMPLATE_PARAMETER
337
338template <class MapFunctor>
339struct MapResultType<QStringList, MapFunctor>
340{
341 typedef QList<typename LazyResultType<MapFunctor>::Type> ResultType;
342};
343
344template <class U, class V>
345struct MapResultType<QStringList, U (*)(V)>
346{
347 typedef QList<U> ResultType;
348};
349
350template <class U, class C>
351struct MapResultType<QStringList, U(C::*)() const>
352{
353 typedef QList<U> ResultType;
354};
355
356#if defined(__cpp_noexcept_function_type) && __cpp_noexcept_function_type >= 201510
357
358template <class U, class V>
359struct MapResultType<QStringList, U (*)(V) noexcept>
360{
361 typedef QList<U> ResultType;
362};
363
364template <class U, class C>
365struct MapResultType<QStringList, U(C::*)() const noexcept>
366{
367 typedef QList<U> ResultType;
368};
369#endif
370
371} // namespace QtPrivate.
372
373
374QT_END_NAMESPACE
375
376#endif // QT_NO_CONCURRENT
377
378#endif
379