swift
Public Member Functions | Static Public Member Functions | List of all members
swift::misc::CWorker Class Referencefinal

Class for doing some arbitrary parcel of work in its own thread. More...

Inheritance diagram for swift::misc::CWorker:
[legend]

Public Member Functions

template<typename R , typename F >
void thenWithResult (F functor)
 Connects to a functor to which will be passed the result when the task is finished. More...
 
template<typename R , typename T , typename F >
void thenWithResult (T *context, F functor)
 Connects to a functor or method to which will be passed the result when the task is finished. More...
 
template<typename R >
result ()
 Returns the result of the task, waiting for it to finish if necessary. More...
 
- Public Member Functions inherited from swift::misc::CWorkerBase
template<typename T , typename F >
void then (T *context, F functor)
 Connects to a functor or method which will be called when the task is finished. More...
 
template<typename F >
void then (F functor)
 Connects to a functor which will be called when the task is finished. More...
 
bool isFinished () const
 Returns true if the task has finished. More...
 
template<typename F >
void doIfFinished (F functor) const
 Executes some code (in the caller's thread) if the task has finished. More...
 
template<typename F >
void doIfNotFinished (F functor) const
 Executes some code (in the caller's thread) if the task has not finished. More...
 
template<typename F1 , typename F2 >
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 has not finished. More...
 
void waitForFinished () noexcept
 Blocks until the task is finished. More...
 
void abandon () noexcept
 Notify the task that its result is no longer needed, so it can finish early. More...
 
void abandonAndWait () noexcept
 Convenience to call abandon() followed by waitForFinished().
 

Static Public Member Functions

template<typename F >
static CWorkerfromTask (QObject *owner, const QString &name, F &&task)
 Returns a new worker object which lives in a new thread. More...
 
- Static Public Member Functions inherited from swift::misc::CWorkerBase
static const QStringList & getLogCategories ()
 Log categories.
 
static const QSet< CWorkerBase * > & allWorkers ()
 All workers currently existing.
 

Additional Inherited Members

- Signals inherited from swift::misc::CWorkerBase
void aboutToStart ()
 Emitted when the task is about to start.
 
void finished ()
 Emitted when the task is finished. More...
 
- Protected Member Functions inherited from swift::misc::CWorkerBase
 CWorkerBase ()
 Constructor.
 
 ~CWorkerBase ()
 Destructor.
 
bool isAbandoned () const
 For the task to check whether it can finish early. More...
 
bool hasStarted () const
 True if the worker has started.
 
void setStarted ()
 Mark the task as started.
 
void setFinished ()
 Mark the task as finished.
 

Detailed Description

Class for doing some arbitrary parcel of work in its own thread.

The task is exposed as a function object, so could be a lambda or a hand-written closure. CWorker can not be subclassed, instead it can be extended with rich callable task objects.

Definition at line 187 of file worker.h.

Member Function Documentation

◆ fromTask()

template<typename F >
static CWorker* swift::misc::CWorker::fromTask ( QObject *  owner,
const QString &  name,
F &&  task 
)
inlinestatic

Returns a new worker object which lives in a new thread.

Note
The worker calls its own deleteLater method when finished. Typically assign it to a QPointer if you want to store it.
Parameters
ownerWill be the parent of the new thread (the worker has no parent).
nameA name for the task, which will be used to create a name for the thread.
taskA function object which will be run by the worker in its thread.

Definition at line 201 of file worker.h.

◆ result()

template<typename R >
R swift::misc::CWorker::result ( )
inline

Returns the result of the task, waiting for it to finish if necessary.

Template Parameters
RThe return type of the task.
Remarks
This function is thread safe.

Definition at line 245 of file worker.h.

◆ thenWithResult() [1/2]

template<typename R , typename F >
void swift::misc::CWorker::thenWithResult ( functor)
inline

Connects to a functor to which will be passed the result when the task is finished.

Template Parameters
RThe return type of the task.
Remarks
This function is thread safe. The functor may not call any method that observes the worker's finished flag.

Definition at line 218 of file worker.h.

◆ thenWithResult() [2/2]

template<typename R , typename T , typename F >
void swift::misc::CWorker::thenWithResult ( T *  context,
functor 
)
inline

Connects to a functor or method to which will be passed the result when the task is finished.

Template Parameters
RThe return type of the task.
Remarks
This function is thread safe. The functor may not call any method that observes the worker's finished flag.

Definition at line 228 of file worker.h.


The documentation for this class was generated from the following files: