From 44c71b05ff282b3ce5a2d71c0f14eed97bfefea6 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 19 Dec 2022 21:00:51 +0200 Subject: [PATCH] Refactor BooleanEvaluator to have overloaded constructors The signature of the function passed in is now used to determine if comparison operators are permitted. --- source/booleanevaluator.cpp | 11 +++++++---- source/booleanevaluator.h | 8 +++++--- source/conditionalloader.cpp | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/source/booleanevaluator.cpp b/source/booleanevaluator.cpp index 074956a..6891abc 100644 --- a/source/booleanevaluator.cpp +++ b/source/booleanevaluator.cpp @@ -3,11 +3,14 @@ using namespace std; -/* I'd rather have overloads with different slots, but that creates an -ambiguity because slots have template constructors. */ -BooleanEvaluator::BooleanEvaluator(const Function &f, bool allow_compare): +BooleanEvaluator::BooleanEvaluator(const ValueFunction &f): + func([&f](const string &value, const string *){ return f(value); }), + ops("&|!") +{ } + +BooleanEvaluator::BooleanEvaluator(const CompareFunction &f): func(f), - ops(allow_compare ? "&|!=^" : "&|!") + ops("&|!=^") { } bool BooleanEvaluator::evaluate(const string &str) diff --git a/source/booleanevaluator.h b/source/booleanevaluator.h index 7c2af6f..c3f61f2 100644 --- a/source/booleanevaluator.h +++ b/source/booleanevaluator.h @@ -8,10 +8,11 @@ class BooleanEvaluator { public: - using Function = std::function; + using ValueFunction = std::function; + using CompareFunction = std::function; private: - Function func; + CompareFunction func; std::string ops; std::vector var_stack; std::vector value_stack; @@ -19,7 +20,8 @@ private: bool last_was_op; public: - BooleanEvaluator(const Function &, bool = true); + BooleanEvaluator(const ValueFunction &); + BooleanEvaluator(const CompareFunction &); bool evaluate(const std::string &); private: diff --git a/source/conditionalloader.cpp b/source/conditionalloader.cpp index 731c559..6233435 100644 --- a/source/conditionalloader.cpp +++ b/source/conditionalloader.cpp @@ -17,7 +17,7 @@ ArchitectureConditional::ArchitectureConditional(const Builder &b, const string void ArchitectureConditional::if_arch(const string &cond) { const Architecture &arch = builder.get_current_arch(); - BooleanEvaluator eval([&arch](const string &value, const string *){ return arch.match_name(value); }, false); + BooleanEvaluator eval([&arch](const string &value){ return arch.match_name(value); }); bool match = eval.evaluate(cond); builder.get_logger().log("configure", format("%s: arch %s %smatched", log_prefix, cond, (match ? "" : "not "))); if(match) -- 2.43.0