CustomTask Class
template <typename Adapter> class Tasking::CustomTaskA class template used for declaring custom task items and defining their setup and done handlers. More...
Header: | #include <solutions/tasking/tasktree.h> |
Inherits: | Tasking::ExecutableItem |
Note: All functions in this class are reentrant.
Public Functions
CustomTask(SetupHandler &&setup = TaskSetupHandler(), DoneHandler &&done = TaskDoneHandler(), Tasking::CallDoneIf callDoneIf = CallDoneIf::SuccessOrError) |
Detailed Description
Describes custom task items within task tree recipes.
Custom task names are aliased with unique names using the CustomTask
template with a given TaskAdapter subclass as a template parameter. For example, ConcurrentCallTask<T>
is an alias to the CustomTask
that is defined to work with ConcurrentCall<T>
as an associated task class. The following table contains example custom tasks and their associated task classes:
Aliased Task Name (Tasking Namespace) | Associated Task Class | Brief Description |
---|---|---|
ConcurrentCallTask<ReturnType> | ConcurrentCall<ReturnType> | Starts an asynchronous task. Runs in a separate thread. |
NetworkQueryTask | NetworkQuery | Sends a network query. |
TaskTreeTask | TaskTree | Starts a nested task tree. |
TimeoutTask | std::chrono::milliseconds | Starts a timer. |
WaitForBarrierTask | MultiBarrier<Limit> | Starts an asynchronous task waiting for the barrier to pass. |
Member Function Documentation
template <typename SetupHandler = Tasking::CustomTask<Adapter>::TaskSetupHandler, typename DoneHandler = Tasking::CustomTask<Adapter>::TaskDoneHandler> CustomTask::CustomTask(SetupHandler &&setup = TaskSetupHandler(), DoneHandler &&done = TaskDoneHandler(), Tasking::CallDoneIf callDoneIf = CallDoneIf::SuccessOrError)
Constructs a CustomTask
instance and attaches the setup and done handlers to the task. When the running task tree is about to start the task, it instantiates the associated Task object, invokes setup handler with a reference to the created task, and starts it. When the running task finishes, the task tree invokes a done handler, with a const
reference to the created task.
The passed setup handler is of the TaskSetupHandler type. For example:
static void parseAndLog(const QString &input); ... const QString input = ...; const auto onFirstSetup = [input](ConcurrentCall<void> &task) { if (input == "Skip") return SetupResult::StopWithSuccess; // This task won't start, the next one will if (input == "Error") return SetupResult::StopWithError; // This task and the next one won't start task.setConcurrentCallData(parseAndLog, input); // This task will start, and the next one will start after this one finished with success return SetupResult::Continue; }; const auto onSecondSetup = [input](ConcurrentCall<void> &task) { task.setConcurrentCallData(parseAndLog, input); }; const Group group { ConcurrentCallTask<void>(onFirstSetup), ConcurrentCallTask<void>(onSecondSetup) };
The done handler is of the TaskDoneHandler type. By default, the done handler is invoked whenever the task finishes. Pass a non-default value for the callDoneIf argument when you want the handler to be called only on a successful or failed execution.
See also TaskSetupHandler and TaskDoneHandler.