From 8c04776924024988fe86787963f3548a10dd8189 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Fri, 13 Sep 2024 20:55:50 +0200 Subject: [PATCH] TaskTree: Make it possible to pass the init data to the Storage Add a storage c'tor taking the StorageData data as an argument. Later, when the StorageData is created by the running task tree, this data is used to initialize the newly created storage. Add a test for it. Change-Id: I05ce7831df7b378035b44b2d1053b5b25fb910d3 Reviewed-by: hjk --- src/libs/solutions/tasking/tasktree.h | 2 ++ tests/auto/solutions/tasking/tst_tasking.cpp | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/libs/solutions/tasking/tasktree.h b/src/libs/solutions/tasking/tasktree.h index 46c5f217a4c..3cdff6fb8ae 100644 --- a/src/libs/solutions/tasking/tasktree.h +++ b/src/libs/solutions/tasking/tasktree.h @@ -181,6 +181,8 @@ class Storage final : public StorageBase { public: Storage() : StorageBase(Storage::ctor(), Storage::dtor()) {} + Storage(const StorageStruct &data) + : StorageBase([data] { return new StorageStruct(data); }, Storage::dtor()) {} StorageStruct &operator*() const noexcept { return *activeStorage(); } StorageStruct *operator->() const noexcept { return activeStorage(); } StorageStruct *activeStorage() const { diff --git a/tests/auto/solutions/tasking/tst_tasking.cpp b/tests/auto/solutions/tasking/tst_tasking.cpp index d9badac13c4..5a0b7849a1d 100644 --- a/tests/auto/solutions/tasking/tst_tasking.cpp +++ b/tests/auto/solutions/tasking/tst_tasking.cpp @@ -120,6 +120,7 @@ private slots: void storageOperators(); void storageDestructor(); void storageZeroInitialization(); + void storageDataInitialization(); void nestedBrokenStorage(); void restart(); void destructorOfTaskEmittingDone(); @@ -4114,7 +4115,22 @@ void tst_Tasking::storageZeroInitialization() taskTree.runBlocking(); QVERIFY(defaultValue); - QCOMPARE(defaultValue, 0); + QCOMPARE(*defaultValue, 0); +} + +// This test ensures that the storage c'tor with data is initialized properly. +void tst_Tasking::storageDataInitialization() +{ + const Storage storage(42); + std::optional defaultValue; + + const auto onSetup = [storage, &defaultValue] { defaultValue = *storage; }; + + TaskTree taskTree({ storage, onGroupSetup(onSetup) }); + taskTree.runBlocking(); + + QVERIFY(defaultValue); + QCOMPARE(*defaultValue, 42); } // This test ensures that when a missing storage object inside the nested task tree is accessed