Gazebo Gazebo

API Reference

6.17.1
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_GAZEBO_COMPONENTS_SERIALIZATION_HH_
18 #define GZ_GAZEBO_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 ignition
44 {
45 namespace gazebo
46 {
47 // Inline bracket to help doxygen filtering.
48 inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
49 namespace traits
50 {
58  template <typename In, typename Out>
60  {
61  private: template <typename InArg, typename OutArg>
62  static auto Test(int _test)
63  -> decltype(std::declval<OutArg>() =
64  ignition::gazebo::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>
109  {
114  public: static std::ostream &Serialize(std::ostream &_out,
115  const DataType &_data)
116  {
117  MsgType msg;
118  // cppcheck-suppress syntaxError
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 
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 
160 
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 
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 
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  {
246  return _in;
247  }
248  };
249 
250  template <typename T>
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
static std::ostream & Serialize(std::ostream &_out, const std::vector< T > &_data)
Serialization for std::vector<T> with serializable T.
Definition: gz/sim/components/Serialization.hh:257
T resize(T... args)
Serializer for components that hold std::string.
Definition: gz/sim/components/Serialization.hh:225
This library is part of the Gazebo project.
STL class.
Definition: gz/sim/components/Serialization.hh:251
static std::istream & Deserialize(std::istream &_in, DataType &_data)
Deserialization.
Definition: gz/sim/components/Serialization.hh:137
static std::istream & Deserialize(std::istream &_in, std::vector< double > &_vec)
Deserialization.
Definition: gz/sim/components/Serialization.hh:182
static constexpr bool value
Definition: gz/sim/components/Serialization.hh:70
static std::istream & Deserialize(std::istream &_in, google::protobuf::Message &_msg)
Deserialization.
Definition: gz/sim/components/Serialization.hh:215
T size(T... args)
static std::istream & Deserialize(std::istream &_in, std::vector< T > &_data)
Deserialization for std::vector<T> with serializable T.
Definition: gz/sim/components/Serialization.hh:270
Serializer for components that hold std::vector<double>.
Definition: gz/sim/components/Serialization.hh:162
static std::ostream & Serialize(std::ostream &_out, const std::vector< double > &_vec)
Serialization.
Definition: gz/sim/components/Serialization.hh:168
static std::ostream & Serialize(std::ostream &_out, const std::string &_data)
Serialization.
Definition: gz/sim/components/Serialization.hh:231
Type trait that determines if an ignition::gazebo::convert from In to Out is defined....
Definition: gz/sim/components/Serialization.hh:59
STL class.
static std::ostream & Serialize(std::ostream &_out, const google::protobuf::Message &_msg)
Serialization.
Definition: gz/sim/components/Serialization.hh:203
T begin(T... args)
Serialization for that converts components data types to gz::msgs. This assumes that gz::sim::convert...
Definition: gz/sim/components/Serialization.hh:108
static std::istream & Deserialize(std::istream &_in, std::string &_data)
Deserialization.
Definition: gz/sim/components/Serialization.hh:242
T end(T... args)
Serializer for components that hold protobuf messages.
Definition: gz/sim/components/Serialization.hh:197
STL class.
T data(T... args)
static std::ostream & Serialize(std::ostream &_out, const DataType &_data)
Serialization.
Definition: gz/sim/components/Serialization.hh:114