Go to the documentation of this file.00001
00023 #ifndef SERIALIZE_H_
00024 #define SERIALIZE_H_
00025
00026 #include <iostream>
00027 #include <fstream>
00028 #include <sstream>
00029 #include <ros/ros.h>
00030 #include <boost/archive/text_oarchive.hpp>
00031 #include <boost/archive/text_iarchive.hpp>
00032 #include <boost/serialization/string.hpp>
00033 #include <boost/serialization/vector.hpp>
00034
00035 namespace micros_swarm{
00036
00037
00038 template<class T>
00039 std::vector<uint8_t> serialize_ros(T t)
00040 {
00041 std::vector<uint8_t> vec;
00042 uint32_t serial_size = ros::serialization::serializationLength(t);
00043 boost::shared_array<uint8_t> buffer(new uint8_t[serial_size]);
00044 ros::serialization::OStream ostream(buffer.get(), serial_size);
00045 ros::serialization::serialize(ostream, t);
00046 vec.resize(serial_size);
00047 std::copy(buffer.get(), buffer.get() + serial_size, vec.begin());
00048 return vec;
00049 }
00050
00051 template<class T>
00052 T deserialize_ros(const std::vector<uint8_t>& vec)
00053 {
00054 T t;
00055 uint32_t serial_size = vec.size();
00056 std::vector<uint8_t> buffer(serial_size);
00057 std::copy(vec.begin(), vec.begin() + serial_size, buffer.begin());
00058 ros::serialization::IStream istream(buffer.data(), serial_size);
00059 ros::serialization::Serializer<T>::read(istream, t);
00060 return t;
00061 }
00062
00063
00064 template<class T>
00065 std::string serialize_boost(T t)
00066 {
00067 std::ostringstream archiveStream;
00068 boost::archive::text_oarchive archive(archiveStream);
00069 archive<<t;
00070 return archiveStream.str();
00071 }
00072
00073 template<class T>
00074 T deserialize_boost(std::string str)
00075 {
00076 T t;
00077 std::istringstream archiveStream(str);
00078 boost::archive::text_iarchive archive(archiveStream);
00079 archive>>t;
00080 return t;
00081 }
00082 };
00083 #endif