Coder Social home page Coder Social logo

hongbingzhu / pbconvertor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hardxuyp/pbconvertor

0.0 0.0 0.0 55 KB

PbConvertor is used to convert protobuf message to C struct or C struct to protobuf message.

C++ 96.63% CMake 2.58% Makefile 0.79%

pbconvertor's Introduction

PbConvertor-C++ convertor between protobuf message and C struct

PbConvertor is used to convert protobuf message to C struct or C struct to protobuf message. PbConvertor is mainly based on protobuf reflection.

Dependency

  • C++11 or higher version
  • protobuf

Usage

Suppose your *.pb.h, *.pb.cc and C struct definition are prepared.

  1. Refer to C struct definition requirements to modify your C struct definition.
  2. Add PbConvertor.h and PbConvertor.cpp into your C++ project.
  3. Use the following methods in PbConvertor class to convert protobuf message to C struct or C struct to protobuf message.
    /**
     * @brief convert C struct to protobuf message.
     * @param pStruct pointer to C struct.
     * @param pPb pointer to protobuf message.
     * @return whether successful.
     */
    static bool struct2Pb(const char *&pStruct, google::protobuf::Message *pPb);

    /**
     * @brief convert C struct to serialized protobuf message.
     * @param pStruct pointer to C struct.
     * @param pbTypeName protobuf message type name.
     * @param pSerializedPb pointer to serialized protobuf message which must be released by user using delete [].
     * @param serializedPbSize serialized protobuf message size.
     * @return whether successful.
     */
    static bool struct2serializedPb(const char *pStruct, const std::string &pbTypeName, char *&pSerializedPb, size_t &serializedPbSize);

    /**
     * @brief convert protobuf message to C struct.
     * @param pPb pointer to protobuf message.
     * @param stru memory tree related to C struct, and pointer to C struct equals to stru.pMem.
     *        Memory tree must be released by user using stru.release().
     * @return whether successful.
     */
    static bool pb2struct(const google::protobuf::Message *pPb, MemTree &stru);

    /**
     * @brief convert serialized protobuf message to C struct.
     * @param pbTypeName protobuf message type name.
     * @param pSerializedPb pointer to serialized protobuf message.
     * @param serializedPbSize serialized protobuf message size.
     * @param stru memory tree related to C struct, and pointer to C struct equals to stru.pMem.
     *        Memory tree must be released by user using stru.release().
     * @return whether successful.
     */
    static bool serializedPb2struct(const std::string &pbTypeName, const char *pSerializedPb, const size_t serializedPbSize, MemTree &stru);
  1. A usage example is provided.

C struct definition requirements

C struct definition must be one byte aligned using #pragma pack(1) and #pragma pack().
C struct member type must correspond to protobuf message field type according to the following table. The other C struct member types not listed in the following table are not allowed.

protobuf message field type C struct member type
double double
float float
int32 int32_t
int64 int64_t
uint32 uint32_t
uint64 uint64_t
bool bool
string
Note:
UTF-8 encoding is required.
char * (not char[N])
Note:
1. UTF-8 encoding is required.
2. NULL value of char * member will be converted to "" of the protobuf message string field.
enum enum : int
message struct
Note:
struct member must meet C struct definition requirements.
repeated element_type
Note:
element_type must meet C struct definition requirements.
element_type * (not element_type[N])
Note:
1. The element amount of the array must be provided by the previous member whose type is int32_t/int64_t/uint32_t/uint64_t.
2. element_type must meet C struct definition requirements.

Attention

Your C++ project must have a multi-threaded configuration, otherwise protobuf will not work well.

pbconvertor's People

Contributors

hardxuyp avatar hongbingzhu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.