4

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;
};
4

2 回答 2

4

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.

于 2009-03-26T03:16:42.543 回答
4

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.

于 2009-04-17T19:14:19.843 回答