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 {
00037 namespace converters {
00038 
00039 /*****************************************************************************
00040 ** Interface
00041 *****************************************************************************/
00042 
00053 template <typename Integral, typename ByteArray>
00054 class ECL_PUBLIC FromByteArray : public ConverterBase {
00055 public:
00073     Integral operator()( const ByteArray &byte_array ) ecl_debug_throw_decl(ecl::StandardException) {
00074         /*********************
00075                 ** Checks
00076                 **********************/
00077         ecl_compile_time_concept_check(ByteContainerConcept<ByteArray>);
00078         ecl_compile_time_assert(is_integral<Integral>::value);
00079         if ( byte_array.size() > ecl::numeric_limits<Integral>::bytes ) {
00080                 ecl_debug_throw(StandardException(LOC,ConversionError,"The byte array is too long for the integral type specified."));
00081                 error_handler = ConversionError;
00082         }
00083                 /*********************
00084                 ** Method
00085                 **********************/
00086         typename ecl::Unsigned<Integral>::type unsigned_value = 0;
00087         for (unsigned int i = 0; i < byte_array.size(); ++i ) {
00088             unsigned_value |= static_cast<unsigned char>(byte_array[i]) << 8*i;
00089         }
00090         if ( ecl::is_signed<Integral>::value ) {
00091                 Integral value = static_cast<Integral>(unsigned_value);
00092                 return value;
00093         } else {
00094                 return unsigned_value;
00095         }
00096     }
00097 };
00098 
00099 } // namespace converters
00106 template <typename Integral>
00107 class ECL_PUBLIC Converter <Integral, std::vector<char> > : public converters::FromByteArray< Integral, std::vector<char> > {};
00111 template <typename Integral>
00112 class ECL_PUBLIC Converter <Integral, std::vector<unsigned char> > : public converters::FromByteArray< Integral, std::vector<unsigned char> > {};
00116 template <typename Integral>
00117 class ECL_PUBLIC Converter <Integral, std::vector<signed char> > : public converters::FromByteArray< Integral, std::vector<signed char> > {};
00118 
00119 } // namespace ecl
00120 
00121 
00122 #endif /* ECL_CONVERTERS_CONVERTERS_FROM_BYTE_ARRAY_HPP_ */


ecl_converters
Author(s): Daniel Stonier
autogenerated on Wed Aug 26 2015 11:27:10