File tree Expand file tree Collapse file tree 3 files changed +62
-0
lines changed
Expand file tree Collapse file tree 3 files changed +62
-0
lines changed Original file line number Diff line number Diff line change @@ -20,4 +20,9 @@ if(BUILD_TESTING)
2020 target_compile_options ( expression_tree_test PRIVATE -fsanitize=address )
2121 add_test ( expression_tree_test ${EXECUTABLE_OUTPUT_PATH} /expression_tree_test )
2222
23+ add_executable ( examples examples.cpp )
24+ target_link_libraries ( examples "-fsanitize=address" )
25+ target_compile_options ( examples PRIVATE -fsanitize=address )
26+ add_test ( examples ${EXECUTABLE_OUTPUT_PATH} /examples )
27+
2328endif ()
Original file line number Diff line number Diff line change 1+ #include < attwoodn/expression_tree.hpp>
2+ #include < cassert>
3+
4+ using namespace attwoodn ::expression_tree;
5+
6+ void expression_memory_safety_example ();
7+
8+ struct my_type {
9+ int my_int = 5 ;
10+ bool my_bool = true ;
11+ };
12+
13+ int main (int argc, char ** argv) {
14+ expression_memory_safety_example ();
15+
16+ return EXIT_SUCCESS;
17+ }
18+
19+ void expression_memory_safety_example () {
20+ // The heap-allocated expression node pointer returned by make_expr becomes owned by the expression_tree
21+ expression_tree<my_type> expr_tree_raw {
22+ make_expr (&my_type::my_bool, op::equals, true )
23+ };
24+
25+
26+ // ...
27+
28+
29+ // The heap-allocated expression node pointer returned by make_expr becomes owned by the unique_ptr
30+ std::unique_ptr<node::expression_tree_node<my_type>> smart_expr {
31+ make_expr (&my_type::my_bool, op::equals, true )
32+ };
33+
34+ // the expression_tree takes ownership of the unique_ptr
35+ expression_tree<my_type> expr_tree_smart (std::move (smart_expr));
36+
37+
38+ // ...
39+
40+
41+ // The heap-allocated expression node pointer returned by make_expr must be explicitly deleted
42+ auto * expr_raw = make_expr (&my_type::my_bool, op::equals, true );
43+ delete expr_raw;
44+ }
Original file line number Diff line number Diff line change 44
55using namespace attwoodn ::expression_tree;
66
7+ void test_expression_tree_std_string_template ();
78void test_simple_expression_tree ();
89void test_complex_expression_tree ();
910void test_moved_expression_tree ();
1011void test_copied_expression_tree ();
1112
1213int main (int argc, char ** argv) {
14+ test_expression_tree_std_string_template ();
1315 test_simple_expression_tree ();
1416 test_complex_expression_tree ();
1517 test_moved_expression_tree ();
@@ -18,6 +20,17 @@ int main(int argc, char** argv) {
1820 return EXIT_SUCCESS;
1921}
2022
23+ void test_expression_tree_std_string_template () {
24+ expression_tree<std::string> expr {
25+ make_expr (&std::string::empty, op::equals, true )
26+ };
27+
28+ assert (expr.evaluate (" " ));
29+ assert (!expr.evaluate (" " ));
30+ assert (!expr.evaluate (" hello" ));
31+ assert (!expr.evaluate (" good bye" ));
32+ }
33+
2134void test_simple_expression_tree () {
2235 auto test_procedure = [](const expression_tree<test_fixture>& expr) {
2336 test_fixture fixture;
You can’t perform that action at this time.
0 commit comments