to_byte_array.hpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9  ** Ifdefs
10  *****************************************************************************/
11 
12 #ifndef ECL_CONVERTERS_TO_BYTE_ARRAY_HPP_
13 #define ECL_CONVERTERS_TO_BYTE_ARRAY_HPP_
14 
15 /*****************************************************************************
16  ** Includes
17  *****************************************************************************/
18 
19 #include <vector>
20 #include <ecl/concepts/containers.hpp>
21 #include <ecl/errors/compile_time_assert.hpp>
22 #include <ecl/exceptions/standard_exception.hpp>
23 #include <ecl/exceptions/macros.hpp>
24 #include <ecl/type_traits/fundamental_types.hpp>
25 #include <ecl/type_traits/numeric_limits.hpp>
26 #include "converter.hpp"
27 #include <iostream>
28 /*****************************************************************************
29  ** Namespaces
30  *****************************************************************************/
31 
32 namespace ecl
33 {
34 
38 namespace converters
39 {
40 
41 /*****************************************************************************
42  ** Interface
43  *****************************************************************************/
44 
55 template<typename ByteArray, typename Integral>
56 class IntegralToByteArray : public ConverterBase
57 {
58 public:
78  const ByteArray& operator()(ByteArray &byte_array, const Integral &input) ecl_debug_throw_decl(ecl::StandardException)
79  {
80  /*********************
81  ** Checks
82  **********************/
86  /*********************
87  ** Configuration
88  **********************/
89  if (byte_array.size() != ecl::numeric_limits<Integral>::bytes)
90  {
91  std::cout << "Failed size check" << std::endl;
93  StandardException(LOC,ConversionError,"The specified byte array size does not match the byte size of the integral type."));
94  error_handler = ConversionError;
95  return byte_array;
96  }
97 
98  /*********************
99  ** Method
100  **********************/
101  for (unsigned int i = 0; i < ecl::numeric_limits<Integral>::bytes; ++i)
102  {
103  byte_array[i] = static_cast<typename ByteArray::value_type>(input >> 8 * i);
104 // Einstein was bit 'AND'ing a 0xff for dslam - is this useful?
105 // byte_array[i] = static_cast<typename ByteArray::value_type>( input >> 8*i & 0xff);
106  }
107  return byte_array;
108  }
112  virtual ~IntegralToByteArray() {}
113 };
114 
115 } // namespace converters
120 /*****************************************************************************
121  ** Integral to Byte Array Converters
122  *****************************************************************************/
126 template<>
127 class Converter<std::vector<char>, int> : public converters::IntegralToByteArray<std::vector<char>, int>
128 {
129 };
130 
134 template<>
135 class Converter<std::vector<unsigned char>, int> : public converters::IntegralToByteArray<std::vector<unsigned char>,
136  int>
137 {
138 };
139 
143 template<>
144 class Converter<std::vector<signed char>, int> : public converters::IntegralToByteArray<std::vector<signed char>, int>
145 {
146 };
147 
151 template<>
152 class Converter<std::vector<char>, unsigned int> : public converters::IntegralToByteArray<std::vector<char>,
153  unsigned int>
154 {
155 };
156 
160 template<>
161 class Converter<std::vector<unsigned char>, unsigned int> : public converters::IntegralToByteArray<
162  std::vector<unsigned char>, unsigned int>
163 {
164 };
165 
169 template<>
170 class Converter<std::vector<signed char>, unsigned int> : public converters::IntegralToByteArray<
171  std::vector<signed char>, unsigned int>
172 {
173 };
174 
178 template<>
179 class Converter<std::vector<char>, long> : public converters::IntegralToByteArray<std::vector<char>, long>
180 {
181 };
182 
186 template<>
187 class Converter<std::vector<unsigned char>, long> : public converters::IntegralToByteArray<std::vector<unsigned char>,
188  long>
189 {
190 };
191 
195 template<>
196 class Converter<std::vector<signed char>, long> : public converters::IntegralToByteArray<std::vector<signed char>, long>
197 {
198 };
199 
203 template<>
204 class Converter<std::vector<char>, unsigned long> : public converters::IntegralToByteArray<std::vector<char>,
205  unsigned long>
206 {
207 };
208 
212 template<>
213 class Converter<std::vector<unsigned char>, unsigned long> : public converters::IntegralToByteArray<
214  std::vector<unsigned char>, unsigned long>
215 {
216 };
217 
221 template<>
222 class Converter<std::vector<signed char>, unsigned long> : public converters::IntegralToByteArray<
223  std::vector<signed char>, unsigned long>
224 {
225 };
226 
230 template<>
231 class Converter<std::vector<char>, long long> : public converters::IntegralToByteArray<std::vector<char>, long long>
232 {
233 };
234 
238 template<>
239 class Converter<std::vector<unsigned char>, long long> : public converters::IntegralToByteArray<std::vector<unsigned char>,
240 long long>
241 {
242 };
243 
247 template<>
248 class Converter<std::vector<signed char>, long long> : public converters::IntegralToByteArray<std::vector<signed char>, long long>
249 {
250 };
251 
252 /*****************************************************************************
253  ** Byte Converter Interface
254  *****************************************************************************/
262 template<>
263 class Converter<std::vector<char>, char*> : public converters::ConverterBase
264 {
265 public:
283  std::vector<char> operator ()(const char* input) ecl_debug_throw_decl(StandardException)
284  {
285 
286  std::vector<char> bytes;
287  state = waiting_zero;
288 
289  while (*input != '\0')
290  {
291  switch (state)
292  {
293  case (waiting_zero):
294  {
295  if (*input == '0')
296  {
297  state = waiting_x;
298  }
299  else if (*input == ' ')
300  {
301  // ignore whitespace
302  }
303  else
304  {
305  ecl_debug_throw(StandardException(LOC,ConversionError));
306  error_handler = ConversionError;
307  bytes.clear();
308  return bytes;
309  }
310  break;
311  }
312  case (waiting_x):
313  {
314  if (*input == 'x')
315  {
316  state = waiting_first_digit;
317  }
318  else
319  {
320  ecl_debug_throw(StandardException(LOC,ConversionError));
321  error_handler = ConversionError;
322  bytes.clear();
323  return bytes;
324  }
325  break;
326  }
327  case (waiting_first_digit):
328  {
329  if ((*input >= '0') && (*input <= '9'))
330  {
331  byte = (*input - '0') << 4;
332  state = waiting_second_digit;
333  }
334  else if ((*input >= 'a') && (*input <= 'f'))
335  {
336  byte = ((*input - 'a') + 0x0A) << 4;
337  state = waiting_second_digit;
338  }
339  else if ((*input >= 'A') && (*input <= 'F'))
340  {
341  byte = ((*input - 'A') + 0x0A) << 4;
342  state = waiting_second_digit;
343  }
344  else
345  {
346  ecl_debug_throw(StandardException(LOC,ConversionError));
347  error_handler = ConversionError;
348  bytes.clear();
349  return bytes;
350  }
351  break;
352  }
353  case (waiting_second_digit):
354  {
355  if ((*input >= '0') && (*input <= '9'))
356  {
357  byte += *input - '0';
358  bytes.push_back(byte);
359  }
360  else if ((*input >= 'a') && (*input <= 'f'))
361  {
362  byte += (*input - 'a') + 0x0A;
363  bytes.push_back(byte);
364  }
365  else if ((*input >= 'A') && (*input <= 'F'))
366  {
367  byte += (*input - 'A') + 0x0A;
368  bytes.push_back(byte);
369  }
370  else
371  {
372  ecl_debug_throw(StandardException(LOC,ConversionError));
373  error_handler = ConversionError;
374  bytes.clear();
375  return bytes;
376  }
377  state = waiting_zero;
378  break;
379  }
380  default:
381  {
382  state = waiting_zero;
383  break;
384  }
385  }
386  ++input;
387  }
388  return bytes;
389  }
390  ;
391  virtual ~Converter()
392  {
393  }
394 
395 private:
396  unsigned int state;
397  char byte;
398  static const unsigned int waiting_zero = 0;
399  static const unsigned int waiting_x = 1;
400  static const unsigned int waiting_first_digit = 2;
401  static const unsigned int waiting_second_digit = 3;
402 };
403 
404 /*****************************************************************************
405  * Byte Array Converter Family
406  ****************************************************************************/
410 template<>
411 class Converter<std::vector<char>, void> : public Converter<std::vector<char>, char*>, public Converter<
412  std::vector<char>, int>,
413  public Converter<std::vector<char>, unsigned int>, public Converter<
414  std::vector<char>, long>,
415  public Converter<std::vector<char>, unsigned long>
416 {
417 public:
418  virtual ~Converter()
419  {
420  }
421 
422  using Converter<std::vector<char>, char*>::operator();
423  using Converter<std::vector<char>, int>::operator();
424  using Converter<std::vector<char>, unsigned int>::operator();
425  using Converter<std::vector<char>, long>::operator();
426  using Converter<std::vector<char>, unsigned long>::operator();
427 };
428 
432 template<>
433 class Converter<std::vector<unsigned char>, void> : public Converter<std::vector<unsigned char>, int>,
434  public Converter<std::vector<unsigned char>, unsigned int>,
435  public Converter<std::vector<unsigned char>, long>,
436  public Converter<std::vector<unsigned char>, unsigned long>
437 {
438 public:
439  virtual ~Converter()
440  {
441  }
442 
443  using Converter<std::vector<unsigned char>, int>::operator();
444  using Converter<std::vector<unsigned char>, unsigned int>::operator();
445  using Converter<std::vector<unsigned char>, long>::operator();
446  using Converter<std::vector<unsigned char>, unsigned long>::operator();
447 };
448 
452 template<>
453 class Converter<std::vector<signed char>, void> : public Converter<std::vector<signed char>, int>,
454  public Converter<std::vector<signed char>, unsigned int>,
455  public Converter<std::vector<signed char>, long>,
456  public Converter<std::vector<signed char>, unsigned long>
457 {
458 public:
459  virtual ~Converter()
460  {
461  }
462 
463  using Converter<std::vector<signed char>, int>::operator();
464  using Converter<std::vector<signed char>, unsigned int>::operator();
465  using Converter<std::vector<signed char>, long>::operator();
466  using Converter<std::vector<signed char>, unsigned long>::operator();
467 };
468 
469 } // namespace ecl
470 
471 #endif /* ECL_CONVERTERS_TO_BYTE_ARRAY_HPP_ */
#define ecl_compile_time_assert(logical_expression)
#define LOC
Stringify the line of code you are at.
ConversionError
Defines validating functionality for the byte container concept.
EndOfLine endl
Standard exception type, provides code location and error string.
Expands the std numeric_limits class.
static bool const value
The value for this integral constant wrapper.
Definition: bool.hpp:34
Array< char, 3 > ByteArray
#define ecl_compile_time_concept_check(Model)
Compile time concept checking assertion.
#define ecl_debug_throw_decl(exception)
Debug mode throw exception declaration.
#define ecl_debug_throw(exception)
Debug mode exception throw.
Defines validating functionality for the container concept.


xbot_driver
Author(s): Roc, wangpeng@droid.ac.cn
autogenerated on Sat Oct 10 2020 03:27:38