forked from MatsuriDayo/nekoray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQThreadCreateThread.hpp
39 lines (31 loc) · 1.13 KB
/
QThreadCreateThread.hpp
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
#pragma once
#include <future>
#include <QThread>
// FOR OLD QT
class QThreadCreateThread : public QThread {
public:
explicit QThreadCreateThread(std::future<void> &&future)
: m_future(std::move(future)) {
// deleteLater
connect(this, &QThread::finished, this, &QThread::deleteLater);
}
private:
void run() override {
m_future.get();
}
std::future<void> m_future;
};
inline QThread *createThreadImpl(std::future<void> &&future) {
return new QThreadCreateThread(std::move(future));
}
template<typename Function, typename... Args>
QThread *createQThread(Function &&f, Args &&... args) {
using DecayedFunction = typename std::decay<Function>::type;
auto threadFunction =
[f = static_cast<DecayedFunction>(std::forward<Function>(f))](auto &&... largs) mutable -> void {
(void) std::invoke(std::move(f), std::forward<decltype(largs)>(largs)...);
};
return createThreadImpl(std::async(std::launch::deferred,
std::move(threadFunction),
std::forward<Args>(args)...));
}