-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_upgraded.cpp
More file actions
80 lines (65 loc) · 1.73 KB
/
Copy pathbasic_upgraded.cpp
File metadata and controls
80 lines (65 loc) · 1.73 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
76
77
78
79
80
// [basic_upgraded]
#include <savepoint/savepoint.hpp>
#include <cassert>
#include <filesystem>
static constexpr SavepointVersion kVersion{0, 0, 0};
// The old entity from basic_usage.cpp
struct EntityV1 : SavepointEntity
{
int X;
int Y;
EntityV1() = default;
EntityV1(int x, int y)
: X{x}
, Y{y}
{
}
void Visit(SavepointVisitor& visitor)
{
visitor(X);
visitor(Y);
}
};
// Added new versions
static constexpr SavepointVersion kVersionAddedZ{0, 0, 1};
static constexpr SavepointVersion kVersionAddedW{0, 0, 2};
// The new entity
struct EntityV2 : SavepointEntity
{
int X;
int Z; // Added a Z component in 0.0.1
int Y;
int W; // Added a W component in 0.0.2
void Visit(SavepointVisitor& visitor)
{
visitor(X);
visitor(Z, kVersionAddedZ, 0); // Deserialize if the version is >= 0.0.1 (otherwise default to 0)
visitor(Y);
visitor(W, kVersionAddedW, 1); // Deserialize if the version is >= 0.0.2 (otherwise default to 1)
}
bool operator==(const EntityV1& other) const
{
return X == other.X && Y == other.Y;
}
};
int main()
{
std::filesystem::remove("savepoint.sqlite3");
Savepoint savepoint;
savepoint.Open(SavepointDriver::SQLite3, "savepoint.sqlite3", kVersion);
// Write a V1 entity
EntityV1 inEntity{1, 2};
savepoint.Write(inEntity, 0);
// Read a V1 entity as a V2 entity
savepoint.Read<EntityV2>([&](EntityV2& outEntity)
{
// X and Y were read
assert(outEntity == inEntity);
// Z and W were not read
assert(outEntity.Z == 0);
assert(outEntity.W == 1);
}, 0);
savepoint.Close();
return 0;
}
// [basic_upgraded]