Skip to content

Commit 1dedb0c

Browse files
committed
updates README
1 parent a8e970f commit 1dedb0c

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

README.md

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Cpp Expression Tree
32

43
Cpp Expression Tree is a header-only, C++14 library for creating logical expression trees and using them to evaluate instances of user-defined data types.
@@ -8,6 +7,7 @@ Inspired by m-peko/booleval.
87
This project is under development and is subject to change. Project contributions and issue reports are welcome. The more the merrier!
98
( ... well, maybe not so much for bug reports)
109

10+
1111
## Table of Contents
1212

1313
* [A Quick Example](#a-quick-example)
@@ -18,9 +18,10 @@ This project is under development and is subject to change. Project contribution
1818
* [Logical Operators](#logical-operators)
1919
* [Boolean Operators](#boolean-operators)
2020
* [Using this Library](#using-this-library)
21-
* [Compilation](#compilation)
21+
* [Compiling](#compiling)
2222
* [Running the Unit Tests](#running-the-unit-tests)
2323

24+
2425
## A Quick Example
2526

2627
```cpp
@@ -80,11 +81,12 @@ Below is a diagram showing the content of the `expression_tree<my_type>` created
8081

8182
As you can imagine, this example code can be expanded to fit a variety of use cases and user-defined types. 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.
8283

84+
8385
## Creating Expression Trees
8486

8587
The `expression_tree` class is a templated, RAII container class that takes ownership of user-defined expressions. Instances of `expression_tree` can be moved and/or copied to different contexts while maintaining consistency and memory safety. The template parameter of `expression_tree` defines the type of object that the `expression_tree` will evaluate. Assuming there is a user-defined struct 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`.
8688

87-
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.
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 deleted by the calling code.
8890

8991
Here are some examples of how a user may safely handle the return value from one of the `make_expr` helper functions:
9092
```cpp
@@ -136,6 +138,7 @@ delete expr_raw;
136138
137139
Please see the section below for more information about expression tree nodes.
138140
141+
139142
## Types of Expression Tree Nodes
140143
141144
There are two types of expression tree nodes: leaf nodes and op nodes.
@@ -148,9 +151,10 @@ Expression tree leaf nodes contain individual, actionable expressions against wh
148151
149152
Expression tree op nodes contain a boolean operation (AND/OR) and have references to a left child node and a right child node. The child nodes may be expression tree leaf nodes, expression tree op nodes, or a permutation of the two. Expression tree op nodes are only ever found in the inner part of the tree. An expression tree op node is always a parent node and it always has two child nodes.
150153
154+
151155
## Logical Operators
152156
153-
There are several logical operator functions defined in the namespace `attwoodn::expression_tree::op`, which can be used to create expression tree leaf nodes. The included operators are:
157+
There are several logical operator functions defined in the namespace `attwoodn::expression_tree::op` that can be used to create expression tree leaf nodes. The included operators are:
154158
* equals
155159
* not_equals
156160
* less_than
@@ -220,46 +224,43 @@ incoming_packet.sender_name = "Jim";
220224
incoming_packet.payload.checksum_ok = true;
221225
incoming_packet.payload.data = "hello!";
222226
incoming_packet.payload.error_code = 0;
223-
assert(expr.evaluate(incoming_packet)); // passes evaluation
227+
assert(expr.evaluate(incoming_packet)); // passes evaluation
224228

225229
// Pam sends the same packet payload
226230
incoming_packet.sender_name = "Pam";
227-
assert(!expr.evaluate(incoming_packet)); // fails evaluation. No messages from Pam are accepted (sorry Pam)
231+
assert(!expr.evaluate(incoming_packet)); // fails evaluation. No packets from Pam are accepted (sorry)
228232

229233
// Jim sends a packet with a bad checksum
230234
incoming_packet.sender_name = "Jim";
231235
incoming_packet.payload.checksum_ok = false;
232-
assert(!expr.evaluate(incoming_packet)); // fails evaluation. The packet was from Jim, but checksum was bad
236+
assert(!expr.evaluate(incoming_packet)); // fails evaluation. Packet was from Jim, but checksum failed
233237

234238
// Jim sends a packet whose payload is too big
235239
incoming_packet.payload.checksum_ok = true;
236240
incoming_packet.payload.data = "Boy do I have a long story for you - so I was talking to Pam ...";
237-
assert(!expr.evaluate(incoming_packet)); // fails evaluation. The payload was too big. Give me the TLDR, Jim
241+
assert(!expr.evaluate(incoming_packet)); // fails evaluation. Payload was too big. Give me the TLDR, Jim
238242

239243
// Jim sends a small, rude packet
240244
incoming_packet.payload.data = "Dwight sux";
241-
assert(expr.evaluate(incoming_packet)); // passes evaluation. The payload was the right size this time
245+
assert(expr.evaluate(incoming_packet)); // passes evaluation. The payload was the right size this time
242246

243247
// Jim sends a packet has an error code
244248
incoming_packet.payload.error_code = 404;
245-
assert(!expr.evaluate(incoming_packet)); // fails evaluation. The payload had an error code
249+
assert(!expr.evaluate(incoming_packet)); // fails evaluation. The payload had an error code
246250
```
247251
248252
## Boolean Operators
249253
250254
Boolean operators are used to chain individual expression tree nodes together. There are two boolean operators that can be used: `AND` and `OR`. These boolean operators are accessible via function calls on the expression nodes. Calling these functions generates a new expression tree node which becomes the parent of the nodes on either side of the boolean operator
251255
252-
```cpp
253-
// some code here
254-
// shows how nodes are chained with AND and OR functions
255-
```
256-
257256
A complex expression tree can be created by calling these functions to chain multiple expression tree nodes together.
258257
258+
259259
## Using this Library
260260
261261
To include this library in your project, simply copy the content of the `include` directory into the `include` directory of your project. That's it! Now where did I put that Staples "Easy" button...?
262262
263+
263264
## Compiling
264265
265266
This project uses the CMake build system. The minimum CMake version is set to 3.10.
@@ -274,12 +275,14 @@ make
274275
275276
### Running the Unit Tests
276277
277-
After cloning and compiling the project, navigate to the build directory that was created. Enable the `BUILD_TESTING` CMake flag if it is not already enabled. Finally, run:
278+
After cloning and compiling the project, navigate to the build directory that was created. Enable the `BUILD_TESTING` CMake flag if it is not already enabled. My preferred tool for setting CMake flags via the command line is `ccmake`. Simply run `ccmake ..` in the build directory to get a command line UI for modifying CMake project flags. There, you can enable or disable the `BUILD_TESTING` flag, or set the build type from Release to Debug.
279+
280+
With the `BUILD_TESTING` flag enabled, run the following command in the build directory:
278281
279282
```
280283
ctest .
281284
```
282285
283286
CTest will execute the unit tests and provide a pass/fail indication for each one.
284287
285-
The address sanitizer is enabled on every unit test executable. A test will fail should memory leak during test execution.
288+
The address sanitizer is enabled on every unit test executable. A test will fail should memory leak during test execution.

0 commit comments

Comments
 (0)