CARLA
 
载入中...
搜索中...
未找到
OdometryPubSubTypes.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 OdometryPubSubTypes.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 "OdometryPubSubTypes.h"
26
27using SerializedPayload_t = eprosima::fastrtps::rtps::SerializedPayload_t;
28using InstanceHandle_t = eprosima::fastrtps::rtps::InstanceHandle_t;
29
30namespace nav_msgs {
31 namespace msg {
33 {
34 // 设置消息类型的名称为 "nav_msgs::msg::dds_::Odometry_"。
35 // 该名称用于在 DDS(数据分发服务)系统中唯一标识 Odometry 消息类型。
36 setName("nav_msgs::msg::dds_::Odometry_");
37
38 // 获取 Odometry 类型的最大 CDR 序列化大小。
39 // CDR(Common Data Representation)是数据交换格式,用于序列化和反序列化消息数据。
40 auto type_size = Odometry::getMaxCdrSerializedSize();
41
42 // 计算可能的子消息对齐。这里将序列化大小对齐到 4 字节边界。
43 type_size += eprosima::fastcdr::Cdr::alignment(type_size, 4); // 可能的子消息对齐
44
45 // 设置消息类型的总大小(包括封装头部)。封装是指在消息的实际数据前后添加的头部信息。
46 m_typeSize = static_cast<uint32_t>(type_size) + 4; // 封装(包括数据和头部)
47
48 // 设置是否定义了该消息的键(Key)。在一些情况下,消息会定义一个唯一标识符(键),用于识别该消息。
49 m_isGetKeyDefined = Odometry::isKeyDefined();
50
51 // 获取 Odometry 消息类型的最大键 CDR 序列化大小。
52 // 如果键的序列化大小大于 16 字节,则使用实际的键大小,否则使用 16 字节。
53 size_t keyLength = Odometry::getKeyMaxCdrSerializedSize() > 16 ?
55
56 // 为消息键分配内存,存储在 m_keyBuffer 中。
57 // 键通常用于标识消息的唯一性。
58 m_keyBuffer = reinterpret_cast<unsigned char*>(malloc(keyLength));
59
60 // 将分配的内存初始化为零,确保 m_keyBuffer 在使用时不会包含任何未初始化的数据。
61 memset(m_keyBuffer, 0, keyLength);
62 }
63
64
66 {
67 if (m_keyBuffer != nullptr)
68 {
69 free(m_keyBuffer);
70 }
71 }
72
74 void* data,
75 SerializedPayload_t* payload)
76 {
77 Odometry* p_type = static_cast<Odometry*>(data);
78
79 // Object that manages the raw buffer.
80 eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(payload->data), payload->max_size);
81 // Object that serializes the data.
82 eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::DDS_CDR);
83 payload->encapsulation = ser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE : CDR_LE;
84 // Serialize encapsulation
85 ser.serialize_encapsulation();
86
87 try
88 {
89 // Serialize the object.
90 p_type->serialize(ser);
91 }
92 catch (eprosima::fastcdr::exception::NotEnoughMemoryException& /*exception*/)
93 {
94 return false;
95 }
96
97 // Get the serialized length
98 payload->length = static_cast<uint32_t>(ser.getSerializedDataLength());
99 return true;
100 }
101
103 SerializedPayload_t* payload,
104 void* data)
105 {
106 try
107 {
108 // 将 data 转换为指向 Odometry 类型的指针
109 Odometry* p_type = static_cast<Odometry*>(data);
110
111 // 管理原始缓冲区的对象。
112 eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(payload->data), payload->length);
113
114 // 用于反序列化数据的对象。
115 eprosima::fastcdr::Cdr deser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::DDS_CDR);
116
117 // 反序列化封装数据。
118 deser.read_encapsulation();
119 // 根据反序列化的字节序设置封装类型(大端或小端)
120 payload->encapsulation = deser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE : CDR_LE;
121
122 // 反序列化 Odometry 对象。
123 p_type->deserialize(deser);
124 }
125 catch (eprosima::fastcdr::exception::NotEnoughMemoryException& /*exception*/)
126 {
127 // 如果内存不足,返回 false 表示反序列化失败
128 return false;
129 }
130
131 // 反序列化成功,返回 true
132 return true;
133 }
134
136 void* data)
137 {
138 return [data]() -> uint32_t
139 {
140 return static_cast<uint32_t>(type::getCdrSerializedSize(*static_cast<Odometry*>(data))) +
141 4u /*encapsulation*/;
142 };
143 }
144
146 {
147 return reinterpret_cast<void*>(new Odometry());
148 }
149
151 void* data)
152 {
153 delete(reinterpret_cast<Odometry*>(data));
154 }
155
157 void* data,
158 InstanceHandle_t* handle,
159 bool force_md5)
160 {
161 if (!m_isGetKeyDefined)
162 {
163 return false;
164 }
165
166 Odometry* p_type = static_cast<Odometry*>(data);
167
168 // Object that manages the raw buffer.
169 eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(m_keyBuffer),
171
172 // Object that serializes the data.
173 eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::BIG_ENDIANNESS);
174 p_type->serializeKey(ser);
175 if (force_md5 || Odometry::getKeyMaxCdrSerializedSize() > 16)
176 {
177 m_md5.init();
178 m_md5.update(m_keyBuffer, static_cast<unsigned int>(ser.getSerializedDataLength()));
179 m_md5.finalize();
180 for (uint8_t i = 0; i < 16; ++i)
181 {
182 handle->value[i] = m_md5.digest[i];
183 }
184 }
185 else
186 {
187 for (uint8_t i = 0; i < 16; ++i)
188 {
189 handle->value[i] = m_keyBuffer[i];
190 }
191 }
192 return true;
193 }
194 } //End of namespace msg
195} //End of namespace nav_msgs
eprosima::fastrtps::rtps::InstanceHandle_t InstanceHandle_t
eprosima::fastrtps::rtps::SerializedPayload_t SerializedPayload_t
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
virtual eProsima_user_DllExport ~OdometryPubSubType() override
virtual eProsima_user_DllExport void deleteData(void *data) override
virtual eProsima_user_DllExport bool serialize(void *data, eprosima::fastrtps::rtps::SerializedPayload_t *payload) override
eProsima_user_DllExport OdometryPubSubType()
virtual eProsima_user_DllExport void * createData() override
virtual eProsima_user_DllExport bool deserialize(eprosima::fastrtps::rtps::SerializedPayload_t *payload, void *data) override
This class represents the structure Odometry defined by the user in the IDL file.
Definition Odometry.h:75
eProsima_user_DllExport void serializeKey(eprosima::fastcdr::Cdr &cdr) const
This function serializes the key members of an object using CDR serialization.
Definition Odometry.cpp:372
eProsima_user_DllExport void deserialize(eprosima::fastcdr::Cdr &cdr)
This function deserializes an object using CDR serialization.
Definition Odometry.cpp:189
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 Odometry.cpp:354
static eProsima_user_DllExport bool isKeyDefined()
This function tells you if the Key has been defined for this type
Definition Odometry.cpp:364
static eProsima_user_DllExport size_t getCdrSerializedSize(const nav_msgs::msg::Odometry &data, size_t current_alignment=0)
This function returns the serialized size of a data depending on the buffer alignment.
Definition Odometry.cpp:154
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 Odometry.cpp:144
eProsima_user_DllExport void serialize(eprosima::fastcdr::Cdr &cdr) const
This function serializes an object using CDR serialization.
Definition Odometry.cpp:175