在本页

QtTaskTree::Storage Class

template <typename StorageStruct> class QtTaskTree::Storage

用于在运行任务树中进行自定义数据交换的类模板。更多

头文件: #include <qtasktree.h>
CMake: find_package(Qt6 REQUIRED COMPONENTS TaskTree)
target_link_libraries(mytarget PRIVATE Qt6::TaskTree)
qmake: QT += tasktree
Qt 6.11
继承于QtTaskTree::StorageBase

注意:该类中的所有函数都是可重入的

公共函数

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

详细说明

存储类模板负责动态创建和销毁自定义StorageStruct 类型的对象。创建和销毁由运行中的任务树管理。如果存储对象被放置在Group 元素内,运行中的任务树会在组启动时、组的设置处理程序调用前创建StorageStruct 对象。之后,只要调用该组内的任何处理程序,任务树就会激活之前创建的StorageStruct 对象实例。这包括存储对象所在组内的所有任务和组的设置和完成处理程序,也包括嵌套组内的所有任务和组的设置和完成处理程序。当存储对象的副本通过 lambda 捕捉传递给处理程序时,处理程序可通过Storage::operator->(),Storage::operator*() 或activeStorage() 方法访问运行中的任务树所激活的实例。如果两个处理程序捕获同一个存储对象,其中一个处理程序可以在其中存储自定义数据,另一个处理程序可以在之后读取该数据。当组结束时,在组的完成处理程序被调用后,之前创建的StorageStruct 对象实例将被销毁。

任务间数据交换示例

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, ...)
};

由于根组按顺序执行任务,因此onFirstDone 处理程序总是在onSecondSetup 处理程序之前被调用。这意味着,在onSecondSetup 处理程序的主体中从storage 读取的QString 数据已被onFirstDone 处理程序设置。在sequential 执行模式下,您可以始终依赖它。

存储的内部结构在其所有副本之间共享。这就是为什么处理程序 lambda 捕捉到的存储对象副本仍指向同一个存储实例的原因。您可以在一个Group 元素中放置多个 "存储 "对象,前提是它们不包含同一个 "存储 "对象的副本。否则,运行时将触发包含错误信息的断言。不过,您可以在同一配方的不同Group 元素中放置同一存储对象的副本。在这种情况下,运行中的任务树将创建StorageStruct 对象的多个实例(每个副本一个),并发生存储阴影。存储阴影的工作方式与嵌套代码块内的 C++ 变量阴影类似:

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
    }
};

存储对象还可用于向已执行的任务树传递初始数据,并在任务树结束前读出最终数据。为此,请分别使用onStorageSetup() 或onStorageDone() 。

注意: 如果您在处理程序中使用了一个无法访问的存储对象,因为您忘记在配方中放置存储,或者放置了存储,但没有放在任何处理程序的祖先组中,则可能会出现崩溃,之前会出现以下消息:在运行树中无法访问引用的存储空间。将返回一个 nullptr,这可能会导致调用代码崩溃。这可能是因为没有将存储添加到树中,或者从引用存储的位置无法到达该存储。

成员函数文档

Storage::Storage()

为给定的StorageStruct 类型创建存储。每当运行中的QTaskTree 进入放置 Storage 的Group 时,它都会使用默认构造函数创建StorageStruct

注意: this 对象的所有副本都被视为同一个存储实例。

[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)

为给定的StorageStruct 类型创建存储。在创建存储时,传入的firstArgargs 将被存储,随后运行的QTaskTree 将使用存储的args 构建StorageStruct

注意: this 对象的所有副本都被视为同一个存储实例。

StorageStruct *Storage::activeStorage() const

返回运行中的任务树创建的活动StorageStruct 对象指针。该函数只能在配方中任何GroupItem 元素的处理程序主体中使用,否则可能会导致崩溃。确保 Storage 位于处理程序组项的任何组祖先中。

注意: 只要创建此实例的组仍在运行,返回的指针就是有效的。

另请参阅 operator->() 和operator*()。

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

返回运行中的任务树创建的活动StorageStruct 对象的引用。该函数只能在配方中任何GroupItem 元素的处理程序主体中使用,否则可能会导致崩溃。确保 Storage 位于处理程序组项的任何组祖先中。

注意: 只要创建此实例的组仍在运行,返回的引用就有效。

另请参阅 activeStorage() 和operator->()。

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

返回运行中的任务树创建的活动StorageStruct 对象指针。该函数只能在配方中任何GroupItem 元素的处理程序主体中使用,否则可能会导致崩溃。确保 Storage 位于处理程序组项的任何组祖先中。

注意: 只要创建此实例的组仍在运行,返回的指针就是有效的。

另请参阅 activeStorage() 和operator*()。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.