- Qt 6.12
- Qt TaskTree
- C++ Classes
- Storage
- Qt 6.12.0 Reference Documentation

# QtTaskTree::Storage Class

template <typename StorageStruct> class QtTaskTree::Storage

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

Header: | `#include <qtasktree.h>`
CMake: | `find_package(Qt6 REQUIRED COMPONENTS TaskTree)` `target_link_libraries(mytarget PRIVATE Qt6::TaskTree)`
qmake: | `QT += tasktree`
Since: | Qt 6.11
Inherits: | QtTaskTree::StorageBase
Status: | Technology preview

This class is in technology preview and is subject to change.

- List of all members, including inherited members

Note: All functions in this class are reentrant.

## Public Functions

| Storage()
| Storage(const FirstArg &firstArg, const Args &... args)
StorageStruct * | activeStorage() const
StorageStruct & | operator*() const
StorageStruct * | operator->() const

## Detailed Description

The Storage class template is responsible for dynamically creating and destructing objects of the custom StorageStruct type. The creation and destruction are managed by the running task tree. If a Storage object is placed inside a Group element, the running task tree creates the `StorageStruct` object when the group is started and before the group's setup handler is called. Later, whenever any handler inside this group is called, the task tree activates the previously created instance of the `StorageStruct` object. This includes all tasks' and groups' setup and done handlers inside the group where the Storage object was placed, also within the nested groups. When a copy of the Storage object is passed to the handler via the lambda capture, the handler may access the instance activated by the running task tree via the Storage::operator->(), Storage::operator*(), or activeStorage() method. If two handlers capture the same Storage object, one of them may store a custom data there, and the other may read it afterwards. When the group is finished, the previously created instance of the `StorageStruct` object is destroyed after the group's done handler is called.

An example of data exchange between tasks:

     const Storage<QString> storage;

     const auto onFirstDone = [storage](const FirstWorker &task) {
         // Assings QString, taken from the first task result, to the active QString instance
         // of the Storage object.
         *storage = task.getResultAsString();
     };

     const auto onSecondSetup = [storage](SecondWorker &task) {
         // Reads QString from the active QString instance of the Storage object and use it to
         // configure the second task before start.
         task.configureWithString(*storage);
     };

     const Group root {
         // The running task tree creates QString instance when root in entered
         storage,
         // The done handler of the first task stores the QString in the storage
         FirstWorkerTask(..., onFirstDone),
         // The setup handler of the second task reads the QString from the storage
         SecondWorkerTask(onSecondSetup, ...)
     };

Since the root group executes its tasks sequentially, the `onFirstDone` handler is always called before the `onSecondSetup` handler. This means that the QString data, read from the `storage` inside the `onSecondSetup` handler's body, has already been set by the `onFirstDone` handler. You can always rely on it in sequential execution mode.

The Storage internals are shared between all of its copies. That is why the copies of the Storage object inside the handlers' lambda captures still refer to the same Storage instance. You may place multiple Storage objects inside one Group element, provided that they do not include copies of the same Storage object. Otherwise, an assert is triggered at runtime that includes an error message. However, you can place copies of the same Storage object in different Group elements of the same recipe. In this case, the running task tree will create multiple instances of the `StorageStruct` objects (one for each copy) and storage shadowing will take place. Storage shadowing works in a similar way to C++ variable shadowing inside the nested blocks of code:

     Storage<QString> storage;

     const Group root {
         storage,                            // Top copy, 1st instance of StorageStruct
         onGroupSetup([storage] { ... }),    // Top copy is active
         Group {
             storage,                        // Nested copy, 2nd instance of StorageStruct,
                                             // shadows Top copy
             onGroupSetup([storage] { ... }) // Nested copy is active
         },
         Group {
             onGroupSetup([storage] { ... }) // Top copy is active
         }
     };

The Storage objects may also be used for passing the initial data to the executed task tree, and for reading the final data out of the task tree before it finishes. To do this, use onStorageSetup() or onStorageDone(), respectively.

Note: If you use an unreachable Storage object inside the handler, because you forgot to place the storage in the recipe, or placed it, but not in any handler's ancestor group, you may expect a crash, preceded by the following message: The referenced storage is not reachable in the running tree. A nullptr will be returned which might lead to a crash in the calling code. It is possible that no storage was added to the tree, or the storage is not reachable from where it is referenced.

## Member Function Documentation

### Storage::Storage()

Creates a storage for the given `StorageStruct` type. Whenever the running QTaskTree enters a Group where the Storage is placed, it creates the `StorageStruct` using default constructor.

Note: All copies of `this` object are considered to be the same Storage instance.

### `[explicit]` template < typename FirstArg, typename... Args, std::enable_if_t<!std::is_same_v<q20::remove_cvref_t<FirstArg>, Storage<StorageStruct>>, bool> = true > Storage::Storage(const FirstArg &firstArg, const Args &... args)

Creates a storage for the given `StorageStruct` type. The passed firstArg and args are stored when creating a storage, and are used later by the running QTaskTree to construct the `StorageStruct` with the stored args.

Note: All copies of `this` object are considered to be the same Storage instance.

### StorageStruct *Storage::activeStorage() const

Returns a pointer to the active `StorageStruct` object, created by the running task tree. Use this function only from inside the handler body of any GroupItem element placed in the recipe, otherwise you may expect a crash. Make sure that Storage is placed in any group ancestor of the handler's group item.

Note: The returned pointer is valid as long as the group that created this instance is still running.

See also operator->() and operator*().

### `[noexcept]` StorageStruct &Storage::operator*() const

Returns a reference to the active `StorageStruct` object, created by the running task tree. Use this function only from inside the handler body of any GroupItem element placed in the recipe, otherwise you may expect a crash. Make sure that Storage is placed in any group ancestor of the handler's group item.

Note: The returned reference is valid as long as the group that created this instance is still running.

See also activeStorage() and operator->().

### `[noexcept]` StorageStruct *Storage::operator->() const

Returns a pointer to the active `StorageStruct` object, created by the running task tree. Use this function only from inside the handler body of any GroupItem element placed in the recipe, otherwise you may expect a crash. Make sure that Storage is placed in any group ancestor of the handler's group item.

Note: The returned pointer is valid as long as the group that created this instance is still running.

See also activeStorage() and operator*().