15 #include <QMutexLocker> 16 #include <QFutureInterface> 28 std::atomic_bool IsPaused_ {
false };
30 QMutex FunctionsMutex_;
33 using QThread::QThread;
35 void SetPaused (
bool);
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 ();
68 virtual
void Initialize () = 0;
69 virtual
void Cleanup () = 0;
78 template<
typename WorkerType>
81 virtual std::unique_ptr<WorkerType> Initialize () = 0;
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;
126 , Initializer_ { std::make_unique<detail::Initializer<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...); };
183 Worker_ = Initializer_->Initialize ();
185 Initializer_.reset ();
WorkerThread(const Head &head, const Rest &... rest)
WorkerThread(QObject *parent=nullptr)
void Initialize() override
void SetAutoQuit(bool autoQuit)
Initializer(std::tuple< Args... > &&tuple)
void SetQuitWait(unsigned long wait)
WorkerThread(QObject *parent, const Args &... args)
constexpr detail::ExprTree< detail::ExprType::LeafStaticPlaceholder, detail::MemberPtrs< Ptr > > f
std::unique_ptr< WorkerType > Worker_
std::unique_ptr< detail::InitializerBase< WorkerType > > Initializer_
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::tuple< Args... > Args_
std::unique_ptr< WorkerType > Initialize() override
std::unique_ptr< WorkerType > Initialize() override