Re: [protobuf] Encoding/Decoding extension

2010-11-29 Thread Kenton Varda
Seems fine to me.

One point:  Rather than use ListFields(), I would recommend adding a type ID
field to Message, e.g.:

  message Message {
optional int32 type_id = 1;
extensions 5 to max;
  }

Reflection is kind of annoying to use, and can be somewhat slow.

On Thu, Nov 25, 2010 at 5:15 AM, Benoit Laurent laurent...@gmail.comwrote:

 Hi,

 I've done some experiment using protocol buffers extension but I kind
 of wondering if I am doing things the right way. For the encoding I've
 seen some explanation on the multi message encoding, and what I am
 doing seems ok. On the other hand I am kind of suspicious about my
 decoding routine.

 my .proto file is pretty straightforward
 message Message {
  extensions 5 to max;
 }
 message Dummy {
  optional float x = 1;
  optional float y = 2;
 }
 message Foo {
  optional int v = 1;
 }
 extend Message {
  optional Dummy dummy = 5;
 }
 extend Message {
 optional Foo foo = 50001;
 }

 My encoding code look like :
 int fd = open(argv[1], O_CREAT|O_RDONLY|O_WRONLY);
 google::protobuf::io::ZeroCopyOutputStream* raw_output = new
 google::protobuf::io::FileOutputStream(fd);
 google::protobuf::io::CodedOutputStream* coded_output = new
 google::protobuf::io::CodedOutputStream(raw_output);
 ns_message::Message message;
 ns_message::Dummy* dummy =
 message.MutableExtension(ns_message::dummy);
 dummy-set_x(5);
 int size = message.ByteSize();
 coded_output-WriteVarint32(size);
 message.SerializeToCodedStream(coded_output);

 message.ClearExtension(ns_message::dummy);
 ns_message::Foo* zoom = message.MutableExtension(csi_message::zoom);
 [encoding the same way]


 For the decoding here is what I am doing:

 int fd = open(argv[1], O_RDONLY);
 google::protobuf::io::ZeroCopyInputStream* raw_input = new
 google::protobuf::io::FileInputStream(fd);
 google::protobuf::io::CodedInputStream* coded_input = new
 google::protobuf::io::CodedInputStream(raw_input);
 ns_message::Message message;
 google::protobuf::uint32 size;
 while ( coded_input-ReadVarint32(size) ) {
  int previous = coded_input-PushLimit(size);
  message.ParseFromCodedStream(coded_input);
  coded_input-PopLimit(previous);
  std::vectorconst google::protobuf::FieldDescriptor* fields;
  const google::protobuf::Reflection* reflection =
 message.GetReflection();
  reflection-ListFields(message, fields);
  for (int i = 0; i  fields.size(); ++i) {
if (fields[i]-is_extension()) {
  getExtensionMessage(message, fields[i]);
}
  }
 }

 getExtensionMessage doesn't do anything fancy for the moment
  if(field-name() == dummy) {
csi_message::* Dummy = dummy.MutableExtension(ns_message::dummy);
print(*dummy);
  }

 Thanks in advance for your comments.

 --
 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.comprotobuf%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.



[protobuf] Encoding/Decoding extension

2010-11-25 Thread Benoit Laurent
Hi,

I've done some experiment using protocol buffers extension but I kind
of wondering if I am doing things the right way. For the encoding I've
seen some explanation on the multi message encoding, and what I am
doing seems ok. On the other hand I am kind of suspicious about my
decoding routine.

my .proto file is pretty straightforward
message Message {
  extensions 5 to max;
}
message Dummy {
  optional float x = 1;
  optional float y = 2;
}
message Foo {
  optional int v = 1;
}
extend Message {
  optional Dummy dummy = 5;
}
extend Message {
optional Foo foo = 50001;
}

My encoding code look like :
int fd = open(argv[1], O_CREAT|O_RDONLY|O_WRONLY);
google::protobuf::io::ZeroCopyOutputStream* raw_output = new
google::protobuf::io::FileOutputStream(fd);
google::protobuf::io::CodedOutputStream* coded_output = new
google::protobuf::io::CodedOutputStream(raw_output);
ns_message::Message message;
ns_message::Dummy* dummy =
message.MutableExtension(ns_message::dummy);
dummy-set_x(5);
int size = message.ByteSize();
coded_output-WriteVarint32(size);
message.SerializeToCodedStream(coded_output);

message.ClearExtension(ns_message::dummy);
ns_message::Foo* zoom = message.MutableExtension(csi_message::zoom);
[encoding the same way]


For the decoding here is what I am doing:

int fd = open(argv[1], O_RDONLY);
google::protobuf::io::ZeroCopyInputStream* raw_input = new
google::protobuf::io::FileInputStream(fd);
google::protobuf::io::CodedInputStream* coded_input = new
google::protobuf::io::CodedInputStream(raw_input);
ns_message::Message message;
google::protobuf::uint32 size;
while ( coded_input-ReadVarint32(size) ) {
  int previous = coded_input-PushLimit(size);
  message.ParseFromCodedStream(coded_input);
  coded_input-PopLimit(previous);
  std::vectorconst google::protobuf::FieldDescriptor* fields;
  const google::protobuf::Reflection* reflection =
message.GetReflection();
  reflection-ListFields(message, fields);
  for (int i = 0; i  fields.size(); ++i) {
if (fields[i]-is_extension()) {
  getExtensionMessage(message, fields[i]);
}
  }
}

getExtensionMessage doesn't do anything fancy for the moment
  if(field-name() == dummy) {
csi_message::* Dummy = dummy.MutableExtension(ns_message::dummy);
print(*dummy);
 }

Thanks in advance for your comments.

-- 
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.