36 #include <QMutexLocker> 37 #include <QFutureInterface> 51 std::atomic_bool IsPaused_ {
false };
53 QMutex FunctionsMutex_;
56 using QThread::QThread;
58 void SetPaused (
bool);
64 iface.reportStarted ();
66 auto reporting = [func, iface] ()
mutable 68 ReportFutureResult (iface, func);
72 QMutexLocker locker { &FunctionsMutex_ };
73 Functions_ << reporting;
78 return iface.future ();
81 template<
typename F,
typename... Args>
84 return ScheduleImpl ([
f, args...] ()
mutable { return std::invoke (f, args...); });
87 virtual size_t GetQueueSize ();
91 virtual
void Initialize () = 0;
92 virtual
void Cleanup () = 0;
101 template<
typename WorkerType>
104 virtual std::unique_ptr<WorkerType> Initialize () = 0;
109 template<
typename WorkerType,
typename... Args>
115 : Args_ { std::move (tuple) }
121 return std::apply ([] (
auto&&... args) {
return std::make_unique<WorkerType> (std::forward<Args> (args)...); }, Args_);
125 template<
typename WorkerType>
130 return std::make_unique<WorkerType> ();
135 template<
typename WorkerType>
138 std::atomic_bool IsAutoQuit_ {
false };
139 unsigned long QuitWait_ = 2000;
141 using W = WorkerType;
149 , Initializer_ { std::make_unique<detail::Initializer<WorkerType>> () }
153 template<
typename... Args>
156 , Initializer_ { std::make_unique<detail::Initializer<WorkerType, std::decay_t<Args>...>> (std::tuple<std::decay_t<Args>...> { args... }) }
163 typename = std::enable_if_t<
164 !std::is_base_of<QObject, std::remove_pointer_t<std::decay_t<Head>>>::value
168 :
WorkerThread {
static_cast<QObject*
> (
nullptr), head, rest... }
181 qWarning () << Q_FUNC_INFO
182 <<
"thread is still running";
187 IsAutoQuit_ = autoQuit;
197 template<
typename F,
typename... Args>
200 const auto fWrapped = [
f,
this] (
auto... args)
mutable {
return std::invoke (
f, Worker_.get (), args...); };
206 Worker_ = Initializer_->Initialize ();
208 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