I have a lot of protobuf messages for which I currently use a manually written lookup function to generate the message by its name. Since the messages get more and more as the project evolves, I'm getting tired of maintaining this lookup code by hand.
So, is there a way to automate this process? Maybe with a protoc plugin that adds some code to the protobuf code so that it may register itself?
The C++ Protobuf library already maintains a pool of "descriptors" for all types compiled into the binary.
https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.descriptor#DescriptorPool.generated_pool.details
So, you could do:
google::protobuf::Descriptor* desc =
google::protobuf::DescriptorPool::generated_pool()
->FindMessageTypeByName("mypkg.MyType");
assert(desc != NULL);
The library also maintains an object that can be used to construct instances of any compiled-in type, given its descriptor:
https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.message#MessageFactory.generated_factory.details
So you'd do:
google::protobuf::Message* message =
google::protobuf::MessageFactory::generated_factory()
->GetPrototype(desc)->New();
Unlikely this approach can not be used as a generic way of creation of any message instance. A message type description appears in the generated_pool()
only after a message of this type was instantiated at least once (e.g. at the moment of MyMessageType* msg = new MyMessageType()
), thus FindMessageTypeByName
never finds a message type of a message that has not been instantiated yet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With