-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversioning.cpp
More file actions
75 lines (58 loc) · 1.59 KB
/
Copy pathversioning.cpp
File metadata and controls
75 lines (58 loc) · 1.59 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <string>
#include <array>
#include <theg/serialization.hpp>
#include <theg/serialization/vector_sink.hpp>
#include <theg/serialization/data_source.hpp>
// Struct holding some data
struct MyStruct_v0
{
THEG_SERIALIZATION_VERSION(0, void);
int id;
char str[32]; // Should be big enough for any strings
double values[4];
};
// New version, had to increase the string buffer size
struct MyStruct_v1
{
THEG_SERIALIZATION_VERSION(1, MyStruct_v0);
// Convert from v0 to v1
explicit MyStruct_v1(const MyStruct_v0& old)
{
id = old.id;
strcpy(str, old.str);
values[0] = old.values[0];
}
MyStruct_v1() = default;
int id;
char str[64]; // Increase to 64, this time it will last
std::array<double, 4> values;
};
// Another version, don't ask
struct MyStruct_v2
{
THEG_SERIALIZATION_VERSION(2, MyStruct_v1);
// Convert from v1 to v2
explicit MyStruct_v2(const MyStruct_v1& old)
{
id = old.id;
str = old.str;
values[0] = old.values[0];
}
MyStruct_v2() = default;
int id;
std::string str; // Screw it
std::array<double, 4> values;
};
static void foo()
{
MyStruct_v0 oldStruct = {42, "Hello World", {1., 2., 3., 4}};
// At some point in the past we serialized the initial version of the struct
theg::serialization_vector_sink sink;
sink.write(oldStruct);
std::vector<std::byte> rawBytes = sink.release();
// Now we can read that old data into our new struct, the correct conversion
// will be applied automatically
theg::serialization_data_source source(rawBytes.data(), rawBytes.size());
auto newStruct = source.read<MyStruct_v2>();
assert(oldStruct.str == newStruct.str);
}