This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasync.h
305 lines (259 loc) · 11.5 KB
/
async.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/****************************************************************************
**
** Copyright (C) 2019 Ömer Göktaş
** Contact: omergoktas.com
**
** This file is part of the Async library.
**
** The Async is free software: you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public License
** version 3 as published by the Free Software Foundation.
**
** The Async is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with the Async. If not, see
** <https://www.gnu.org/licenses/>.
**
****************************************************************************/
#ifndef ASYNC_H
#define ASYNC_H
#include "optional.h"
#include "async_global.h"
#include <QCoreApplication>
#include <QFuture>
#include <QThreadPool>
namespace Async {
using StackSize = Optional<uint>;
template <typename...> using void_t = void;
template <typename T, typename = void>
struct is_overload_viable : std::false_type {};
template <typename T>
struct is_overload_viable<T, void_t<std::result_of_t<T>>> : std::true_type {};
template <typename F, typename... A>
struct is_future_overload : is_overload_viable<std::decay_t<F>(QFutureInterfaceBase*, std::decay_t<A>...)> {};
template <typename F, typename... A>
static constexpr const bool is_future_overload_v = is_future_overload<F, A...>::value;
template <typename F, typename... A>
using result_of_t = typename std::conditional_t<is_future_overload_v<F, A...>,
std::result_of<std::decay_t<F>(QFutureInterfaceBase*, std::decay_t<A>...)>,
std::result_of<std::decay_t<F>(std::decay_t<A>...)>>::type;
namespace Internal {
/*!
void (QFutureInterface*) | Plain call - No report
void (no QFutureInterface*) | Plain call - No need to report
non-void (QFutureInterface*) | Plain call - No report
non-void (no QFutureInterface*) | Report call - Report the result
*/
// Plain call
template <typename ResultType, typename Function, typename... Args>
void call(std::true_type, QFutureInterface<ResultType>*, Function&& function, Args&&... args)
{
std::forward<Function>(function)(std::forward<Args>(args)...);
}
// Report call
template <typename ResultType, typename Function, typename... Args>
void call(std::false_type, QFutureInterface<ResultType>* future, Function&& function, Args&&... args)
{
future->reportResult(std::forward<Function>(function)(std::forward<Args>(args)...));
}
// Call with QFutureInterface*
template <typename ResultType, typename Function, typename... Args>
void dispatchFuture(std::true_type, QFutureInterface<ResultType>* future, Function&& function, Args&&... args)
{
call(std::true_type(), future, std::forward<Function>(function),
future, std::forward<Args>(args)...);
}
// Call without QFutureInterface*
template <typename ResultType, typename Function, typename... Args>
void dispatchFuture(std::false_type, QFutureInterface<ResultType>* future, Function&& function, Args&&... args)
{
call(std::is_void<std::result_of_t<Function(Args...)>>(), future,
std::forward<Function>(function), std::forward<Args>(args)...);
}
template <typename ResultType, typename Function, typename... Args>
class Runnable final : public QRunnable
{
Q_DISABLE_COPY(Runnable)
using FunctionStorage = std::tuple<std::decay_t<Function>, std::decay_t<Args>...>;
public:
Runnable(Function&& function, Args&&... args) : m_priority(QThread::InheritPriority)
, m_functionStorage(std::decay_t<Function>(std::forward<Function>(function)),
std::decay_t<Args>(std::forward<Args>(args))...)
{
m_futureInterface.setRunnable(this);
m_futureInterface.reportStarted();
}
~Runnable() override
{
m_futureInterface.reportFinished();
}
QFuture<ResultType> future()
{
return m_futureInterface.future();
}
void setThreadPool(QThreadPool* threadPool)
{
m_futureInterface.setThreadPool(threadPool);
}
void setThreadPriority(QThread::Priority priority)
{
m_priority = priority;
}
void run() override
{
if (m_priority != QThread::InheritPriority) {
if (QThread* thread = QThread::currentThread()) {
if (thread != qApp->thread())
thread->setPriority(m_priority);
}
}
if (m_futureInterface.isCanceled()) {
m_futureInterface.reportFinished();
return;
}
dispatch(std::make_index_sequence<std::tuple_size<FunctionStorage>::value>());
}
private:
template <std::size_t... index>
void dispatch(std::index_sequence<index...>)
{
dispatchFuture(is_future_overload<Function, Args...>(),
&m_futureInterface, std::move(std::get<index>(m_functionStorage))...);
if (m_futureInterface.isPaused())
m_futureInterface.waitForResume();
m_futureInterface.reportFinished();
}
private:
QThread::Priority m_priority;
FunctionStorage m_functionStorage;
QFutureInterface<ResultType> m_futureInterface;
};
class ASYNC_EXPORT RunnableThread final : public QThread
{
Q_OBJECT
Q_DISABLE_COPY(RunnableThread)
public:
explicit RunnableThread(QRunnable* runnable, QObject* parent = nullptr);
private:
void run() override;
private:
QRunnable* m_runnable;
};
template <typename ResultType, typename Function, typename... Args>
auto run(QThreadPool* pool, QThread::Priority priority, const StackSize& stackSize,
Function&& function, Args&&... args)
{
auto runnable = new Internal::Runnable<ResultType, Function, Args...>
(std::forward<Function>(function), std::forward<Args>(args)...);
runnable->setThreadPriority(priority);
if (pool) {
Q_ASSERT(!stackSize);
runnable->setThreadPool(pool);
pool->start(runnable);
} else {
auto thread = new Internal::RunnableThread(runnable);
if (stackSize)
thread->setStackSize(stackSize.value());
thread->moveToThread(qApp->thread());
QObject::connect(thread, &QThread::finished, thread, &QObject::deleteLater);
thread->start(priority);
}
return runnable->future();
}
} // Internal
template <typename ResultType, typename Function, typename... Args>
auto run(QThreadPool* pool, QThread::Priority priority, Function&& function, Args&&... args)
{
return Internal::run<ResultType>(pool, priority, StackSize(),
std::forward<Function>(function), std::forward<Args>(args)...);
}
template <typename Function, typename... Args>
auto run(QThreadPool* pool, QThread::Priority priority, Function&& function, Args&&... args)
{
return Internal::run<result_of_t<Function, Args...>>(pool, priority, StackSize(),
std::forward<Function>(function),
std::forward<Args>(args)...);
}
template <typename ResultType, typename Function, typename... Args>
auto run(const StackSize& stackSize, QThread::Priority priority, Function&& function, Args&&... args)
{
return Internal::run<ResultType>(nullptr, priority, stackSize,
std::forward<Function>(function), std::forward<Args>(args)...);
}
template <typename Function, typename... Args>
auto run(const StackSize& stackSize, QThread::Priority priority, Function&& function, Args&&... args)
{
return Internal::run<result_of_t<Function, Args...>>(nullptr, priority, stackSize,
std::forward<Function>(function),
std::forward<Args>(args)...);
}
template <typename ResultType, typename Function, typename... Args>
auto run(QThread::Priority priority, Function&& function, Args&&... args)
{
return Internal::run<ResultType>(nullptr, priority, StackSize(),
std::forward<Function>(function), std::forward<Args>(args)...);
}
template <typename Function, typename... Args>
auto run(QThread::Priority priority, Function&& function, Args&&... args)
{
return Internal::run<result_of_t<Function, Args...>>(nullptr, priority, StackSize(),
std::forward<Function>(function),
std::forward<Args>(args)...);
}
template <typename ResultType, typename Function, typename... Args,
typename = std::enable_if_t<!std::is_same<std::decay_t<Function>, QThread::Priority>::value>>
auto run(const StackSize& stackSize, Function&& function, Args&&... args)
{
return Internal::run<ResultType>(nullptr, QThread::InheritPriority, stackSize,
std::forward<Function>(function), std::forward<Args>(args)...);
}
template <typename Function, typename... Args,
typename = std::enable_if_t<!std::is_same<std::decay_t<Function>, QThread::Priority>::value>>
auto run(const StackSize& stackSize, Function&& function, Args&&... args)
{
return Internal::run<result_of_t<Function, Args...>>(nullptr, QThread::InheritPriority, stackSize,
std::forward<Function>(function),
std::forward<Args>(args)...);
}
template <typename ResultType, typename Function, typename... Args,
typename = std::enable_if_t<!std::is_same<std::decay_t<Function>, QThread::Priority>::value>>
auto run(QThreadPool* pool, Function&& function, Args&&... args)
{
return Internal::run<ResultType>(pool, QThread::InheritPriority, StackSize(),
std::forward<Function>(function), std::forward<Args>(args)...);
}
template <typename Function, typename... Args,
typename = std::enable_if_t<!std::is_same<std::decay_t<Function>, QThread::Priority>::value>>
auto run(QThreadPool* pool, Function&& function, Args&&... args)
{
return Internal::run<result_of_t<Function, Args...>>(pool, QThread::InheritPriority, StackSize(),
std::forward<Function>(function),
std::forward<Args>(args)...);
}
template <typename ResultType, typename Function, typename... Args,
typename = std::enable_if_t<
!std::is_convertible<Function, StackSize>::value &&
!std::is_same<std::decay_t<Function>, QThreadPool>::value &&
!std::is_same<std::decay_t<Function>, QThread::Priority>::value>>
auto run(Function&& function, Args&&... args)
{
return Internal::run<ResultType>(nullptr, QThread::InheritPriority, StackSize(),
std::forward<Function>(function), std::forward<Args>(args)...);
}
template <typename Function, typename... Args,
typename = std::enable_if_t<
!std::is_convertible<Function, StackSize>::value &&
!std::is_same<std::decay_t<Function>, QThreadPool>::value &&
!std::is_same<std::decay_t<Function>, QThread::Priority>::value>>
auto run(Function&& function, Args&&... args)
{
return Internal::run<result_of_t<Function, Args...>>(nullptr, QThread::InheritPriority, StackSize(),
std::forward<Function>(function),
std::forward<Args>(args)...);
}
} // Async
#endif // ASYNC_H