12 #ifndef ECL_CONVERTERS_TO_BYTE_ARRAY_HPP_ 13 #define ECL_CONVERTERS_TO_BYTE_ARRAY_HPP_ 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> 55 template<
typename ByteArray,
typename Integral>
56 class IntegralToByteArray :
public ConverterBase
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."));
101 for (
unsigned int i = 0; i < ecl::numeric_limits<Integral>::bytes; ++i)
103 byte_array[i] =
static_cast<typename ByteArray::value_type
>(input >> 8 * i);
112 virtual ~IntegralToByteArray() {}
127 class Converter<
std::vector<char>, int> :
public converters::IntegralToByteArray<std::vector<char>, int>
135 class Converter<std::vector<unsigned char>, int> :
public converters::IntegralToByteArray<std::vector<unsigned char>,
144 class Converter<std::vector<signed char>, int> :
public converters::IntegralToByteArray<std::vector<signed char>, int>
152 class Converter<std::vector<char>, unsigned int> :
public converters::IntegralToByteArray<std::vector<char>,
161 class Converter<std::vector<unsigned char>, unsigned int> :
public converters::IntegralToByteArray<
162 std::vector<unsigned char>, unsigned int>
170 class Converter<std::vector<signed char>, unsigned int> :
public converters::IntegralToByteArray<
171 std::vector<signed char>, unsigned int>
179 class Converter<std::vector<char>, long> :
public converters::IntegralToByteArray<std::vector<char>, long>
187 class Converter<std::vector<unsigned char>, long> :
public converters::IntegralToByteArray<std::vector<unsigned char>,
196 class Converter<std::vector<signed char>, long> :
public converters::IntegralToByteArray<std::vector<signed char>, long>
204 class Converter<std::vector<char>, unsigned long> :
public converters::IntegralToByteArray<std::vector<char>,
213 class Converter<std::vector<unsigned char>, unsigned long> :
public converters::IntegralToByteArray<
214 std::vector<unsigned char>, unsigned long>
222 class Converter<std::vector<signed char>, unsigned long> :
public converters::IntegralToByteArray<
223 std::vector<signed char>, unsigned long>
231 class Converter<std::vector<char>, long long> :
public converters::IntegralToByteArray<std::vector<char>, long long>
239 class Converter<std::vector<unsigned char>, long long> :
public converters::IntegralToByteArray<std::vector<unsigned char>,
248 class Converter<std::vector<signed char>, long long> :
public converters::IntegralToByteArray<std::vector<signed char>, long long>
263 class Converter<std::vector<char>, char*> :
public converters::ConverterBase
286 std::vector<char> bytes;
287 state = waiting_zero;
289 while (*input !=
'\0')
299 else if (*input ==
' ')
316 state = waiting_first_digit;
327 case (waiting_first_digit):
329 if ((*input >=
'0') && (*input <=
'9'))
331 byte = (*input -
'0') << 4;
332 state = waiting_second_digit;
334 else if ((*input >=
'a') && (*input <=
'f'))
336 byte = ((*input -
'a') + 0x0A) << 4;
337 state = waiting_second_digit;
339 else if ((*input >=
'A') && (*input <=
'F'))
341 byte = ((*input -
'A') + 0x0A) << 4;
342 state = waiting_second_digit;
353 case (waiting_second_digit):
355 if ((*input >=
'0') && (*input <=
'9'))
357 byte += *input -
'0';
358 bytes.push_back(byte);
360 else if ((*input >=
'a') && (*input <=
'f'))
362 byte += (*input -
'a') + 0x0A;
363 bytes.push_back(byte);
365 else if ((*input >=
'A') && (*input <=
'F'))
367 byte += (*input -
'A') + 0x0A;
368 bytes.push_back(byte);
377 state = waiting_zero;
382 state = waiting_zero;
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;
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>
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();
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>
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();
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>
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();
#define ecl_compile_time_assert(logical_expression)
#define LOC
Stringify the line of code you are at.
Defines validating functionality for the byte container concept.
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.
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.