Skip to content

Commit 95a1a54

Browse files
committed
implements expression tree leaf node class. Now to implement the boolean operation logic to tie it all together
1 parent 84d51ac commit 95a1a54

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

include/attwoodn/expression_tree.hpp

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ namespace attwoodn::expression_tree {
6060

6161
namespace node {
6262

63+
template<typename Obj, typename LeftChild, typename RightChild>
64+
class expression_tree_op_node;
65+
66+
template<typename Obj, typename Op, typename CompValue>
67+
class expression_tree_leaf_node;
68+
6369
/**
6470
* @brief The base class representing all expression tree nodes.
6571
*/
@@ -139,7 +145,6 @@ namespace attwoodn::expression_tree {
139145
return false;
140146
}
141147

142-
143148
private:
144149
boolean_op bool_op_;
145150
LeftChild* left_ { nullptr };
@@ -153,7 +158,55 @@ namespace attwoodn::expression_tree {
153158
*/
154159
template<typename Obj, typename Op, typename CompValue>
155160
class expression_tree_leaf_node : public expression_tree_node_base<Obj> {
161+
public:
162+
using this_type = expression_tree_leaf_node<Obj, Op, CompValue>;
163+
164+
/**
165+
* @brief Constructor that accepts a reference to a member variable of Obj
166+
*/
167+
expression_tree_leaf_node(CompValue Obj::* obj_mem_var, Op op, CompValue comp_value)
168+
: member_var_(obj_mem_var),
169+
logical_op_(op),
170+
comp_value_(comp_value) {}
171+
172+
/**
173+
* @brief Constructor that accepts a reference to a const member function of Obj
174+
*/
175+
expression_tree_leaf_node(CompValue (Obj::* obj_mem_func)() const, Op op, CompValue comp_value)
176+
: member_func_(obj_mem_func),
177+
logical_op_(op),
178+
comp_value_(comp_value) {}
156179

180+
~expression_tree_leaf_node() override {};
181+
182+
bool evaluate(const Obj& obj) override {
183+
if (!member_func_ && !member_var_) {
184+
throw std::runtime_error("expression_tree_leaf_node has a nullptr for both member function reference " +
185+
std::string("and member variable reference. At least one is required"));
186+
}
187+
188+
CompValue actual_value;
189+
190+
if (member_func_) {
191+
// invoke member function and store the result
192+
actual_value = (obj.*member_func_)();
193+
}
194+
195+
else if (member_var_) {
196+
// get value from obj's data member
197+
actual_value = obj.*member_var_;
198+
}
199+
200+
else return false;
201+
202+
return Op(actual_value, comp_value_);
203+
}
204+
205+
private:
206+
CompValue (Obj::* member_func_)() const = nullptr;
207+
CompValue Obj::* member_var_ = nullptr;
208+
Op logical_op_;
209+
CompValue comp_value_;
157210
};
158211

159212
}

0 commit comments

Comments
 (0)