Go to the documentation of this file.00001
00009
00010
00011
00012
00013 #ifndef ROBOT_DATA_HPP_
00014 #define ROBOT_DATA_HPP_
00015
00016
00017
00018
00019
00020 #include <ecl/containers.hpp>
00021 #include <stdint.h>
00022
00023
00024
00025
00026
00027 namespace packet_handler
00028 {
00029
00030
00031
00032
00038 class payloadBase
00039 {
00040 public:
00041
00046 bool yes;
00047
00048
00049
00050
00051 payloadBase() : yes(false) {};
00052 virtual ~payloadBase() {};
00053
00054
00055
00056
00057 virtual bool serialise(ecl::PushAndPop<unsigned char> & byteStream)=0;
00058 virtual bool deserialise(ecl::PushAndPop<unsigned char> & byteStream)=0;
00059
00060
00061
00062 protected:
00063
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
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166 }
00167 ;
00168
00169
00170 #endif