Add option for short circuit evaluation of conditions

This commit is contained in:
WarmUpTill
2024-08-30 21:21:21 +02:00
committed by WarmUpTill
parent 0906c6bb3f
commit c7c48d03e9
9 changed files with 168 additions and 46 deletions

View File

@@ -19,6 +19,15 @@ const std::map<Logic::Type, const char *> Logic::localeMap = {
bool Logic::ApplyConditionLogic(Type type, bool currentMatchResult,
bool conditionMatched, const char *context)
{
return ApplyConditionLogic(
type, currentMatchResult,
[conditionMatched]() { return conditionMatched; }, context);
}
bool Logic::ApplyConditionLogic(Type type, bool currentMatchResult,
const std::function<bool()> &evaluateCondition,
const char *context)
{
if (!context) {
context = "";
@@ -26,22 +35,22 @@ bool Logic::ApplyConditionLogic(Type type, bool currentMatchResult,
switch (type) {
case Type::ROOT_NONE:
return conditionMatched;
return evaluateCondition();
case Type::ROOT_NOT:
return !conditionMatched;
return !evaluateCondition();
case Type::ROOT_LAST:
break;
case Type::NONE:
vblog(LOG_INFO, "skipping condition check for '%s'", context);
return currentMatchResult;
case Type::AND:
return currentMatchResult && conditionMatched;
return currentMatchResult && evaluateCondition();
case Type::OR:
return currentMatchResult || conditionMatched;
return currentMatchResult || evaluateCondition();
case Type::AND_NOT:
return currentMatchResult && !conditionMatched;
return currentMatchResult && !evaluateCondition();
case Type::OR_NOT:
return currentMatchResult || !conditionMatched;
return currentMatchResult || !evaluateCondition();
case Type::LAST:
default:
blog(LOG_WARNING, "ignoring invalid logic check (%s)", context);