Skip to content

Commit c438286

Browse files
committed
modifies AND/OR functions for easier reading through auto return types, removes deprecated test helper function
1 parent 4a90580 commit c438286

File tree

3 files changed

+41
-52
lines changed

3 files changed

+41
-52
lines changed

include/attwoodn/expression_tree.hpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,10 @@ namespace attwoodn::expression_tree {
162162
* and the other node that was AND'ed with this node. This node becomes the left child. The other node becomes
163163
* the right child.
164164
*/
165-
template<typename OtherOp, typename OtherCompValue, typename OtherLeafNode,
166-
std::enable_if<std::is_same<OtherLeafNode, expression_tree_leaf_node<Obj, OtherOp, OtherCompValue>>::value>* = nullptr>
167-
expression_tree_op_node<Obj, this_type, OtherLeafNode>* AND (OtherLeafNode* other) {
168-
auto* op_node = new expression_tree_op_node<Obj, this_type, OtherLeafNode>(boolean_op::AND);
165+
template<typename OtherOp, typename OtherCompValue>
166+
auto* AND (expression_tree_leaf_node<Obj, OtherOp, OtherCompValue>* other) {
167+
using ret = expression_tree_op_node<Obj, this_type, expression_tree_leaf_node<Obj, OtherOp, OtherCompValue>>;
168+
ret* op_node = new ret(boolean_op::AND);
169169
op_node->set_left(this);
170170
op_node->set_right(other);
171171
return op_node;
@@ -177,10 +177,10 @@ namespace attwoodn::expression_tree {
177177
* and the other node that was OR'ed with this node. This node becomes the left child. The other node becomes
178178
* the right child.
179179
*/
180-
template<typename OtherOp, typename OtherCompValue, typename OtherLeafNode,
181-
std::enable_if<std::is_same<OtherLeafNode, expression_tree_leaf_node<Obj, OtherOp, OtherCompValue>>::value>* = nullptr>
182-
expression_tree_op_node<Obj, this_type, OtherLeafNode>* OR (OtherLeafNode* other) {
183-
auto* op_node = new expression_tree_op_node<Obj, this_type, OtherLeafNode>(boolean_op::OR);
180+
template<typename OtherOp, typename OtherCompValue>
181+
auto* OR (expression_tree_leaf_node<Obj, OtherOp, OtherCompValue>* other) {
182+
using ret = expression_tree_op_node<Obj, this_type, expression_tree_leaf_node<Obj, OtherOp, OtherCompValue>>;
183+
ret* op_node = new ret(boolean_op::OR);
184184
op_node->set_left(this);
185185
op_node->set_right(other);
186186
return op_node;
@@ -192,10 +192,10 @@ namespace attwoodn::expression_tree {
192192
* and the other node that was AND'ed with this node. This node becomes the left child. The other node becomes
193193
* the right child.
194194
*/
195-
template<typename OtherLeftChild, typename OtherRightChild, typename OtherOpNode,
196-
std::enable_if<std::is_same<OtherOpNode, expression_tree_op_node<Obj, OtherLeftChild, OtherRightChild>>::value>* = nullptr>
197-
expression_tree_op_node<Obj, this_type, OtherOpNode>* AND (OtherOpNode* other) {
198-
auto* op_node = new expression_tree_op_node<Obj, this_type, OtherOpNode>(boolean_op::AND);
195+
template<typename OtherLeftChild, typename OtherRightChild>
196+
auto* AND (expression_tree_op_node<Obj, OtherLeftChild, OtherRightChild>* other) {
197+
using ret = expression_tree_op_node<Obj, this_type, expression_tree_op_node<Obj, OtherLeftChild, OtherRightChild>>;
198+
ret* op_node = new ret(boolean_op::AND);
199199
op_node->set_left(this);
200200
op_node->set_right(other);
201201
return op_node;
@@ -207,10 +207,10 @@ namespace attwoodn::expression_tree {
207207
* and the other node that was OR'ed with this node. This node becomes the left child. The other node becomes
208208
* the right child.
209209
*/
210-
template<typename OtherLeftChild, typename OtherRightChild, typename OtherOpNode,
211-
std::enable_if<std::is_same<OtherOpNode, expression_tree_op_node<Obj, OtherLeftChild, OtherRightChild>>::value>* = nullptr>
212-
expression_tree_op_node<Obj, this_type, OtherOpNode>* OR (OtherOpNode* other) {
213-
auto* op_node = new expression_tree_op_node<Obj, this_type, OtherOpNode>(boolean_op::OR);
210+
template<typename OtherLeftChild, typename OtherRightChild>
211+
auto* OR (expression_tree_op_node<Obj, OtherLeftChild, OtherRightChild>* other) {
212+
using ret = expression_tree_op_node<Obj, this_type, expression_tree_op_node<Obj, OtherLeftChild, OtherRightChild>>;
213+
ret* op_node = new ret(boolean_op::OR);
214214
op_node->set_left(this);
215215
op_node->set_right(other);
216216
return op_node;
@@ -289,10 +289,10 @@ namespace attwoodn::expression_tree {
289289
* and the other node that was AND'ed with this node. This node becomes the left child. The other node becomes
290290
* the right child.
291291
*/
292-
template<typename OtherOp, typename OtherCompValue, typename OtherLeafNode,
293-
std::enable_if<std::is_same<OtherLeafNode, expression_tree_leaf_node<Obj, OtherOp, OtherCompValue>>::value>* = nullptr>
294-
expression_tree_op_node<Obj, this_type, OtherLeafNode>* AND (OtherLeafNode* other) {
295-
auto* op_node = new expression_tree_op_node<Obj, this_type, OtherLeafNode>(boolean_op::AND);
292+
template<typename OtherOp, typename OtherCompValue>
293+
auto* AND (expression_tree_leaf_node<Obj, OtherOp, OtherCompValue>* other) {
294+
using ret = expression_tree_op_node<Obj, this_type, expression_tree_leaf_node<Obj, OtherOp, OtherCompValue>>;
295+
ret* op_node = new ret(boolean_op::AND);
296296
op_node->set_left(this);
297297
op_node->set_right(other);
298298
return op_node;
@@ -304,10 +304,10 @@ namespace attwoodn::expression_tree {
304304
* and the other node that was OR'ed with this node. This node becomes the left child. The other node becomes
305305
* the right child.
306306
*/
307-
template<typename OtherOp, typename OtherCompValue, typename OtherLeafNode,
308-
std::enable_if<std::is_same<OtherLeafNode, expression_tree_leaf_node<Obj, OtherOp, OtherCompValue>>::value>* = nullptr>
309-
expression_tree_op_node<Obj, this_type, OtherLeafNode>* OR (OtherLeafNode* other) {
310-
auto* op_node = new expression_tree_op_node<Obj, this_type, OtherLeafNode>(boolean_op::OR);
307+
template<typename OtherOp, typename OtherCompValue>
308+
auto* OR (expression_tree_leaf_node<Obj, OtherOp, OtherCompValue>* other) {
309+
using ret = expression_tree_op_node<Obj, this_type, expression_tree_leaf_node<Obj, OtherOp, OtherCompValue>>;
310+
ret* op_node = new ret(boolean_op::OR);
311311
op_node->set_left(this);
312312
op_node->set_right(other);
313313
return op_node;
@@ -319,10 +319,10 @@ namespace attwoodn::expression_tree {
319319
* and the other node that was AND'ed with this node. This node becomes the left child. The other node becomes
320320
* the right child.
321321
*/
322-
template<typename OtherLeftChild, typename OtherRightChild, typename OtherOpNode,
323-
std::enable_if<std::is_same<OtherOpNode, expression_tree_op_node<Obj, OtherLeftChild, OtherRightChild>>::value>* = nullptr>
324-
expression_tree_op_node<Obj, this_type, OtherOpNode>* AND (OtherOpNode* other) {
325-
auto* op_node = new expression_tree_op_node<Obj, this_type, OtherOpNode>(boolean_op::AND);
322+
template<typename OtherLeftChild, typename OtherRightChild>
323+
auto* AND (expression_tree_op_node<Obj, OtherLeftChild, OtherRightChild>* other) {
324+
using ret = expression_tree_op_node<Obj, this_type, expression_tree_op_node<Obj, OtherLeftChild, OtherRightChild>>;
325+
ret* op_node = new ret(boolean_op::AND);
326326
op_node->set_left(this);
327327
op_node->set_right(other);
328328
return op_node;
@@ -334,10 +334,10 @@ namespace attwoodn::expression_tree {
334334
* and the other node that was OR'ed with this node. This node becomes the left child. The other node becomes
335335
* the right child.
336336
*/
337-
template<typename OtherLeftChild, typename OtherRightChild, typename OtherOpNode,
338-
std::enable_if<std::is_same<OtherOpNode, expression_tree_op_node<Obj, OtherLeftChild, OtherRightChild>>::value>* = nullptr>
339-
expression_tree_op_node<Obj, this_type, OtherOpNode>* OR (OtherOpNode* other) {
340-
auto* op_node = new expression_tree_op_node<Obj, this_type, OtherOpNode>(boolean_op::OR);
337+
template<typename OtherLeftChild, typename OtherRightChild>
338+
auto* OR (expression_tree_op_node<Obj, OtherLeftChild, OtherRightChild>* other) {
339+
using ret = expression_tree_op_node<Obj, this_type, expression_tree_op_node<Obj, OtherLeftChild, OtherRightChild>>;
340+
ret* op_node = new ret(boolean_op::OR);
341341
op_node->set_left(this);
342342
op_node->set_right(other);
343343
return op_node;

tests/expression_tree_op_node.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ int main(int argc, char** argv) {
1818
}
1919

2020
void test_AND_op_node_evaluation() {
21-
auto child_expr1 = make_expr(&test_fixture::some_string, op::equals, std::string("hello, world!"));
22-
auto child_expr2 = make_expr(&test_fixture::some_uint, op::less_than, (uint16_t) 500);
23-
24-
auto expr = make_op_node<test_fixture>(child_expr1, node::boolean_op::AND, child_expr2);
21+
auto expr = std::unique_ptr<node::expression_tree_node<test_fixture>>(
22+
make_expr(&test_fixture::some_string, op::equals, std::string("hello, world!"))
23+
->AND(make_expr(&test_fixture::some_uint, op::less_than, (uint16_t) 500))
24+
);
2525

2626
test_fixture fixture;
2727
fixture.some_string = "hello, world!";
@@ -69,10 +69,10 @@ void test_AND_op_node_evaluation() {
6969
}
7070

7171
void test_OR_op_node_evaluation() {
72-
auto child_expr1 = make_expr(&test_fixture::some_string, op::equals, std::string("hello, world!"));
73-
auto child_expr2 = make_expr(&test_fixture::some_uint, op::less_than, (uint16_t) 500);
74-
75-
auto expr = make_op_node<test_fixture>(child_expr1, node::boolean_op::OR, child_expr2);
72+
auto expr = std::unique_ptr<node::expression_tree_node<test_fixture>>(
73+
make_expr(&test_fixture::some_string, op::equals, std::string("hello, world!"))
74+
->OR(make_expr(&test_fixture::some_uint, op::less_than, (uint16_t) 500))
75+
);
7676

7777
test_fixture fixture;
7878
fixture.some_string = "hello, world!";

tests/test_utils.hpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,4 @@ struct test_fixture {
1515
bool is_some_uint_greater_than_zero() const {
1616
return some_uint;
1717
}
18-
};
19-
20-
/**
21-
* Helper function for creating inner expression tree nodes. I'm undecided if this should be included in the public API.
22-
*/
23-
template<typename Obj, typename LeftChild, typename RightChild>
24-
std::unique_ptr<et::node::expression_tree_node<Obj>> make_op_node(LeftChild* left, et::node::boolean_op op, RightChild* right) {
25-
auto node = new et::node::expression_tree_op_node<Obj, LeftChild, RightChild>(op);
26-
node->set_left(left);
27-
node->set_right(right);
28-
return std::unique_ptr<et::node::expression_tree_node<Obj>>(node);
29-
}
18+
};

0 commit comments

Comments
 (0)