from_byte_array.hpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009  ** Ifdefs
00010  *****************************************************************************/
00011 
00012 #ifndef ECL_CONVERTERS_CONVERTERS_FROM_BYTE_ARRAY_HPP_
00013 #define ECL_CONVERTERS_CONVERTERS_FROM_BYTE_ARRAY_HPP_
00014 
00015 /*****************************************************************************
00016  ** Includes
00017  *****************************************************************************/
00018 
00019 #include <vector>
00020 #include <ecl/config/macros.hpp>
00021 #include <ecl/concepts/containers.hpp>
00022 #include <ecl/errors/compile_time_assert.hpp>
00023 #include <ecl/exceptions/standard_exception.hpp>
00024 #include <ecl/mpl/converters.hpp>
00025 #include <ecl/type_traits/fundamental_types.hpp>
00026 #include <ecl/type_traits/numeric_limits.hpp>
00027 #include "converter.hpp"
00028 
00029 /*****************************************************************************
00030  ** Namespaces
00031  *****************************************************************************/
00032 
00033 namespace ecl
00034 {
00038 namespace converters
00039 {
00040 
00041 /*****************************************************************************
00042  ** Interface
00043  *****************************************************************************/
00044 
00055 template<typename Integral, typename ByteArray>
00056 class ECL_PUBLIC FromByteArray : public ConverterBase
00057 {
00058 public:
00076   Integral operator()(const ByteArray &byte_array) ecl_debug_throw_decl(ecl::StandardException)
00077   {
00078     /*********************
00079      ** Checks
00080      **********************/
00081     ecl_compile_time_concept_check(ByteContainerConcept<ByteArray>);
00082     ecl_compile_time_assert(is_integral<Integral>::value);
00083     if (byte_array.size() > ecl::numeric_limits<Integral>::bytes)
00084     {
00085       ecl_debug_throw(
00086           StandardException(LOC,ConversionError,"The byte array is too long for the integral type specified."));
00087       error_handler = ConversionError;
00088     }
00089     /*********************
00090      ** Method
00091      **********************/
00092     typename ecl::Unsigned<Integral>::type unsigned_value = 0;
00093     for (unsigned int i = 0; i < byte_array.size(); ++i)
00094     {
00095       unsigned_value |= static_cast<unsigned char>(byte_array[i]) << 8 * i;
00096     }
00097     if (ecl::is_signed<Integral>::value)
00098     {
00099       Integral value = static_cast<Integral>(unsigned_value);
00100       return value;
00101     }
00102     else
00103     {
00104       return unsigned_value;
00105     }
00106   }
00110   virtual ~FromByteArray() {}
00111 };
00112 
00113 } // namespace converters
00114 
00122 template<typename Integral>
00123 class ECL_PUBLIC Converter<Integral, std::vector<char> > :
00124     public converters::FromByteArray<Integral, std::vector<char> >
00125 {
00126 };
00130 template<typename Integral>
00131 class ECL_PUBLIC Converter<Integral, std::vector<unsigned char> > : public converters::FromByteArray<Integral,
00132     std::vector<unsigned char> >
00133 {
00134 };
00138 template<typename Integral>
00139 class ECL_PUBLIC Converter<Integral, std::vector<signed char> > : public converters::FromByteArray<Integral,
00140     std::vector<signed char> >
00141 {
00142 };
00143 
00144 } // namespace ecl
00145 
00146 #endif /* ECL_CONVERTERS_CONVERTERS_FROM_BYTE_ARRAY_HPP_ */


ecl_converters
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 21:17:34