You are here: Part 2: Core Concepts > Data Types and DDS Data Samples > Interacting Dynamically with User Data Types > Defining New Types

Defining New Types

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 (unless type-code support is disabled with the -notypecode option). 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;
	string<128> my_string; // @key

};

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 Sending Only a Few Fields for more information.

© 2015 RTI