CARLA
 
载入中...
搜索中...
未找到
PointPubSubTypes.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 PointPubSubTypes.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
25#include "PointPubSubTypes.h"
26
27using SerializedPayload_t = eprosima::fastrtps::rtps::SerializedPayload_t;
28using InstanceHandle_t = eprosima::fastrtps::rtps::InstanceHandle_t;
29// 命名空间geometry_msgs,用于组织相关的消息类型相关代码
30namespace geometry_msgs {
31 // 嵌套的msg命名空间,进一步细分消息相关的类型和操作
32 namespace msg {
33 // PointPubSubType类的定义,用于处理Point类型消息的发布/订阅相关操作(如序列化、反序列化等)
35 {
36 // 设置类型名称,这里明确指定了与geometry_msgs::msg::dds_::Point_相关的名称
37 setName("geometry_msgs::msg::dds_::Point_");
38 // 获取Point类型的最大CDR序列化大小,CDR是一种数据序列化格式
39 auto type_size = Point::getMaxCdrSerializedSize();
40 // 根据可能的子消息对齐要求,对类型大小进行对齐处理(按4字节对齐)
41 type_size += eprosima::fastcdr::Cdr::alignment(type_size, 4); /* possible submessage alignment */
42 // 计算最终的类型大小,加上4字节用于封装
43 m_typeSize = static_cast<uint32_t>(type_size) + 4; /*encapsulation*/
44 // 判断Point类型是否定义了获取键(Key)的操作
45 m_isGetKeyDefined = Point::isKeyDefined();
46 // 根据Point类型获取键的最大CDR序列化大小来确定键缓冲区的长度,取较大值
47 size_t keyLength = Point::getKeyMaxCdrSerializedSize() > 16 ?
49 // 分配键缓冲区内存空间
50 m_keyBuffer = reinterpret_cast<unsigned char*>(malloc(keyLength));
51 // 将键缓冲区初始化为全0
52 memset(m_keyBuffer, 0, keyLength);
53 }
54// PointPubSubType类的析构函数,用于释放之前分配的键缓冲区内存
56 {
57 if (m_keyBuffer != nullptr)
58 {
59 free(m_keyBuffer);
60 }
61 }
62// 序列化函数,将Point类型的数据对象序列化为可以传输的格式(存入SerializedPayload_t中)
64 void* data,
65 SerializedPayload_t* payload)
66 {
67 // 将传入的void*类型数据转换为Point*类型指针,方便后续操作
68 Point* p_type = static_cast<Point*>(data);
69
70 // Object that manages the raw buffer.
71 eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(payload->data), payload->max_size);
72 // Object that serializes the data.
73 eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::DDS_CDR);
74 payload->encapsulation = ser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE : CDR_LE;
75 // Serialize encapsulation
76 ser.serialize_encapsulation();
77
78 try
79 {
80 // Serialize the object.
81 p_type->serialize(ser);
82 }
83 catch (eprosima::fastcdr::exception::NotEnoughMemoryException& /*exception*/)
84 {
85 // 如果出现内存不足异常,返回false表示序列化失败
86 return false;
87 }
88
89 // Get the serialized length
90 payload->length = static_cast<uint32_t>(ser.getSerializedDataLength());
91 return true;
92 }
93// 反序列化函数,将接收到的SerializedPayload_t格式的数据转换为Point类型的数据对象
95 SerializedPayload_t* payload,
96 void* data)
97 {
98 try
99 {
100 //Convert DATA to pointer of your type
101 Point* p_type = static_cast<Point*>(data);
102
103 // Object that manages the raw buffer.
104 eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(payload->data), payload->length);
105
106 // Object that deserializes the data.
107 eprosima::fastcdr::Cdr deser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::DDS_CDR);
108
109 // Deserialize encapsulation.
110 deser.read_encapsulation();
111 payload->encapsulation = deser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE : CDR_LE;
112
113 // Deserialize the object.
114 p_type->deserialize(deser);
115 }
116 catch (eprosima::fastcdr::exception::NotEnoughMemoryException& /*exception*/)
117 {
118 // 如果出现内存不足异常,返回false表示反序列化失败
119 return false;
120 }
121
122 return true;
123 }
124// 返回一个函数对象,该函数对象用于获取给定Point数据对象序列化后的大小
126 void* data)
127 {
128 return [data]() -> uint32_t
129 {
130 // 计算并返回序列化后的大小,包括Point类型自身序列化大小和封装的4字节
131 return static_cast<uint32_t>(type::getCdrSerializedSize(*static_cast<Point*>(data))) +
132 4u /*encapsulation*/;
133 };
134 }
135 // 创建一个新的Point类型数据对象,返回其void*指针,用于后续的赋值等操作
137 {
138 return reinterpret_cast<void*>(new Point());
139 }
140// 删除之前创建的Point类型数据对象,释放内存
142 void* data)
143 {
144 delete(reinterpret_cast<Point*>(data));
145 }
146 // 获取给定Point数据对象的键(Key)信息,用于标识该对象,存入InstanceHandle_t中
148 void* data,
149 InstanceHandle_t* handle,
150 bool force_md5)
151 {
152 if (!m_isGetKeyDefined)
153 {
154 // 如果Point类型没有定义获取键的操作,直接返回false
155 return false;
156 }
157
158 Point* p_type = static_cast<Point*>(data);
159
160 // Object that manages the raw buffer.
161 eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(m_keyBuffer),
163
164 // Object that serializes the data.
165 eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::BIG_ENDIANNESS);
166 p_type->serializeKey(ser);
167 if (force_md5 || Point::getKeyMaxCdrSerializedSize() > 16)
168 {
169 // 如果需要使用MD5计算或者键的最大序列化大小超过16字节,则进行MD5相关操作
170 m_md5.init();
171 m_md5.update(m_keyBuffer, static_cast<unsigned int>(ser.getSerializedDataLength()));
172 m_md5.finalize();
173 for (uint8_t i = 0; i < 16; ++i)
174 {
175 handle->value[i] = m_md5.digest[i];
176 }
177 }
178 else
179 {
180 // 否则直接将键缓冲区中的内容复制到InstanceHandle_t的value数组中
181 for (uint8_t i = 0; i < 16; ++i)
182 {
183 handle->value[i] = m_keyBuffer[i];
184 }
185 }
186 return true;
187 }
188 } //End of namespace msg
189} //End of namespace geometry_msgs
eprosima::fastrtps::rtps::InstanceHandle_t InstanceHandle_t
eprosima::fastrtps::rtps::SerializedPayload_t SerializedPayload_t
virtual eProsima_user_DllExport ~PointPubSubType() override
virtual eProsima_user_DllExport void deleteData(void *data) override
virtual eProsima_user_DllExport void * createData() override
virtual eProsima_user_DllExport bool deserialize(eprosima::fastrtps::rtps::SerializedPayload_t *payload, void *data) override
eProsima_user_DllExport PointPubSubType()
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 bool getKey(void *data, eprosima::fastrtps::rtps::InstanceHandle_t *ihandle, bool force_md5=false) override
This class represents the structure Point defined by the user in the IDL file.
Definition Point.h:71
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.
Definition Point.cpp:109
eProsima_user_DllExport void serialize(eprosima::fastcdr::Cdr &cdr) const
This function serializes an object using CDR serialization.
Definition Point.cpp:129
static eProsima_user_DllExport size_t getCdrSerializedSize(const geometry_msgs::msg::Point &data, size_t current_alignment=0)
This function returns the serialized size of a data depending on the buffer alignment.
Definition Point.cpp:116
static eProsima_user_DllExport bool isKeyDefined()
This function tells you if the Key has been defined for this type
Definition Point.cpp:236
eProsima_user_DllExport void serializeKey(eprosima::fastcdr::Cdr &cdr) const
This function serializes the key members of an object using CDR serialization.
Definition Point.cpp:241
eProsima_user_DllExport void deserialize(eprosima::fastcdr::Cdr &cdr)
This function deserializes an object using CDR serialization.
Definition Point.cpp:137
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...
Definition Point.cpp:229