// Boost endian_pack.hpp header file -------------------------------------------------------// // (C) Copyright Darin Adler 2000 // (C) Copyright Beman Dawes 2006, 2009 // Distributed under the Boost Software License, Version 1.0. // See http://www.boost.org/LICENSE_1_0.txt // See library home page at http://www.boost.org/libs/endian //--------------------------------------------------------------------------------------// // Original design developed by Darin Adler based on classes developed by Mark // Borgerding. Four original class templates were combined into a single endian // class template by Beman Dawes, who also added the unrolled_byte_loops sign // partial specialization to correctly extend the sign when cover integer size // differs from endian representation size. // TODO: When a compiler supporting constexpr becomes available, try possible uses. #ifndef BOOST_ENDIAN_PACK_HPP #define BOOST_ENDIAN_PACK_HPP #ifdef BOOST_ENDIAN_LOG # include #endif #if defined(__BORLANDC__) || defined( __CODEGEARC__) # pragma pack(push, 1) #endif #include #include #include #include #include #include //~ #include #include # if CHAR_BIT != 8 # error Platforms with CHAR_BIT != 8 are not supported # endif # ifdef BOOST_NO_DEFAULTED_FUNCTIONS # define BOOST_ENDIAN_DEFAULT_CONSTRUCT {} // C++03 # else # define BOOST_ENDIAN_DEFAULT_CONSTRUCT = default; // C++0x # endif # if defined(BOOST_NO_DEFAULTED_FUNCTIONS) && defined(BOOST_ENDIAN_FORCE_PODNESS) # define BOOST_ENDIAN_NO_CTORS # endif namespace boost { namespace detail { // Unrolled loops for loading and storing streams of bytes. template ::value > struct unrolled_byte_loops { typedef unrolled_byte_loops next; static T load_big(const unsigned char* bytes) { return *(bytes - 1) | (next::load_big(bytes - 1) << 8); } static T load_little(const unsigned char* bytes) { return *bytes | (next::load_little(bytes + 1) << 8); } static void store_big(char* bytes, T value) { *(bytes - 1) = static_cast(value); next::store_big(bytes - 1, value >> 8); } static void store_little(char* bytes, T value) { *bytes = static_cast(value); next::store_little(bytes + 1, value >> 8); } }; template struct unrolled_byte_loops { static T load_big(const unsigned char* bytes) { return *(bytes - 1); } static T load_little(const unsigned char* bytes) { return *bytes; } static void store_big(char* bytes, T value) { *(bytes - 1) = static_cast(value); } static void store_little(char* bytes, T value) { *bytes = static_cast(value); } }; template struct unrolled_byte_loops { static T load_big(const unsigned char* bytes) { return *reinterpret_cast(bytes - 1); } static T load_little(const unsigned char* bytes) { return *reinterpret_cast(bytes); } static void store_big(char* bytes, T value) { *(bytes - 1) = static_cast(value); } static void store_little(char* bytes, T value) { *bytes = static_cast(value); } }; template inline T load_big_endian(const void* bytes) { return unrolled_byte_loops::load_big (static_cast(bytes) + n_bytes); } template inline T load_little_endian(const void* bytes) { return unrolled_byte_loops::load_little (static_cast(bytes)); } template inline void store_big_endian(void* bytes, T value) { unrolled_byte_loops::store_big (static_cast(bytes) + n_bytes, value); } template inline void store_little_endian(void* bytes, T value) { unrolled_byte_loops::store_little (static_cast(bytes), value); } } // namespace detail namespace integer { # ifdef BOOST_ENDIAN_LOG bool endian_log(true); # endif // endian class template and specializations ---------------------------------------// BOOST_SCOPED_ENUM_START(endianness) { big, little, native }; BOOST_SCOPED_ENUM_END BOOST_SCOPED_ENUM_START(alignment) { unaligned, aligned }; BOOST_SCOPED_ENUM_END template class endian_pack; // Specializations that represent unaligned bytes. // Taking an integer type as a parameter provides a nice way to pass both // the size and signedness of the desired integer and get the appropriate // corresponding integer type for the interface. // unaligned big endian specialization template class endian_pack< endianness::big, T, n_bits, alignment::unaligned > { BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits ); public: typedef T value_type; # ifndef BOOST_ENDIAN_NO_CTORS endian_pack() BOOST_ENDIAN_DEFAULT_CONSTRUCT explicit endian_pack(T val) { # ifdef BOOST_ENDIAN_LOG if ( endian_log ) std::clog << "big, unaligned, " << n_bits << "-bits, construct(" << val << ")\n"; # endif detail::store_big_endian(m_value, val); } # endif endian_pack & operator=(T val) { detail::store_big_endian(m_value, val); return *this; } operator T() const { # ifdef BOOST_ENDIAN_LOG if ( endian_log ) std::clog << "big, unaligned, " << n_bits << "-bits, convert(" << detail::load_big_endian(m_value) << ")\n"; # endif return detail::load_big_endian(m_value); } private: char m_value[n_bits/8]; }; // unaligned little endian specialization template class endian_pack< endianness::little, T, n_bits, alignment::unaligned > { BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits ); public: typedef T value_type; # ifndef BOOST_ENDIAN_NO_CTORS endian_pack() BOOST_ENDIAN_DEFAULT_CONSTRUCT explicit endian_pack(T val) { # ifdef BOOST_ENDIAN_LOG if ( endian_log ) std::clog << "little, unaligned, " << n_bits << "-bits, construct(" << val << ")\n"; # endif detail::store_little_endian(m_value, val); } # endif endian_pack & operator=(T val) { detail::store_little_endian(m_value, val); return *this; } operator T() const { # ifdef BOOST_ENDIAN_LOG if ( endian_log ) std::clog << "little, unaligned, " << n_bits << "-bits, convert(" << detail::load_little_endian(m_value) << ")\n"; # endif return detail::load_little_endian(m_value); } private: char m_value[n_bits/8]; }; // unaligned native endian specialization template class endian_pack< endianness::native, T, n_bits, alignment::unaligned > { BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits ); public: typedef T value_type; # ifndef BOOST_ENDIAN_NO_CTORS endian_pack() BOOST_ENDIAN_DEFAULT_CONSTRUCT # ifdef BOOST_BIG_ENDIAN explicit endian_pack(T val) { detail::store_big_endian(m_value, val); } # else explicit endian_pack(T val) { detail::store_little_endian(m_value, val); } # endif # endif # ifdef BOOST_BIG_ENDIAN endian_pack & operator=(T val) { detail::store_big_endian(m_value, val); return *this; } operator T() const { return detail::load_big_endian(m_value); } # else endian_pack & operator=(T val) { detail::store_little_endian(m_value, val); return *this; } operator T() const { return detail::load_little_endian(m_value); } # endif private: char m_value[n_bits/8]; }; // Specializations that mimic built-in integer types. // These typically have the same alignment as the underlying types. // aligned big endian specialization template class endian_pack< endianness::big, T, n_bits, alignment::aligned > { BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits ); BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 ); public: typedef T value_type; # ifndef BOOST_ENDIAN_NO_CTORS endian_pack() BOOST_ENDIAN_DEFAULT_CONSTRUCT # ifdef BOOST_BIG_ENDIAN endian_pack(T val) : m_value(val) { } # else explicit endian_pack(T val) { detail::store_big_endian(&m_value, val); } # endif # endif # ifdef BOOST_BIG_ENDIAN endian_pack & operator=(T val) { m_value = val); return *this; } operator T() const { return m_value; } # else endian_pack & operator=(T val) { detail::store_big_endian(&m_value, val); return *this; } operator T() const { return detail::load_big_endian(&m_value); } # endif private: T m_value; }; // aligned little endian specialization template class endian_pack< endianness::little, T, n_bits, alignment::aligned > { BOOST_STATIC_ASSERT( (n_bits/8)*8 == n_bits ); BOOST_STATIC_ASSERT( sizeof(T) == n_bits/8 ); public: typedef T value_type; # ifndef BOOST_ENDIAN_NO_CTORS endian_pack() BOOST_ENDIAN_DEFAULT_CONSTRUCT # ifdef BOOST_LITTLE_ENDIAN endian_pack(T val) : m_value(val) { } # else explicit endian_pack(T val) { detail::store_little_endian(&m_value, val); } # endif # endif # ifdef BOOST_LITTLE_ENDIAN endian_pack & operator=(T val) { m_value = val; return *this; } operator T() const { return m_value; } #else endian_pack & operator=(T val) { detail::store_little_endian(&m_value, val); return *this; } operator T() const { return detail::load_little_endian(&m_value); } #endif private: T m_value; }; // naming convention typedefs ------------------------------------------------------// // unaligned big endian_pack signed integer types typedef endian_pack< endianness::big, int_least8_t, 8 > big8_pt; typedef endian_pack< endianness::big, int_least16_t, 16 > big16_pt; typedef endian_pack< endianness::big, int_least32_t, 24 > big24_pt; typedef endian_pack< endianness::big, int_least32_t, 32 > big32_pt; typedef endian_pack< endianness::big, int_least64_t, 40 > big40_pt; typedef endian_pack< endianness::big, int_least64_t, 48 > big48_pt; typedef endian_pack< endianness::big, int_least64_t, 56 > big56_pt; typedef endian_pack< endianness::big, int_least64_t, 64 > big64_pt; // unaligned big endian_pack unsigned integer types typedef endian_pack< endianness::big, uint_least8_t, 8 > ubig8_pt; typedef endian_pack< endianness::big, uint_least16_t, 16 > ubig16_pt; typedef endian_pack< endianness::big, uint_least32_t, 24 > ubig24_pt; typedef endian_pack< endianness::big, uint_least32_t, 32 > ubig32_pt; typedef endian_pack< endianness::big, uint_least64_t, 40 > ubig40_pt; typedef endian_pack< endianness::big, uint_least64_t, 48 > ubig48_pt; typedef endian_pack< endianness::big, uint_least64_t, 56 > ubig56_pt; typedef endian_pack< endianness::big, uint_least64_t, 64 > ubig64_pt; // unaligned little endian_pack signed integer types typedef endian_pack< endianness::little, int_least8_t, 8 > little8_pt; typedef endian_pack< endianness::little, int_least16_t, 16 > little16_pt; typedef endian_pack< endianness::little, int_least32_t, 24 > little24_pt; typedef endian_pack< endianness::little, int_least32_t, 32 > little32_pt; typedef endian_pack< endianness::little, int_least64_t, 40 > little40_pt; typedef endian_pack< endianness::little, int_least64_t, 48 > little48_pt; typedef endian_pack< endianness::little, int_least64_t, 56 > little56_pt; typedef endian_pack< endianness::little, int_least64_t, 64 > little64_pt; // unaligned little endian_pack unsigned integer types typedef endian_pack< endianness::little, uint_least8_t, 8 > ulittle8_pt; typedef endian_pack< endianness::little, uint_least16_t, 16 > ulittle16_pt; typedef endian_pack< endianness::little, uint_least32_t, 24 > ulittle24_pt; typedef endian_pack< endianness::little, uint_least32_t, 32 > ulittle32_pt; typedef endian_pack< endianness::little, uint_least64_t, 40 > ulittle40_pt; typedef endian_pack< endianness::little, uint_least64_t, 48 > ulittle48_pt; typedef endian_pack< endianness::little, uint_least64_t, 56 > ulittle56_pt; typedef endian_pack< endianness::little, uint_least64_t, 64 > ulittle64_pt; // unaligned native endian_pack signed integer types typedef endian_pack< endianness::native, int_least8_t, 8 > native8_pt; typedef endian_pack< endianness::native, int_least16_t, 16 > native16_pt; typedef endian_pack< endianness::native, int_least32_t, 24 > native24_pt; typedef endian_pack< endianness::native, int_least32_t, 32 > native32_pt; typedef endian_pack< endianness::native, int_least64_t, 40 > native40_pt; typedef endian_pack< endianness::native, int_least64_t, 48 > native48_pt; typedef endian_pack< endianness::native, int_least64_t, 56 > native56_pt; typedef endian_pack< endianness::native, int_least64_t, 64 > native64_pt; // unaligned native endian_pack unsigned integer types typedef endian_pack< endianness::native, uint_least8_t, 8 > unative8_pt; typedef endian_pack< endianness::native, uint_least16_t, 16 > unative16_pt; typedef endian_pack< endianness::native, uint_least32_t, 24 > unative24_pt; typedef endian_pack< endianness::native, uint_least32_t, 32 > unative32_pt; typedef endian_pack< endianness::native, uint_least64_t, 40 > unative40_pt; typedef endian_pack< endianness::native, uint_least64_t, 48 > unative48_pt; typedef endian_pack< endianness::native, uint_least64_t, 56 > unative56_pt; typedef endian_pack< endianness::native, uint_least64_t, 64 > unative64_pt; #define BOOST_HAS_INT16_T #define BOOST_HAS_INT32_T #define BOOST_HAS_INT64_T // These types only present if platform has exact size integers: // aligned big endian_pack signed integer types // aligned big endian_pack unsigned integer types // aligned little endian_pack signed integer types // aligned little endian_pack unsigned integer types // aligned native endian_pack typedefs are not provided because // types are superior for this use case # if defined(BOOST_HAS_INT16_T) typedef endian_pack< endianness::big, int16_t, 16, alignment::aligned > aligned_big16_pt; typedef endian_pack< endianness::big, uint16_t, 16, alignment::aligned > aligned_ubig16_pt; typedef endian_pack< endianness::little, int16_t, 16, alignment::aligned > aligned_little16_pt; typedef endian_pack< endianness::little, uint16_t, 16, alignment::aligned > aligned_ulittle16_pt; # endif # if defined(BOOST_HAS_INT32_T) typedef endian_pack< endianness::big, int32_t, 32, alignment::aligned > aligned_big32_pt; typedef endian_pack< endianness::big, uint32_t, 32, alignment::aligned > aligned_ubig32_pt; typedef endian_pack< endianness::little, int32_t, 32, alignment::aligned > aligned_little32_pt; typedef endian_pack< endianness::little, uint32_t, 32, alignment::aligned > aligned_ulittle32_pt; # endif # if defined(BOOST_HAS_INT64_T) typedef endian_pack< endianness::big, int64_t, 64, alignment::aligned > aligned_big64_pt; typedef endian_pack< endianness::big, uint64_t, 64, alignment::aligned > aligned_ubig64_pt; typedef endian_pack< endianness::little, int64_t, 64, alignment::aligned > aligned_little64_pt; typedef endian_pack< endianness::little, uint64_t, 64, alignment::aligned > aligned_ulittle64_pt; # endif } // namespace integer } // namespace boost #if defined(__BORLANDC__) || defined( __CODEGEARC__) # pragma pack(pop) #endif #endif // BOOST_ENDIAN_HPP