serialize.h
Go to the documentation of this file.
1 
23 #ifndef SERIALIZE_H_
24 #define SERIALIZE_H_
25 
26 #include <iostream>
27 #include <fstream>
28 #include <sstream>
29 #include <ros/ros.h>
30 #include <boost/archive/text_oarchive.hpp>
31 #include <boost/archive/text_iarchive.hpp>
32 #include <boost/serialization/string.hpp>
33 #include <boost/serialization/vector.hpp>
34 
35 namespace micros_swarm{
36 
37  // serializer using ROS.
38  template<class T>
39  std::vector<uint8_t> serialize_ros(T t)
40  {
41  std::vector<uint8_t> vec;
42  uint32_t serial_size = ros::serialization::serializationLength(t);
43  boost::shared_array<uint8_t> buffer(new uint8_t[serial_size]);
44  ros::serialization::OStream ostream(buffer.get(), serial_size);
46  vec.resize(serial_size);
47  std::copy(buffer.get(), buffer.get() + serial_size, vec.begin());
48  return vec;
49  }
50  // deserializer using ROS.
51  template<class T>
52  T deserialize_ros(const std::vector<uint8_t>& vec)
53  {
54  T t;
55  uint32_t serial_size = vec.size();
56  std::vector<uint8_t> buffer(serial_size);
57  std::copy(vec.begin(), vec.begin() + serial_size, buffer.begin());
58  ros::serialization::IStream istream(buffer.data(), serial_size);
60  return t;
61  }
62 
63  // serializer using Boost.
64  template<class T>
65  std::string serialize_boost(T t)
66  {
67  std::ostringstream archiveStream;
68  boost::archive::text_oarchive archive(archiveStream);
69  archive<<t;
70  return archiveStream.str();
71  }
72  // deserializer using Boost.
73  template<class T>
74  T deserialize_boost(std::string str)
75  {
76  T t;
77  std::istringstream archiveStream(str);
78  boost::archive::text_iarchive archive(archiveStream);
79  archive>>t;
80  return t;
81  }
82 };
83 #endif
T deserialize_ros(const std::vector< uint8_t > &vec)
Definition: serialize.h:52
std::string serialize_boost(T t)
Definition: serialize.h:65
T deserialize_boost(std::string str)
Definition: serialize.h:74
std::vector< uint8_t > serialize_ros(T t)
Definition: serialize.h:39
void serialize(Stream &stream, const T &t)
static void read(Stream &stream, typename boost::call_traits< T >::reference t)
uint32_t serializationLength(const T &t)


micros_swarm
Author(s):
autogenerated on Mon Jun 10 2019 14:02:06