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_RUN_H
41#define QTCONCURRENT_RUN_H
42
43#include <QtConcurrent/qtconcurrentcompilertest.h>
44
45#if !defined(QT_NO_CONCURRENT) || defined(Q_CLANG_QDOC)
46
47#include <QtConcurrent/qtconcurrentrunbase.h>
48#include <QtConcurrent/qtconcurrentstoredfunctioncall.h>
49
50QT_BEGIN_NAMESPACE
51
52#ifdef Q_CLANG_QDOC
53
54typedef int Function;
55
56namespace QtConcurrent {
57
58 template <typename T>
59 QFuture<T> run(Function function, ...);
60
61 template <typename T>
62 QFuture<T> run(QThreadPool *pool, Function function, ...);
63
64 template <typename T>
65 QFuture<T> runWithPromise(Function function, ...);
66
67 template <typename T>
68 QFuture<T> runWithPromise(QThreadPool *pool, Function function, ...);
69
70} // namespace QtConcurrent
71
72#else
73
74namespace QtConcurrent {
75
76// Note: It's a copy taken from qfuture_impl.h with some specialization added
77// TODO: Get rid of the code repetition and unify this for both purposes, see QTBUG-83331
78template<typename...>
79struct ArgsType;
80
81template<typename Arg, typename... Args>
82struct ArgsType<Arg, Args...>
83{
84 using PromiseType = void;
85 static const bool IsPromise = false;
86};
87
88// Note: this specialization was added
89template<typename Arg, typename... Args>
90struct ArgsType<QPromise<Arg> &, Args...>
91{
92 using PromiseType = Arg;
93 static const bool IsPromise = true;
94};
95
96template<>
97struct ArgsType<>
98{
99 using PromiseType = void;
100 static const bool IsPromise = false;
101};
102
103template<typename F>
104struct ArgResolver : ArgResolver<decltype(&std::decay_t<F>::operator())>
105{
106};
107
108// Note: this specialization was added, see callableObjectWithState() test in qtconcurrentrun
109template<typename F>
110struct ArgResolver<std::reference_wrapper<F>> : ArgResolver<decltype(&std::decay_t<F>::operator())>
111{
112};
113
114template<typename R, typename... Args>
115struct ArgResolver<R(Args...)> : public ArgsType<Args...>
116{
117};
118
119template<typename R, typename... Args>
120struct ArgResolver<R (*)(Args...)> : public ArgsType<Args...>
121{
122};
123
124// Note: this specialization was added, see light() test in qtconcurrentrun
125template<typename R, typename... Args>
126struct ArgResolver<R (*&)(Args...)> : public ArgsType<Args...>
127{
128};
129
130// Note: this specialization was added, see light() test in qtconcurrentrun
131template<typename R, typename... Args>
132struct ArgResolver<R (* const)(Args...)> : public ArgsType<Args...>
133{
134};
135
136template<typename R, typename... Args>
137struct ArgResolver<R (&)(Args...)> : public ArgsType<Args...>
138{
139};
140
141template<typename Class, typename R, typename... Args>
142struct ArgResolver<R (Class::*)(Args...)> : public ArgsType<Args...>
143{
144};
145
146template<typename Class, typename R, typename... Args>
147struct ArgResolver<R (Class::*)(Args...) noexcept> : public ArgsType<Args...>
148{
149};
150
151template<typename Class, typename R, typename... Args>
152struct ArgResolver<R (Class::*)(Args...) const> : public ArgsType<Args...>
153{
154};
155
156template<typename Class, typename R, typename... Args>
157struct ArgResolver<R (Class::*)(Args...) const noexcept> : public ArgsType<Args...>
158{
159};
160
161// Note: this specialization was added, see crefFunction() test in qtconcurrentrun
162template<typename Class, typename R, typename... Args>
163struct ArgResolver<R (Class::* const)(Args...) const> : public ArgsType<Args...>
164{
165};
166
167template<typename Class, typename R, typename... Args>
168struct ArgResolver<R (Class::* const)(Args...) const noexcept> : public ArgsType<Args...>
169{
170};
171
172template <class Function, class ...Args>
173[[nodiscard]]
174auto run(QThreadPool *pool, Function &&f, Args &&...args)
175{
176 return (new StoredFunctionCall<Function, Args...>(
177 std::forward<Function>(f), std::forward<Args>(args)...))->start(pool);
178}
179
180template <class Function, class ...Args>
181[[nodiscard]]
182auto run(Function &&f, Args &&...args)
183{
184 return run(QThreadPool::globalInstance(), std::forward<Function>(f), std::forward<Args>(args)...);
185}
186
187template <class PromiseType, class Function, class ...Args>
188[[nodiscard]]
189auto runWithPromise(QThreadPool *pool, Function &&f, Args &&...args)
190{
191 return (new StoredFunctionCallWithPromise<Function, PromiseType, Args...>(
192 std::forward<Function>(f), std::forward<Args>(args)...))->start(pool);
193}
194
195template <class Function, class ...Args>
196[[nodiscard]]
197auto runWithPromise(QThreadPool *pool, Function &&f, Args &&...args)
198{
199 static_assert(ArgResolver<Function>::IsPromise, "The first argument of passed callable object isn't a QPromise<T> & type.");
200 using PromiseType = typename ArgResolver<Function>::PromiseType;
201 return runWithPromise<PromiseType>(pool, std::forward<Function>(f), std::forward<Args>(args)...);
202}
203
204template <class Function, class ...Args>
205[[nodiscard]]
206auto runWithPromise(QThreadPool *pool, std::reference_wrapper<const Function> &&functionWrapper, Args &&...args)
207{
208 static_assert(ArgResolver<const Function>::IsPromise, "The first argument of passed callable object isn't a QPromise<T> & type.");
209 using PromiseType = typename ArgResolver<const Function>::PromiseType;
210 return runWithPromise<PromiseType>(pool, std::forward<const Function>(functionWrapper.get()), std::forward<Args>(args)...);
211}
212
213template <class PromiseType, class Function, class ...Args>
214[[nodiscard]]
215auto runWithPromise(Function &&f, Args &&...args)
216{
217 return runWithPromise<PromiseType>(QThreadPool::globalInstance(), std::forward<Function>(f), std::forward<Args>(args)...);
218}
219
220template <class Function, class ...Args>
221[[nodiscard]]
222auto runWithPromise(Function &&f, Args &&...args)
223{
224 return runWithPromise(QThreadPool::globalInstance(), std::forward<Function>(f), std::forward<Args>(args)...);
225}
226
227} //namespace QtConcurrent
228
229#endif // Q_CLANG_QDOC
230
231QT_END_NAMESPACE
232
233#endif // QT_NO_CONCURRENT
234
235#endif
236