payload_base.hpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10  ** Ifdefs
11  *****************************************************************************/
12 
13 #ifndef ROBOT_DATA_HPP_
14 #define ROBOT_DATA_HPP_
15 
16 /*****************************************************************************
17  ** Includes
18  *****************************************************************************/
19 
20 #include <ecl/containers.hpp>
21 #include <stdint.h>
22 
23 /*****************************************************************************
24  ** Namespaces
25  *****************************************************************************/
26 
27 namespace packet_handler
28 {
29 
30 /*****************************************************************************
31  ** Interface
32  *****************************************************************************/
39 {
40 public:
41 
46  bool yes;
47 
52  const bool is_dynamic;
53 
59  const unsigned char length;
60 
61  /*
62  * construct and destruct
63  */
64  payloadBase(const bool is_dynamic_ = false, const unsigned char length_ = 0 )
65  : yes(false)
66  , is_dynamic(is_dynamic_)
67  , length(length_)
68  {};
69  virtual ~payloadBase() {};
70 
71  /*
72  * serialisation
73  */
74  virtual bool serialise(ecl::PushAndPop<unsigned char> & byteStream)=0;
75  virtual bool deserialise(ecl::PushAndPop<unsigned char> & byteStream)=0;
76 
77  // utilities
78  // todo; let's put more useful converters here. Or we may use generic converters
79 protected:
80  // below funciton should be replaced wiht converter
81  template<typename T>
83  {
84  if (buffer.size() < sizeof(T))
85  return;
86  V = static_cast<unsigned char>(buffer.pop_front());
87 
88  unsigned int size_value(sizeof(T));
89  for (unsigned int i = 1; i < size_value; i++)
90  {
91  V |= ((static_cast<unsigned char>(buffer.pop_front())) << (8 * i));
92  }
93  }
94 
95  template<typename T>
96  void buildBytes(const T & V, ecl::PushAndPop<unsigned char> & buffer)
97  {
98  unsigned int size_value(sizeof(T));
99  for (unsigned int i = 0; i < size_value; i++)
100  {
101  buffer.push_back(static_cast<unsigned char>((V >> (i * 8)) & 0xff));
102  }
103  }
104 };
105 
112 template<>
113 inline void payloadBase::buildVariable<float>(float & V, ecl::PushAndPop<unsigned char> & buffer)
114  {
115  if (buffer.size() < 4)
116  return;
117  unsigned int ui;
118  ui = static_cast<unsigned char>(buffer.pop_front());
119 
120  unsigned int size_value(4);
121  for (unsigned int i = 1; i < size_value; i++)
122  {
123  ui |= ((static_cast<unsigned char>(buffer.pop_front())) << (8 * i));
124  }
125 
126  V = reinterpret_cast<float&>(ui);
127  }
128 
129 template<>
130 inline void payloadBase::buildBytes<float>(const float & V, ecl::PushAndPop<unsigned char> & buffer)
131  {
132  if (buffer.size() < 4)
133  return;
134  unsigned int size_value(4);
135  unsigned int ui(reinterpret_cast<const unsigned int&>(V));
136  for (unsigned int i = 0; i < size_value; i++)
137  {
138  buffer.push_back(static_cast<unsigned char>((ui >> (i * 8)) & 0xff));
139  }
140  }
141 
142 //#define FRAC_MAX 9223372036854775807LL /* 2**63 - 1 */
143 //
144 //struct dbl_packed
145 //{
146 // int exp;
147 // long long frac;
148 //};
149 //
150 //void pack(double x, struct dbl_packed *r)
151 //{
152 // double xf = fabs(frexp(x, &r->exp)) - 0.5;
153 //
154 // if (xf < 0.0)
155 // {
156 // r->frac = 0;
157 // return;
158 // }
159 //
160 // r->frac = 1 + (long long)(xf * 2.0 * (FRAC_MAX - 1));
161 //
162 // if (x < 0.0)
163 // r->frac = -r->frac;
164 //}
165 //
166 //double unpack(const struct dbl_packed *p)
167 //{
168 // double xf, x;
169 //
170 // if (p->frac == 0)
171 // return 0.0;
172 //
173 // xf = ((double)(llabs(p->frac) - 1) / (FRAC_MAX - 1)) / 2.0;
174 //
175 // x = ldexp(xf + 0.5, p->exp);
176 //
177 // if (p->frac < 0)
178 // x = -x;
179 //
180 // return x;
181 //}
182 
183 }
184 ;
185 // namespace packet_handler
186 
187 #endif /* ROBOT_DATA_HPP_ */
unsigned int size() const
const unsigned char length
virtual bool deserialise(ecl::PushAndPop< unsigned char > &byteStream)=0
Provides base class for payloads.
void push_back(const Type &datum)
virtual bool serialise(ecl::PushAndPop< unsigned char > &byteStream)=0
payloadBase(const bool is_dynamic_=false, const unsigned char length_=0)
void buildVariable(T &V, ecl::PushAndPop< unsigned char > &buffer)
void buildBytes(const T &V, ecl::PushAndPop< unsigned char > &buffer)


kobuki_driver
Author(s): Daniel Stonier , Younghun Ju , Jorge Santos Simon
autogenerated on Fri Sep 18 2020 03:22:02