2018-02-14 19:18:51 +01:00
|
|
|
|
#ifndef TASKEXECUTOR_H
|
|
|
|
|
|
#define TASKEXECUTOR_H
|
|
|
|
|
|
/* From answer by Hatter
|
|
|
|
|
|
*
|
|
|
|
|
|
* https://stackoverflow.com/questions/5423058/qfuture-that-can-be-cancelled-and-report-progress
|
|
|
|
|
|
*/
|
|
|
|
|
|
#include "ControllableTask.h"
|
|
|
|
|
|
#include "RunControllableTask.h"
|
2018-12-30 15:46:15 +01:00
|
|
|
|
#include <QFuture>
|
2018-02-14 19:18:51 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief The TaskExecutor class
|
|
|
|
|
|
*
|
|
|
|
|
|
* The user should sublass ControllableTask, implement background routine which checks sometimes
|
|
|
|
|
|
* method shouldRun() of TaskControl instance passed to run(TaskControl&) and then use it like:
|
|
|
|
|
|
*
|
|
|
|
|
|
* QFututre<int> futureValue = TaskExecutor::run(new SomeControllableTask(inputForThatTask));
|
|
|
|
|
|
*
|
|
|
|
|
|
* Then she may cancel it by calling futureValue.cancel(), bearing in mind that cancellation is
|
|
|
|
|
|
* graceful and not immediate.
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
|
|
|
|
|
class TaskExecutor {
|
|
|
|
|
|
public:
|
|
|
|
|
|
template <class T>
|
|
|
|
|
|
static QFuture<T> run(ControllableTask<T>* task) {
|
|
|
|
|
|
return (new RunControllableTask<T>(task))->start();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif // TASKEXECUTOR_H
|