Skip to content

Commit

Permalink
Merge pull request #30 from m-peko/update-example
Browse files Browse the repository at this point in the history
Update example and refactor result_visitor
  • Loading branch information
m-peko authored Apr 11, 2020
2 parents cc884ba + ef40fa0 commit 0cd52bb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
48 changes: 18 additions & 30 deletions include/booleval/tree/result_visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,45 +82,33 @@ 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 <typename T>
[[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 <typename T>
[[nodiscard]] bool visit_or(tree_node const& node, T const& obj) {
return visit(*node.left, obj) || visit(*node.right, obj);
template <typename T, typename F>
[[nodiscard]] bool visit_logical(tree_node const& node, T const& obj, F&& func) {
return func(visit(*node.left, obj), visit(*node.right, obj));
}

/**
* Visits tree node representing one of relational operations.
*
* @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 <typename T, typename Cmp>
[[nodiscard]] bool visit(tree_node const& node, T const& obj, Cmp&& cmp) {
template <typename T, typename F>
[[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:
Expand All @@ -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;
Expand Down

0 comments on commit 0cd52bb

Please # to comment.