-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathflatbuffers.hpp
35 lines (28 loc) · 1.03 KB
/
flatbuffers.hpp
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
#pragma once
#include "flatbuffers/flatbuffers.h"
#include "rpc_core/serialize.hpp"
namespace rpc_core {
template <typename T, typename std::enable_if<std::is_base_of<::flatbuffers::NativeTable, T>::value, int>::type = 0>
serialize_oarchive& operator>>(const T& t, serialize_oarchive& oa) {
using TableType = typename T::TableType;
flatbuffers::FlatBufferBuilder fbb(1024);
auto offset = TableType::Pack(fbb, &t);
fbb.Finish(offset);
auto data = fbb.GetBufferPointer();
auto size = fbb.GetSize();
oa.data.append((char*)data, size);
return oa;
}
template <typename T, typename std::enable_if<std::is_base_of<::flatbuffers::NativeTable, T>::value, int>::type = 0>
serialize_iarchive& operator<<(T& t, serialize_iarchive& ia) {
using TableType = typename T::TableType;
flatbuffers::Verifier verifier((uint8_t*)ia.data, ia.size);
bool ok = verifier.VerifyBuffer<TableType>();
if (!ok) {
ia.error = true;
return ia;
}
flatbuffers::GetRoot<TableType>(ia.data)->UnPackTo(&t);
return ia;
}
} // namespace rpc_core