On Fri, Nov 5, 2010 at 3:26 PM, AdamM <adammaga...@gmail.com> wrote:

> Is there a way to return the entire extension part of the message into
> a separate message like:
>
> message Foo {
>  extensions 100 to 199;
> }
>
>
> message Baz {
>  extend Foo {
>    optional int32 bar = 126;
>  }
> }
>
> In C++ Code:
>
> Foo foo;
> foo.SetExtension(Baz::bar, 15);
>
> Elsewhere in code, obviously the code below does not work but is there
> a way to do something like that?


> Baz baz;
> baz = foo.GetExtension(Baz);
>

No, you can't do that. In your .proto definition, Baz is just a container
for the extension identifier. There is no relationship between the
extensions and the Baz message type. You could have written your proto
definition as:

message Foo {
 extensions 100 to 199;
}

extend Foo {
  optional int32 bar = 126;
}

And done:
Foo foo;
foo.SetExtension(bar, 15);
int value = foo.GetExtension(bar);   // equals 15

If you want to use a message type, do:

message Baz {
  optional int32 bar = 1;
  ...
}
extend Foo {
  optional Baz baz_extension = 126;
}

To access the message in the extensions:
Foo foo;
Baz* baz = foo.MutableExtension(baz_extension);
const Baz& baz = foo.GetExtension(baz_extension);


>
>
>
>
> On Nov 5, 5:28 pm, Daniel Wright <dwri...@google.com> wrote:
> > On Fri, Nov 5, 2010 at 2:12 PM, AdamM <adammaga...@gmail.com> wrote:
> > > Thank you Daniel is was not aware of this feature in PB I will give it
> > > a try.
> >
> > There is a slight gain for smaller numbers, but it's small enough that
> it's
> > not worth the maintenance headache of trying to choose unique small
> numbers.
> >  Basically, tags with numbers less than 16 use 1 byte.  16 to 2048 use
> two
> > bytes, 2048 to 262144 use 3 bytes, 262144 to 33554432 use 4 bytes etc
> (you
> > get 7 bits per byte, except for the first byte which only gets 4 bits.
> http://code.google.com/apis/protocolbuffers/docs/encoding.htmlhas the
> > details).
> >
> > Note that the actual "extensions 100 to 1000000000;" line in the .proto
> file
> > costs nothing -- the gain I'm talking about above is when you encode a
> > message with a given field number.
> >
> > One question though is there any optimization gain you get from using
> >
> >
> >
> >
> >
> >
> >
> > > "extensions 100 to 200;" over "extensions 100 to 1000000000;"?
> >
> > > On Nov 5, 3:21 pm, Daniel Wright <dwri...@google.com> wrote:
> > > > This is exactly what extensions are for -- seehttp://
> > > code.google.com/apis/protocolbuffers/docs/proto.html#extensions
> >
> > > > It would look something like:
> >
> > > > message BaseMessage {
> > > >   required MsgType type = 1;
> > > >   extensions 100 to 1000000000;
> >
> > > > }
> >
> > > > Then each module would have a message like:
> >
> > > > message Msg1 {
> > > >   extend BaseMessage {
> > > >     optional Msg1 msg1 = <some_unique_id>
> > > >   }
> > > >   required int32 field = 1;
> >
> > > > }
> >
> > > > Then the main program can pass the entire BaseMessage to the right
> module
> > > > based on type, and the module can retrieve the parsed extension.
> >
> > > > On Fri, Nov 5, 2010 at 10:31 AM, AdamM <adammaga...@gmail.com>
> wrote:
> > > > > Hello PB Group,
> >
> > > > > I am programming a group of programs in C++ that use PB to
> > > > > communicate. In my first version of the program I was using a
> .proto
> > > > > file that looked similar to the example 1 below. I would then run a
> > > > > switch statement on a MsgType and create the proper message.
> >
> > > > > I am now starting to write my second version of the program to
> include
> > > > > a plugin module architecture. My program calls for a core program
> and
> > > > > then multiple modules that are written against a base module. Well
> in
> > > > > my core program I get packets over the network and then parse them
> > > > > info a PB Message.
> >
> > > > > I would like a way to have some sort of Base Message that the my
> core
> > > > > program would use to parse with the  base message would contain a
> > > > > packet packet operation code and the data for the actual message
> > > > > structure. I want to program it so the Core program has no idea
> what
> > > > > the actual message structure is. It would pass it to the respective
> > > > > module based on the operation code who would then  parse the actual
> > > > > message structure because it knows what that structure should be.
> >
> > > > > Does anyone have any suggestions how to do this? Any help would be
> > > > > much appreciated.
> >
> > > > > Example 1.
> >
> > > > > enum MsgType {
> > > > >   MSG1,
> > > > >   MSG2,
> > > > >   MSG3
> > > > > }
> >
> > > > > Message Msg1 {
> > > > >   required int32 field = 1 ;
> > > > > }
> >
> > > > > Message Msg2 {
> > > > >   required int32 field = 1 ;
> > > > > }
> >
> > > > > Message Msg3 {
> > > > >   required int32 field = 1 ;
> > > > > }
> >
> > > > > Message BaseMessage {
> > > > >   required MsgType type = 1 ;
> > > > >   optional Msg1 = 2 ;
> > > > >   optional Msg2 = 3 ;
> > > > >   optional Msg3 = 4 ;
> > > > > }
> >
> > > > > --
> > > > > You received this message because you are subscribed to the Google
> > > Groups
> > > > > "Protocol Buffers" group.
> > > > > To post to this group, send email to proto...@googlegroups.com.
> > > > > To unsubscribe from this group, send email to
> > > > > protobuf+unsubscr...@googlegroups.com<protobuf%2bunsubscr...@googlegroups.com>
> <protobuf%2bunsubscr...@googlegroups.c om>
> > > <protobuf%2bunsubscr...@googlegroups.c om>
> > > > > .
> > > > > For more options, visit this group at
> > > > >http://groups.google.com/group/protobuf?hl=en.
> >
> > > --
> > > You received this message because you are subscribed to the Google
> Groups
> > > "Protocol Buffers" group.
> > > To post to this group, send email to proto...@googlegroups.com.
> > > To unsubscribe from this group, send email to
> > > protobuf+unsubscr...@googlegroups.com<protobuf%2bunsubscr...@googlegroups.com>
> <protobuf%2bunsubscr...@googlegroups.c om>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/protobuf?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To post to this group, send email to proto...@googlegroups.com.
> To unsubscribe from this group, send email to
> protobuf+unsubscr...@googlegroups.com<protobuf%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/protobuf?hl=en.
>
>

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

Reply via email to