TaskTree: Introduce DoneResult, use it for Sync setup

Instead of using ambiguous bool.

Change-Id: Iec45b920a839ac3383abc2d0676e5834f282dddf
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Jarek Kobus
2023-11-06 17:48:56 +01:00
parent 88ac19f188
commit 7ff13f1eaa
4 changed files with 33 additions and 21 deletions

View File

@@ -159,6 +159,13 @@ enum class SetupResult
};
Q_ENUM_NS(SetupResult);
enum class DoneResult
{
Success,
Error
};
Q_ENUM_NS(DoneResult);
enum class DoneWith
{
Success,
@@ -374,15 +381,15 @@ public:
private:
template <typename Handler>
static GroupSetupHandler wrapHandler(Handler &&handler) {
// B, V stands for: [B]ool, [V]oid
static constexpr bool isB = isInvocable<bool, Handler>();
// D, V stands for: [D]oneResult, [V]oid
static constexpr bool isB = isInvocable<DoneResult, Handler>();
static constexpr bool isV = isInvocable<void, Handler>();
static_assert(isB || isV,
"Sync handler needs to take no arguments and has to return void or bool. "
"Sync handler needs to take no arguments and has to return void or DoneResult. "
"The passed handler doesn't fulfill these requirements.");
return [=] {
if constexpr (isB) {
return std::invoke(handler) ? SetupResult::StopWithSuccess
return std::invoke(handler) == DoneResult::Success ? SetupResult::StopWithSuccess
: SetupResult::StopWithError;
}
std::invoke(handler);

View File

@@ -59,7 +59,7 @@ CppIncludesFilter::CppIncludesFilter()
setDefaultShortcutString("ai");
setDefaultIncludedByDefault(true);
const auto invalidate = [this] { m_cache.invalidate(); };
setRefreshRecipe(Tasking::Sync([invalidate] { invalidate(); return true; }));
setRefreshRecipe(Tasking::Sync([invalidate] { invalidate(); }));
setPriority(ILocatorFilter::Low);
connect(ProjectExplorerPlugin::instance(), &ProjectExplorerPlugin::fileListChanged,

View File

@@ -63,19 +63,19 @@ GroupItem NimCompilerCleanStep::runRecipe()
if (!m_buildDir.exists()) {
emit addOutput(Tr::tr("Build directory \"%1\" does not exist.")
.arg(m_buildDir.toUserOutput()), OutputFormat::ErrorMessage);
return false;
return DoneResult::Error;
}
if (!removeCacheDirectory()) {
emit addOutput(Tr::tr("Failed to delete the cache directory."),
OutputFormat::ErrorMessage);
return false;
return DoneResult::Error;
}
if (!removeOutFilePath()) {
emit addOutput(Tr::tr("Failed to delete the out file."), OutputFormat::ErrorMessage);
return false;
return DoneResult::Error;
}
emit addOutput(Tr::tr("Clean step completed successfully."), OutputFormat::NormalMessage);
return true;
return DoneResult::Success;
};
return Sync(onSetup);
}

View File

@@ -397,6 +397,11 @@ static Handler doneToTweakHandler(bool result)
return result ? Handler::TweakDoneToSuccess : Handler::TweakDoneToError;
}
static Handler doneToTweakHandler(DoneResult result)
{
return result == DoneResult::Success ? Handler::TweakDoneToSuccess : Handler::TweakDoneToError;
}
static TestData storageShadowing()
{
// This test check if storage shadowing works OK.
@@ -555,11 +560,11 @@ void tst_Tasking::testTree_data()
const auto createSync = [storage](int taskId) {
return Sync([storage, taskId] { storage->m_log.append({taskId, Handler::Sync}); });
};
const auto createSyncWithTweak = [storage](int taskId, bool desiredResult) {
return Sync([storage, taskId, desiredResult] {
const auto createSyncWithTweak = [storage](int taskId, DoneResult result) {
return Sync([storage, taskId, result] {
storage->m_log.append({taskId, Handler::Sync});
storage->m_log.append({taskId, doneToTweakHandler(desiredResult)});
return desiredResult;
storage->m_log.append({taskId, doneToTweakHandler(result)});
return result;
});
};
@@ -2083,11 +2088,11 @@ void tst_Tasking::testTree_data()
{
const Group root {
Storage(storage),
createSyncWithTweak(1, true),
createSyncWithTweak(2, true),
createSyncWithTweak(3, true),
createSyncWithTweak(4, true),
createSyncWithTweak(5, true)
createSyncWithTweak(1, DoneResult::Success),
createSyncWithTweak(2, DoneResult::Success),
createSyncWithTweak(3, DoneResult::Success),
createSyncWithTweak(4, DoneResult::Success),
createSyncWithTweak(5, DoneResult::Success)
};
const Log log {
{1, Handler::Sync},
@@ -2130,7 +2135,7 @@ void tst_Tasking::testTree_data()
parallel,
createSync(1),
createSync(2),
createSyncWithTweak(3, false),
createSyncWithTweak(3, DoneResult::Error),
createSync(4),
createSync(5)
};
@@ -2171,7 +2176,7 @@ void tst_Tasking::testTree_data()
Storage(storage),
createSync(1),
createSuccessTask(2),
createSyncWithTweak(3, false),
createSyncWithTweak(3, DoneResult::Error),
createSuccessTask(4),
createSync(5),
groupDone(0)