CARLA
 
载入中...
搜索中...
未找到
TFMessagePubSubTypes.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 TFMessagePubSubTypes.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
30// 定义在tf2_msgs命名空间下的msg命名空间
31namespace tf2_msgs {
32 namespace msg {
33 // TFMessagePubSubType类的构造函数
35 {
36 // 设置类型名称
37 setName("tf2_msgs::msg::dds_::TFMessage_");
38 // 获取TFMessage的最大CDR序列化大小
39 auto type_size = TFMessage::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 // 判断TFMessage是否定义了获取键的操作
45 m_isGetKeyDefined = TFMessage::isKeyDefined();
46 // 根据TFMessage获取键的最大CDR序列化大小来确定键缓冲区的长度,如果大于16则取其本身大小,否则取16
47 size_t keyLength = TFMessage::getKeyMaxCdrSerializedSize() > 16?
49 // 分配键缓冲区内存
50 m_keyBuffer = reinterpret_cast<unsigned char*>(malloc(keyLength));
51 // 初始化键缓冲区内存为0
52 memset(m_keyBuffer, 0, keyLength);
53 }
54
55 // TFMessagePubSubType类的析构函数
57 {
58 // 如果键缓冲区不为空,则释放其内存
59 if (m_keyBuffer!= nullptr)
60 {
61 free(m_keyBuffer);
62 }
63 }
64
65 // 序列化函数,用于将数据序列化为特定格式(可能用于网络传输等)
67 void* data,
68 SerializedPayload_t* payload)
69 {
70 // 将传入的void*类型数据转换为TFMessage*类型指针,方便后续操作
71 TFMessage* p_type = static_cast<TFMessage*>(data);
72
73 // 创建一个FastBuffer对象,用于管理原始缓冲区,它关联了payload中的数据指针和最大尺寸
74 eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(payload->data), payload->max_size);
75 // 创建一个Cdr对象,用于进行序列化操作,指定了缓冲区、字节序(默认字节序)以及相关的CDR模式
76 eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::DDS_CDR);
77 // 根据序列化对象的字节序来设置payload的封装字节序(大端序或小端序)
78 payload->encapsulation = ser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS? CDR_BE : CDR_LE;
79 // 序列化封装相关信息(可能是一些头部等通用的封装结构)
80 ser.serialize_encapsulation();
81
82 try
83 {
84 // 调用TFMessage对象的serialize方法,将实际的数据进行序列化到之前创建的Cdr对象(也就是关联的缓冲区中)
85 p_type->serialize(ser);
86 }
87 catch (eprosima::fastcdr::exception::NotEnoughMemoryException& /*exception*/)
88 {
89 // 如果内存不足导致序列化失败,返回false
90 return false;
91 }
92
93 // 获取序列化后的数据长度,并设置到payload结构体中
94 payload->length = static_cast<uint32_t>(ser.getSerializedDataLength());
95 return true;
96 }
97
98 // 反序列化函数,用于将特定格式的数据还原为原始数据对象
100 SerializedPayload_t* payload,
101 void* data)
102 {
103 try
104 {
105 // 将传入的void*类型数据转换为TFMessage*类型指针,方便后续操作
106 TFMessage* p_type = static_cast<TFMessage*>(data);
107
108 // 创建一个FastBuffer对象,用于管理原始缓冲区,它关联了payload中的数据指针和实际的数据长度(这里用的是payload的length字段)
109 eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(payload->data), payload->length);
110
111 // 创建一个Cdr对象,用于进行反序列化操作,指定了缓冲区、字节序(默认字节序)以及相关的CDR模式
112 eprosima::fastcdr::Cdr deser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::DDS_CDR);
113
114 // 先反序列化封装相关信息(可能解析头部等通用的封装结构)
115 deser.read_encapsulation();
116 // 根据反序列化对象的字节序来设置payload的封装字节序(大端序或小端序)
117 payload->encapsulation = deser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS? CDR_BE : CDR_LE;
118
119 // 调用TFMessage对象的deserialize方法,将缓冲区中的数据反序列化到对应的TFMessage对象中
120 p_type->deserialize(deser);
121 }
122 catch (eprosima::fastcdr::exception::NotEnoughMemoryException& /*exception*/)
123 {
124 // 如果内存不足导致反序列化失败,返回false
125 return false;
126 }
127
128 return true;
129 }
130
131 // 返回一个函数对象,该函数对象用于获取给定数据的序列化大小(包含封装相关的额外大小)
133 void* data)
134 {
135 return [data]() -> uint32_t
136 {
137 return static_cast<uint32_t>(type::getCdrSerializedSize(*static_cast<TFMessage*>(data))) +
138 4u /*encapsulation*/;
139 };
140 }
141
142 // 创建一个TFMessage类型的数据对象(在堆上分配内存),并返回其void*类型的指针
144 {
145 return reinterpret_cast<void*>(new TFMessage());
146 }
147
148 // 删除之前通过createData函数创建的数据对象(释放其内存)
150 void* data)
151 {
152 delete(reinterpret_cast<TFMessage*>(data));
153 }
154
155 // 获取给定数据的键值(可能用于标识等目的),根据情况可能使用MD5计算等操作
157 void* data,
158 InstanceHandle_t* handle,
159 bool force_md5)
160 {
161 if (!m_isGetKeyDefined)
162 {
163 return false;
164 }
165
166 TFMessage* p_type = static_cast<TFMessage*>(data);
167
168 // 创建一个FastBuffer对象,用于管理键缓冲区,关联了m_keyBuffer指针和TFMessage获取键的最大CDR序列化大小
169 eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(m_keyBuffer),
171
172 // 创建一个Cdr对象,用于序列化键数据,指定了字节序(大端序)
173 eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::BIG_ENDIANNESS);
174 p_type->serializeKey(ser);
175 if (force_md5 || TFMessage::getKeyMaxCdrSerializedSize() > 16)
176 {
177 // 初始化MD5对象
178 m_md5.init();
179 // 使用MD5对象更新数据,传入键缓冲区和序列化后的数据长度
180 m_md5.update(m_keyBuffer, static_cast<unsigned int>(ser.getSerializedDataLength()));
181 // 完成MD5计算
182 m_md5.finalize();
183 for (uint8_t i = 0; i < 16; ++i)
184 {
185 handle->value[i] = m_md5.digest[i];
186 }
187 }
188 else
189 {
190 for (uint8_t i = 0; i < 16; ++i)
191 {
192 handle->value[i] = m_keyBuffer[i];
193 }
194 }
195 return true;
196 }
197 } //End of namespace msg
198} //End of namespace tf2_msgs
eprosima::fastrtps::rtps::InstanceHandle_t InstanceHandle_t
eprosima::fastrtps::rtps::SerializedPayload_t SerializedPayload_t
eProsima_user_DllExport TFMessagePubSubType()
virtual eProsima_user_DllExport std::function< uint32_t()> getSerializedSizeProvider(void *data) override
virtual eProsima_user_DllExport bool serialize(void *data, eprosima::fastrtps::rtps::SerializedPayload_t *payload) override
virtual eProsima_user_DllExport bool deserialize(eprosima::fastrtps::rtps::SerializedPayload_t *payload, void *data) override
virtual eProsima_user_DllExport void deleteData(void *data) override
virtual eProsima_user_DllExport ~TFMessagePubSubType() 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
static eProsima_user_DllExport size_t getCdrSerializedSize(const tf2_msgs::msg::TFMessage &data, size_t current_alignment=0)
static eProsima_user_DllExport bool isKeyDefined()
eProsima_user_DllExport void serializeKey(eprosima::fastcdr::Cdr &cdr) const
eProsima_user_DllExport void deserialize(eprosima::fastcdr::Cdr &cdr)
eProsima_user_DllExport void serialize(eprosima::fastcdr::Cdr &cdr) const
static eProsima_user_DllExport size_t getKeyMaxCdrSerializedSize(size_t current_alignment=0)
static eProsima_user_DllExport size_t getMaxCdrSerializedSize(size_t current_alignment=0)