6#ifndef SWIFT_MISC_WORKER_H
7#define SWIFT_MISC_WORKER_H
19#include <QMutexLocker>
65 std::atomic<void *> m_handle {
nullptr };
96 template <
typename T,
typename F>
97 void then(T *context, F functor)
104 private_ns::invokeSlot(functor, context);
110 template <
typename F>
131 template <
typename F>
143 template <
typename F>
155 template <
typename F1,
typename F2>
171 void waitForFinished() noexcept;
175 void abandon() noexcept;
178 void abandonAndWait() noexcept;
214 virtual void quit() noexcept {}
215 virtual void quitAndWait() noexcept { waitForFinished(); }
217 bool m_started =
false;
218 bool m_finished =
false;
242 template <
typename F>
246 int typeId = qMetaTypeId<std::decay_t<decltype(std::forward<F>(task)())>>();
247 return fromTaskImpl(owner, name, typeId, [task = std::forward<F>(task)]()
mutable {
248 if constexpr (std::is_void_v<
decltype(task())>)
263 template <
typename R,
typename F>
266 Q_ASSERT_X(m_result.canConvert<R>(), Q_FUNC_INFO,
"Type in thenWithResult must match return type of task");
267 then([
this, functor]() { functor(this->resultNoWait<R>()); });
273 template <
typename R,
typename T,
typename F>
276 Q_ASSERT_X(m_result.canConvert<R>(), Q_FUNC_INFO,
"Type in thenWithResult must match return type of task");
281 auto promise = std::make_shared<std::promise<R>>();
282 auto future = promise->get_future().share();
283 then([
this, promise]() { promise->set_value(this->resultNoWait<R>()); });
284 then(context, [context, functor, future]() { private_ns::invokeSlot(functor, context, future.get()); });
290 template <
typename R>
294 return this->resultNoWait<R>();
303 static CWorker *fromTaskImpl(
QObject *owner,
const QString &name,
int typeId,
304 const std::function<
QVariant()> &task);
306 template <
typename R>
309 Q_ASSERT(m_result.canConvert<R>());
310 return m_result.value<R>();
338 void quit()
noexcept final;
374 void setEnabled(
bool enabled) { m_enabled = enabled; }
376 using CWorkerBase::setStarted;
377 using CWorkerBase::setFinished;
381 std::atomic<bool> m_enabled {
true };
virtual void beforeQuit() noexcept
Called before quit is called.
bool isEnabled() const
Enabled (running)?
const QObject * owner() const
Owner of the worker.
virtual void cleanup()
Called when the thread is finished.
void quit() noexcept final
Stops the thread the next time around its event loop. The thread and the worker will then be deleted.
virtual void initialize()
Called when the thread is started.
virtual unsigned long waitTimeoutMs() const
Wait time for quitAndWait, 0 means not waiting.
void quitAndWait() noexcept final
Calls quit() and blocks until the thread is finished.
void start(QThread::Priority priority=QThread::InheritPriority)
Starts a thread and moves the worker into it.
CContinuousWorker(QObject *owner, const QString &name)
Constructor.
const QString & getName()
Name of the worker.
Just a subclass of QThread whose destructor waits for the thread to finish.
CRegularThread(QObject *parent=nullptr)
Constructor.
CRegularThread & operator=(CRegularThread &&)=delete
Move assignment.
CRegularThread & operator=(const CRegularThread &)=delete
Copy assignment.
CRegularThread(CRegularThread &&)=delete
Move constructor.
CRegularThread(const CRegularThread &)=delete
Copy constructor.
CWorkerBase & operator=(const CWorkerBase &)=delete
Copy assignment.
void doIfFinished(F functor) const
Executes some code (in the caller's thread) if the task has finished.
void aboutToStart()
Emitted when the task is about to start.
void then(T *context, F functor)
Connects to a functor or method which will be called when the task is finished.
void setStarted()
Mark the task as started.
CWorkerBase(const CWorkerBase &)=delete
Copy constructor.
CWorkerBase(CWorkerBase &&)=delete
Move constructor.
static const QStringList & getLogCategories()
Log categories.
void finished()
Emitted when the task is finished.
void then(F functor)
Connects to a functor which will be called when the task is finished.
static const QSet< CWorkerBase * > & allWorkers()
All workers currently existing.
bool isAbandoned() const
For the task to check whether it can finish early.
bool isFinished() const
Returns true if the task has finished.
CWorkerBase & operator=(CWorkerBase &&)=delete
Move assignment.
void doIfNotFinished(F functor) const
Executes some code (in the caller's thread) if the task has not finished.
bool hasStarted() const
True if the worker has started.
void doIfFinishedElse(F1 ifFunctor, F2 elseFunctor) const
Executes some code (in the caller's thread) if the task has finished and some different code if it ha...
void setFinished()
Mark the task as finished.
void waitForFinished() noexcept
Blocks until the task is finished.
Class for doing some arbitrary parcel of work in its own thread.
R result()
Returns the result of the task, waiting for it to finish if necessary.
static CWorker * fromTask(QObject *owner, const QString &name, F &&task)
Returns a new worker object which lives in a new thread.
void thenWithResult(T *context, F functor)
Connects to a functor or method to which will be passed the result when the task is finished.
void thenWithResult(F functor)
Connects to a functor to which will be passed the result when the task is finished.
Free functions in swift::misc.
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
QThread * currentThread()
QVariant fromValue(T &&value)
#define SWIFT_MISC_EXPORT
Export a class or function from the library.