views:

509

answers:

2

We plan to replace Boost.serialization with protocol-buffers used in distributed system design. How protocol-buffers support complicated data structures such as std containers?

For instance, such a class is required to be serialized/deserialized in our case:

class Foo
{
  std::vector< std::pair< unsigned int, std::vector< std::pair< int, int> > > > data;
};
+1  A: 

Protocol buffers have employ a parser which takes a .proto file and creates appropriate serialization routines. See this.

Update: You can represent a vector of strings as:

message MyCollection {   
   repeated string str = 1;
}

in your proto file.

and use:

std::vector<std::string> my_strings; 
// add strings to vector MyCollection proto; 
vector<string>::iterator e = my_strings.end();
for (vector<string>::iterator i = my_strings.begin(); 
    i != e; 
    ++i) {
   *proto.add_str() = *i; 
}

It should be easy to extend for another collection/collection of collections.

dirkgently
Google's tutorial has only presented how to serialze a simple structure. My requirement is about the std container. How to write proto files as what I have referred above?
Updated with some code.
dirkgently
+2  A: 

The protobuf representation of your Foo would look something like this:

message IntPair {
    required int32 first  = 1;
    required int32 second = 2;
};

message FooElem {
    required uint32 first = 1;
    repeated IntPair second = 2;
};

message Foo {
    repeated FooElem data = 1;
};

Note that Protocol Buffers doesn't provide a "seamless" (de)serialization to/from your own structures like Boost.Serialization does. You work with generated objects that come from running the protoc compiler on a file like the above.

These generated classes will not contain std::pair and std::vector members however, so you'll need to copy data too/from the generated objects if you want to keep working with your legacy Foo structure.

Bklyn