Giter Site home page Giter Site logo

picoproto's Introduction

picoproto

Abominably Tiny Protobuf File Parser in C++

What is it?

Picoproto is a single file C++ implementation of protocol buffer parsing. It's designed to have no dependencies other than the standard library available in C++11, and to compile to a small binary size.

Why did I build it?

Protocol Buffers can make large projects a lot easier by using a common language to describe data objects. Unfortunately this comes at a cost in executable size and complexity that can be too much for resource-limited platforms. On a reasonably-sized project, the base protobuf libraries and the access classes created for each type can easily take up several hundred kilobytes.

This frustrated me, because I wanted to retain the ease of data transfer that protobufs offer, even on systems that can't afford increases in executable size. In many cases, all I want to do is treat protobuf as a file format, and read data from those files as efficiently as possible. I don't need to write out protobuf objects, or have automatically-created access classes for each type. I also don't want to do any processing of .proto files at all, since that adds build complexity which can cause a lot of headaches when moving around platforms.

How do you use it?

The main class is Message. This is a key/value store that holds the parsed contents of a protobuf form. The keys are the field numbers that were defined in a .proto file, and the values are the contents of each field.

To start, create an empty Message object:

picoproto::Message message;

Then load a protobuf file into an array of raw bytes:

FILE* f = fopen("somefile.pb", "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);
uint8_t* bytes = new uint8_t[fsize];
fread(bytes, fsize, 1, f);
fclose(f);

Deserialize the contents of those bytes into the Message:

message.ParseFromBytes(bytes, fsize);

The message object will now contain all of the fields that were stored in the protobuf file. To access them, you need to know the number and type of each field. This is a big difference from the full protobuf library, because there you can use convenient functions to access any field by its name. To keep the implementation simple, picoproto doesn't offer this convenience.

For example, if you had loaded a proto created with this definition:

message Test1 {
  required int32 a = 1;
}

Then you would access the a member like this:

int32_t a = message.GetInt32(1);

For a repeated or optional field, you would use the array accessors:

message Test2 {
  repeated string name = 7;
}
std::vector<string> names = message.GetStringArray(7);

Embedded messages are fully supported, and can be accessed using similar functions:

message Test3 {
  required Test1 c = 3;
}
Message* c = message.GetMessage(3);
int32_t a = c->GetInt32(1);

For more the examples, see picoproto_test.cc and tf_to_dot.cc.

picoproto's People

Contributors

petewarden avatar

Watchers

 avatar  avatar  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.