forked from suancaiyu/FlatBufferDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
92 lines (64 loc) · 2.61 KB
/
main.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
//
// main.cpp
// FlatBufferDemo
//
// Created by derek on 2018/12/16.
// Copyright © 2018 derek. All rights reserved.
//
#include <iostream>
#include "Layer_Fb_generated.h"
#include "idl.h"
using namespace std;
flatbuffers::FlatBufferBuilder build_data;
void serialize() {
// super
auto position_fb = Layer::CreatePoint_Fb(build_data, 1, 1);
auto super_fb = Layer::CreateNode_Fb(build_data, build_data.CreateString("father"), position_fb);
// friend
auto friend_fb = Layer::CreateNode_Fb(build_data, build_data.CreateString("tom"), Layer::CreatePoint_Fb(build_data, 2, 2));
// children
std::vector<flatbuffers::Offset<Layer::Node_Fb>> nodeVector;
auto child1 = Layer::CreateNode_Fb(build_data, build_data.CreateString("son"), Layer::CreatePoint_Fb(build_data, 3, 3));
auto child2 = Layer::CreateNode_Fb(build_data, build_data.CreateString("daughter"), Layer::CreatePoint_Fb(build_data, 4, 4));
nodeVector.push_back(child1);
nodeVector.push_back(child2);
auto children_fb = build_data.CreateVector(nodeVector);
// layer
auto layer_fb = Layer::CreateLayer_Fb(build_data, super_fb, friend_fb, children_fb);
build_data.Finish(layer_fb);
cout<<"serialize size:"<<build_data.GetSize()<<endl;
}
void deserialize() {
// load binary file
std::string binaryfile;
bool ok = flatbuffers::LoadFile("layer.bin", false, &binaryfile);
if (!ok) {
return;
}
flatbuffers::FlatBufferBuilder builder_out;
builder_out.PushBytes(reinterpret_cast<unsigned char*>(const_cast<char*>(binaryfile.c_str())), binaryfile.size());
cout<<"deserialize size:"<<build_data.GetSize()<<endl;
// verify
flatbuffers::Verifier layer_verify(builder_out.GetCurrentBufferPointer(), build_data.GetSize());
bool verify_flag = Layer::VerifyLayer_FbBuffer(layer_verify);
if (!verify_flag) {
return;
}
// read
auto layer_fb = Layer::GetLayer_Fb(builder_out.GetCurrentBufferPointer());
cout<<"father name:"+layer_fb->super()->name()->str()<<endl;
cout<<"friend name:"+layer_fb->friend_()->name()->str()<<endl;
for (int i=0; i<layer_fb->children()->Length(); i++) {
string name = layer_fb->children()->Get(i)->name()->str();
cout<<"child "<<i<<":"+name<<endl;
}
}
int main(int argc, const char * argv[]) {
// insert code here...
// 序列化
serialize();
flatbuffers::SaveFile("layer.bin", reinterpret_cast<char*>(build_data.GetBufferPointer()), build_data.GetSize(), true);
// 反序列化
deserialize();
return 0;
}