14.4. Dynamic Types IDL Parsing

Fast DDS supports the implementation of Dynamic Language Binding for parsing IDL files at runtime to generate dynamic data types.

This feature enables applications to dynamically load type definitions from IDL files instead of relying solely on pre-generated types or XML profiles. This enhances flexibility, as new data types can be introduced without modifying and recompiling the application.

14.4.1. Supported Types

Below, the types supported by eProsima Fast DDS IDL parsing are presented. For further information about the supported Dynamic Language Binding, please, refer to Supported Types.

The following types are currently not supported by the IDL parsing feature:

14.4.2. Create a Dynamic Type from a IDL file

Fast DDS application can use dynamic types generated in runtime just by loading the corresponding IDL files into the DynamicTypeBuilderFactory using DynamicTypeBuilderFactory::create_type_w_uri. After getting the DynamicType, objects of DynamicPubSubType class might be instantiated and used to write/read data.

Warning

DynamicTypeBuilderFactory::create_type_w_uri requires the fully qualified name of the type defined in the IDL file. If no scope is provided, global scope is assumed.

Note

The preprocessor can be manually selected using DynamicTypeBuilderFactory::set_preprocessor

14.4.2.1. Example

The following snippet shows the previously explained steps:

// Create a DomainParticipant in the desired domain
DomainParticipant* participant =
        DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT);
if (nullptr == participant)
{
    // Error
    return;
}

// Optional: Set the preprocessor to be executed before parsing the IDL
DynamicTypeBuilderFactory::get_instance()->set_preprocessor("<optional_path_to_your_preprocessor_executable>");

// Load the IDL file
std::string idl_file = "<path_to_idl>.idl";
std::string type_name = "YourType";
std::vector<std::string> include_paths;
include_paths.push_back("<path/to/folder/containing/included/idl/files>");

// Retrieve the instance of the desired type
DynamicTypeBuilder::_ref_type dyn_type_builder =
        DynamicTypeBuilderFactory::get_instance()->create_type_w_uri(idl_file, type_name, include_paths);

// Register dynamic type
TypeSupport dyn_type_support(new DynamicPubSubType(dyn_type_builder->build()));
dyn_type_support.register_type(participant, nullptr);

// Create a Topic with the registered type.
Topic* topic =
        participant->create_topic("topic_name", dyn_type_support.get_type_name(), TOPIC_QOS_DEFAULT);
if (nullptr == topic)
{
    // Error
    return;
}

14.4.3. Iterate over the parsed IDL types

Fast DDS application supports a simple way to iterate over each aggregated type declared and parsed in the IDL file, i.e:

using DynamicTypeBuilderFactory::for_each_type_w_uri method. User can customize the runtime behavior by providing a lambda expression, which will be triggered when a new aggregated type is parsed.

The callback provided by the user receives the DynamicTypeBuilder instance related to the parsed type and returns a boolean value, indicating whether the iteration should continue or not. In case of returning false for a given type, the parsing process will stop and no further types will be processed.

14.4.3.1. Example

The following snippet shows how to iterate over the aggregated types declared in the IDL file, storing their names in a vector:

// Optional: Set the preprocessor to be executed before parsing the IDL
DynamicTypeBuilderFactory::get_instance()->set_preprocessor("<optional_path_to_your_preprocessor_executable>");

// Load the IDL file
std::string idl_file = "<path_to_idl>.idl";
std::vector<std::string> include_paths;
include_paths.push_back("<path/to/folder/containing/included/idl/files>");

// Vector to store the names of the parsed types
std::vector<std::string> parsed_types;

// Define the function callback to be executed for each aggregated type parsed in the IDL file
auto callback = [&parsed_types](traits<DynamicTypeBuilder>::ref_type builder)
{
    if (builder)
    {
        parsed_types.emplace_back(builder->get_name().to_string());
    }

    return true; // continue parsing
};

// Store the names of the parsed types in the vector
DynamicTypeBuilderFactory::get_instance()->for_each_type_w_uri(idl_file, include_paths, callback);