11.9. Dynamic HelloWorld Examples

These are complete working examples that make use of dynamic types. You can explore them to find how this feature connects to the rest of Fast DDS, and learn how to integrate it in your own application.

11.9.1. DynamicHelloWorldExample

This example is located in folder examples/C++/DynamicHelloWorldExample. It shows the use of DynamicType generation to provide the TopicDataType. This example is compatible with the classic HelloWorldExample.

As a quick reference, the following piece of code shows how the HelloWorld type is created using DynamicTypes:

// In HelloWorldPublisher.h
// Dynamic Types
eprosima::fastrtps::types::DynamicData* m_DynHello;
eprosima::fastrtps::types::DynamicPubSubType m_DynType;

// In HelloWorldPublisher.cpp
// Create basic builders
DynamicTypeBuilder_ptr struct_type_builder(DynamicTypeBuilderFactory::get_instance()->create_struct_builder());

// Add members to the struct.
struct_type_builder->add_member(0, "index", DynamicTypeBuilderFactory::get_instance()->create_uint32_type());
struct_type_builder->add_member(1, "message", DynamicTypeBuilderFactory::get_instance()->create_string_type());
struct_type_builder->set_name("HelloWorld");

DynamicType_ptr dynType = struct_type_builder->build();
m_DynType.SetDynamicType(dynType);
m_DynHello = DynamicDataFactory::get_instance()->create_data(dynType);
m_DynHello->set_uint32_value(0, 0);
m_DynHello->set_string_value("HelloWorld", 1);

11.9.2. DDSDynamicHelloWorldExample

This example uses the DDS API, and it is located in the examples/C++/DDS/DynamicHelloWorldExample folder. It shows a publisher that loads a type from an XML file, and shares it during discovery. The subscriber discovers the type using Discovery-Time Data Typing, and registers the discovered type on the on_type_discovery listener function.

11.9.3. TypeLookupService

This example uses the DDS API, and it is located in the examples/C++/DDS/TypeLookupService folder. It’s very similar to DDSDynamicHelloWorldExample, but the shared type is complex enough to require the TypeLookup Service due to the dependency of inner struct types. Specifically, it uses the register_remote_type approach with a callback.