In your C struct example, you know the type "T" that has "int v" as a 
member.

If you know that, you can do the same thing with protobuf:

// foo.proto
message T {
  optional int32 v = 1;
}

// main.cc
T a;
a.set_v(5);
a.SerializeToString(&str);

T b;
b.ParseFromString(str);
  
On Thursday, January 4, 2018 at 11:36:06 PM UTC-8, 陆林 wrote:
>
> auto proto_des = 
> google::protobuf::DescriptorPool::generated_pool()->FindMessageTypeByName(typeName);
> auto proto_type = 
> google::protobuf::MessageFactory::generated_factory()->GetPrototype(descriptor);
> auto protobuf_message = type->New();
> protobuf_message->ParseFromArray(buf, len);
>
> when we know "typeName", 
> we can get protobuf_message from an knowned length buf.
>
> if we dont know "typeName", but we know this length of buf is of couse a 
> protobuf message!!!
> can we unserialize this buf to an protobuf message instance??
>
> just like c struct:
> struct T
> {
> int v;
> };
> T a;
> a.v = 5;
> char buf[128] = {0};
> memcpy(buf, &a, sizeof(T));
>
> T b;
> memcpy(&b, buf, sizeof(T));
> /////
> we can get b.v = 5;
>
> can protobuf just act like this??
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to