TaskTree: Use DoneResult in Group done handler

Instead of using ambiguous bool.

Change-Id: Icf63f0b129d4b81cc4324d7a100f5aaf2c8af44b
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Jarek Kobus
2023-11-06 19:17:28 +01:00
parent 0f6e6cebf7
commit f771faf82d
3 changed files with 28 additions and 33 deletions

View File

@@ -1086,7 +1086,7 @@ public:
SetupResult startChildren(int nextChild); SetupResult startChildren(int nextChild);
SetupResult childDone(bool success); SetupResult childDone(bool success);
void stop(); void stop();
bool invokeDoneHandler(DoneWith result); bool invokeDoneHandler(DoneWith doneWith);
bool isRunning() const { return m_runtimeData.has_value(); } bool isRunning() const { return m_runtimeData.has_value(); }
bool isStarting() const { return isRunning() && m_runtimeData->m_startGuard.isLocked(); } bool isStarting() const { return isRunning() && m_runtimeData->m_startGuard.isLocked(); }
@@ -1451,15 +1451,15 @@ static bool shouldCall(CallDoneIf callDoneIf, DoneWith result)
return callDoneIf != CallDoneIf::Success; return callDoneIf != CallDoneIf::Success;
} }
bool TaskContainer::invokeDoneHandler(DoneWith result) bool TaskContainer::invokeDoneHandler(DoneWith doneWith)
{ {
bool success = result == DoneWith::Success; DoneResult result = toDoneResult(doneWith);
const GroupItem::GroupHandler &groupHandler = m_constData.m_groupHandler; const GroupItem::GroupHandler &groupHandler = m_constData.m_groupHandler;
if (groupHandler.m_doneHandler && shouldCall(groupHandler.m_callDoneIf, result)) if (groupHandler.m_doneHandler && shouldCall(groupHandler.m_callDoneIf, doneWith))
success = invokeHandler(this, groupHandler.m_doneHandler, result); result = invokeHandler(this, groupHandler.m_doneHandler, doneWith);
m_runtimeData->m_callStorageDoneHandlersOnDestruction = true; m_runtimeData->m_callStorageDoneHandlersOnDestruction = true;
m_runtimeData.reset(); m_runtimeData.reset();
return success; return result == DoneResult::Success;
} }
SetupResult TaskNode::start() SetupResult TaskNode::start()

View File

@@ -194,7 +194,7 @@ public:
// Called when group entered, after group's storages are created // Called when group entered, after group's storages are created
using GroupSetupHandler = std::function<SetupResult()>; using GroupSetupHandler = std::function<SetupResult()>;
// Called when group done, before group's storages are deleted // Called when group done, before group's storages are deleted
using GroupDoneHandler = std::function<bool(DoneWith)>; using GroupDoneHandler = std::function<DoneResult(DoneWith)>;
struct TaskHandler { struct TaskHandler {
TaskCreateHandler m_createHandler; TaskCreateHandler m_createHandler;
@@ -315,24 +315,24 @@ private:
template <typename Handler> template <typename Handler>
static GroupDoneHandler wrapGroupDone(Handler &&handler) static GroupDoneHandler wrapGroupDone(Handler &&handler)
{ {
// B, V, D stands for: [B]ool, [V]oid, [D]oneWith // D, V, W stands for: [D]oneResult, [V]oid, Done[W]ith
static constexpr bool isBD = isInvocable<bool, Handler, DoneWith>(); static constexpr bool isDW = isInvocable<DoneResult, Handler, DoneWith>();
static constexpr bool isB = isInvocable<bool, Handler>(); static constexpr bool isD = isInvocable<DoneResult, Handler>();
static constexpr bool isVD = isInvocable<void, Handler, DoneWith>(); static constexpr bool isVW = isInvocable<void, Handler, DoneWith>();
static constexpr bool isV = isInvocable<void, Handler>(); static constexpr bool isV = isInvocable<void, Handler>();
static_assert(isBD || isB || isVD || isV, static_assert(isDW || isD || isVW || isV,
"Group done handler needs to take (DoneWith) or (void) as an argument and has to " "Group done handler needs to take (DoneWith) or (void) as an argument and has to "
"return void or bool. The passed handler doesn't fulfill these requirements."); "return void or DoneResult. The passed handler doesn't fulfill these requirements.");
return [=](DoneWith result) { return [=](DoneWith result) {
if constexpr (isBD) if constexpr (isDW)
return std::invoke(handler, result); return std::invoke(handler, result);
if constexpr (isB) if constexpr (isD)
return std::invoke(handler); return std::invoke(handler);
if constexpr (isVD) if constexpr (isVW)
std::invoke(handler, result); std::invoke(handler, result);
else if constexpr (isV) else if constexpr (isV)
std::invoke(handler); std::invoke(handler);
return result == DoneWith::Success; return result == DoneWith::Success ? DoneResult::Success : DoneResult::Error;
}; };
}; };
}; };

View File

@@ -392,11 +392,6 @@ static Handler toTweakSetupHandler(SetupResult result)
return Handler::TweakSetupToContinue; return Handler::TweakSetupToContinue;
} }
static Handler toTweakDoneHandler(bool success)
{
return success ? Handler::TweakDoneToSuccess : Handler::TweakDoneToError;
}
static Handler toTweakDoneHandler(DoneResult result) static Handler toTweakDoneHandler(DoneResult result)
{ {
return result == DoneResult::Success ? Handler::TweakDoneToSuccess : Handler::TweakDoneToError; return result == DoneResult::Success ? Handler::TweakDoneToSuccess : Handler::TweakDoneToError;
@@ -551,11 +546,11 @@ void tst_Tasking::testTree_data()
return desiredResult; return desiredResult;
}); });
}; };
const auto groupDoneWithTweak = [storage](int taskId, bool desiredResult) { const auto groupDoneWithTweak = [storage](int taskId, DoneResult result) {
return onGroupDone([storage, taskId, desiredResult](DoneWith result) { return onGroupDone([storage, taskId, result](DoneWith doneWith) {
storage->m_log.append({taskId, resultToGroupHandler(result)}); storage->m_log.append({taskId, resultToGroupHandler(doneWith)});
storage->m_log.append({taskId, toTweakDoneHandler(desiredResult)}); storage->m_log.append({taskId, toTweakDoneHandler(result)});
return desiredResult; return result;
}); });
}; };
const auto createSync = [storage](int taskId) { const auto createSync = [storage](int taskId) {
@@ -1611,18 +1606,18 @@ void tst_Tasking::testTree_data()
{ {
// This test checks whether group done handler's result is properly dispatched. // This test checks whether group done handler's result is properly dispatched.
const auto createRoot = [storage, createTask, groupDone, groupDoneWithTweak]( const auto createRoot = [storage, createTask, groupDone, groupDoneWithTweak](
DoneResult firstResult, bool desiredResult) { DoneResult firstResult, DoneResult secondResult) {
return Group { return Group {
Storage(storage), Storage(storage),
Group { Group {
createTask(1, firstResult), createTask(1, firstResult),
groupDoneWithTweak(1, desiredResult) groupDoneWithTweak(1, secondResult)
}, },
groupDone(0) groupDone(0)
}; };
}; };
const Group root1 = createRoot(DoneResult::Success, true); const Group root1 = createRoot(DoneResult::Success, DoneResult::Success);
const Log log1 { const Log log1 {
{1, Handler::Setup}, {1, Handler::Setup},
{1, Handler::Success}, {1, Handler::Success},
@@ -1633,7 +1628,7 @@ void tst_Tasking::testTree_data()
QTest::newRow("GroupDoneWithSuccessTweakToSuccess") QTest::newRow("GroupDoneWithSuccessTweakToSuccess")
<< TestData{storage, root1, log1, 1, OnDone::Success}; << TestData{storage, root1, log1, 1, OnDone::Success};
const Group root2 = createRoot(DoneResult::Success, false); const Group root2 = createRoot(DoneResult::Success, DoneResult::Error);
const Log log2 { const Log log2 {
{1, Handler::Setup}, {1, Handler::Setup},
{1, Handler::Success}, {1, Handler::Success},
@@ -1644,7 +1639,7 @@ void tst_Tasking::testTree_data()
QTest::newRow("GroupDoneWithSuccessTweakToError") QTest::newRow("GroupDoneWithSuccessTweakToError")
<< TestData{storage, root2, log2, 1, OnDone::Failure}; << TestData{storage, root2, log2, 1, OnDone::Failure};
const Group root3 = createRoot(DoneResult::Error, true); const Group root3 = createRoot(DoneResult::Error, DoneResult::Success);
const Log log3 { const Log log3 {
{1, Handler::Setup}, {1, Handler::Setup},
{1, Handler::Error}, {1, Handler::Error},
@@ -1655,7 +1650,7 @@ void tst_Tasking::testTree_data()
QTest::newRow("GroupDoneWithErrorTweakToSuccess") QTest::newRow("GroupDoneWithErrorTweakToSuccess")
<< TestData{storage, root3, log3, 1, OnDone::Success}; << TestData{storage, root3, log3, 1, OnDone::Success};
const Group root4 = createRoot(DoneResult::Error, false); const Group root4 = createRoot(DoneResult::Error, DoneResult::Error);
const Log log4 { const Log log4 {
{1, Handler::Setup}, {1, Handler::Setup},
{1, Handler::Error}, {1, Handler::Error},