RTI DDS -- 基本数据类型与动态定义结构

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";
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

baibingql

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值