36 #include <QMutexLocker>
37 #include <QFutureInterface>
52 std::atomic_bool IsPaused_ {
false };
54 QMutex FunctionsMutex_;
57 using QThread::QThread;
59 void SetPaused (
bool);
65 iface.reportStarted ();
67 auto reporting = [func, iface] ()
mutable
73 QMutexLocker locker { &FunctionsMutex_ };
74 Functions_ << reporting;
79 return iface.future ();
82 template<
typename F,
typename... Args>
85 return ScheduleImpl ([f, args...] ()
mutable {
return Invoke (f, args...); });
88 virtual size_t GetQueueSize ();
90 void run () override final;
92 virtual
void Initialize () = 0;
93 virtual
void Cleanup () = 0;
102 template<
typename WorkerType>
105 virtual std::unique_ptr<WorkerType> Initialize () = 0;
108 template<
typename WorkerType,
typename... Args>
114 : Args_ { std::move (tuple) }
120 return CPP17::Apply ([] (
auto&&... args) {
return std::make_unique<WorkerType> (std::forward<Args> (args)...); }, Args_);
124 template<
typename WorkerType>
129 return std::make_unique<WorkerType> ();
134 template<
typename WorkerType>
137 std::atomic_bool IsAutoQuit_ {
false };
139 using W = WorkerType;
147 ,
Initializer_ { std::make_unique<detail::Initializer<WorkerType>> () }
151 template<
typename... Args>
154 ,
Initializer_ { std::make_unique<detail::Initializer<WorkerType, std::decay_t<Args>...>> (std::tuple<std::decay_t<Args>...> { args... }) }
161 typename = std::enable_if_t<
162 !std::is_base_of<QObject, std::remove_pointer_t<std::decay_t<Head>>>::value
166 : WorkerThread {
static_cast<QObject*
> (
nullptr), head, rest... }
179 qWarning () << Q_FUNC_INFO
180 <<
"thread is still running";
187 IsAutoQuit_ = autoQuit;
190 template<
typename F,
typename... Args>
193 const auto fWrapped = [f,
this] (
auto... args)
mutable {
return Invoke (f,
Worker_.get (), args...); };
WorkerThread(QObject *parent=nullptr)
std::tuple< Args...> Args_
std::enable_if_t<!std::is_same< R, void >::value > ReportFutureResult(QFutureInterface< R > &iface, F &&f, Args &&...args)
Initializer(std::tuple< Args...> &&tuple)
auto Apply(F &&f, Tuple &&tuple)
void SetAutoQuit(bool autoQuit)
WorkerThread(QObject *parent, const Args &...args)
std::unique_ptr< WorkerType > Initialize() override
std::unique_ptr< WorkerType > Worker_
QFuture< std::result_of_t< F(WorkerType *, Args...)> > ScheduleImpl(F f, Args &&...args)
QFuture< std::result_of_t< F()> > ScheduleImpl(F func)
QFuture< std::result_of_t< F(Args...)> > ScheduleImpl(F f, Args &&...args)
std::unique_ptr< detail::InitializerBase< WorkerType > > Initializer_
auto Invoke(F &&f, Args &&...args) -> decltype(std::forward< F >(f)(std::forward< Args >(args)...))
void Initialize() override
std::unique_ptr< WorkerType > Initialize() override