You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As you can imagine, this example code can be expanded to fit a variety of use cases and struct/class type structures. More complex code examples are provided in the documentation below. Further, there are a number of unit tests located in the `tests` directory, which may be helpful for getting familiar with the library.
84
+
38
85
## Creating Expression Trees
39
86
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. Assuming there is a user-defined class named `my_type`, the templated `expression_tree` type would look like this: `expression_tree<my_type>`. The template argument of `expression_tree` cannot be a primitive type, like `int`, `char`, or `double`.
87
+
The `expression_tree` class is a templated, RAII container class that takes ownership of user-defined expressions. The `expression_tree` class can be moved and/or copied to different contexts while maintaining consistency and safety. The template parameter of `expression_tree` is the type of object that the `expression_tree` can evaluate. Assuming there is a user-defined class named `my_type`, the templated `expression_tree` type would look like this: `expression_tree<my_type>`. The template argument of `expression_tree` cannot be a primitive type, like `int`, `char`, or `double`.
41
88
42
89
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`. `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.
43
90
@@ -49,20 +96,29 @@ Here are some examples of how you might handle the return value from one of the
49
96
50
97
usingnamespaceattwoodn::expression_tree;
51
98
99
+
// let's bring back the same implementation of my_type as shown above
52
100
struct my_type {
53
-
int my_int = 5;
54
-
bool my_bool = true;
101
+
int my_int;
102
+
bool my_bool;
103
+
104
+
int get_my_int() const {
105
+
return my_int;
106
+
}
55
107
};
56
108
109
+
57
110
...
58
111
112
+
59
113
// The heap-allocated expression node pointer returned by make_expr becomes owned by the expression_tree
60
114
expression_tree<my_type> expr_tree_raw {
61
115
make_expr(&my_type::my_bool, op::equals, true)
62
116
};
63
117
118
+
64
119
...
65
120
121
+
66
122
// The heap-allocated expression node pointer returned by make_expr becomes owned by the unique_ptr
0 commit comments