15 #include <QMutexLocker> 16 #include <QFutureInterface> 28 std::atomic_bool IsPaused_ {
false };
30 QMutex FunctionsMutex_;
33 using QThread::QThread;
35 void SetPaused (
bool);
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 ();
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 unsigned long QuitWait_ = 2000;
117 using W = WorkerType;
125 , Initializer_ { std::make_unique<detail::Initializer<WorkerType>> () }
129 template<
typename... Args>
132 , Initializer_ { std::make_unique<detail::Initializer<WorkerType, std::decay_t<Args>...>> (std::tuple<std::decay_t<Args>...> { args... }) }
139 typename = std::enable_if_t<
140 !std::is_base_of<QObject, std::remove_pointer_t<std::decay_t<Head>>>::value
144 :
WorkerThread {
static_cast<QObject*
> (
nullptr), head, rest... }
154 qWarning () << Q_FUNC_INFO
155 <<
"thread is still running";
165 template<
typename F,
typename... Args>
168 const auto fWrapped = [
f,
this] (
auto... args)
mutable {
return std::invoke (
f, Worker_.get (), args...); };
174 Worker_ = Initializer_->Initialize ();
176 Initializer_.reset ();
WorkerThread(const Head &head, const Rest &... rest)
WorkerThread(QObject *parent=nullptr)
void Initialize() override
Initializer(std::tuple< Args... > &&tuple)
void SetQuitWait(unsigned long wait)
QFuture< std::invoke_result_t< F, WorkerType *, Args... > > ScheduleImpl(F f, Args &&... args)
constexpr detail::ExprTree< detail::ExprType::LeafStaticPlaceholder, detail::MemberPtrs< Ptrs... > > tuple
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::invoke_result_t< F > > ScheduleImpl(F func)
std::tuple< Args... > Args_
std::unique_ptr< WorkerType > Initialize() override
QFuture< std::invoke_result_t< F, Args... > > ScheduleImpl(F f, Args &&... args)
std::unique_ptr< WorkerType > Initialize() override