Skip to content

Commit b352bb9

Browse files
committed
updates README
1 parent 0c2ddb0 commit b352bb9

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

README.md

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,71 @@ There are several logical operator functions defined in the namespace `attwoodn:
103103
Users of the library can also easily define their own logical operators and use them when creating expressions. Here is an example of how a user might create their own operator functions and use them in an expression:
104104
105105
```cpp
106-
// some code here
107-
// is_pointer functions: always returns false (value parameters), always returns true (pointer parameters)
106+
using namespace attwoodn::expression_tree;
107+
108+
struct packet_payload {
109+
uint16_t error_code;
110+
std::string data;
111+
bool checksum_ok;
112+
113+
uint64_t payload_size() const {
114+
return data.size();
115+
}
116+
};
117+
118+
class data_packet {
119+
public:
120+
std::string sender_name;
121+
packet_payload payload;
122+
};
123+
124+
...
125+
126+
// user creates their own logical operator for evaluating incoming packet_payload objects
127+
auto is_small_packet_payload = [](const packet_payload& incoming, const packet_payload&) -> bool {
128+
if(incoming.error_code == 0 && incoming.checksum_ok && incoming.payload_size() <= 10) {
129+
return true;
130+
}
131+
return false;
132+
};
133+
134+
// Create an expression that only accepts small, non-errored data packets from Jim.
135+
// evaluate packet contents using the user-defined lambda operator defined above
136+
expression_tree<data_packet> expr {
137+
make_expr(&data_packet::sender_name, op::equals, std::string("Jim"))
138+
->AND(make_expr(&data_packet::payload, is_small_packet_payload, packet_payload()))
139+
};
140+
141+
data_packet incoming_packet;
142+
143+
// Jim sends a small, non-errored data packet
144+
incoming_packet.sender_name = "Jim";
145+
incoming_packet.payload.checksum_ok = true;
146+
incoming_packet.payload.data = "hello!";
147+
incoming_packet.payload.error_code = 0;
148+
assert(expr.evaluate(incoming_packet)); // passes evaluation
149+
150+
// Pam sends the same packet payload
151+
incoming_packet.sender_name = "Pam";
152+
assert(!expr.evaluate(incoming_packet)); // fails evaluation. No messages from Pam are accepted (sorry Pam)
153+
154+
// Jim sends a packet with a bad checksum
155+
incoming_packet.sender_name = "Jim";
156+
incoming_packet.payload.checksum_ok = false;
157+
assert(!expr.evaluate(incoming_packet)); // fails evaluation. The packet was from Jim, but the checksum was bad
158+
159+
// Jim sends a packet whose payload is too big
160+
incoming_packet.payload.checksum_ok = true;
161+
incoming_packet.payload.data = "Boy do I have a long story for you - so I was talking to Pam ...";
162+
assert(!expr.evaluate(incoming_packet)); // fails evaluation. The packet's payload was too big. Give me the TLDR next time, Jim
163+
164+
// Jim sends a small, rude packet
165+
incoming_packet.payload.data = "Dwight sux";
166+
assert(expr.evaluate(incoming_packet)); // passes evaluation. The packet's payload was the right size this time
167+
168+
// Jim sends a packet has an error code
169+
incoming_packet.payload.error_code = 404;
170+
assert(!expr.evaluate(incoming_packet)); // fails evaluation. The packet's payload had an error code
108171
```
109172

110173
## Boolean Operators

0 commit comments

Comments
 (0)