Ignition Gazebo

API Reference

3.5.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 
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 = ignition::gazebo::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 = ignition::gazebo::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  {
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  {
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 }
188 }
189 }
190 
191 #endif
static std::istream & Deserialize(std::istream &_in, DataType &_data)
Deserialization.
Definition: Serialization.hh:90
static std::istream & Deserialize(std::istream &_in, std::vector< double > &_vec)
Deserialization.
Definition: Serialization.hh:124
Serializer for components that hold protobuf messages.
Definition: Serialization.hh:136
::google::protobuf::RepeatedField< double > * mutable_data()
static std::ostream & Serialize(std::ostream &_out, const std::vector< double > &_vec)
Serialization.
Definition: Serialization.hh:111
T end(T... args)
STL class.
static std::ostream & Serialize(std::ostream &_out, const std::string &_data)
Serialization.
Definition: Serialization.hh:168
STL class.
static std::ostream & Serialize(std::ostream &_out, const google::protobuf::Message &_msg)
Serialization.
Definition: Serialization.hh:142
static std::ostream & Serialize(std::ostream &_out, const DataType &_data)
Serialization.
Definition: Serialization.hh:78
static std::istream & Deserialize(std::istream &_in, google::protobuf::Message &_msg)
Deserialization.
Definition: Serialization.hh:153
T begin(T... args)
double data(int index) const
Serializer for components that hold std::string.
Definition: Serialization.hh:162
Serialization for that converts components data types to ignition::msgs. This assumes that convert<Da...
Definition: Serialization.hh:72
This library is part of the Ignition Robotics project.
STL class.
Serializer for components that hold std::vector<double>.
Definition: Serialization.hh:105
static std::istream & Deserialize(std::istream &_in, std::string &_data)
Deserialization.
Definition: Serialization.hh:179