-
Notifications
You must be signed in to change notification settings - Fork 43
/
arrays_vectors_strings.cpp
39 lines (33 loc) · 1.21 KB
/
arrays_vectors_strings.cpp
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
33
34
35
36
37
38
39
#include <alpaca/alpaca.h>
int main() {
struct MyStruct {
std::array<int, 3> a;
std::vector<std::vector<float>> b;
std::string c;
};
MyStruct s{{1, 2, 3}, {{3.14, 1.61}, {2.71, -1}}, {"Hello"}};
// Serialize
std::vector<uint8_t> bytes;
auto bytes_written = alpaca::serialize(s, bytes); // 28 bytes
// bytes:
// {
// 0x01 0x02 0x03 // array {1, 2, 3}
// 0x02 // 2-element vector
// 0x02 // 2-element (inner) vector
// 0xc3 0xf5 0x48 0x40 // vector[0][0] = 3.14
// 0x7b 0x14 0xce 0x3f // vector[0][1] = 1.61
// 0x02 // 2-element (inner) vector
// 0xa4 0x70 0x2d 0x40 // vector[1][0] = 2.71
// 0x00 0x00 0x80 0xbf // vector[1][1] = -1
// 0x05 // start of 5-byte string
// 0x48 0x65 0x6c 0x6c 0x6f // string "Hello"
// }
// Deserialize
std::error_code ec;
auto recovered = alpaca::deserialize<MyStruct>(bytes, ec);
assert((bool)ec == false);
assert((recovered.a == std::array<int, 3>{1, 2, 3}));
assert((recovered.b ==
std::vector<std::vector<float>>{{3.14, 1.61}, {2.71, -1}}));
assert((recovered.c == std::string{"Hello"}));
}