C++ collections

294 views
Skip to first unread message

世尚书

unread,
Jul 8, 2008, 5:47:48 AM7/8/08
to Protocol Buffers
Hi,
I' m a new user of Protocol Buffers. I want to known could c++
collections, e.g. vector or list, be stored by Protocol Buffers, and
how?

thanks.

bxl

Alkis Evlogimenos ('Αλκης Ευλογημένος)

unread,
Jul 8, 2008, 5:55:03 AM7/8/08
to 世尚书, Protocol Buffers
If it is a vector of strings you can just dump it into a repeated field of strings. For example:

message MyCollection {
  repeated string str = 1;
}

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

Similarly for anything else.
--

Alkis

世尚书

unread,
Jul 8, 2008, 9:01:57 AM7/8/08
to Protocol Buffers
thanks


On 7月8日, 下午5时55分, "Alkis Evlogimenos ('Αλκης Ευλογημένος)"
<al...@evlogimenos.com> wrote:
> If it is a vector of strings you can just dump it into a repeated field of
> strings. For example:
>
> message MyCollection {
>   repeated string str = 1;
>
> }
>
> std::vector<std::string> my_strings;
> // add strings to vector
> MyCollection proto;
> for (std::vector<std::string>::iterator i = my_strings.begin(),
>      e = my_strings.end(); i != e; ++i)
>   *proto.add_str() = *i;
>
> Similarly for anything else.
>

rpopescu

unread,
Jul 8, 2008, 9:13:51 AM7/8/08
to Protocol Buffers
Hi all,

This looks a bit low-level to me, compared to the features of Boost
Serialization...

All the best,
Radu

Gregory P. Smith

unread,
Jul 8, 2008, 1:57:47 PM7/8/08
to rpopescu, Protocol Buffers
On Tue, Jul 8, 2008 at 6:13 AM, rpopescu <rpop...@gmail.com> wrote:

Hi all,

This looks a bit low-level to me, compared to the features of Boost
Serialization...

All the best,
Radu

That is intentional so that data structure design is given more thought rather than just blindly serializing whatever a particular implementation happens to represent its data as in memory as that becomes difficult to deal with in the future when the implementation needs to change.

-gps


Radu-Adrian Popescu

unread,
Jul 8, 2008, 5:43:42 PM7/8/08
to Gregory P. Smith, Protocol Buffers
I fail to see how not having good support for STL's containers is a good thing.
Also, not designing the data structures using a particular framework's
tools and means of expression does not mean you have to "blindly
serialize" anything, really.

2008/7/8 Gregory P. Smith <gr...@krypto.org>:

--
Radu-Adrian Popescu
Mobile +31-(0)611 00 45 20

Alkis Evlogimenos ('Αλκης Ευλογημένος)

unread,
Jul 9, 2008, 5:26:51 AM7/9/08
to Radu-Adrian Popescu, Gregory P. Smith, Protocol Buffers
What do you mean by good STL support?
--

Alkis

Radu-Adrian Popescu

unread,
Jul 9, 2008, 7:27:11 AM7/9/08
to Alkis Evlogimenos ('Αλκης Ευλογημένος), Gregory P. Smith, Protocol Buffers
I didn't say "STL support", but "good support for STL's containers",
and what I meant was being able to use for instance a std::vector to
represent a repeatable entry.
I've seen the RepeatedPtrField<T> in the meantime, and my comments
were biased by the very good experience I've had with applying the
boost serialization library to existing code, and how well it takes to
things like shared pointers, containers, no inheritance requirements
etc. All of which doesn't have much to do with the intended usage
pattern for Protocol Buffers, but due to the serialization library's
flexibility, it's easy to overlook that.
Sorry for not being clear enough, and keep up the good work!

2008/7/9 Alkis Evlogimenos ('Αλκης Ευλογημένος) <al...@evlogimenos.com>:

Alkis Evlogimenos ('Αλκης Ευλογημένος)

unread,
Jul 9, 2008, 7:51:18 AM7/9/08
to Radu-Adrian Popescu, Gregory P. Smith, Protocol Buffers
I think it would be nice to see some utilities to do easy ordered container -> repeatable field and maybe a more or less recommended way of map container -> repeatable pair. It doesn't sound that hard to write and it would be useful to more than a couple of people.
--

Alkis
Reply all
Reply to author
Forward
0 new messages