- Extending Qt Creator Manual
- Tasking Namespace
- Extending Qt Creator Manual 13.0.0

# Tasking Namespace

The Tasking namespace encloses all classes and global functions of the Tasking solution. More...

Header: | #include <Tasking>

## Classes

class | CustomTask
class | ExecutableItem
class | Group
class | GroupItem
class | Storage
class | Sync
class | TaskAdapter
class | TaskInterface
class | TaskTree

## Types

enum class | CallDoneIf { SuccessOrError, Success, Error }
enum class | DoneResult { Success, Error }
enum class | DoneWith { Success, Error, Cancel }
enum class | SetupResult { Continue, StopWithSuccess, StopWithError }
| TaskTreeTask
| TimeoutTask
enum class | WorkflowPolicy { StopOnError, ContinueOnError, StopOnSuccess, ContinueOnSuccess, StopOnSuccessOrError, …, FinishAllAndError }

## Variables

const Tasking::GroupItem | continueOnError
const Tasking::GroupItem | continueOnSuccess
const Tasking::GroupItem | finishAllAndError
const Tasking::GroupItem | finishAllAndSuccess
const Tasking::GroupItem | parallel
const Tasking::GroupItem | sequential
const Tasking::GroupItem | stopOnError
const Tasking::GroupItem | stopOnSuccess
const Tasking::GroupItem | stopOnSuccessOrError

## Functions

Tasking::GroupItem | onGroupDone(Handler &&handler, Tasking::CallDoneIf callDoneIf = CallDoneIf::SuccessOrError)
Tasking::GroupItem | onGroupSetup(Handler &&handler)
Tasking::GroupItem | parallelLimit(int limit)
Tasking::GroupItem | workflowPolicy(Tasking::WorkflowPolicy policy)

## Detailed Description

## Classes

### class CustomTask

A class template used for declaring custom task items and defining their setup and done handlers. More...

### class ExecutableItem

Base class for executable task items. More...

### class Group

Group represents the basic element for composing declarative recipes describing how to execute and handle a nested tree of asynchronous tasks. More...

### class GroupItem

GroupItem represents the basic element that may be a part of any Group. More...

### class Storage

A class template for custom data exchange in the running task tree. More...

### class Sync

Synchronously executes a custom handler between other tasks. More...

### class TaskAdapter

A class template for implementing custom task adapters. More...

### class TaskInterface

TaskInterface is the abstract base class for implementing custom task adapters. More...

### class TaskTree

The TaskTree class runs an async task tree structure defined in a declarative way. More...

## Type Documentation

### enum class Tasking::CallDoneIf

This enum is an optional argument for the onGroupDone() element or custom task's constructor. It instructs the task tree on when the group's or task's done handler should be invoked.

Constant | Value | Description
--- | --- | ---
`Tasking::CallDoneIf::SuccessOrError` | `0` | The done handler is always invoked.
`Tasking::CallDoneIf::Success` | `1` | The done handler is invoked only after successful execution, that is, when DoneWith::Success.
`Tasking::CallDoneIf::Error` | `2` | The done handler is invoked only after failed execution, that is, when DoneWith::Error or when DoneWith::Cancel.

### enum class Tasking::DoneResult

This enum is optionally returned from the group's or task's done handler function. When the done handler doesn't return any value, that is, its return type is `void`, its final return value is automatically deduced by the running task tree and reported to its parent group.

When the done handler returns the DoneResult, you can tweak the final return value inside the handler.

When the DoneResult is returned by the group's done handler, the group's workflow policy is ignored.

This enum is also used inside the TaskInterface::done() signal and it indicates whether the task finished with success or an error.

Constant | Value | Description
--- | --- | ---
`Tasking::DoneResult::Success` | `0` | The group's or task's execution ends with success.
`Tasking::DoneResult::Error` | `1` | The group's or task's execution ends with an error.

### enum class Tasking::DoneWith

This enum is an optional argument for the group's or task's done handler. It indicates whether the group or task finished with success or an error, or it was canceled.

It is also used as an argument inside the TaskTree::done() signal, indicating the final result of the TaskTree execution.

Constant | Value | Description
--- | --- | ---
`Tasking::DoneWith::Success` | `0` | The group's or task's execution ended with success.
`Tasking::DoneWith::Error` | `1` | The group's or task's execution ended with an error.
`Tasking::DoneWith::Cancel` | `2` | The group's or task's execution was canceled. This happens when the user calls TaskTree::cancel() for the running task tree or when the group's workflow policy results in canceling some of its running children. Tweaking the done handler's final result by returning Tasking::DoneResult from the handler is no-op when the group's or task's execution was canceled.

### enum class Tasking::SetupResult

This enum is optionally returned from the group's or task's setup handler function. It instructs the running task tree on how to proceed after the setup handler's execution finished.

Constant | Value | Description
--- | --- | ---
`Tasking::SetupResult::Continue` | `0` | Default. The group's or task's execution continues normally. When a group's or task's setup handler returns void, it's assumed that it returned Continue.

`Tasking::SetupResult::StopWithSuccess`

`1`

The group's or task's execution stops immediately with success. When returned from the group's setup handler, all child tasks are skipped, and the group's onGroupDone() handler is invoked with DoneWith::Success. The group reports success to its parent. The group's workflow policy is ignored. When returned from the task's setup handler, the task isn't started, its done handler isn't invoked, and the task reports success to its parent.

`Tasking::SetupResult::StopWithError`

`2`

The group's or task's execution stops immediately with an error. When returned from the group's setup handler, all child tasks are skipped, and the group's onGroupDone() handler is invoked with DoneWith::Error. The group reports an error to its parent. The group's workflow policy is ignored. When returned from the task's setup handler, the task isn't started, its error handler isn't invoked, and the task reports an error to its parent.

### `[alias]` Tasking::TaskTreeTask

Type alias for the CustomTask, to be used inside recipes, associated with the TaskTree task.

### `[alias]` Tasking::TimeoutTask

Type alias for the CustomTask, to be used inside recipes, associated with the `std::chrono::milliseconds` type. `std::chrono::milliseconds` is used to set up the timeout duration. The default timeout is `std::chrono::milliseconds::zero()`, that is, the TimeoutTask finishes as soon as the control returns to the running event loop.

Example usage:

     using namespace std::chrono;
     using namespace std::chrono_literals;

     const auto onSetup = [](milliseconds &timeout) { timeout = 1000ms; }
     const auto onDone = [] { qDebug() << "Timed out."; }

     const Group root {
         Timeout(onSetup, onDone)
     };

### enum class Tasking::WorkflowPolicy

This enum describes the possible behavior of the Group element when any group's child task finishes its execution. It's also used when the running Group is canceled.

Constant | Value | Description
--- | --- | ---
`Tasking::WorkflowPolicy::StopOnError` | `0` | Default. Corresponds to the stopOnError global element. If any child task finishes with an error, the group stops and finishes with an error. If all child tasks finished with success, the group finishes with success. If a group is empty, it finishes with success.

`Tasking::WorkflowPolicy::ContinueOnError`

`1`

Corresponds to the continueOnError global element. Similar to stopOnError, but in case any child finishes with an error, the execution continues until all tasks finish, and the group reports an error afterwards, even when some other tasks in the group finished with success. If all child tasks finish successfully, the group finishes with success. If a group is empty, it finishes with success.

`Tasking::WorkflowPolicy::StopOnSuccess` | `2` | Corresponds to the stopOnSuccess global element. If any child task finishes with success, the group stops and finishes with success. If all child tasks finished with an error, the group finishes with an error. If a group is empty, it finishes with an error.

`Tasking::WorkflowPolicy::ContinueOnSuccess`

`3`

Corresponds to the continueOnSuccess global element. Similar to stopOnSuccess, but in case any child finishes successfully, the execution continues until all tasks finish, and the group reports success afterwards, even when some other tasks in the group finished with an error. If all child tasks finish with an error, the group finishes with an error. If a group is empty, it finishes with an error.

`Tasking::WorkflowPolicy::StopOnSuccessOrError`

`4`

Corresponds to the stopOnSuccessOrError global element. The group starts as many tasks as it can. When any task finishes, the group stops and reports the task's result. Useful only in parallel mode. In sequential mode, only the first task is started, and when finished, the group finishes too, so the other tasks are always skipped. If a group is empty, it finishes with an error.

`Tasking::WorkflowPolicy::FinishAllAndSuccess` | `5` | Corresponds to the finishAllAndSuccess global element. The group executes all tasks and ignores their return results. When all tasks finished, the group finishes with success. If a group is empty, it finishes with success.
`Tasking::WorkflowPolicy::FinishAllAndError` | `6` | Corresponds to the finishAllAndError global element. The group executes all tasks and ignores their return results. When all tasks finished, the group finishes with an error. If a group is empty, it finishes with an error.

Whenever a child task's result causes the Group to stop, that is, in case of StopOnError, StopOnSuccess, or StopOnSuccessOrError policies, the Group cancels the other running child tasks (if any - for example in parallel mode), and skips executing tasks it has not started yet (for example, in the sequential mode - those, that are placed after the failed task). Both canceling and skipping child tasks may happen when parallelLimit() is used.

The table below summarizes the differences between various workflow policies:

WorkflowPolicy | Executes all child tasks | Result | Result when the group is empty
--- | --- | --- | ---
StopOnError | Stops when any child task finished with an error and reports an error | An error when at least one child task failed, success otherwise | Success
ContinueOnError | Yes | An error when at least one child task failed, success otherwise | Success
StopOnSuccess | Stops when any child task finished with success and reports success | Success when at least one child task succeeded, an error otherwise | An error
ContinueOnSuccess | Yes | Success when at least one child task succeeded, an error otherwise | An error
StopOnSuccessOrError | Stops when any child task finished and reports child task's result | Success or an error, depending on the finished child task's result | An error
FinishAllAndSuccess | Yes | Success | Success
FinishAllAndError | Yes | An error | An error

If a child of a group is also a group, the child group runs its tasks according to its own workflow policy. When a parent group stops the running child group because of parent group's workflow policy, that is, when the StopOnError, StopOnSuccess, or StopOnSuccessOrError policy was used for the parent, the child group's result is reported according to the Result column and to the child group's workflow policy row in the table above.

## Variable Documentation

### const Tasking::GroupItem Tasking::continueOnError

A convenient global group's element describing the ContinueOnError workflow policy.

### const Tasking::GroupItem Tasking::continueOnSuccess

A convenient global group's element describing the ContinueOnSuccess workflow policy.

### const Tasking::GroupItem Tasking::finishAllAndError

A convenient global group's element describing the FinishAllAndError workflow policy.

### const Tasking::GroupItem Tasking::finishAllAndSuccess

A convenient global group's element describing the FinishAllAndSuccess workflow policy.

### const Tasking::GroupItem Tasking::parallel

A convenient global group's element describing the parallel execution mode.

All the direct child tasks of a group are started after the group is started, without waiting for the previous child tasks to finish. In this mode, all child tasks run simultaneously.

See also sequential and parallelLimit().

### const Tasking::GroupItem Tasking::sequential

A convenient global group's element describing the sequential execution mode.

This is the default execution mode of the Group element.

When a Group has no execution mode, it runs in the sequential mode. All the direct child tasks of a group are started in a chain, so that when one task finishes, the next one starts. This enables you to pass the results from the previous task as input to the next task before it starts. This mode guarantees that the next task is started only after the previous task finishes.

See also parallel and parallelLimit().

### const Tasking::GroupItem Tasking::stopOnError

A convenient global group's element describing the StopOnError workflow policy.

This is the default workflow policy of the Group element.

### const Tasking::GroupItem Tasking::stopOnSuccess

A convenient global group's element describing the StopOnSuccess workflow policy.

### const Tasking::GroupItem Tasking::stopOnSuccessOrError

A convenient global group's element describing the StopOnSuccessOrError workflow policy.

## Function Documentation

### template <typename Handler> Tasking::GroupItem Tasking::onGroupDone(Handler &&handler, Tasking::CallDoneIf callDoneIf = CallDoneIf::SuccessOrError)

Constructs a group's element holding the group done handler. By default, the handler is invoked whenever the group 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. Depending on the group's workflow policy, this handler may also be called when the running group is canceled (e.g. when stopOnError element was used).

The passed handler is of the `std::function<DoneResult(DoneWith)>` type. Optionally, each of the return DoneResult type or the argument DoneWith type may be omitted (that is, its return type may be `void`). For more information on a possible handler type, refer to GroupItem::GroupDoneHandler.

When the handler is invoked, all of the group's child tasks are already finished.

If a group contains the Storage elements, the handler is invoked before the storages are destructed, so that the handler may still perform a last read of the active storages' data.

See also GroupItem::GroupDoneHandler and onGroupSetup().

### template <typename Handler> Tasking::GroupItem Tasking::onGroupSetup(Handler &&handler)

Constructs a group's element holding the group setup handler. The handler is invoked whenever the group starts.

The passed handler is either of the `std::function<SetupResult()>` or the `std::function<void()>` type. For more information on a possible handler type, refer to GroupItem::GroupSetupHandler.

When the handler is invoked, none of the group's child tasks are running yet.

If a group contains the Storage elements, the handler is invoked after the storages are constructed, so that the handler may already perform some initial modifications to the active storages.

See also GroupItem::GroupSetupHandler and onGroupDone().

### Tasking::GroupItem Tasking::parallelLimit(int limit)

Constructs a group's element describing the execution mode.

The execution mode element in a Group specifies how the direct child tasks of the Group are started.

For convenience, when appropriate, the sequential or parallel global elements may be used instead.

The limit defines the maximum number of direct child tasks running in parallel:

- When limit equals to 0, there is no limit, and all direct child tasks are started together, in the oder in which they appear in a group. This means the fully parallel execution, and the parallel element may be used instead.
- When limit equals to 1, it means that only one child task may run at the time. This means the sequential execution, and the sequential element may be used instead. In this case, child tasks run in chain, so the next child task starts after the previous child task has finished.
- When other positive number is passed as limit, the group's child tasks run in parallel, but with a limited number of tasks running simultanously. The limit defines the maximum number of tasks running in parallel in a group. When the group is started, the first batch of tasks is started (the number of tasks in a batch equals to the passed limit, at most), while the others are kept waiting. When any running task finishes, the group starts the next remaining one, so that the limit of simultaneously running tasks inside a group isn't exceeded. This repeats on every child task's finish until all child tasks are started. This enables you to limit the maximum number of tasks that run simultaneously, for example if running too many processes might block the machine for a long time.

In all execution modes, a group starts tasks in the oder in which they appear.

If a child of a group is also a group, the child group runs its tasks according to its own execution mode.

See also sequential and parallel.

### Tasking::GroupItem Tasking::workflowPolicy(Tasking::WorkflowPolicy policy)

Constructs a group's workflow policy element for a given policy.

For convenience, global elements may be used instead.

See also stopOnError, continueOnError, stopOnSuccess, continueOnSuccess, stopOnSuccessOrError, finishAllAndSuccess, finishAllAndError, and WorkflowPolicy.