Ignition Gazebo

API Reference

3.0.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>
21 #include <ignition/msgs/double_v.pb.h>
22 
23 #include <vector>
24 #include <sdf/Sensor.hh>
25 
27 
28 // This header holds serialization operators which are shared among several
29 // components
30 
31 namespace ignition
32 {
33 namespace gazebo
34 {
35 // Inline bracket to help doxygen filtering.
36 inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
55 
56 namespace serializers
57 {
70  template <typename DataType, typename MsgType>
72  {
77  public: static std::ostream &Serialize(std::ostream &_out,
78  const DataType &_data)
79  {
80  auto msg = ignition::gazebo::convert<MsgType>(_data);
81  msg.SerializeToOstream(&_out);
82  return _out;
83  }
84 
89  public: static std::istream &Deserialize(std::istream &_in,
90  DataType &_data)
91  {
92  MsgType msg;
93  msg.ParseFromIstream(&_in);
94 
95  _data = ignition::gazebo::convert<DataType>(msg);
96  return _in;
97  }
98  };
99 
102 
105  {
110  public: static std::ostream &Serialize(std::ostream &_out,
111  const std::vector<double> &_vec)
112  {
113  ignition::msgs::Double_V msg;
114  *msg.mutable_data() = {_vec.begin(), _vec.end()};
115  msg.SerializeToOstream(&_out);
116  return _out;
117  }
118 
123  public: static std::istream &Deserialize(std::istream &_in,
124  std::vector<double> &_vec)
125  {
126  ignition::msgs::Double_V msg;
127  msg.ParseFromIstream(&_in);
128 
129  _vec = {msg.data().begin(), msg.data().end()};
130  return _in;
131  }
132  };
133 
136  {
141  public: static std::ostream &Serialize(std::ostream &_out,
142  const google::protobuf::Message &_msg)
143  {
144  _msg.SerializeToOstream(&_out);
145  return _out;
146  }
147 
152  public: static std::istream &Deserialize(std::istream &_in,
153  google::protobuf::Message &_msg)
154  {
155  _msg.ParseFromIstream(&_in);
156  return _in;
157  }
158  };
159 }
160 }
161 }
162 }
163 
164 #endif
static std::istream & Deserialize(std::istream &_in, DataType &_data)
Deserialization.
Definition: Serialization.hh:89
static std::istream & Deserialize(std::istream &_in, std::vector< double > &_vec)
Deserialization.
Definition: Serialization.hh:123
Serializer for components that hold protobuf messages.
Definition: Serialization.hh:135
static std::ostream & Serialize(std::ostream &_out, const std::vector< double > &_vec)
Serialization.
Definition: Serialization.hh:110
T end(T... args)
STL class.
static std::ostream & Serialize(std::ostream &_out, const google::protobuf::Message &_msg)
Serialization.
Definition: Serialization.hh:141
T data(T... args)
static std::ostream & Serialize(std::ostream &_out, const DataType &_data)
Serialization.
Definition: Serialization.hh:77
static std::istream & Deserialize(std::istream &_in, google::protobuf::Message &_msg)
Deserialization.
Definition: Serialization.hh:152
T begin(T... args)
Serialization for that converts components data types to ignition::msgs. This assumes that convert<Da...
Definition: Serialization.hh:71
This library is part of the Ignition Robotics project.
STL class.
Serializer for components that hold std::vector<double>.
Definition: Serialization.hh:104