serialization.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2012, 2013 Instituto de Sistemas e Robotica, Instituto Superior Tecnico
00003  *
00004  * This file is part of SocRob Multicast.
00005  *
00006  * SocRob Multicast is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation, either version 3 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * SocRob Multicast is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with SocRob Multicast.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 #ifndef _SOCROB_MULTICAST_SERIALIZATION_H_
00021 #define _SOCROB_MULTICAST_SERIALIZATION_H_
00022 
00023 #include <cstddef>
00024 #include <stdint.h>
00025 
00026 #include <vector>
00027 
00028 #include <ros/console.h>
00029 #include <ros/serialization.h>
00030 
00031 
00032 
00033 namespace socrob
00034 {
00035   namespace multicast
00036   {
00046     template<typename T>
00047     void
00048     serialize_append (std::vector<uint8_t> & out,
00049                       T const& msg)
00050     {
00051       std::size_t out_offset = out.size();
00052       std::size_t msg_size = ros::serialization::serializationLength (msg);
00053       out.resize (out_offset + msg_size);
00054       ros::serialization::OStream stream (& (out[out_offset]), msg_size);
00055       ros::serialization::serialize (stream, msg);
00056     }
00057     
00067     template<typename T>
00068     void
00069     serialize_overwrite (std::vector<uint8_t> & out,
00070                          T const& msg)
00071     {
00072       std::size_t msg_size = ros::serialization::serializationLength (msg);
00073       out.resize (msg_size);
00074       ros::serialization::OStream stream (& (out[0]), msg_size);
00075       ros::serialization::serialize (stream, msg);
00076     }
00077     
00095     template<typename T>
00096     std::size_t
00097     deserialize (T& msg,
00098                  std::vector<uint8_t> & in,
00099                  std::size_t offset = 0)
00100     {
00101       if (offset >= in.size()) {
00102         ROS_FATAL ("deserialize called with offset after the end of input");
00103         abort();
00104       }
00105       ros::serialization::IStream stream (& (in[offset]), in.size() - offset);
00106       ros::serialization::deserialize (stream, msg);
00107       return ros::serialization::serializationLength (msg);
00108     }
00109   }
00110 }
00111 
00112 #endif


socrob_multicast
Author(s): Joao Reis/jreis@isr.ist.utl.pt
autogenerated on Mon Jan 6 2014 11:47:49