RTI DDS 数据类型比较丰富,支持根据IDL生成主题与动态主题。如下截图,详情请参考官方文档。
1. 基本数据类型
2.动态结构实例
const char* HelloWorldType_get_type_name() {
return "HelloWorld";
}
/*****************************************************************************/
/* HelloWorldType_create */
/* */
/* Creates type code for a structure corresponding to: */
/* */
/* struct HelloWorld { */
/* string<HELLODDS_MAX_STRING_SIZE> prefix; */
/* long sampleId; */
/* sequence<octet, HELLODDS_MAX_PAYLOAD_SIZE> payload; */
/* }; */
/* */
/* Returns NULL in case of error */
/*****************************************************************************/
DDS_TypeCode* HelloWorldType_create() {
DDS_TypeCodeFactory* factory = NULL;
DDS_TypeCode* sequenceTc = NULL; // Typecode for the 'payload' sequence
DDS_TypeCode* stringTc = NULL; // Typecode for the 'prefix' string;
DDS_TypeCode* structTc = NULL; // Top-level typecode
DDS_ExceptionCode_t ex;
factory = DDS_TypeCodeFactory::get_instance();
if (factory == NULL) {
std::cerr << "! Unable to get type code factory singleton"
<< std::endl;
goto done;
}
// Create typecode for 'prefix' string of max size HELLODDS_MAX_STRING_SIZE
stringTc = factory->create_string_tc(HELLODDS_MAX_STRING_SIZE, ex);
if (ex != DDS_NO_EXCEPTION_CODE) {
std::cerr << "! Unable to create 'prefix' string typecode: " << ex
<< std::endl;
goto done;
}
// Create typecode for 'payload' octet sequence
sequenceTc = factory->create_sequence_tc(HELLODDS_MAX_PAYLOAD_SIZE,
factory->get_primitive_tc(DDS_TK_OCTET),
ex);
if (ex != DDS_NO_EXCEPTION_CODE) {
std::cerr << "! Unable to create 'payload' sequence typecode: " << ex
<< std::endl;
goto done;
}
structTc = factory->create_struct_tc("HelloWorld", DDS_StructMemberSeq(),
ex);
if (ex != DDS_NO_EXCEPTION_CODE) {
std::cerr << "! Unable to create struct typecode, error=" << ex
<< std::endl;
goto done;
}
structTc->add_member("prefix",
DDS_MEMBER_ID_INVALID,
stringTc,
DDS_TYPECODE_NONKEY_MEMBER,
ex);
if (ex != DDS_NO_EXCEPTION_CODE) {
std::cerr << "! Unable to add member to struct typecode, error=" << ex
<< std::endl;
goto done;
}
structTc->add_member("sampleId",
DDS_MEMBER_ID_INVALID,
factory->get_primitive_tc(DDS_TK_LONG),
DDS_TYPECODE_NONKEY_MEMBER,
ex);
if (ex != DDS_NO_EXCEPTION_CODE) {
std::cerr << "! Unable to add member to struct typecode, error=" << ex
<< std::endl;
goto done;
}
structTc->add_member("payload",
DDS_MEMBER_ID_INVALID,
sequenceTc,
DDS_TYPECODE_NONKEY_MEMBER,
ex);
if (ex != DDS_NO_EXCEPTION_CODE) {
std::cerr << "! Unable to add member to struct typecode, error=" << ex
<< std::endl;
goto done;
}
if (sequenceTc != NULL) {
factory->delete_tc(sequenceTc, ex);
sequenceTc = NULL;
}
if (stringTc != NULL) {
factory->delete_tc(stringTc, ex);
stringTc = NULL;
}
return structTc;
done:
// In case of failure, do a cleanup of all the temporary data structures
if (sequenceTc != NULL) {
factory->delete_tc(sequenceTc, ex);
}
if (stringTc != NULL) {
factory->delete_tc(stringTc, ex);
}
if (structTc != NULL) {
factory->delete_tc(structTc, ex);
}
return NULL;
}
/*****************************************************************************/
/* HelloWorldType_delete */
/* */
/* Deletes type code created by HellOWorldType_create */
/* */
/*****************************************************************************/
void HelloWorldType_delete(DDS_TypeCode* type) {
DDS_TypeCodeFactory* factory = NULL;
DDS_ExceptionCode_t ex;
factory = DDS_TypeCodeFactory::get_instance();
if (factory == NULL) {
std::cerr << "! Unable to get type code factory singleton"
<< std::endl;
return;
}
if (type != NULL) {
factory->delete_tc(type, ex);
}
if (ex != DDS_NO_EXCEPTION_CODE) {
std::cerr << "! Unable to delete typecode: " << ex
<< std::endl;
return;
}
}
//动态主题1
/* * const long HELLODDS_MAX_PAYLOAD_SIZE = 8192;
* const long HELLODDS_MAX_STRING_SIZE = 64;
* struct HelloWorld {
* string<HELLODDS_MAX_STRING_SIZE> prefix;
* long sampleId;
* sequence<octet, HELLODDS_MAX_PAYLOAD_SIZE> payload;
* };
*/
const char* typeName = HelloWorldType_get_type_name();
DDS_TypeCode* structTc = HelloWorldType_create();
if (structTc) {
DDS_DynamicDataTypeProperty_t props;
DDSDynamicDataTypeSupport* typeSupport = new DDSDynamicDataTypeSupport(structTc , props);
if (typeSupport == NULL) {
LOG_Error("! Unable to create dynamic data type support");
return;
}
//注册主题
if (typeSupport->register_type(_p->participant, typeName ) != DDS_ReturnCode_t::DDS_RETCODE_OK) {
LOG_Error(
"ERROR: RegistDynamicType() - register_type failed! typyname=" + std::string(typeName));
std::cout << "ERROR: RegistDynamicType() - register_type failed! typyname=" << typeName;
return;
}
//创建主题
DDSTopic* topic =
_p->participant->create_topic(typeName, typeName, _p->topicQos, NULL, DDS_STATUS_MASK_NONE);
if (!topic) {
LOG_Error(
"ERROR: RegistDynamicType() - Create TOPIC failed! typyname=" + std::string(typeName));
return;
}
else {
std::cout << "init failed!\n";
}