下载地址:
https://github.com/google/flatbuffers/releases
Solution(包括示例工程):
flatbuffers-1.1.0\build\VS2010\FlatBuffers.sln
flatc
生成器
flatsamplebinary
FlatBufferBuilder的使用示例
引入monster_generated.h头文件,创建Monster对象,并使用flatbuffers::FlatBufferBuilder生成二进制buffer数据
flatsampletext
Parser的使用示例
加载monster.fbs、和monsterdata.json,使用flatbuffers::Parser,将它们的文本转换为二进制buffer数据,再用二进制buffer数据转换回文本
flattests
测试用例
FuzzTest1 使用确定性随机序列来模拟builder中添加不同类型的数据组合,创建Table,然后解析table中对应位置的值,判断是否相等
FuzzTest2
四个头文件:
flatbuffers-1.1.0\include\flatbuffers\flatbuffers.h
flatbuffers-1.1.0\include\flatbuffers\hash.h
flatbuffers-1.1.0\include\flatbuffers\idl.h
flatbuffers-1.1.0\include\flatbuffers\util.h
idl.h
GenerateText
从 buffer 转 json
schema <-> cpp.h <-> buffer (bin) <-> text (json)
创建schema文件:
TestFlat.fbs
先使用vs2013编译得到: flatc.exe
进入命令行:
f:
cd F:\sonikk\b编程\flatBuffers\flatBuffers_2015-4-28 193412
flatc -c -o ./ ./TestFlat.fbs
生成了:
TestFlat_generated.h
flatsamplebinary工程t的sample_binary.cpp改写:
#include "flatbuffers/flatbuffers.h"
#include "monster_generated.h"
using namespace MyGame::Sample;
// sonikk
#include
void save_to_file_bin(std::string file_name, unsigned char* data, unsigned int length)
{
std::ofstream rs(file_name.c_str(), std::ios::binary);
rs.write((char*)data, length);
rs.close();
}
// @data OUT
unsigned int read_from_file_bin(std::string file_name, unsigned char*& data)
{
std::ifstream rs(file_name.c_str(), std::ios::binary);
//获取文件大小
rs.seekg(0, std::ios_base::end); // 对输入文件定位
long long length = rs.tellg();
rs.seekg(0, std::ios_base::beg); // 定位回开头,这样才可以读取
data = new unsigned char[(int)length];
rs.read((char*)data, length);
rs.close();
return (unsigned int)length;
}
int main(int /*argc*/, const char * /*argv*/[])
{
// Build up a serialized buffer algorithmically:
flatbuffers::FlatBufferBuilder builder;
auto vec = Vec3(1, 2, 3);
auto name = builder.CreateString("MyMonster");
unsigned char inv_data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
auto inventory = builder.CreateVector(inv_data, 10);
// Shortcut for creating monster with all fields set:
auto mloc = CreateMonster(builder, &vec, 150, 80, name, inventory,
Color_Blue);
builder.Finish(mloc);
// We now have a FlatBuffer we can store or send somewhere.
// ** file/network code goes here :) **
// access builder.GetBufferPointer() for builder.GetSize() bytes
// ------------------------------------------------------------------------------------
// 保存二进制到文件当中 @sonikk 2015-4-28 20:11:16
save_to_file_bin("test_bin.bin", builder.GetBufferPointer(), builder.GetSize() );
// -> flatbuffers-1.1.0\flatbuffers-1.1.0\build\VS2010\test_bin.bin
unsigned char* data = nullptr;
read_from_file_bin("test_bin.bin", data);
auto monster1 = GetMonster( (const void*) data);
auto hp = monster1->hp();
auto mana = monster1->mana();
// 成功!!! @sonikk 2015-4-28 20:52:13
hp = 1;
mana = 2;
// ------------------------------------------------------------------------------------
// Instead, we're going to access it straight away.
// Get access to the root:
auto monster = GetMonster(builder.GetBufferPointer());
assert(monster->hp() == 80);
assert(monster->mana() == 150); // default
assert(!strcmp(monster->name()->c_str(), "MyMonster"));
auto pos = monster->pos();
assert(pos);
assert(pos->z() == 3);
(void)pos;
auto inv = monster->inventory();
assert(inv);
assert(inv->Get(9) == 9);
(void)inv;
}