Gazebo Sim
API Reference
9.0.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
msg.SerializeToOstream(&_out);
129
return
_out;
130
}
131
136
public
:
static
std::istream
&
Deserialize
(
std::istream
&_in,
137
DataType &_data)
138
{
139
MsgType msg;
140
msg.ParseFromIstream(&_in);
141
142
if
constexpr
(
traits::HasGazeboConvert<MsgType, DataType>::value
)
143
{
144
_data = gz::sim::convert<DataType>(msg);
145
}
146
else
147
{
148
_data =
gz::msgs::Convert
(msg);
149
}
150
return
_in;
151
}
152
};
153
155
using
SensorSerializer
=
ComponentToMsgSerializer<sdf::Sensor, msgs::Sensor>
;
156
158
class
VectorDoubleSerializer
159
{
164
public
:
static
std::ostream
&
Serialize
(
std::ostream
&_out,
165
const
std::vector<double>
&_vec)
166
{
167
gz::msgs::Double_V msg;
168
*msg.mutable_data() = {_vec.
begin
(), _vec.
end
()};
169
msg.SerializeToOstream(&_out);
170
return
_out;
171
}
172
177
public
:
static
std::istream
&
Deserialize
(
std::istream
&_in,
178
std::vector<double>
&_vec)
179
{
180
gz::msgs::Double_V msg;
181
msg.ParseFromIstream(&_in);
182
183
_vec = {msg.
data
().begin(), msg.data().end()};
184
return
_in;
185
}
186
};
187
189
class
MsgSerializer
190
{
195
public
:
static
std::ostream
&
Serialize
(
std::ostream
&_out,
196
const
google::protobuf::Message &_msg)
197
{
198
_msg.SerializeToOstream(&_out);
199
return
_out;
200
}
201
206
public
:
static
std::istream
&
Deserialize
(
std::istream
&_in,
207
google::protobuf::Message &_msg)
208
{
209
_msg.ParseFromIstream(&_in);
210
return
_in;
211
}
212
};
213
215
class
StringSerializer
216
{
221
public
:
static
std::ostream
&
Serialize
(
std::ostream
&_out,
222
const
std::string
&_data)
223
{
224
_out << _data;
225
return
_out;
226
}
227
232
public
:
static
std::istream
&
Deserialize
(
std::istream
&_in,
233
std::string
&_data)
234
{
235
_data =
std::string
(
std::istreambuf_iterator<char>
(_in), {});
236
return
_in;
237
}
238
};
239
240
template
<
typename
T>
241
class
VectorSerializer
242
{
247
public
:
static
std::ostream
&
Serialize
(
std::ostream
&_out,
248
const
std::vector<T>
&_data)
249
{
250
_out << _data.
size
();
251
for
(
const
auto
& datum : _data)
252
_out <<
" "
<< datum;
253
return
_out;
254
}
255
260
public
:
static
std::istream
&
Deserialize
(
std::istream
&_in,
261
std::vector<T>
&_data)
262
{
263
size_t
size;
264
_in >> size;
265
_data.
resize
(size);
266
for
(
size_t
i = 0; i < size; ++i)
267
{
268
_in >> _data[i];
269
}
270
return
_in;
271
}
272
};
273
}
274
}
275
}
276
}
277
278
#endif