Ignition Gazebo

API Reference

6.6.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;
108  // cppcheck-suppress syntaxError
110  {
111  msg = ignition::gazebo::convert<MsgType>(_data);
112  }
113  else
114  {
115  msg = ignition::msgs::Convert(_data);
116  }
117 
118  msg.SerializeToOstream(&_out);
119  return _out;
120  }
121 
126  public: static std::istream &Deserialize(std::istream &_in,
127  DataType &_data)
128  {
129  MsgType msg;
130  msg.ParseFromIstream(&_in);
131 
133  {
134  _data = ignition::gazebo::convert<DataType>(msg);
135  }
136  else
137  {
138  _data = ignition::msgs::Convert(msg);
139  }
140  return _in;
141  }
142  };
143 
146 
149  {
154  public: static std::ostream &Serialize(std::ostream &_out,
155  const std::vector<double> &_vec)
156  {
158  *msg.mutable_data() = {_vec.begin(), _vec.end()};
159  msg.SerializeToOstream(&_out);
160  return _out;
161  }
162 
167  public: static std::istream &Deserialize(std::istream &_in,
168  std::vector<double> &_vec)
169  {
171  msg.ParseFromIstream(&_in);
172 
173  _vec = {msg.data().begin(), msg.data().end()};
174  return _in;
175  }
176  };
177 
180  {
185  public: static std::ostream &Serialize(std::ostream &_out,
186  const google::protobuf::Message &_msg)
187  {
188  _msg.SerializeToOstream(&_out);
189  return _out;
190  }
191 
196  public: static std::istream &Deserialize(std::istream &_in,
197  google::protobuf::Message &_msg)
198  {
199  _msg.ParseFromIstream(&_in);
200  return _in;
201  }
202  };
203 
206  {
211  public: static std::ostream &Serialize(std::ostream &_out,
212  const std::string &_data)
213  {
214  _out << _data;
215  return _out;
216  }
217 
222  public: static std::istream &Deserialize(std::istream &_in,
223  std::string &_data)
224  {
226  return _in;
227  }
228  };
229 
230  template <typename T>
232  {
237  public: static std::ostream &Serialize(std::ostream &_out,
238  const std::vector<T> &_data)
239  {
240  _out << _data.size();
241  for (const auto& datum : _data)
242  _out << " " << datum;
243  return _out;
244  }
245 
250  public: static std::istream &Deserialize(std::istream &_in,
251  std::vector<T> &_data)
252  {
253  size_t size;
254  _in >> size;
255  _data.resize(size);
256  for (size_t i = 0; i < size; ++i)
257  {
258  _in >> _data[i];
259  }
260  return _in;
261  }
262  };
263 }
264 }
265 }
266 }
267 
268 #endif
static std::istream & Deserialize(std::istream &_in, DataType &_data)
Deserialization.
Definition: Serialization.hh:126
static std::ostream & Serialize(std::ostream &_out, const std::vector< T > &_data)
Serialization for std::vector<T> with serializable T.
Definition: Serialization.hh:237
static std::istream & Deserialize(std::istream &_in, std::vector< double > &_vec)
Deserialization.
Definition: Serialization.hh:167
Serializer for components that hold protobuf messages.
Definition: Serialization.hh:179
::google::protobuf::RepeatedField< double > * mutable_data()
static std::ostream & Serialize(std::ostream &_out, const std::vector< double > &_vec)
Serialization.
Definition: Serialization.hh:154
static std::istream & Deserialize(std::istream &_in, std::vector< T > &_data)
Deserialization for std::vector<T> with serializable T.
Definition: Serialization.hh:250
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:211
STL class.
static std::ostream & Serialize(std::ostream &_out, const google::protobuf::Message &_msg)
Serialization.
Definition: Serialization.hh:185
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:196
T begin(T... args)
double data(int index) const
Serializer for components that hold std::string.
Definition: Serialization.hh:205
ignition::math::Vector3d Convert(const msgs::Vector3d &_v)
Definition: Serialization.hh:231
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:148
static std::istream & Deserialize(std::istream &_in, std::string &_data)
Deserialization.
Definition: Serialization.hh:222