Skip to content

Commit b833839

Browse files
authored
updates README.md
1 parent b36cffa commit b833839

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

README.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This project is under development and is subject to change. Project contribution
1111
## Table of Contents
1212

1313
* [A Quick Example](#a-quick-example)
14+
* [Creating Expression Trees](#creating-expression-trees)
1415
* [Types of Expression Tree Nodes](#types-of-expression-tree-nodes)
1516
* [Expression Tree Leaf Nodes](#expression-tree-leaf-nodes)
1617
* [Expression Tree Op Nodes](#expression-tree-op-nodes)
@@ -31,9 +32,54 @@ This project is under development and is subject to change. Project contribution
3132
Below is a tree diagram showing the content of the expression_tree that was created in the example code above:
3233

3334
<p align="center">
34-
<img src="docs/booleval-tree.png"/>
35+
<img src="docs/expression-tree.png"/>
3536
</p>
3637

38+
## Creating Expression Trees
39+
40+
The expression_tree class is a templated, RAII container class that takes ownership of user-defined expressions. The template parameter of expression_tree is the type of object that the expression_tree can evaluate. The template parameter of expression_tree cannot be a primitive type, like `int` or `char`.
41+
42+
An expression_tree cannot be default constructed - it must be initialized with an expression. Users can easily and intuitively define expressions using one of the `make_expr` helper functions found in the namespace `attwoodn::expression_tree`.
43+
44+
`make_expr` generates heap-allocated pointers to expression tree nodes and returns them. As such, the returned expression tree node pointers should be managed carefully. If the returned pointers are not wrapped in an expression_tree or a smart pointer, they will need to be explicitly `delete`d by the calling code.
45+
46+
Here are some examples of how you might handle the return value from one of the `make_expr` helper functions:
47+
```cpp
48+
#include <attwoodn/expression_tree.hpp>
49+
50+
using namespace attwoodn::expression_tree;
51+
52+
struct my_type {
53+
int my_int = 5;
54+
bool my_bool = true;
55+
};
56+
57+
...
58+
59+
// The heap-allocated expression node pointer returned by make_expr becomes owned by the expression_tree
60+
expression_tree<my_type> expr_tree_raw {
61+
make_expr(&my_type::my_bool, op::equals, true)
62+
};
63+
64+
...
65+
66+
// The heap-allocated expression node pointer returned by make_expr becomes owned by the unique_ptr
67+
std::unique_ptr<node::expression_tree_node<my_type>> smart_expr {
68+
make_expr(&my_type::my_bool, op::equals, true)
69+
};
70+
71+
// the expression_tree takes ownership of the unique_ptr
72+
expression_tree<my_type> expr_tree_smart(std::move(smart_expr));
73+
74+
...
75+
76+
// The heap-allocated expression node pointer returned by make_expr must be explicitly deleted
77+
auto* expr_raw = make_expr(&my_type::my_bool, op::equals, true);
78+
delete expr_raw;
79+
```
80+
81+
Please see the section below for more information about expression tree nodes.
82+
3783
## Types of Expression Tree Nodes
3884
3985
There are two types of expression tree nodes: leaf nodes and op nodes.

0 commit comments

Comments
 (0)