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
00052 const bool is_dynamic;
00053
00059 const unsigned char length;
00060
00061
00062
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
00073
00074 virtual bool serialise(ecl::PushAndPop<unsigned char> & byteStream)=0;
00075 virtual bool deserialise(ecl::PushAndPop<unsigned char> & byteStream)=0;
00076
00077
00078
00079 protected:
00080
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
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
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 }
00184 ;
00185
00186
00187 #endif