This section does not apply when using the separate add-on product, Ada Language Support, which does not support Dynamic Types.
Locally, your application can access the type code for a generated type "Foo" by calling the FooTypeSupport::get_typecode() (Traditional C++ Notation) operation in the code for the type generated by RTI Code Generator. But you can also create TypeCodes at run time without any code generation.
Creating a TypeCode is parallel to the way you would define the type statically: you define the type itself with some name, then you add members to it, each with its own name and type.
For example, consider the following statically defined type. It might be in C, C++, or IDL; the syntax is largely the same.
struct MyType { long my_integer; float my_float; bool my_bool; @key string<128> my_string; };
DDS_ExceptionCode_t ex = DDS_NO_EXCEPTION_CODE; DDS_StructMemberSeq structMembers; // ignore for now DDS_TypeCodeFactory* factory = DDS_TypeCodeFactory::get_instance(); DDS_TypeCode* structTc = factory->create_struct_tc( "MyType", structMembers, ex); // If structTc is NULL, check 'ex' for more information. structTc->add_member( "my_integer", DDS_TYPECODE_MEMBER_ID_INVALID, factory->get_primitive_tc(DDS_TK_LONG) DDS_TYPECODE_NONKEY_REQUIRED_MEMBER, ex); structTc->add_member( "my_float", DDS_TYPECODE_MEMBER_ID_INVALID, factory->get_primitive_tc(DDS_TK_FLOAT), DDS_TYPECODE_NONKEY_REQUIRED_MEMBER, ex); structTc->add_member( "my_bool", DDS_TYPECODE_MEMBER_ID_INVALID, factory->get_primitive_tc(DDS_TK_BOOLEAN), DDS_TYPECODE_NONKEY_REQUIRED_MEMBER, ex); structTc->add_member( "my_string", DDS_TYPECODE_MEMBER_ID_INVALID, factory->create_string_tc(128), DDS_TYPECODE_KEY_MEMBER, ex);
More detailed documentation for the methods and constants you see above, including example code, can be found in the API Reference HTML documentation, which is available for all supported programming languages.
If, as in the example above, you know all of the fields that will exist in the type at the time of its construction, you can use the StructMemberSeq to simplify the code:
DDS_StructMemberSeq structMembers; structMembers.ensure_length(4, 4); DDS_TypeCodeFactory* factory = DDS_TypeCodeFactory::get_instance(); structMembers[0].name = DDS_String_dup("my_integer"); structMembers[0].type = factory->get_primitive_tc(DDS_TK_LONG); structMembers[1].name = DDS_String_dup("my_float"); structMembers[1].type = factory->get_primitive_tc(DDS_TK_FLOAT); structMembers[2].name = DDS_String_dup("my_bool"); structMembers[2].type = factory->get_primitive_tc(DDS_TK_BOOLEAN); structMembers[3].name = DDS_String_dup("my_string"); structMembers[3].type = factory->create_string_tc(128); structMembers[3].is_key = DDS_BOOLEAN_TRUE; DDS_ExceptionCode_t ex = DDS_NO_EXCEPTION_CODE; DDS_TypeCode* structTc = factory->create_struct_tc( "MyType", structMembers, ex);
After you have defined the TypeCode, you will register it with a DomainParticipant using a logical name (note: this step is not required in the Modern C++ API). You will use this logical name later when you create a Topic.
DDSDynamicDataTypeSupport* type_support =
new DDSDynamicDataTypeSupport(structTc,
DDS_DYNAMIC_DATA_TYPE_PROPERTY_DEFAULT); DDS_ReturnCode_t retcode = type_support->register_type(participant,
"My Logical Type Name");
For code examples for the Modern C++ API, please refer to the API Reference HTML documentation: Modules, Programming How-To's, DynamicType and DynamicData Use Cases.
Now that you have created a type, you will need to know how to interact with objects of that type. See 3.8.3 Sending Only a Few Fields for more information.
© 2020 RTI