FlatBuffers使用总结(1)

1.下载编译FlatBuffer

下载地址:https://github.com/fanyun-01/flatbuffers

2.编译FlatBuffer

     解压flatbuffers-master.zip文件文件,使用VS2015打开flatbuffers-master\build\VS2015\FlatBuffers.sln文件进行编译。编译后生成的文件如下:

(1).flatc.exe - 编译schema文件、生成代码

(2).flatsamplebinary.exe - 测试程序,将二进制转换为C++对象

(3).flatsampletext.exe - 测试程序,将json转换为C++对象

(4).flattests.exe - 测试flatbuffers

3.编写test.fbs文件

namespace TestApp;

struct KV {
    key: ulong;
    value: double;
}

table TestObj {
    id: ulong;
    name: string;
    flag: ubyte = 0;
    list: [ulong];
    kv: KV;
}

root_type TestObj;

4.然后利用"flatc.exe"生成.h文件,并使用此.h文件

int _tmain(int argc, _TCHAR* argv[])
{
    FlatBufferBuilder fbb;

    // 设置数据
    std::vector v = { 1, 2, 3, 4, 5 };
    auto kv = KV(1, 1.0);
    auto name = fbb.CreateString("123");
    auto vec = fbb.CreateVector(v);

    TestObjBuilder builder(fbb);
    builder.add_id(100);
    builder.add_name(name);
    builder.add_flag(1);
    builder.add_list(vec);
    builder.add_kv(&kv);
    fbb.Finish(builder.Finish());

    // 注册
    FlatBufferJsonFactory::Instance().Register("E:\\c++\\use_flat_buffers\\use_flat_buffers\\test.fbs");

    std::string json = FlatBufferJsonFactory::Instance().toJson(fbb.GetBufferPointer());
    std::string buffer = FlatBufferJsonFactory::Instance().parserJson(json);

    const TestApp::TestObj* obj = GetTestObj(buffer.c_str());
    const char* names = obj->name()->c_str();

    return 0;
}
 

5.json与C++对象相互转换

FlatBuffer的测试程序中有json与c++对象相互转换的例子,但是不够方便,在这里我们可以封装一下。

FlatBufferJsonFactory.h

#include "TypeBind.h" // 详见我的另一篇博文"C++模板之类型与数据绑定"

class FlatBufferJsonFactory {
private:

    struct ParserInfo {
        ParserInfo(const std::string& _path)
        : path(_path), parser(nullptr)
        {}

        std::string path;                // fbs文件路径
        flatbuffers::Parser* parser;    // flatbuffers解析器
    };

public:
    // 将json字符串转换为了对象缓冲区
    template
    std::string parserJson(const std::string& s) {
        const ParserInfo& info = getParser();
        info.parser->Parse(s.c_str(), nullptr);

        const size_t size = info.parser->builder_.GetSize();
        const char* ptr = (const char*)info.parser->builder_.GetBufferPointer();
        std::string buffer(ptr, ptr + size);
        return buffer;
    }

    // 将对象缓存转换为json字符串
    template
    std::string toJson(const void* buffer) {
        std::string json;
        const ParserInfo& info = getParser();
        flatbuffers::GeneratorOptions options;
        options.strict_json = true;
        GenerateText(*info.parser, buffer, options, &json);
        return json;
    }

    // 注册
    template
    void Register(const std::string& path) {
        ParserInfo info(path);
        m_map.bind(info);
    }

    template
    const ParserInfo& getParser() {

        ParserInfo& info = m_map.find();
        if (!info.parser) {
            std::string buf;
            flatbuffers::LoadFile(info.path.c_str(), false, &buf);

            flatbuffers::Parser* parser = new flatbuffers::Parser();
            bool ret = parser->Parse(buf.c_str(), nullptr);
            info.parser = parser;
        }
        return info;
    }

    static FlatBufferJsonFactory& Instance() {
        static FlatBufferJsonFactory* instance = nullptr;
        if (!instance)
            instance = new FlatBufferJsonFactory();
        return *instance;
    }

private:
    FlatBufferJsonFactory() {}

    TypeBind m_map;
};

经典博文参考:

https://www.jianshu.com/p/9c7d5ee7c2dd

你可能感兴趣的:(C++,数据结构,FlatBuffers)