GobySoft / dccl

Dynamic Compact Control Language
Other
17 stars 13 forks source link

Cannot dynamic_cast google::protobuf::Message types that are generated from the DynamicProtobufManager #6

Closed tsaubergine closed 8 years ago

tsaubergine commented 8 years ago

Example snippet of code that fails (thanks to @berkowski):

  // End part of a function that returns a shared pointer to a Message
  std::string full_msg_bytes = //concat'd bytes from multiple packets.
  goby::acomms::DCCLCodec *codec = goby::acomms::DCCLCodec::get();
  return codec->decode<std::shared_ptr<google::protobuf::Message>>(full_msg_bytes);

  // Try to convert message that is known to be MyMessage
  auto my_msg = std::dynamic_pointer_cast<MyMessage>(returned_ptr) 
tsaubergine commented 8 years ago

Email from @berkowski

Now, I have gotten this to work w/ the current branches for DCCL and goby using the following line somewhere near the start of initialization:

dccl::DynamicProtobufManager::msg_factory().SetDelegateToGeneratedFactory(true);

tsaubergine commented 8 years ago

From https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.dynamic_message#DynamicMessageFactory.SetDelegateToGeneratedFactory.details

void DynamicMessageFactory::SetDelegateToGeneratedFactory(
        bool enable)

Call this to tell the DynamicMessageFactory that if it is given a Descriptor d for which:

d->file()->pool() == DescriptorPool::generated_pool(), then it should delegate to MessageFactory::generated_factory() instead of constructing a dynamic implementation of the message. In theory there is no down side to doing this, so it may become the default in the future.