# 结构类型(struct)
IDL 结构(struct)类型直接映射为 C++结构(struct)类型。每个 IDL 结构的成员映射为 C++结构的相应成员。生成的 C++结构包含构造函数和析构函数。例如:
//IDL
	struct UserDataType {
   	long l;
   	float f;
	};
1
2
3
4
5
6
2
3
4
5
6
映射为:
//C++
	struct UserDataType {
  		UserDataType (){
		l = 0;
		f = 0;
}
  		UserDataType (const UserDataType &IDL_s);
  		~ UserDataType (){}
UserDataType & operator= (const UserDataType &IDL_s);
  		long l;
  		float f;
 		//其他一些这里不关注的函数
	};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# IDL 定义示例
接口定义语言(IDL)字符串类型在 idl 文档中定义(以结构体为例):
struct  UserDataType{
	long l;
   	float f;
};
1
2
3
4
5
2
3
4
5
# 代码示例
生成测试用例后在 pub.cpp 中创建数据样本后添加赋值语句:
/* 6. 创建一个数据样本 */
/* 建议:该数据需要申请空间后使用,使用后用户需要调用delete_data进行内存等资源的释放*/
	UserDataType *instance = NULL;
	instance = UserDataTypeTypeSupport::create_data();
	instance->l = 1;
	instance->f = 2;
1
2
3
4
5
6
7
2
3
4
5
6
7
# 数据收发示例
图 1 为使用IDL 定义示例中结构体生成的测试 demo,在 Windows 系统上启动发布端、在 Linux 系统启动订阅端后的数据收发情况。

图 1 结构体数据收发示例(Windows->Linux)
图 2 为使用IDL 定义示例中结构体生成的测试 demo,在 Windows 系统上启动订阅端、在 Linux 系统启动发布端后数据收发情况。

图 2 结构体数据收发示例(Linux -> Windows)
