Hi All,I'm seeing an incompatibility between a dynamically generated google::protobuf::Message (DynamicMessage?) created with a google::protobuf::DynamicMessageFactory and a normal google::protobuf::Message.What I'm running up against ultimately comes down to this: I can't dynamic_cast a DynamicMessage to a known derived type of google::protobuf::Message. For example, let's say my proto file has the following message:package my.package;messsage Foo{string blah = 1;uint32 bleh = 2;}and I successfully create a my::package::Foo with some code along the lines of:const google::protobuf::Descriptor* descriptor = myImporter.pool()->FindMessageTypeByName( "my.package.Foo" );google::protobuf::Message* message = myDynamicMessageFactory.GetPrototype( descriptor )->New();after populating message successfully with other commands not shown here, I try to do something like:my::package::Foo foo;foo.CopyFrom( *message );
and I get a segfault in CopyFrom() because ultimately the following doesn't work (the pointer pFoo ends up being NULL down inside CopyFrom()/MergeFrom()):
my::package::Foo* pFoo = dynamic_cast< my::package::Foo* >( message );It appears as though the derived type of message is not actually my::package::Foo, but instead google::protobuf::DynamicMessage (or something like that), so naturally the dynamic_cast fails. I was sure to observe the output of message->DebugString() and message->GetTypeName() which returned all the appropriate information.Is there any way to get a real my::package::Foo object from a dynamic one?
const gpb::Descriptor* descriptor = gpb::DescriptorPool::generated_pool()->FindMessageTypeByName( "my.package.Foo" );
gpb::MessageFactory* factory = gpb::MessageFactory::generated_factory();
const gpb::Message* prototype = factory->GetPrototype( descriptor );
gpb::Message* message = prototype->New();
gpb::Message* m = gpb::TextFormat::ParseFromString( "blah: \"some string\" bleh: 3", message );
Hi All,I'm seeing an incompatibility between a dynamically generated google::protobuf::Message (DynamicMessage?) created with a google::protobuf::DynamicMessageFactory and a normal google::protobuf::Message.What I'm running up against ultimately comes down to this: I can't dynamic_cast a DynamicMessage to a known derived type of google::protobuf::Message. For example, let's say my proto file has the following message:package my.package;messsage Foo{string blah = 1;uint32 bleh = 2;}and I successfully create a my::package::Foo with some code along the lines of:const google::protobuf::Descriptor* descriptor = myImporter.pool()->FindMessageTypeByName( "my.package.Foo" );google::protobuf::Message* message = myDynamicMessageFactory.GetPrototype( descriptor )->New();after populating message successfully with other commands not shown here, I try to do something like:my::package::Foo foo;foo.CopyFrom( *message );and I get a segfault in CopyFrom() because ultimately the following doesn't work (the pointer pFoo ends up being NULL down inside CopyFrom()/MergeFrom()):
gpb::TextFormat::ParseFromString( "blah: \"some string\" bleh: 3", message );
The furthest I followed it was looking down into the implementation of CopyFrom() which calls MergeFrom(). Inside of MergeFrom() there's a spot where it calls something like "dynamic_cast_if_available" which returns NULL (failing to dynamic_cast).
--To view this discussion on the web visit https://groups.google.com/d/msg/protobuf/-/iGRNOvXkgtEJ.
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.