c++ extension question

127 views
Skip to first unread message

Wade Berrier

unread,
Dec 31, 2009, 2:41:00 PM12/31/09
to Protocol Buffers
Hi,

Given the following:

----------------------
message MessageContainer {
optional string common_header_info = 1 [default = ""];
extensions 1000 to max;
}

message SubMessage
{
optional string mySubString = 1;
}

extend MessageContainer
{
optional SubMessage subMessage = 1001;
}

----------------------

If I have a Message* (which is actually a SubMessage), how to I set or
copy that extension into MessageContainer, without knowing that the
Message* is a SubMessage?

It looks like all extensions accessors need to use
ExtensionIdentifiers. Is the above possible?

The idea is that I'll use MessageContainer to contain one of several
messages that extend MessageContainer, but I want to have generic code
that doesn't have to know what type of Message is being dealt with.
And I want the code that deals with Messages not to have to worry
about the MessageContainer message.

Another question: is it possible to set an extension message without
making a copy of the message? I read a post about MutableExtension
being designed the way it was because object ownership is clear. But,
I'd hate to have to make a copy if it's not necessary.

Any help/feedback would be greatly appreciated.

Wade

Kenton Varda

unread,
Dec 31, 2009, 4:37:21 PM12/31/09
to Wade Berrier, Protocol Buffers
If you have the FieldDescriptor representing the extension, you can use the container's Reflection interface to do it:

  container.GetReflection()->MutableMessage(&container, descriptor)->CopyFrom(*message);

Nope, you have to copy.  The way to avoid copies is to construct the message in-place in the first place.
 

Any help/feedback would be greatly appreciated.

Wade

--

You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.



Reply all
Reply to author
Forward
0 new messages