-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathflatbuffers.cpp
126 lines (115 loc) · 5.32 KB
/
flatbuffers.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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//MIT License
//
//Copyright (c) 2017 Mindaugas Vinkelis
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
#include <testing/test.h>
#include <algorithm>
#include "monster_generated.h"
using namespace MyGame::Sample;
class FlatbuffersArchiver : public ISerializerTest {
public:
auto createWeapon(flatbuffers::FlatBufferBuilder &builder, const MyTypes::Weapon &weapon) {
auto name = builder.CreateString(weapon.name);
return CreateWeapon(builder, name, weapon.damage);
}
Buf serialize(const std::vector<MyTypes::Monster> &data) override {
_builder.Clear();
std::vector<flatbuffers::Offset < Monster>>
monstersVec{};
monstersVec.reserve(data.size());
for (auto &m:data) {
// Create a FlatBuffer's `vector` from the `std::vector`.
std::vector<flatbuffers::Offset < Weapon>>
weaponsVec{};
weaponsVec.reserve(m.weapons.size());
for (auto &w:m.weapons)
weaponsVec.push_back(createWeapon(_builder, w));
auto weapons = _builder.CreateVector(weaponsVec);
// Second, serialize the rest of the objects needed by the Monster.
auto position = Vec3(m.pos.x, m.pos.y, m.pos.z);
auto name = _builder.CreateString(m.name);
auto inventory = _builder.CreateVector(m.inventory);
std::vector<Vec3> pathVec{};
pathVec.reserve(m.path.size());
for (auto &p:m.path)
pathVec.push_back(Vec3(p.x, p.y, p.z));
auto path = _builder.CreateVectorOfStructs(pathVec);
// Shortcut for creating monster with all fields set:
monstersVec.push_back(
CreateMonster(_builder,
&position,
m.mana,
m.hp,
name,
inventory,
static_cast<Color>(m.color),
weapons,
createWeapon(_builder, m.equipped),
path));
}
auto monsters = _builder.CreateVector(monstersVec);
auto root = CreateMonstersList(_builder, monsters);
_builder.Finish(root);
return Buf{_builder.GetBufferPointer(), _builder.GetSize()};
}
void deserialize(Buf buf, std::vector<MyTypes::Monster> &res) override {
auto ver = flatbuffers::Verifier(buf.ptr, buf.bytesCount);
if (VerifyMonstersListBuffer(ver)) {
auto monsters = GetMonstersList(buf.ptr);
auto data = monsters->data();
res.resize(data->size());
for (auto i = 0u; i < data->size(); ++i) {
auto m = data->Get(i);
auto &resM = res[i];
resM.equipped = MyTypes::Weapon{m->equipped()->name()->data(), m->equipped()->damage()};
resM.name.resize(m->name()->size());
//cannot memcpy, because data is not contigous on flatbuffers
std::copy(m->name()->begin(), m->name()->end(), resM.name.begin());
resM.color = static_cast<MyTypes::Color>(m->color());
resM.hp = m->hp();
resM.mana = m->mana();
resM.pos = MyTypes::Vec3{m->pos()->x(), m->pos()->y(), m->pos()->z()};
resM.inventory.resize(m->inventory()->size());
std::copy(m->inventory()->begin(), m->inventory()->end(), resM.inventory.begin());
resM.weapons.resize(m->weapons()->size());
std::transform(m->weapons()->begin(), m->weapons()->end(), resM.weapons.begin(), [](const auto &w) {
return MyTypes::Weapon{w->name()->data(), w->damage()};
});
resM.path.resize(m->path()->size());
std::transform(m->path()->begin(), m->path()->end(), resM.path.begin(), [](const auto &p) {
return MyTypes::Vec3{p->x(), p->y(), p->z()};
});
}
}
};
TestInfo testInfo() const override {
return {
SerializationLibrary::FLATBUFFERS,
"general",
""
};
}
private:
flatbuffers::FlatBufferBuilder _builder;
};
int main() {
FlatbuffersArchiver test;
return runTest(test);
}