from_byte_array.hpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9  ** Ifdefs
10  *****************************************************************************/
11 
12 #ifndef ECL_CONVERTERS_CONVERTERS_FROM_BYTE_ARRAY_HPP_
13 #define ECL_CONVERTERS_CONVERTERS_FROM_BYTE_ARRAY_HPP_
14 
15 /*****************************************************************************
16  ** Includes
17  *****************************************************************************/
18 
19 #include <vector>
20 #include <ecl/config/macros.hpp>
21 #include <ecl/concepts/containers.hpp>
22 #include <ecl/errors/compile_time_assert.hpp>
23 #include <ecl/exceptions/standard_exception.hpp>
24 #include <ecl/mpl/converters.hpp>
25 #include <ecl/type_traits/fundamental_types.hpp>
26 #include <ecl/type_traits/numeric_limits.hpp>
27 #include "converter.hpp"
28 
29 /*****************************************************************************
30  ** Namespaces
31  *****************************************************************************/
32 
33 namespace ecl
34 {
38 namespace converters
39 {
40 
41 /*****************************************************************************
42  ** Interface
43  *****************************************************************************/
44 
55 template<typename Integral, typename ByteArray>
56 class ECL_PUBLIC FromByteArray : public ConverterBase
57 {
58 public:
76  Integral operator()(const ByteArray &byte_array) ecl_debug_throw_decl(ecl::StandardException)
77  {
78  /*********************
79  ** Checks
80  **********************/
81  ecl_compile_time_concept_check(ByteContainerConcept<ByteArray>);
83  if (byte_array.size() > ecl::numeric_limits<Integral>::bytes)
84  {
86  StandardException(LOC,ConversionError,"The byte array is too long for the integral type specified."));
87  error_handler = ConversionError;
88  }
89  /*********************
90  ** Method
91  **********************/
92  typename ecl::Unsigned<Integral>::type unsigned_value = 0;
93  for (unsigned int i = 0; i < byte_array.size(); ++i)
94  {
95  unsigned_value |= static_cast<unsigned char>(byte_array[i]) << 8 * i;
96  }
98  {
99  Integral value = static_cast<Integral>(unsigned_value);
100  return value;
101  }
102  else
103  {
104  return unsigned_value;
105  }
106  }
110  virtual ~FromByteArray() {}
111 };
112 
113 } // namespace converters
114 
122 template<typename Integral>
123 class ECL_PUBLIC Converter<Integral, std::vector<char> > :
124  public converters::FromByteArray<Integral, std::vector<char> >
125 {
126 };
130 template<typename Integral>
131 class ECL_PUBLIC Converter<Integral, std::vector<unsigned char> > : public converters::FromByteArray<Integral,
132  std::vector<unsigned char> >
133 {
134 };
138 template<typename Integral>
139 class ECL_PUBLIC Converter<Integral, std::vector<signed char> > : public converters::FromByteArray<Integral,
140  std::vector<signed char> >
141 {
142 };
143 
144 } // namespace ecl
145 
146 #endif /* ECL_CONVERTERS_CONVERTERS_FROM_BYTE_ARRAY_HPP_ */
Primary template for signed to unsigned type metafunctions.
#define ecl_compile_time_assert(logical_expression)
#define LOC
Stringify the line of code you are at.
ConversionError
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_PUBLIC
const GenericPointer< typename T::ValueType > T2 value
#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.
Default action for detection of a signed integral type (false).


xbot_node
Author(s): Roc, wangpeng@droid.ac.cn
autogenerated on Sat Oct 10 2020 03:28:13