Skip to content

Question: Dynamically loading messages

Toby Schneider edited this page Mar 27, 2023 · 1 revision

matthew-william-lock commented Mar 10, 2023

Hi, I am wondering if there is currently a way to dynamically load messages into the codec from a list? More explicitly, am I able to select the messages to load at runtime as opposed to compile time? In the Python example shown below, the message loaded is defined by a string and could likely be changed dynamically, is this possible for cpp as well?

codec = dccl.Codec() codec.load("NavigationReport")

For more context, I am looking for an effective way to map ROS messages to DCCL messages so that ROS messages can be republished once received over the acoustic channel. I would like to avoid having to change the cpp file every time we add a new message type (at least manually).

tsaubergine commented Mar 10, 2023

Hi -

Yes this is definitely (conceptually) possible.

using dccl::Codec; using dccl::DynamicProtobufManager; Codec codec; codec.load(DynamicProtobufManager::find_descriptor("NavigationReport"));

Before you can do this you have to ensure that "NavigationReport" is known to the DynamicProtobufManager via one of the "google::protobuf::DescriptorPool"s, either by being compiled in or loaded via a shared library using Codec::load_library() (in which case it will automatically find it (google::protobuf::DescriptorPool::generated_pool()), loaded at runtime from the proto file (DynamicProtobufManager::load_from_proto_file("/path/to/nav.proto")) added via the FileDescriptorProto (essentially a protobuf message that defines a .proto file) (DynamicProtobufManager::add_protobuf_file(const google::protobuf::FileDescriptorProto& proto)), or managed in your own google::protobuf::DescriptorDatabase (DynamicProtobufManager::add_database(...)).

See:

https://libdccl.org/4.0/classdccl_1_1DynamicProtobufManager.html
https://protobuf.dev/reference/cpp/api-docs/google.protobuf.descriptor/#DescriptorPool
https://protobuf.dev/reference/cpp/api-docs/google.protobuf.descriptor_database/
Clone this wiki locally