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>
24 #include <ecl/mpl/converters.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_ */
#define ecl_compile_time_assert(logical_expression)
ConversionError
Primary templates for the family of converter classes.
std::vector< unsigned char > ByteArray
Primary template and general fallback for converter classes.
Definition: converter.hpp:56
static bool const value
#define ecl_compile_time_concept_check(Model)
#define ecl_debug_throw_decl(exception)
#define ecl_debug_throw(exception)
#define ECL_PUBLIC


ecl_converters
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:22