From 8b13f36749237791d7aee3ac198cbb3c5620c17f Mon Sep 17 00:00:00 2001 From: m-peko Date: Sat, 11 Apr 2020 08:56:48 +0200 Subject: [PATCH 1/2] Update example --- README.md | 2 +- examples/evaluator.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b7660dc..4b9cbe7 100644 --- a/README.md +++ b/README.md @@ -188,7 +188,7 @@ int main() { }); std::cout << std::boolalpha << evaluator.evaluate(pass) << std::endl; // output: true - std::cout << std::boolalpha << evaluator.evaluate(fail) << std::endl; // output: false + std::cout << std::boolalpha << evaluator.evaluate(fail) << std::endl; // output: false } else { std::cerr << "Evaluator is not activated!" << std::endl; } diff --git a/examples/evaluator.cpp b/examples/evaluator.cpp index 7bd7298..e8b4bb4 100644 --- a/examples/evaluator.cpp +++ b/examples/evaluator.cpp @@ -68,8 +68,8 @@ int main() { { "field_b", &obj::field_b } }); - std::cout << std::boolalpha << evaluator.evaluate(pass) << std::endl; // output: true - std::cout << std::boolalpha << evaluator.evaluate(fail) << std::endl; // output: false + std::cout << std::boolalpha << evaluator.evaluate(pass) << std::endl; + std::cout << std::boolalpha << evaluator.evaluate(fail) << std::endl; } else { std::cerr << "Evaluator is not activated!" << std::endl; } From ef40fa0c82f2a88a8214a1b2b2cdb1483a0b5acb Mon Sep 17 00:00:00 2001 From: m-peko Date: Sat, 11 Apr 2020 09:10:28 +0200 Subject: [PATCH 2/2] Refactor result_visitor --- include/booleval/tree/result_visitor.h | 48 ++++++++++---------------- 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/include/booleval/tree/result_visitor.h b/include/booleval/tree/result_visitor.h index 3f600d0..901c82f 100644 --- a/include/booleval/tree/result_visitor.h +++ b/include/booleval/tree/result_visitor.h @@ -82,29 +82,17 @@ class result_visitor { private: /** - * Visits tree node representing logical operation AND. + * Visits tree node representing one of logical operations. * * @param node Currently visited tree node * @param obj Object to be evaluated + * @param func Logical operation function * - * @return Result of logical operation AND + * @return Result of logical operation */ - template - [[nodiscard]] bool visit_and(tree_node const& node, T const& obj) { - return visit(*node.left, obj) && visit(*node.right, obj); - } - - /** - * Visits tree node representing logical operation OR. - * - * @param node Currently visited tree node - * @param obj Object to be evaluated - * - * @return Result of logical operation OR - */ - template - [[nodiscard]] bool visit_or(tree_node const& node, T const& obj) { - return visit(*node.left, obj) || visit(*node.right, obj); + template + [[nodiscard]] bool visit_logical(tree_node const& node, T const& obj, F&& func) { + return func(visit(*node.left, obj), visit(*node.right, obj)); } /** @@ -112,15 +100,15 @@ class result_visitor { * * @param node Currently visited tree node * @param obj Object to be evaluated - * @param cmp Comparison function + * @param func Comparison function * * @return Result of relational operation */ - template - [[nodiscard]] bool visit(tree_node const& node, T const& obj, Cmp&& cmp) { + template + [[nodiscard]] bool visit_relational(tree_node const& node, T const& obj, F&& func) { auto key = node.left->token; auto value = node.right->token; - return cmp(fields_[key.value()].invoke(obj), value.value()); + return func(fields_[key.value()].invoke(obj), value.value()); } private: @@ -135,28 +123,28 @@ bool result_visitor::visit(tree_node const& node, T const& obj) { switch (node.token.type()) { case token::token_type::logical_and: - return visit_and(node, obj); + return visit_logical(node, obj, std::logical_and<>()); case token::token_type::logical_or: - return visit_or(node, obj); + return visit_logical(node, obj, std::logical_or<>()); case token::token_type::eq: - return visit(node, obj, std::equal_to<>()); + return visit_relational(node, obj, std::equal_to<>()); case token::token_type::neq: - return visit(node, obj, std::not_equal_to<>()); + return visit_relational(node, obj, std::not_equal_to<>()); case token::token_type::gt: - return visit(node, obj, std::greater<>()); + return visit_relational(node, obj, std::greater<>()); case token::token_type::lt: - return visit(node, obj, std::less<>()); + return visit_relational(node, obj, std::less<>()); case token::token_type::geq: - return visit(node, obj, std::greater_equal<>()); + return visit_relational(node, obj, std::greater_equal<>()); case token::token_type::leq: - return visit(node, obj, std::less_equal<>()); + return visit_relational(node, obj, std::less_equal<>()); default: return false;