11.1. Overview of Dynamic Types

This section describes the classes related to dynamic types that are used through the rest of the documentation. At the bottom of the section you can also find a short example using the functionality.

11.1.1. Involved classes

The following class diagram describes the relationship among the classes related to dynamic types. Please, refer to the description of each class to find its purpose and the nature of the relationship with the rest of the classes.

../../_images/dynamic_types_class_diagram.svg

Dynamic types class diagram

11.1.1.1. Dynamic Type

Base class of all types declared dynamically. It represents a dynamic data type that can be used to create Dynamic Data values. By design, the structure of a dynamic type (its member fields) cannot be modified once the type is created.

11.1.1.2. Dynamic Type Builder Factory

Singleton class that is in charge of the creation and the management of every Dynamic Type and Dynamic Type Builder. It declares functions to create builders for each kind of supported types. Given a builder for a specific type, it can also create the corresponding Dynamic Type. Some simpler types can be created directly, avoiding the step of creating a Dynamic Type Builder. Please, refer to the Supported Types documentation for details about which ones support this option.

Every object created by the factory must be deleted to avoid memory leaking. Refer to the Memory management section for details.

11.1.1.3. Dynamic Type Builder

Intermediate class used to configure a Dynamic Type before it is created. By design, the structure of a Dynamic Type (its member fields) cannot be modified once the object is created. Therefore, all its structure must be defined prior to its creation. The builder is the object used to set up this structure.

Once defined, the Dynamic Type Builder Factory is used to create the Dynamic Type from the information contained in the builder. As a shortcut, the builder exposes a function build that internally uses the Dynamic Type Builder Factory to return a fully constructed Dynamic Type. The types created with build are still subject to the Memory management restrictions, and must be deleted by the Dynamic Type Builder Factory.

Builders can be reused after the creation of a Dynamic Type, as the changes applied to the builder do not affect to types created previously.

11.1.1.4. Type Descriptor

Stores the information about one type with its relationships and restrictions. This is the class that describes the inner structure of a Dynamic Type. The Dynamic Type Builder has an internal instance of TypeDescriptor that modifies during the type building process. When the Dynamic Type is created, the Dynamic Type Builder Factory uses the information of the TypeDescriptor in the builder to create the Dynamic Type. During the creation, the TypeDescriptor is copied to the Dynamic Type, so that it becomes independent from the Dynamic Type Builder, and the builder can be reused for another type.

11.1.1.5. Dynamic Type Member

Represents a data member of a Dynamic Type that is also a Dynamic Type. Compound types (dynamic types that are composed of other dynamic types) have a DynamicTypeMember for every child Dynamic Type added to it.

11.1.1.6. Member Descriptor

Just as a Type Descriptor describes the inner structure of a Dynamic Type, a MemberDescriptor stores all the information needed to manage a Dynamic Type Member, like their name, their unique ID, or the default value after the creation. This information is copied to the Dynamic Data on its creation.

11.1.1.7. Dynamic Data

While a Dynamic Type describes a type, DynamicData represents a data instance of a Dynamic Type. It provides functions to access and modify the data values in the instance.

There are two ways to work with DynamicData:

  • Activating the macro DYNAMIC_TYPES_CHECKING, which creates a variable for each primitive kind to help the debug process.

  • Without this macro, the size of the DynamicData is reduced, using only the minimum needed internal values, but it makes the code harder to debug.

11.1.1.8. Dynamic Data Factory

Singleton class that is in charge of the creation and the management of every Dynamic Data. It can take a Dynamic Type and create an instance of a corresponding Dynamic Data. Every data object created by the factory must be deleted to avoid memory leaking. Refer to the Memory management section for details.

It also allows to create a TypeIdentifier and a (Minimal and Complete) TypeObject from a TypeDescriptor.

11.1.1.9. Dynamic PubSubType

This class is an adapter that allows using Dynamic Data on Fast DDS. It inherits from TopicDataType and implements the functions needed to communicate the Dynamic Data between Publishers and Subscribers.

11.1.2. Minimum example

This is a short example to illustrate the use of the dynamic types and how the classes describe above interact with each other. While the code snippet can be used as a quick reference for code building, the sequence diagram below provides a visual interpretation of the actions.

// Create a builder for a specific type
DynamicTypeBuilder_ptr builder = DynamicTypeBuilderFactory::get_instance()->create_enum_builder();

// Use the builder to configure the type
builder->add_empty_member(0, "DEFAULT");
builder->add_empty_member(1, "FIRST");
builder->add_empty_member(2, "SECOND");

// Create the data type using the builder
// The builder will internally use the DynamicTypeBuilderFactory to create the type
DynamicType_ptr type = builder->build();

// Create a new data instance of the create data type
DynamicData_ptr data (DynamicDataFactory::get_instance()->create_data(type));

// Now we can set or read data values
data->set_int32_value(1);

// No need of deleting the objects, since we used the
// automanaged smart pointers
../../_images/dynamic_types_sequence_diagram.svg

Sequence diagram of the code above