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 
00048   /*
00049    * construct and destruct
00050    */
00051   payloadBase() : yes(false) {};
00052   virtual ~payloadBase() {};
00053 
00054   /*
00055    * serialisation
00056    */
00057   virtual bool serialise(ecl::PushAndPop<unsigned char> & byteStream)=0;
00058   virtual bool deserialise(ecl::PushAndPop<unsigned char> & byteStream)=0;
00059 
00060   // utilities
00061   // todo; let's put more useful converters here. Or we may use generic converters
00062 protected:
00063   // below funciton should be replaced wiht converter
00064   template<typename T>
00065     void buildVariable(T & V, ecl::PushAndPop<unsigned char> & buffer)
00066     {
00067       if (buffer.size() < sizeof(T))
00068         return;
00069       V = static_cast<unsigned char>(buffer.pop_front());
00070 
00071       unsigned int size_value(sizeof(T));
00072       for (unsigned int i = 1; i < size_value; i++)
00073       {
00074         V |= ((static_cast<unsigned char>(buffer.pop_front())) << (8 * i));
00075       }
00076     }
00077 
00078   template<typename T>
00079     void buildBytes(const T & V, ecl::PushAndPop<unsigned char> & buffer)
00080     {
00081       unsigned int size_value(sizeof(T));
00082       for (unsigned int i = 0; i < size_value; i++)
00083       {
00084         buffer.push_back(static_cast<unsigned char>((V >> (i * 8)) & 0xff));
00085       }
00086     }
00087 };
00088 
00095 template<>
00096 inline   void payloadBase::buildVariable<float>(float & V, ecl::PushAndPop<unsigned char> & buffer)
00097   {
00098     if (buffer.size() < 4)
00099       return;
00100     unsigned int ui;
00101     ui = static_cast<unsigned char>(buffer.pop_front());
00102 
00103     unsigned int size_value(4);
00104     for (unsigned int i = 1; i < size_value; i++)
00105     {
00106       ui |= ((static_cast<unsigned char>(buffer.pop_front())) << (8 * i));
00107     }
00108 
00109     V = reinterpret_cast<float&>(ui);
00110   }
00111 
00112 template<>
00113 inline void payloadBase::buildBytes<float>(const float & V, ecl::PushAndPop<unsigned char> & buffer)
00114   {
00115     if (buffer.size() < 4)
00116       return;
00117     unsigned int size_value(4);
00118     unsigned int ui(reinterpret_cast<const unsigned int&>(V));
00119     for (unsigned int i = 0; i < size_value; i++)
00120     {
00121       buffer.push_back(static_cast<unsigned char>((ui >> (i * 8)) & 0xff));
00122     }
00123   }
00124 
00125 //#define FRAC_MAX 9223372036854775807LL /* 2**63 - 1 */
00126 //
00127 //struct dbl_packed
00128 //{
00129 //    int exp;
00130 //    long long frac;
00131 //};
00132 //
00133 //void pack(double x, struct dbl_packed *r)
00134 //{
00135 //    double xf = fabs(frexp(x, &r->exp)) - 0.5;
00136 //
00137 //    if (xf < 0.0)
00138 //    {
00139 //        r->frac = 0;
00140 //        return;
00141 //    }
00142 //
00143 //    r->frac = 1 + (long long)(xf * 2.0 * (FRAC_MAX - 1));
00144 //
00145 //    if (x < 0.0)
00146 //        r->frac = -r->frac;
00147 //}
00148 //
00149 //double unpack(const struct dbl_packed *p)
00150 //{
00151 //    double xf, x;
00152 //
00153 //    if (p->frac == 0)
00154 //        return 0.0;
00155 //
00156 //    xf = ((double)(llabs(p->frac) - 1) / (FRAC_MAX - 1)) / 2.0;
00157 //
00158 //    x = ldexp(xf + 0.5, p->exp);
00159 //
00160 //    if (p->frac < 0)
00161 //        x = -x;
00162 //
00163 //    return x;
00164 //}
00165 
00166 }
00167 ;
00168 // namespace packet_handler
00169 
00170 #endif /* ROBOT_DATA_HPP_ */


kobuki_driver
Author(s): Daniel Stonier , Younghun Ju , Jorge Santos Simon
autogenerated on Mon Oct 6 2014 01:31:10