payload_base.hpp
Go to the documentation of this file.
00001 
00009 /*****************************************************************************
00010  ** Ifdefs
00011  *****************************************************************************/
00012 
00013 #ifndef ROBOT_DATA_HPP_
00014 #define ROBOT_DATA_HPP_
00015 
00016 /*****************************************************************************
00017  ** Includes
00018  *****************************************************************************/
00019 
00020 #include <ecl/containers.hpp>
00021 #include <stdint.h>
00022 
00023 /*****************************************************************************
00024  ** Namespaces
00025  *****************************************************************************/
00026 
00027 namespace packet_handler
00028 {
00029 
00030 /*****************************************************************************
00031  ** Interface
00032  *****************************************************************************/
00038 class payloadBase
00039 {
00040 public:
00041 
00046   bool yes;
00047 
00052   const bool is_dynamic;
00053 
00059   const unsigned char length;
00060 
00061   /*
00062    * construct and destruct
00063    */
00064   payloadBase(const bool is_dynamic_ = false, const unsigned char length_ = 0 )
00065     : yes(false)
00066     , is_dynamic(is_dynamic_)
00067     , length(length_)
00068   {};
00069   virtual ~payloadBase() {};
00070 
00071   /*
00072    * serialisation
00073    */
00074   virtual bool serialise(ecl::PushAndPop<unsigned char> & byteStream)=0;
00075   virtual bool deserialise(ecl::PushAndPop<unsigned char> & byteStream)=0;
00076 
00077   // utilities
00078   // todo; let's put more useful converters here. Or we may use generic converters
00079 protected:
00080   // below funciton should be replaced wiht converter
00081   template<typename T>
00082     void buildVariable(T & V, ecl::PushAndPop<unsigned char> & buffer)
00083     {
00084       if (buffer.size() < sizeof(T))
00085         return;
00086       V = static_cast<unsigned char>(buffer.pop_front());
00087 
00088       unsigned int size_value(sizeof(T));
00089       for (unsigned int i = 1; i < size_value; i++)
00090       {
00091         V |= ((static_cast<unsigned char>(buffer.pop_front())) << (8 * i));
00092       }
00093     }
00094 
00095   template<typename T>
00096     void buildBytes(const T & V, ecl::PushAndPop<unsigned char> & buffer)
00097     {
00098       unsigned int size_value(sizeof(T));
00099       for (unsigned int i = 0; i < size_value; i++)
00100       {
00101         buffer.push_back(static_cast<unsigned char>((V >> (i * 8)) & 0xff));
00102       }
00103     }
00104 };
00105 
00112 template<>
00113 inline   void payloadBase::buildVariable<float>(float & V, ecl::PushAndPop<unsigned char> & buffer)
00114   {
00115     if (buffer.size() < 4)
00116       return;
00117     unsigned int ui;
00118     ui = static_cast<unsigned char>(buffer.pop_front());
00119 
00120     unsigned int size_value(4);
00121     for (unsigned int i = 1; i < size_value; i++)
00122     {
00123       ui |= ((static_cast<unsigned char>(buffer.pop_front())) << (8 * i));
00124     }
00125 
00126     V = reinterpret_cast<float&>(ui);
00127   }
00128 
00129 template<>
00130 inline void payloadBase::buildBytes<float>(const float & V, ecl::PushAndPop<unsigned char> & buffer)
00131   {
00132     if (buffer.size() < 4)
00133       return;
00134     unsigned int size_value(4);
00135     unsigned int ui(reinterpret_cast<const unsigned int&>(V));
00136     for (unsigned int i = 0; i < size_value; i++)
00137     {
00138       buffer.push_back(static_cast<unsigned char>((ui >> (i * 8)) & 0xff));
00139     }
00140   }
00141 
00142 //#define FRAC_MAX 9223372036854775807LL /* 2**63 - 1 */
00143 //
00144 //struct dbl_packed
00145 //{
00146 //    int exp;
00147 //    long long frac;
00148 //};
00149 //
00150 //void pack(double x, struct dbl_packed *r)
00151 //{
00152 //    double xf = fabs(frexp(x, &r->exp)) - 0.5;
00153 //
00154 //    if (xf < 0.0)
00155 //    {
00156 //        r->frac = 0;
00157 //        return;
00158 //    }
00159 //
00160 //    r->frac = 1 + (long long)(xf * 2.0 * (FRAC_MAX - 1));
00161 //
00162 //    if (x < 0.0)
00163 //        r->frac = -r->frac;
00164 //}
00165 //
00166 //double unpack(const struct dbl_packed *p)
00167 //{
00168 //    double xf, x;
00169 //
00170 //    if (p->frac == 0)
00171 //        return 0.0;
00172 //
00173 //    xf = ((double)(llabs(p->frac) - 1) / (FRAC_MAX - 1)) / 2.0;
00174 //
00175 //    x = ldexp(xf + 0.5, p->exp);
00176 //
00177 //    if (p->frac < 0)
00178 //        x = -x;
00179 //
00180 //    return x;
00181 //}
00182 
00183 }
00184 ;
00185 // namespace packet_handler
00186 
00187 #endif /* ROBOT_DATA_HPP_ */


kobuki_driver
Author(s): Daniel Stonier , Younghun Ju , Jorge Santos Simon
autogenerated on Thu Aug 27 2015 13:43:58