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 <hjk@qt.io>
This commit is contained in:
Jarek Kobus
2024-09-13 20:55:50 +02:00
parent 22d04f7fa2
commit 8c04776924
2 changed files with 19 additions and 1 deletions

View File

@@ -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 {

View File

@@ -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<int> storage(42);
std::optional<int> 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