CARLA
 
载入中...
搜索中...
未找到
RegionOfInterestPubSubTypes.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 RegionOfInterestPubSubTypes.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
27using SerializedPayload_t = eprosima::fastrtps::rtps::SerializedPayload_t;
28using InstanceHandle_t = eprosima::fastrtps::rtps::InstanceHandle_t;
29
30namespace sensor_msgs {
31 namespace msg {
32 // RegionOfInterestPubSubType类的构造函数
33 // 主要用于初始化一些与类型相关的属性,比如类型名称、类型大小等
35 {
36 // 设置类型的名称
37 setName("sensor_msgs::msg::dds_::RegionOfInterest_");
38 // 获取RegionOfInterest类型的最大CDR序列化大小
40 // 考虑可能的子消息对齐情况(按4字节对齐)
41 type_size += eprosima::fastcdr::Cdr::alignment(type_size, 4);
42 // 计算最终的类型大小,包含封装部分(额外加4字节)
43 m_typeSize = static_cast<uint32_t>(type_size) + 4;
44 // 判断RegionOfInterest类型是否定义了键(用于后续一些操作判断)
45 m_isGetKeyDefined = RegionOfInterest::isKeyDefined();
46 // 根据RegionOfInterest类型的键的最大CDR序列化大小来确定键缓冲区的长度
47 // 如果键的最大序列化大小大于16字节,则取实际大小,否则取16字节
48 size_t keyLength = RegionOfInterest::getKeyMaxCdrSerializedSize() > 16 ?
50 // 分配键缓冲区内存
51 m_keyBuffer = reinterpret_cast<unsigned char*>(malloc(keyLength));
52 // 初始化键缓冲区内容为0
53 memset(m_keyBuffer, 0, keyLength);
54 }
55 // RegionOfInterestPubSubType类的析构函数
56 // 用于释放构造函数中分配的键缓冲区内存
58 {
59 if (m_keyBuffer != nullptr)
60 {
61 free(m_keyBuffer);
62 }
63 }
64 // 序列化函数,将给定的数据(RegionOfInterest类型)序列化为SerializedPayload_t格式
66 void* data,
67 SerializedPayload_t* payload)
68 {
69 // 将传入的void*类型数据转换为RegionOfInterest*类型指针,方便后续操作
70 RegionOfInterest* p_type = static_cast<RegionOfInterest*>(data);
71 // 创建一个FastBuffer对象,用于管理原始缓冲区,关联到payload的数据部分及最大大小
72 eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(payload->data), payload->max_size);
73 // 创建一个Cdr对象,用于执行序列化操作,设置了字节序等相关参数
74 eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::DDS_CDR);
75 // 根据Cdr对象的字节序设置payload的封装字节序标识(大端序或小端序)
76 payload->encapsulation = ser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE : CDR_LE;
77
78 try
79 {
80 // 先序列化封装部分(可能包含一些头部等信息)
81 ser.serialize_encapsulation();
82 // 再序列化具体的RegionOfInterest对象
83 p_type->serialize(ser);
84 }
85 catch (eprosima::fastcdr::exception::Exception& /*exception*/)
86 {
87 // 如果序列化过程出现异常,返回false表示失败
88 return false;
89 }
90 // 获取序列化后的数据长度,设置到payload的length成员中
91 payload->length = static_cast<uint32_t>(ser.getSerializedDataLength());
92 // 序列化成功,返回true
93 return true;
94 }
95 // 反序列化函数,将SerializedPayload_t格式的数据反序列化为RegionOfInterest类型
97 SerializedPayload_t* payload,
98 void* data)
99 {
100 try
101 {
102 // 将传入的void*类型数据转换为RegionOfInterest*类型指针,方便后续操作
103 RegionOfInterest* p_type = static_cast<RegionOfInterest*>(data);
104 // 创建一个FastBuffer对象,用于管理原始缓冲区,关联到payload的数据部分及实际长度
105 eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(payload->data), payload->length);
106 // 创建一个Cdr对象,用于执行反序列化操作,设置了字节序等相关参数
107 eprosima::fastcdr::Cdr deser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::DDS_CDR);
108 // 先反序列化封装部分(读取可能的头部等信息)
109 deser.read_encapsulation();
110 // 根据Cdr对象的字节序设置payload的封装字节序标识(大端序或小端序)
111 payload->encapsulation = deser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE : CDR_LE;
112 // 再反序列化具体的RegionOfInterest对象
113 p_type->deserialize(deser);
114 }
115 catch (eprosima::fastcdr::exception::Exception& /*exception*/)
116 {
117 // 如果反序列化过程出现异常,返回false表示失败
118 return false;
119 }
120 // 反序列化成功,返回true
121 return true;
122 }
123 // 返回一个函数对象,该函数对象用于获取给定数据序列化后的大小(包含封装部分)
125 void* data)
126 {
127 return [data]() -> uint32_t
128 {
129 // 获取RegionOfInterest对象的CDR序列化大小并加上封装部分的4字节,返回总大小
130 return static_cast<uint32_t>(type::getCdrSerializedSize(*static_cast<RegionOfInterest*>(data))) +
131 4u /*encapsulation*/;
132 };
133 }
134 // 创建一个RegionOfInterest类型的数据对象(在堆上分配内存),并返回其void*指针
136 {
137 return reinterpret_cast<void*>(new RegionOfInterest());
138 }
139 // 删除传入的void*指针指向的RegionOfInterest类型的数据对象(释放内存)
141 void* data)
142 {
143 delete(reinterpret_cast<RegionOfInterest*>(data));
144 }
145 // 获取给定数据(RegionOfInterest类型)对应的键信息,用于一些实例标识等操作
147 void* data,
148 InstanceHandle_t* handle,
149 bool force_md5)
150 {
151 // 如果没有定义键,直接返回false
152 if (!m_isGetKeyDefined)
153 {
154 return false;
155 }
156 RegionOfInterest* p_type = static_cast<RegionOfInterest*>(data);
157 // 创建一个FastBuffer对象,用于管理键缓冲区,关联到m_keyBuffer及键的最大CDR序列化大小
158 eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(m_keyBuffer),
160 // 创建一个Cdr对象,用于序列化键信息,设置为大端序
161 eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::BIG_ENDIANNESS);
162 // 序列化键信息
163 p_type->serializeKey(ser);
164 if (force_md5 || RegionOfInterest::getKeyMaxCdrSerializedSize() > 16)
165 {
166 // 如果需要强制使用MD5或者键的最大序列化大小大于16字节
167 m_md5.init();
168 m_md5.update(m_keyBuffer, static_cast<unsigned int>(ser.getSerializedDataLength()));
169 m_md5.finalize();
170 for (uint8_t i = 0; i < 16; ++i)
171 {
172 handle->value[i] = m_md5.digest[i];
173 }
174 }
175 else
176 {
177 // 如果键的最大序列化大小小于等于16字节,直接复制键缓冲区内容到实例句柄的value数组中
178 for (uint8_t i = 0; i < 16; ++i)
179 {
180 handle->value[i] = m_keyBuffer[i];
181 }
182 }
183 return true;
184 }
185 } //End of namespace msg
186} //End of namespace sensor_msgs
eprosima::fastrtps::rtps::InstanceHandle_t InstanceHandle_t
eprosima::fastrtps::rtps::SerializedPayload_t SerializedPayload_t
virtual eProsima_user_DllExport bool serialize(void *data, eprosima::fastrtps::rtps::SerializedPayload_t *payload) override
virtual eProsima_user_DllExport ~RegionOfInterestPubSubType() 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 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
virtual eProsima_user_DllExport void * createData() override
This class represents the structure RegionOfInterest defined by the user in the IDL file.
static eProsima_user_DllExport size_t getCdrSerializedSize(const sensor_msgs::msg::RegionOfInterest &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...
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.
static eProsima_user_DllExport bool isKeyDefined()
This function tells you if the Key has been defined for this type
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 getMaxCdrSerializedSize(size_t current_alignment=0)
This function returns the maximum serialized size of an object depending on the buffer alignment.