Gazebo Sim
API Reference
9.5.0
insert_drive_file
Tutorials
library_books
Classes
toc
Namespaces
insert_drive_file
Files
launch
Gazebo Website
Index
List
Hierarchy
Members: All
Members: Functions
Members: Variables
Members: Typedefs
Members: Enumerations
Members: Enumerator
List
Members
Functions
Typedefs
Variables
Enumerations
Enumerator
src
gz-sim
include
gz
sim
components
Serialization.hh
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2019 Open Source Robotics Foundation
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*
16
*/
17
#ifndef GZ_SIM_COMPONENTS_SERIALIZATION_HH_
18
#define GZ_SIM_COMPONENTS_SERIALIZATION_HH_
19
20
#ifdef _MSC_VER
21
#pragma warning(push)
22
#pragma warning(disable: 4251)
23
#endif
24
25
#include <google/protobuf/message_lite.h>
26
27
#ifdef _MSC_VER
28
#pragma warning(pop)
29
#endif
30
31
#include <gz/msgs/double_v.pb.h>
32
33
#include <string>
34
#include <vector>
35
#include <sdf/Sensor.hh>
36
37
#include <
gz/sim/Conversions.hh
>
38
#include <
gz/msgs/Utility.hh
>
39
40
// This header holds serialization operators which are shared among several
41
// components
42
43
namespace
gz
44
{
45
namespace
sim
46
{
47
// Inline bracket to help doxygen filtering.
48
inline
namespace
GZ_SIM_VERSION_NAMESPACE {
49
namespace
traits
50
{
58
template
<
typename
In,
typename
Out>
59
class
HasGazeboConvert
60
{
61
private
:
template
<
typename
InArg,
typename
OutArg>
62
static
auto
Test(
int
_test)
63
->
decltype
(std::declval<OutArg>() =
64
gz::sim::convert<OutArg>(std::declval<const InArg &>()),
65
std::true_type
());
66
67
private
:
template
<
typename
,
typename
>
68
static
auto
Test(...) ->
std::false_type
;
69
70
public
:
static
constexpr
bool
value =
// NOLINT
71
decltype
(Test<In, Out>(
true
))::value;
72
};
73
}
92
93
namespace
serializers
94
{
107
template
<
typename
DataType,
typename
MsgType>
108
class
ComponentToMsgSerializer
109
{
114
public
:
static
std::ostream
&
Serialize
(
std::ostream
&_out,
115
const
DataType &_data)
116
{
117
MsgType msg;
118
// cppcheck-suppress syntaxError
119
if
constexpr
(
traits::HasGazeboConvert<DataType, MsgType>::value
)
120
{
121
msg = gz::sim::convert<MsgType>(_data);
122
}
123
else
124
{
125
msg =
gz::msgs::Convert
(_data);
126
}
127
128
auto
result = msg.SerializeToOstream(&_out);
129
(void)result;
130
return
_out;
131
}
132
137
public
:
static
std::istream
&
Deserialize
(
std::istream
&_in,
138
DataType &_data)
139
{
140
MsgType msg;
141
if
(!msg.ParseFromIstream(&_in))
142
{
143
return
_in;
144
}
145
146
if
constexpr
(
traits::HasGazeboConvert<MsgType, DataType>::value
)
147
{
148
_data = gz::sim::convert<DataType>(msg);
149
}
150
else
151
{
152
_data =
gz::msgs::Convert
(msg);
153
}
154
return
_in;
155
}
156
};
157
159
using
SensorSerializer
=
ComponentToMsgSerializer<sdf::Sensor, msgs::Sensor>
;
160
162
class
VectorDoubleSerializer
163
{
168
public
:
static
std::ostream
&
Serialize
(
std::ostream
&_out,
169
const
std::vector<double>
&_vec)
170
{
171
gz::msgs::Double_V msg;
172
*msg.mutable_data() = {_vec.
begin
(), _vec.
end
()};
173
auto
result = msg.SerializeToOstream(&_out);
174
(void)result;
175
return
_out;
176
}
177
182
public
:
static
std::istream
&
Deserialize
(
std::istream
&_in,
183
std::vector<double>
&_vec)
184
{
185
gz::msgs::Double_V msg;
186
if
(!msg.ParseFromIstream(&_in))
187
{
188
return
_in;
189
}
190
191
_vec = {msg.
data
().begin(), msg.data().end()};
192
return
_in;
193
}
194
};
195
197
class
MsgSerializer
198
{
203
public
:
static
std::ostream
&
Serialize
(
std::ostream
&_out,
204
const
google::protobuf::Message &_msg)
205
{
206
auto
result = _msg.SerializeToOstream(&_out);
207
(void)result;
208
return
_out;
209
}
210
215
public
:
static
std::istream
&
Deserialize
(
std::istream
&_in,
216
google::protobuf::Message &_msg)
217
{
218
auto
result = _msg.ParseFromIstream(&_in);
219
(void)result;
220
return
_in;
221
}
222
};
223
225
class
StringSerializer
226
{
231
public
:
static
std::ostream
&
Serialize
(
std::ostream
&_out,
232
const
std::string
&_data)
233
{
234
_out << _data;
235
return
_out;
236
}
237
242
public
:
static
std::istream
&
Deserialize
(
std::istream
&_in,
243
std::string
&_data)
244
{
245
_data =
std::string
(
std::istreambuf_iterator<char>
(_in), {});
246
return
_in;
247
}
248
};
249
250
template
<
typename
T>
251
class
VectorSerializer
252
{
257
public
:
static
std::ostream
&
Serialize
(
std::ostream
&_out,
258
const
std::vector<T>
&_data)
259
{
260
_out << _data.
size
();
261
for
(
const
auto
& datum : _data)
262
_out <<
" "
<< datum;
263
return
_out;
264
}
265
270
public
:
static
std::istream
&
Deserialize
(
std::istream
&_in,
271
std::vector<T>
&_data)
272
{
273
size_t
size;
274
_in >> size;
275
_data.
resize
(size);
276
for
(
size_t
i = 0; i < size; ++i)
277
{
278
_in >> _data[i];
279
}
280
return
_in;
281
}
282
};
283
}
284
}
285
}
286
}
287
288
#endif