-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONMessagePubSubTypes.cxx
More file actions
221 lines (194 loc) · 7.27 KB
/
JSONMessagePubSubTypes.cxx
File metadata and controls
221 lines (194 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*!
* @file JSONMessagePubSubTypes.cpp
* This header file contains the implementation of the serialization functions.
*
* This file was generated by the tool fastddsgen.
*/
#include <fastdds/rtps/common/CdrSerialization.hpp>
#include "JSONMessagePubSubTypes.h"
#include "JSONMessageCdrAux.hpp"
using SerializedPayload_t = eprosima::fastrtps::rtps::SerializedPayload_t;
using InstanceHandle_t = eprosima::fastrtps::rtps::InstanceHandle_t;
using DataRepresentationId_t = eprosima::fastdds::dds::DataRepresentationId_t;
JSONMessagePubSubType::JSONMessagePubSubType()
{
setName("JSONMessage");
uint32_t type_size =
#if FASTCDR_VERSION_MAJOR == 1
static_cast<uint32_t>(JSONMessage::getMaxCdrSerializedSize());
#else
JSONMessage_max_cdr_typesize;
#endif
type_size += static_cast<uint32_t>(eprosima::fastcdr::Cdr::alignment(type_size, 4)); /* possible submessage alignment */
m_typeSize = type_size + 4; /*encapsulation*/
m_isGetKeyDefined = false;
uint32_t keyLength = JSONMessage_max_key_cdr_typesize > 16 ? JSONMessage_max_key_cdr_typesize : 16;
m_keyBuffer = reinterpret_cast<unsigned char*>(malloc(keyLength));
memset(m_keyBuffer, 0, keyLength);
}
JSONMessagePubSubType::~JSONMessagePubSubType()
{
if (m_keyBuffer != nullptr)
{
free(m_keyBuffer);
}
}
bool JSONMessagePubSubType::serialize(
void* data,
SerializedPayload_t* payload,
DataRepresentationId_t data_representation)
{
JSONMessage* p_type = static_cast<JSONMessage*>(data);
// Object that manages the raw buffer.
eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(payload->data), payload->max_size);
// Object that serializes the data.
eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
data_representation == DataRepresentationId_t::XCDR_DATA_REPRESENTATION ?
eprosima::fastcdr::CdrVersion::XCDRv1 : eprosima::fastcdr::CdrVersion::XCDRv2);
payload->encapsulation = ser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE : CDR_LE;
#if FASTCDR_VERSION_MAJOR > 1
ser.set_encoding_flag(
data_representation == DataRepresentationId_t::XCDR_DATA_REPRESENTATION ?
eprosima::fastcdr::EncodingAlgorithmFlag::PLAIN_CDR :
eprosima::fastcdr::EncodingAlgorithmFlag::DELIMIT_CDR2);
#endif // FASTCDR_VERSION_MAJOR > 1
try
{
// Serialize encapsulation
ser.serialize_encapsulation();
// Serialize the object.
ser << *p_type;
}
catch (eprosima::fastcdr::exception::Exception& /*exception*/)
{
return false;
}
// Get the serialized length
#if FASTCDR_VERSION_MAJOR == 1
payload->length = static_cast<uint32_t>(ser.getSerializedDataLength());
#else
payload->length = static_cast<uint32_t>(ser.get_serialized_data_length());
#endif // FASTCDR_VERSION_MAJOR == 1
return true;
}
bool JSONMessagePubSubType::deserialize(
SerializedPayload_t* payload,
void* data)
{
try
{
// Convert DATA to pointer of your type
JSONMessage* p_type = static_cast<JSONMessage*>(data);
// Object that manages the raw buffer.
eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(payload->data), payload->length);
// Object that deserializes the data.
eprosima::fastcdr::Cdr deser(fastbuffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN
#if FASTCDR_VERSION_MAJOR == 1
, eprosima::fastcdr::Cdr::CdrType::DDS_CDR
#endif // FASTCDR_VERSION_MAJOR == 1
);
// Deserialize encapsulation.
deser.read_encapsulation();
payload->encapsulation = deser.endianness() == eprosima::fastcdr::Cdr::BIG_ENDIANNESS ? CDR_BE : CDR_LE;
// Deserialize the object.
deser >> *p_type;
}
catch (eprosima::fastcdr::exception::Exception& /*exception*/)
{
return false;
}
return true;
}
std::function<uint32_t()> JSONMessagePubSubType::getSerializedSizeProvider(
void* data,
DataRepresentationId_t data_representation)
{
return [data, data_representation]() -> uint32_t
{
#if FASTCDR_VERSION_MAJOR == 1
static_cast<void>(data_representation);
return static_cast<uint32_t>(type::getCdrSerializedSize(*static_cast<JSONMessage*>(data))) +
4u /*encapsulation*/;
#else
try
{
eprosima::fastcdr::CdrSizeCalculator calculator(
data_representation == DataRepresentationId_t::XCDR_DATA_REPRESENTATION ?
eprosima::fastcdr::CdrVersion::XCDRv1 :eprosima::fastcdr::CdrVersion::XCDRv2);
size_t current_alignment {0};
return static_cast<uint32_t>(calculator.calculate_serialized_size(
*static_cast<JSONMessage*>(data), current_alignment)) +
4u /*encapsulation*/;
}
catch (eprosima::fastcdr::exception::Exception& /*exception*/)
{
return 0;
}
#endif // FASTCDR_VERSION_MAJOR == 1
};
}
void* JSONMessagePubSubType::createData()
{
return reinterpret_cast<void*>(new JSONMessage());
}
void JSONMessagePubSubType::deleteData(
void* data)
{
delete(reinterpret_cast<JSONMessage*>(data));
}
bool JSONMessagePubSubType::getKey(
void* data,
InstanceHandle_t* handle,
bool force_md5)
{
if (!m_isGetKeyDefined)
{
return false;
}
JSONMessage* p_type = static_cast<JSONMessage*>(data);
// Object that manages the raw buffer.
eprosima::fastcdr::FastBuffer fastbuffer(reinterpret_cast<char*>(m_keyBuffer),
JSONMessage_max_key_cdr_typesize);
// Object that serializes the data.
eprosima::fastcdr::Cdr ser(fastbuffer, eprosima::fastcdr::Cdr::BIG_ENDIANNESS, eprosima::fastcdr::CdrVersion::XCDRv1);
#if FASTCDR_VERSION_MAJOR == 1
p_type->serializeKey(ser);
#else
eprosima::fastcdr::serialize_key(ser, *p_type);
#endif // FASTCDR_VERSION_MAJOR == 1
if (force_md5 || JSONMessage_max_key_cdr_typesize > 16)
{
m_md5.init();
#if FASTCDR_VERSION_MAJOR == 1
m_md5.update(m_keyBuffer, static_cast<unsigned int>(ser.getSerializedDataLength()));
#else
m_md5.update(m_keyBuffer, static_cast<unsigned int>(ser.get_serialized_data_length()));
#endif // FASTCDR_VERSION_MAJOR == 1
m_md5.finalize();
for (uint8_t i = 0; i < 16; ++i)
{
handle->value[i] = m_md5.digest[i];
}
}
else
{
for (uint8_t i = 0; i < 16; ++i)
{
handle->value[i] = m_keyBuffer[i];
}
}
return true;
}