CARLA
 
载入中...
搜索中...
未找到
PointCloud2PubSubTypes.cpp
浏览该文件的文档.
1// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*!
16 * @file PointCloud2PubSubTypes.cpp
17 * This header file contains the implementation of the serialization functions.
18 *
19 * This file was generated by the tool fastcdrgen.
20 */
21
22#include <fastcdr/FastBuffer.h>
23#include <fastcdr/Cdr.h>
24
26
27// 定义SerializedPayload_t类型别名,等同于eprosima::fastrtps::rtps::SerializedPayload_t,用于表示序列化后的负载数据类型,通常在数据传输、存储等场景中承载具体要处理的数据
28using SerializedPayload_t = eprosima::fastrtps::rtps::SerializedPayload_t;
29// 定义InstanceHandle_t类型别名,等同于eprosima::fastrtps::rtps::InstanceHandle_t,一般用于标识数据实例,例如在发布/订阅系统中区分不同的消息实例
30using InstanceHandle_t = eprosima::fastrtps::rtps::InstanceHandle_t;
31
32namespace sensor_msgs {
33 namespace msg {
34 // PointCloud2PubSubType类的构造函数,用于初始化该类型相关的属性和资源
36 {
37 // 设置此类型的名称,这里明确指定为 "sensor_msgs::msg::dds_::PointCloud2_",方便在系统中识别和管理该类型
38 setName("sensor_msgs::msg::dds_::PointCloud2_");
39 // 获取PointCloud2类型数据能被CDR(Common Data Representation,一种通用数据表示格式)序列化后的最大尺寸
40 auto type_size = PointCloud2::getMaxCdrSerializedSize();
41 // 考虑可能存在的子消息对齐情况,按照4字节对齐规则对类型尺寸进行调整(增加必要的填充字节等),以满足数据存储或传输时的对齐要求
42 type_size += eprosima::fastcdr::Cdr::alignment(type_size, 4); /* possible submessage alignment */
43 // 计算最终包含封装部分(可能用于添加头部等额外信息)的类型尺寸,额外加上4字节作为封装相关的尺寸
44 m_typeSize = static_cast<uint32_t>(type_size) + 4; /*encapsulation*/
45 // 判断PointCloud2类型是否定义了获取键(通常用于唯一标识一个数据实例)的相关操作
46 m_isGetKeyDefined = PointCloud2::isKeyDefined();
47 // 根据PointCloud2获取键的最大CDR序列化尺寸来确定键缓冲区的长度
48 // 如果尺寸大于16字节就用实际尺寸,否则用16字节作为长度,为后续存储键相关数据分配合适的内存空间
49 size_t keyLength = PointCloud2::getKeyMaxCdrSerializedSize() > 16?
51 // 分配键缓冲区的内存空间,将返回的无符号字符指针转换为合适类型后赋值给m_keyBuffer
52 m_keyBuffer = reinterpret_cast<unsigned char*>(malloc(keyLength));
53 // 将键缓冲区的内存初始化为0,确保内存中的数据处于已知的初始状态
54 memset(m_keyBuffer, 0, keyLength);
55 }
56
57 // PointCloud2PubSubType类的析构函数,用于释放构造函数中分配的键缓冲区内存资源
59 {
60 if (m_keyBuffer!= nullptr)
61 {
62 free(m_keyBuffer);
63 }
64 }
65
66 // 序列化函数,将PointCloud2类型的数据对象序列化为SerializedPayload_t格式,以便进行数据传输、存储等操作
68 void* data,
69 SerializedPayload_t* payload)
70 {
71 // 将传入的void*类型数据转换为PointCloud2*类型指针,方便后续按照PointCloud2类型的规则进行序列化操作
72 PointCloud2* p_type = static_cast<PointCloud2*>(data);
73
74 // 创建一个FastBuffer对象,用于管理原始缓冲区,它关联到payload的data(实际存储数据的内存位置)和max_size(缓冲区最大尺寸)
75 eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(payload->data), payload->max_size);
76 // 创建一个Cdr对象,用于执行序列化操作,设置了默认字节序(DEFAULT_ENDIAN)以及特定的CDR模式(DDS_CDR,与数据分发服务相关的模式)
77 eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::DDS_CDR);
78 // 根据序列化对象的字节序设置payload的封装字节序标识(大端或小端),以便后续在反序列化等操作中能正确识别数据的字节序
79 payload->encapsulation = ser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS? CDR_BE : CDR_LE;
80 // 序列化封装相关的头部等信息(具体内容由相关协议或实现定义),这一步可能是添加一些用于标识、校验等的额外数据
81 ser.serialize_encapsulation();
82
83 try
84 {
85 // 调用PointCloud2对象的serialize函数,将实际的PointCloud2类型的数据内容按照Cdr对象设置的规则序列化到ser所管理的缓冲区中
86 p_type->serialize(ser);
87 }
88 catch (eprosima::fastcdr::exception::NotEnoughMemoryException& /*exception*/)
89 {
90 // 如果在序列化过程中出现内存不足的异常情况,返回false表示序列化失败
91 return false;
92 }
93
94 // 获取序列化后数据的实际长度,将其设置到payload的length字段中,以便后续使用者能知道有效数据的长度
95 payload->length = static_cast<uint32_t>(ser.getSerializedDataLength());
96 return true;
97 }
98
99 // 反序列化函数,将SerializedPayload_t格式的数据还原为PointCloud2类型的数据对象
101 SerializedPayload_t* payload,
102 void* data)
103 {
104 try
105 {
106 // 将传入的void*类型数据转换为PointCloud2*类型指针,方便后续按照PointCloud2类型的规则进行反序列化操作
107 PointCloud2* p_type = static_cast<PointCloud2*>(data);
108
109 // 创建一个FastBuffer对象,用于管理原始缓冲区,关联到payload的data(实际存储数据的内存位置)和length(实际有效数据的长度)
110 eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(payload->data), payload->length);
111
112 // 创建一个Cdr对象,用于执行反序列化操作,设置了默认字节序(DEFAULT_ENDIAN)以及特定的CDR模式(DDS_CDR,与数据分发服务相关的模式)
113 eprosima::fastcdr::Cdr deser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::DDS_CDR);
114
115 // 反序列化封装相关的头部等信息(具体内容由相关协议或实现定义),恢复之前序列化时添加的用于标识、校验等的额外数据
116 deser.read_encapsulation();
117 // 根据反序列化对象的字节序设置payload的封装字节序标识(大端或小端),确保后续对数据的解析能按照正确的字节序进行
118 payload->encapsulation = deser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS? CDR_BE : CDR_LE;
119
120 // 调用PointCloud2对象的deserialize函数,从deser所管理的缓冲区中按照PointCloud2类型的规则还原出实际的数据内容
121 p_type->deserialize(deser);
122 }
123 catch (eprosima::fastcdr::exception::NotEnoughMemoryException& /*exception*/)
124 {
125 // 如果在反序列化过程中出现内存不足的异常情况,返回false表示反序列化失败
126 return false;
127 }
128
129 return true;
130 }
131
132 // 获取序列化尺寸提供函数,返回一个lambda表达式,用于计算给定的PointCloud2类型数据序列化后的尺寸(包含封装部分)
134 void* data)
135 {
136 return [data]() -> uint32_t
137 {
138 // 先获取PointCloud2类型数据本身的CDR序列化尺寸,再加上4字节的封装部分尺寸,以此得到总的序列化后尺寸
139 return static_cast<uint32_t>(type::getCdrSerializedSize(*static_cast<PointCloud2*>(data))) +
140 4u /*encapsulation*/;
141 };
142 }
143
144 // 创建PointCloud2类型的数据对象,返回的是转换为void*类型的指针,这样在一些需要统一管理不同类型数据的场景(如基于多态的设计)中能方便地进行操作
146 {
147 return reinterpret_cast<void*>(new PointCloud2());
148 }
149
150 // 删除PointCloud2类型的数据对象,释放之前创建该对象时分配的内存资源,避免内存泄漏
152 void* data)
153 {
154 delete(reinterpret_cast<PointCloud2*>(data));
155 }
156
157 // 获取用于唯一标识的键(根据PointCloud2类型的数据生成实例句柄的键值部分),用于在系统中区分不同的实例
159 void* data,
160 InstanceHandle_t* handle,
161 bool force_md5)
162 {
163 if (!m_isGetKeyDefined)
164 {
165 // 如果PointCloud2类型没有定义获取键的相关操作,直接返回false,表示无法获取键
166 return false;
167 }
168
169 PointCloud2* p_type = static_cast<PointCloud2*>(data);
170
171 // 创建一个FastBuffer对象,用于管理键相关的原始缓冲区,关联到m_keyBuffer(之前分配的键缓冲区内存)和PointCloud2获取键的最大CDR序列化尺寸
172 eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(m_keyBuffer),
174
175 // 创建一个Cdr对象,用于将PointCloud2类型的数据序列化为键相关的格式,这里设置为大端字节序(通常在键相关处理中有特定要求)
176 eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::BIG_ENDIANNESS);
177 // 调用PointCloud2对象的serializeKey函数,将相关数据按照键的格式要求序列化到ser所管理的缓冲区中
178 p_type->serializeKey(ser);
179 if (force_md5 || PointCloud2::getKeyMaxCdrSerializedSize() > 16)
180 {
181 // 如果强制使用MD5(可能用于生成更可靠、唯一的标识)或者键的序列化尺寸大于16字节,进行MD5计算相关操作
182 m_md5.init();
183 m_md5.update(m_keyBuffer, static_cast<unsigned int>(ser.getSerializedDataLength()));
184 m_md5.finalize();
185 for (uint8_t i = 0; i < 16; ++i)
186 {
187 handle->value[i] = m_md5.digest[i];
188 }
189 }
190 else
191 {
192 // 如果键的序列化尺寸小于等于16字节,直接将键缓冲区的内容复制到实例句柄的键值部分,作为实例的标识
193 for (uint8_t i = 0; i < 16; ++i)
194 {
195 handle->value[i] = m_keyBuffer[i];
196 }
197 }
198 return true;
199 }
200 } //End of namespace msg
201} //End of namespace sensor_msgs
eprosima::fastrtps::rtps::InstanceHandle_t InstanceHandle_t
eprosima::fastrtps::rtps::SerializedPayload_t SerializedPayload_t
virtual eProsima_user_DllExport ~PointCloud2PubSubType() override
virtual eProsima_user_DllExport bool serialize(void *data, eprosima::fastrtps::rtps::SerializedPayload_t *payload) override
virtual eProsima_user_DllExport std::function< uint32_t()> getSerializedSizeProvider(void *data) override
virtual eProsima_user_DllExport void deleteData(void *data) override
virtual eProsima_user_DllExport void * createData() override
virtual eProsima_user_DllExport bool getKey(void *data, eprosima::fastrtps::rtps::InstanceHandle_t *ihandle, bool force_md5=false) override
virtual eProsima_user_DllExport bool deserialize(eprosima::fastrtps::rtps::SerializedPayload_t *payload, void *data) override
This class represents the structure PointCloud2 defined by the user in the IDL file.
Definition PointCloud2.h:74
static eProsima_user_DllExport bool isKeyDefined()
This function tells you if the Key has been defined for this type
static eProsima_user_DllExport size_t getMaxCdrSerializedSize(size_t current_alignment=0)
This function returns the maximum serialized size of an object depending on the buffer alignment.
eProsima_user_DllExport void deserialize(eprosima::fastcdr::Cdr &cdr)
This function deserializes an object using CDR serialization.
eProsima_user_DllExport void serialize(eprosima::fastcdr::Cdr &cdr) const
This function serializes an object using CDR serialization.
eProsima_user_DllExport void serializeKey(eprosima::fastcdr::Cdr &cdr) const
This function serializes the key members of an object using CDR serialization.
static eProsima_user_DllExport size_t getCdrSerializedSize(const sensor_msgs::msg::PointCloud2 &data, size_t current_alignment=0)
This function returns the serialized size of a data depending on the buffer alignment.
static eProsima_user_DllExport size_t getKeyMaxCdrSerializedSize(size_t current_alignment=0)
This function returns the maximum serialized size of the Key of an object depending on the buffer ali...