Gazebo Gazebo

API Reference

3.15.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 #include <google/protobuf/message_lite.h>
21 #include <gz/msgs/double_v.pb.h>
22 
23 #include <string>
24 #include <vector>
25 #include <sdf/Sensor.hh>
26 
27 #include <gz/sim/Conversions.hh>
28 
29 // This header holds serialization operators which are shared among several
30 // components
31 
32 namespace ignition
33 {
34 namespace gazebo
35 {
36 // Inline bracket to help doxygen filtering.
37 inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
56 
57 namespace serializers
58 {
71  template <typename DataType, typename MsgType>
73  {
78  public: static std::ostream &Serialize(std::ostream &_out,
79  const DataType &_data)
80  {
81  auto msg = gz::sim::convert<MsgType>(_data);
82  msg.SerializeToOstream(&_out);
83  return _out;
84  }
85 
90  public: static std::istream &Deserialize(std::istream &_in,
91  DataType &_data)
92  {
93  MsgType msg;
94  msg.ParseFromIstream(&_in);
95 
96  _data = gz::sim::convert<DataType>(msg);
97  return _in;
98  }
99  };
100 
103 
106  {
111  public: static std::ostream &Serialize(std::ostream &_out,
112  const std::vector<double> &_vec)
113  {
114  gz::msgs::Double_V msg;
115  *msg.mutable_data() = {_vec.begin(), _vec.end()};
116  msg.SerializeToOstream(&_out);
117  return _out;
118  }
119 
124  public: static std::istream &Deserialize(std::istream &_in,
125  std::vector<double> &_vec)
126  {
127  gz::msgs::Double_V msg;
128  msg.ParseFromIstream(&_in);
129 
130  _vec = {msg.data().begin(), msg.data().end()};
131  return _in;
132  }
133  };
134 
137  {
142  public: static std::ostream &Serialize(std::ostream &_out,
143  const google::protobuf::Message &_msg)
144  {
145  _msg.SerializeToOstream(&_out);
146  return _out;
147  }
148 
153  public: static std::istream &Deserialize(std::istream &_in,
154  google::protobuf::Message &_msg)
155  {
156  _msg.ParseFromIstream(&_in);
157  return _in;
158  }
159  };
160 
163  {
168  public: static std::ostream &Serialize(std::ostream &_out,
169  const std::string &_data)
170  {
171  _out << _data;
172  return _out;
173  }
174 
179  public: static std::istream &Deserialize(std::istream &_in,
180  std::string &_data)
181  {
183  return _in;
184  }
185  };
186 
187  template <typename T>
189  {
194  public: static std::ostream &Serialize(std::ostream &_out,
195  const std::vector<T> &_data)
196  {
197  _out << _data.size();
198  for (const auto& datum : _data)
199  _out << " " << datum;
200  return _out;
201  }
202 
207  public: static std::istream &Deserialize(std::istream &_in,
208  std::vector<T> &_data)
209  {
210  size_t size;
211  _in >> size;
212  _data.resize(size);
213  for (size_t i = 0; i < size; ++i)
214  {
215  _in >> _data[i];
216  }
217  return _in;
218  }
219  };
220 }
221 }
222 }
223 }
224 
225 #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:194
T resize(T... args)
Serializer for components that hold std::string.
Definition: gz/sim/components/Serialization.hh:162
This library is part of the Ignition Robotics project.
STL class.
Definition: gz/sim/components/Serialization.hh:188
static std::istream & Deserialize(std::istream &_in, DataType &_data)
Deserialization.
Definition: gz/sim/components/Serialization.hh:90
static std::istream & Deserialize(std::istream &_in, std::vector< double > &_vec)
Deserialization.
Definition: gz/sim/components/Serialization.hh:124
static std::istream & Deserialize(std::istream &_in, google::protobuf::Message &_msg)
Deserialization.
Definition: gz/sim/components/Serialization.hh:153
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:207
Serializer for components that hold std::vector<double>.
Definition: gz/sim/components/Serialization.hh:105
static std::ostream & Serialize(std::ostream &_out, const std::vector< double > &_vec)
Serialization.
Definition: gz/sim/components/Serialization.hh:111
static std::ostream & Serialize(std::ostream &_out, const std::string &_data)
Serialization.
Definition: gz/sim/components/Serialization.hh:168
STL class.
static std::ostream & Serialize(std::ostream &_out, const google::protobuf::Message &_msg)
Serialization.
Definition: gz/sim/components/Serialization.hh:142
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:72
static std::istream & Deserialize(std::istream &_in, std::string &_data)
Deserialization.
Definition: gz/sim/components/Serialization.hh:179
T end(T... args)
Serializer for components that hold protobuf messages.
Definition: gz/sim/components/Serialization.hh:136
STL class.
T data(T... args)
static std::ostream & Serialize(std::ostream &_out, const DataType &_data)
Serialization.
Definition: gz/sim/components/Serialization.hh:78