Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google protocol buffers and stl vectors, maps and boost shared pointers

Does google protocol buffers support stl vectors, maps and boost shared pointers? I have some objects that make heavy use of stl containers like maps, vectors and also boost::shared_ptr. I want to use google protocol buffers to serialize these objects across the network to different machines.

I want to know does google protobuf support these containers? Also if I use apache thrift instead, will it be better? I only need to serialize/de-serialize data and don't need the network transport that apache thrift offers. Also apache thrift not having proper documentation puts me off.

like image 352
user601134 Avatar asked Feb 03 '11 08:02

user601134


1 Answers

Protocol buffers directly handles an intentionally small number of constructs; vectors map nicely to the "repeated" element type, but how this is presented in C++ is via "add" methods - you don't (AFAIK) just hand it a vector. See "Repeated Embedded Message Fields" here for more info.

Re maps; there is no inbuilt mechanism for that, but a key/value pair is easily represented in .proto (typically key = 1, value = 2) and then handled via "repeated".

A shared_ptr itself would seem to have little meaning in a serialized file. But the object may be handled (presumably) as a message.

Note that in the google C++ version the DTO layer is generated, so you may need to map between them and any existing object model. This is usually pretty trivial.

For some languages/platforms there are protobuf variants that work against existing object models.

(sorry, I can't comment on thrift - I'm not familiar with it)

like image 198
Marc Gravell Avatar answered Oct 12 '22 15:10

Marc Gravell