#ifndef BOOST_ENDIAN_DETAIL_ENDIAN_STORE_HPP_INCLUDED #define BOOST_ENDIAN_DETAIL_ENDIAN_STORE_HPP_INCLUDED // Copyright 2019 Peter Dimov // // Distributed under the Boost Software License, Version 1.0. // http://www.boost.org/LICENSE_1_0.txt #include #include #include #include #include #include #include #include #include namespace boost { namespace endian { namespace detail { template struct endian_store_impl { }; } // namespace detail // Requires: // // sizeof(T) must be 1, 2, 4, or 8 // 1 <= N <= sizeof(T) // T is TriviallyCopyable // if N < sizeof(T), T is integral or enum template inline void endian_store( unsigned char * p, T const & v ) BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8 ); BOOST_STATIC_ASSERT( N >= 1 && N <= sizeof(T) ); return detail::endian_store_impl()( p, v ); } namespace detail { // same endianness, same size template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_trivially_copyable::value ); std::memcpy( p, &v, N ); } }; // same size, reverse endianness template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_trivially_copyable::value ); typename integral_by_size::type tmp; std::memcpy( &tmp, &v, N ); endian_reverse_inplace( tmp ); std::memcpy( p, &tmp, N ); } }; // truncating store 2 -> 1 template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 2 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[0]; } }; template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 2 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[1]; } }; // truncating store 4 -> 1 template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 4 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[0]; } }; template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 4 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[3]; } }; // truncating store 4 -> 2 template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 4 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[0]; p[1] = tmp[1]; } }; template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 4 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[2]; p[1] = tmp[3]; } }; // truncating store 4 -> 3 template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 4 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[0]; p[1] = tmp[1]; p[2] = tmp[2]; } }; template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 4 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[1]; p[1] = tmp[2]; p[2] = tmp[3]; } }; // truncating store 8 -> 1 template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 8 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[0]; } }; template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 8 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[7]; } }; // truncating store 8 -> 2 template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 8 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[0]; p[1] = tmp[1]; } }; template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 8 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[6]; p[1] = tmp[7]; } }; // truncating store 8 -> 3 template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 8 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[0]; p[1] = tmp[1]; p[2] = tmp[2]; } }; template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 8 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[5]; p[1] = tmp[6]; p[2] = tmp[7]; } }; // truncating store 8 -> 4 template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 8 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[0]; p[1] = tmp[1]; p[2] = tmp[2]; p[3] = tmp[3]; } }; template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 8 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[4]; p[1] = tmp[5]; p[2] = tmp[6]; p[3] = tmp[7]; } }; // truncating store 8 -> 5 template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 8 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[0]; p[1] = tmp[1]; p[2] = tmp[2]; p[3] = tmp[3]; p[4] = tmp[4]; } }; template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 8 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[3]; p[1] = tmp[4]; p[2] = tmp[5]; p[3] = tmp[6]; p[4] = tmp[7]; } }; // truncating store 8 -> 6 template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 8 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[0]; p[1] = tmp[1]; p[2] = tmp[2]; p[3] = tmp[3]; p[4] = tmp[4]; p[5] = tmp[5]; } }; template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 8 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[2]; p[1] = tmp[3]; p[2] = tmp[4]; p[3] = tmp[5]; p[4] = tmp[6]; p[5] = tmp[7]; } }; // truncating store 8 -> 7 template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 8 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[0]; p[1] = tmp[1]; p[2] = tmp[2]; p[3] = tmp[3]; p[4] = tmp[4]; p[5] = tmp[5]; p[6] = tmp[6]; } }; template struct endian_store_impl { inline void operator()( unsigned char * p, T const & v ) const BOOST_NOEXCEPT { BOOST_STATIC_ASSERT( is_integral::value || is_enum::value ); unsigned char tmp[ 8 ]; boost::endian::endian_store( tmp, v ); p[0] = tmp[1]; p[1] = tmp[2]; p[2] = tmp[3]; p[3] = tmp[4]; p[4] = tmp[5]; p[5] = tmp[6]; p[6] = tmp[7]; } }; } // namespace detail } // namespace endian } // namespace boost #endif // BOOST_ENDIAN_DETAIL_ENDIAN_STORE_HPP_INCLUDED