Ignition Gazebo

API Reference

6.1.0
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 IGNITION_GAZEBO_COMPONENTS_SERIALIZATION_HH_
18 #define IGNITION_GAZEBO_COMPONENTS_SERIALIZATION_HH_
19 
20 #include <google/protobuf/message_lite.h>
22 
23 #include <string>
24 #include <vector>
25 #include <sdf/Sensor.hh>
26 
28 #include <ignition/msgs/Utility.hh>
29 
30 // This header holds serialization operators which are shared among several
31 // components
32 
33 namespace ignition
34 {
35 namespace gazebo
36 {
37 // Inline bracket to help doxygen filtering.
38 inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
39 namespace traits
40 {
48  template <typename In, typename Out>
50  {
51  private: template <typename InArg, typename OutArg>
52  static auto Test(int _test)
53  -> decltype(std::declval<OutArg>() =
54  ignition::gazebo::convert<OutArg>(std::declval<const InArg &>()),
55  std::true_type());
56 
57  private: template <typename, typename>
58  static auto Test(...) -> std::false_type;
59 
60  public: static constexpr bool value = // NOLINT
61  decltype(Test<In, Out>(true))::value;
62  };
63 }
82 
83 namespace serializers
84 {
97  template <typename DataType, typename MsgType>
99  {
104  public: static std::ostream &Serialize(std::ostream &_out,
105  const DataType &_data)
106  {
107  MsgType msg;
109  {
110  msg = ignition::gazebo::convert<MsgType>(_data);
111  }
112  else
113  {
114  msg = ignition::msgs::Convert(_data);
115  }
116 
117  msg.SerializeToOstream(&_out);
118  return _out;
119  }
120 
125  public: static std::istream &Deserialize(std::istream &_in,
126  DataType &_data)
127  {
128  MsgType msg;
129  msg.ParseFromIstream(&_in);
130 
132  {
133  _data = ignition::gazebo::convert<DataType>(msg);
134  }
135  else
136  {
137  _data = ignition::msgs::Convert(msg);
138  }
139  return _in;
140  }
141  };
142 
145 
148  {
153  public: static std::ostream &Serialize(std::ostream &_out,
154  const std::vector<double> &_vec)
155  {
157  *msg.mutable_data() = {_vec.begin(), _vec.end()};
158  msg.SerializeToOstream(&_out);
159  return _out;
160  }
161 
166  public: static std::istream &Deserialize(std::istream &_in,
167  std::vector<double> &_vec)
168  {
170  msg.ParseFromIstream(&_in);
171 
172  _vec = {msg.data().begin(), msg.data().end()};
173  return _in;
174  }
175  };
176 
179  {
184  public: static std::ostream &Serialize(std::ostream &_out,
185  const google::protobuf::Message &_msg)
186  {
187  _msg.SerializeToOstream(&_out);
188  return _out;
189  }
190 
195  public: static std::istream &Deserialize(std::istream &_in,
196  google::protobuf::Message &_msg)
197  {
198  _msg.ParseFromIstream(&_in);
199  return _in;
200  }
201  };
202 
205  {
210  public: static std::ostream &Serialize(std::ostream &_out,
211  const std::string &_data)
212  {
213  _out << _data;
214  return _out;
215  }
216 
221  public: static std::istream &Deserialize(std::istream &_in,
222  std::string &_data)
223  {
225  return _in;
226  }
227  };
228 
229  template <typename T>
231  {
236  public: static std::ostream &Serialize(std::ostream &_out,
237  const std::vector<T> &_data)
238  {
239  _out << _data.size();
240  for (const auto& datum : _data)
241  _out << " " << datum;
242  return _out;
243  }
244 
249  public: static std::istream &Deserialize(std::istream &_in,
250  std::vector<T> &_data)
251  {
252  size_t size;
253  _in >> size;
254  _data.resize(size);
255  for (size_t i = 0; i < size; ++i)
256  {
257  _in >> _data[i];
258  }
259  return _in;
260  }
261  };
262 }
263 }
264 }
265 }
266 
267 #endif
static std::istream & Deserialize(std::istream &_in, DataType &_data)
Deserialization.
Definition: Serialization.hh:125
static std::ostream & Serialize(std::ostream &_out, const std::vector< T > &_data)
Serialization for std::vector<T> with serializable T.
Definition: Serialization.hh:236
static std::istream & Deserialize(std::istream &_in, std::vector< double > &_vec)
Deserialization.
Definition: Serialization.hh:166
Serializer for components that hold protobuf messages.
Definition: Serialization.hh:178
::google::protobuf::RepeatedField< double > * mutable_data()
static std::ostream & Serialize(std::ostream &_out, const std::vector< double > &_vec)
Serialization.
Definition: Serialization.hh:153
static std::istream & Deserialize(std::istream &_in, std::vector< T > &_data)
Deserialization for std::vector<T> with serializable T.
Definition: Serialization.hh:249
T end(T... args)
STL class.
T resize(T... args)
static std::ostream & Serialize(std::ostream &_out, const std::string &_data)
Serialization.
Definition: Serialization.hh:210
STL class.
static std::ostream & Serialize(std::ostream &_out, const google::protobuf::Message &_msg)
Serialization.
Definition: Serialization.hh:184
Type trait that determines if an convert from In to Out is defined. Usage:
Definition: Serialization.hh:49
static std::ostream & Serialize(std::ostream &_out, const DataType &_data)
Serialization.
Definition: Serialization.hh:104
T size(T... args)
static std::istream & Deserialize(std::istream &_in, google::protobuf::Message &_msg)
Deserialization.
Definition: Serialization.hh:195
T begin(T... args)
double data(int index) const
Serializer for components that hold std::string.
Definition: Serialization.hh:204
ignition::math::Vector3d Convert(const msgs::Vector3d &_v)
Definition: Serialization.hh:230
Serialization for that converts components data types to ignition::msgs. This assumes that convert<Da...
Definition: Serialization.hh:98
This library is part of the Ignition Robotics project.
STL class.
Serializer for components that hold std::vector<double>.
Definition: Serialization.hh:147
static std::istream & Deserialize(std::istream &_in, std::string &_data)
Deserialization.
Definition: Serialization.hh:221