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 | |
50 | QT_BEGIN_NAMESPACE |
51 | |
52 | #ifdef Q_CLANG_QDOC |
53 | |
54 | typedef int Function; |
55 | |
56 | namespace 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 | |
74 | namespace 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 |
78 | template<typename...> |
79 | struct ArgsType; |
80 | |
81 | template<typename Arg, typename... Args> |
82 | struct ArgsType<Arg, Args...> |
83 | { |
84 | using PromiseType = void; |
85 | static const bool IsPromise = false; |
86 | }; |
87 | |
88 | // Note: this specialization was added |
89 | template<typename Arg, typename... Args> |
90 | struct ArgsType<QPromise<Arg> &, Args...> |
91 | { |
92 | using PromiseType = Arg; |
93 | static const bool IsPromise = true; |
94 | }; |
95 | |
96 | template<> |
97 | struct ArgsType<> |
98 | { |
99 | using PromiseType = void; |
100 | static const bool IsPromise = false; |
101 | }; |
102 | |
103 | template<typename F> |
104 | struct ArgResolver : ArgResolver<decltype(&std::decay_t<F>::operator())> |
105 | { |
106 | }; |
107 | |
108 | // Note: this specialization was added, see callableObjectWithState() test in qtconcurrentrun |
109 | template<typename F> |
110 | struct ArgResolver<std::reference_wrapper<F>> : ArgResolver<decltype(&std::decay_t<F>::operator())> |
111 | { |
112 | }; |
113 | |
114 | template<typename R, typename... Args> |
115 | struct ArgResolver<R(Args...)> : public ArgsType<Args...> |
116 | { |
117 | }; |
118 | |
119 | template<typename R, typename... Args> |
120 | struct ArgResolver<R (*)(Args...)> : public ArgsType<Args...> |
121 | { |
122 | }; |
123 | |
124 | // Note: this specialization was added, see light() test in qtconcurrentrun |
125 | template<typename R, typename... Args> |
126 | struct ArgResolver<R (*&)(Args...)> : public ArgsType<Args...> |
127 | { |
128 | }; |
129 | |
130 | // Note: this specialization was added, see light() test in qtconcurrentrun |
131 | template<typename R, typename... Args> |
132 | struct ArgResolver<R (* const)(Args...)> : public ArgsType<Args...> |
133 | { |
134 | }; |
135 | |
136 | template<typename R, typename... Args> |
137 | struct ArgResolver<R (&)(Args...)> : public ArgsType<Args...> |
138 | { |
139 | }; |
140 | |
141 | template<typename Class, typename R, typename... Args> |
142 | struct ArgResolver<R (Class::*)(Args...)> : public ArgsType<Args...> |
143 | { |
144 | }; |
145 | |
146 | template<typename Class, typename R, typename... Args> |
147 | struct ArgResolver<R (Class::*)(Args...) noexcept> : public ArgsType<Args...> |
148 | { |
149 | }; |
150 | |
151 | template<typename Class, typename R, typename... Args> |
152 | struct ArgResolver<R (Class::*)(Args...) const> : public ArgsType<Args...> |
153 | { |
154 | }; |
155 | |
156 | template<typename Class, typename R, typename... Args> |
157 | struct ArgResolver<R (Class::*)(Args...) const noexcept> : public ArgsType<Args...> |
158 | { |
159 | }; |
160 | |
161 | // Note: this specialization was added, see crefFunction() test in qtconcurrentrun |
162 | template<typename Class, typename R, typename... Args> |
163 | struct ArgResolver<R (Class::* const)(Args...) const> : public ArgsType<Args...> |
164 | { |
165 | }; |
166 | |
167 | template<typename Class, typename R, typename... Args> |
168 | struct ArgResolver<R (Class::* const)(Args...) const noexcept> : public ArgsType<Args...> |
169 | { |
170 | }; |
171 | |
172 | template <class Function, class ...Args> |
173 | [[nodiscard]] |
174 | auto 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 | |
180 | template <class Function, class ...Args> |
181 | [[nodiscard]] |
182 | auto run(Function &&f, Args &&...args) |
183 | { |
184 | return run(QThreadPool::globalInstance(), std::forward<Function>(f), std::forward<Args>(args)...); |
185 | } |
186 | |
187 | template <class PromiseType, class Function, class ...Args> |
188 | [[nodiscard]] |
189 | auto 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 | |
195 | template <class Function, class ...Args> |
196 | [[nodiscard]] |
197 | auto 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 | |
204 | template <class Function, class ...Args> |
205 | [[nodiscard]] |
206 | auto 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 | |
213 | template <class PromiseType, class Function, class ...Args> |
214 | [[nodiscard]] |
215 | auto runWithPromise(Function &&f, Args &&...args) |
216 | { |
217 | return runWithPromise<PromiseType>(QThreadPool::globalInstance(), std::forward<Function>(f), std::forward<Args>(args)...); |
218 | } |
219 | |
220 | template <class Function, class ...Args> |
221 | [[nodiscard]] |
222 | auto 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 | |
231 | QT_END_NAMESPACE |
232 | |
233 | #endif // QT_NO_CONCURRENT |
234 | |
235 | #endif |
236 | |