15#include <QMutexLocker>
16#include <QFutureInterface>
28 std::atomic_bool IsPaused_ {
false };
30 QMutex FunctionsMutex_;
31 QList<std::function<void ()>> Functions_;
33 using QThread::QThread;
40 QFutureInterface<std::invoke_result_t<F>> iface;
41 iface.reportStarted ();
43 auto reporting = [func, iface] ()
mutable
45 ReportFutureResult (iface, func);
49 QMutexLocker locker { &FunctionsMutex_ };
50 Functions_ << reporting;
55 return iface.future ();
58 template<
typename F,
typename... Args>
61 return ScheduleImpl ([f, args...] ()
mutable { return std::invoke (f, args...); });
64 virtual size_t GetQueueSize ();
78 template<
typename WorkerType>
86 template<
typename WorkerType,
typename... Args>
92 :
Args_ { std::move (tuple) }
98 return std::apply ([] (
auto&&... args) {
return std::make_unique<WorkerType> (std::forward<Args> (args)...); },
Args_);
102 template<
typename WorkerType>
107 return std::make_unique<WorkerType> ();
112 template<
typename WorkerType>
115 std::atomic_bool IsAutoQuit_ {
false };
116 unsigned long QuitWait_ = 2000;
118 using W = WorkerType;
130 template<
typename... Args>
133 ,
Initializer_ { std::make_unique<
detail::Initializer<WorkerType, std::decay_t<Args>...>> (std::tuple<std::decay_t<Args>...> { args... }) }
140 typename = std::enable_if_t<
141 !std::is_base_of<QObject, std::remove_pointer_t<std::decay_t<Head>>>::value
145 :
WorkerThread { static_cast<QObject*> (nullptr), head, rest... }
158 qWarning () << Q_FUNC_INFO
159 <<
"thread is still running";
164 IsAutoQuit_ = autoQuit;
174 template<
typename F,
typename... Args>
177 const auto fWrapped = [f,
this] (
auto... args)
mutable {
return std::invoke (f,
Worker_.get (), args...); };
virtual void Initialize()=0
QFuture< std::invoke_result_t< F > > ScheduleImpl(F func)
QFuture< std::invoke_result_t< F, Args... > > ScheduleImpl(F f, Args &&... args)
WorkerThread(QObject *parent=nullptr)
WorkerThread(const Head &head, const Rest &... rest)
void Initialize() override
void SetQuitWait(unsigned long wait)
WorkerThread(QObject *parent, const Args &... args)
QFuture< std::invoke_result_t< F, WorkerType *, Args... > > ScheduleImpl(F f, Args &&... args)
std::unique_ptr< detail::InitializerBase< WorkerType > > Initializer_
void SetAutoQuit(bool autoQuit)
std::unique_ptr< WorkerType > Worker_
std::unique_ptr< WorkerType > Initialize() override
virtual std::unique_ptr< WorkerType > Initialize()=0
virtual ~InitializerBase()=default
Initializer(std::tuple< Args... > &&tuple)
std::tuple< Args... > Args_
std::unique_ptr< WorkerType > Initialize() override