FlatBuffers 的 直接上手C++示例

1、生成scheme文件,即fbs 文件,保存为 flatbuffs_for_zmq.fbs,内容如下

table TransMsg {
    type:int;
    content:string;
}

root_type TransMsg;

2、编译scheme文件

flatc.exe --cpp flatbuffs_for_zmq.fbs

3、编译生成后的文件flatbuffs_for_zmq_generated.h,内容如下

// automatically generated by the FlatBuffers compiler, do not modify

#ifndef FLATBUFFERS_GENERATED_FLATBUFFSFORZMQ_H_
#define FLATBUFFERS_GENERATED_FLATBUFFSFORZMQ_H_

#include "flatbuffers/flatbuffers.h"



struct TransMsg;

struct TransMsg FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
  enum {
    VT_TYPE = 4,
    VT_CONTENT = 6
  };
  int32_t type() const { return GetField(VT_TYPE, 0); }
  const flatbuffers::String *content() const { return GetPointer(VT_CONTENT); }
  bool Verify(flatbuffers::Verifier &verifier) const {
    return VerifyTableStart(verifier) &&
           VerifyField(verifier, VT_TYPE) &&
           VerifyField(verifier, VT_CONTENT) &&
           verifier.Verify(content()) &&
           verifier.EndTable();
  }
};

struct TransMsgBuilder {
  flatbuffers::FlatBufferBuilder &fbb_;
  flatbuffers::uoffset_t start_;
  void add_type(int32_t type) { fbb_.AddElement(TransMsg::VT_TYPE, type, 0); }
  void add_content(flatbuffers::Offset content) { fbb_.AddOffset(TransMsg::VT_CONTENT, content); }
  TransMsgBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); }
  TransMsgBuilder &operator=(const TransMsgBuilder &);
  flatbuffers::Offset Finish() {
    auto o = flatbuffers::Offset(fbb_.EndTable(start_, 2));
    return o;
  }
};

inline flatbuffers::Offset CreateTransMsg(flatbuffers::FlatBufferBuilder &_fbb,
   int32_t type = 0,
   flatbuffers::Offset content = 0) {
  TransMsgBuilder builder_(_fbb);
  builder_.add_content(content);
  builder_.add_type(type);
  return builder_.Finish();
}

inline const TransMsg *GetTransMsg(const void *buf) { return flatbuffers::GetRoot(buf); }

inline bool VerifyTransMsgBuffer(flatbuffers::Verifier &verifier) { return verifier.VerifyBuffer(); }

inline void FinishTransMsgBuffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset root) { fbb.Finish(root); }


#endif  // FLATBUFFERS_GENERATED_FLATBUFFSFORZMQ_H_

4、序列化和反序列化

#include 

/*序列化*/
char* generateBuffer()
{
    int len = 0;
    {
        int ret = 0;
        char* p = NULL;
        int zmqRet = 0;
        flatbuffers::FlatBufferBuilder builder;
        flatbuffers::Offset content;
        flatbuffers::Offset zmqData;

        char buffer[128] = {"Hello world!"};

        builder.Clear();

        content = builder.CreateString(buffer, strlen("Hello world!")+1);

        TransMsgBuilder msgData(builder);
        msgData.add_type(1001);
        msgData.add_content(content);
        zmqData = msgData.Finish();
        builder.Finish(zmqData);

        len = builder.GetSize();
        p = new char[len];
        memcpy(p, builder.GetBufferPointer(), builder.GetSize());
    }
}

/*反序列化*/
void decodeBuffer()
{
    const TransMsg* pTransMsg = GetTransMsg(p);
    printf("type is %d, msg is %s\n", pTransMsg->type(),                 pTransMsg->content()->c_str());
}


int main(int argc, char* argv[])
{
    char* p = NULL;
    p = generateBuffer;
    decodeBuffer();

    delete p;

}

 

你可能感兴趣的:(flatbuffers)