forked from vikshanker/sponge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_example.cc
32 lines (29 loc) · 1005 Bytes
/
parser_example.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const uint32_t val1 = 0xdeadbeef;
const uint16_t val2 = 0xc0c0;
const uint8_t val3 = 0xff;
const uint32_t val4 = 0x0c05fefe;
// first, let's serialize it
std::string buffer;
buffer.push_back(0x32); // manually added to beginning of string
{
NetUnparser p;
p.u32(buffer, val1);
p.u16(buffer, val2);
p.u8(buffer, val3);
p.u32(buffer, val4);
} // p goes out of scope, data is in buffer
// now let's deserialize it
uint8_t out0, out3;
uint32_t out1, out4;
uint16_t out2;
{
NetParser p{std::string(buffer)}; // NOTE: starting at offset 0
out0 = p.u8(); // buffer[0], which we manually set to 0x32 above
out1 = p.u32(); // parse out val1
out2 = p.u16(); // val2
out3 = p.u8(); // val3
out4 = p.u32(); // val4
} // p goes out of scope
if (out0 != 0x32 || out1 != val1 || out2 != val2 || out3 != val3 || out4 != val4) {
throw std::runtime_error("bad parse");
}