| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589 | #ifndef BOOST_ENDIAN_DETAIL_ENDIAN_LOAD_HPP_INCLUDED#define BOOST_ENDIAN_DETAIL_ENDIAN_LOAD_HPP_INCLUDED// Copyright 2019 Peter Dimov//// Distributed under the Boost Software License, Version 1.0.// http://www.boost.org/LICENSE_1_0.txt#include <boost/endian/detail/endian_reverse.hpp>#include <boost/endian/detail/order.hpp>#include <boost/endian/detail/integral_by_size.hpp>#include <boost/endian/detail/is_trivially_copyable.hpp>#include <boost/type_traits/is_signed.hpp>#include <boost/type_traits/is_integral.hpp>#include <boost/type_traits/is_enum.hpp>#include <boost/static_assert.hpp>#include <cstddef>#include <cstring>namespace boost{namespace endian{namespace detail{template<class T, std::size_t N1, BOOST_SCOPED_ENUM(order) O1, std::size_t N2, BOOST_SCOPED_ENUM(order) O2> struct endian_load_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 enumtemplate<class T, std::size_t N, BOOST_SCOPED_ENUM(order) Order>inline T endian_load( unsigned char const * p ) 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_load_impl<T, sizeof(T), order::native, N, Order>()( p );}namespace detail{// same endianness, same sizetemplate<class T, std::size_t N, BOOST_SCOPED_ENUM(order) O> struct endian_load_impl<T, N, O, N, O>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_trivially_copyable<T>::value );        T t;        std::memcpy( &t, p, N );        return t;    }};// same size, reverse endiannesstemplate<class T, std::size_t N, BOOST_SCOPED_ENUM(order) O1, BOOST_SCOPED_ENUM(order) O2> struct endian_load_impl<T, N, O1, N, O2>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_trivially_copyable<T>::value );        typename integral_by_size<N>::type tmp;        std::memcpy( &tmp, p, N );        endian_reverse_inplace( tmp );        T t;        std::memcpy( &t, &tmp, N );        return t;    }};// expanding load 1 -> 2template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 2, Order, 1, order::little>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 2 ];        tmp[0] = p[0];        tmp[1] = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;        return boost::endian::endian_load<T, 2, order::little>( tmp );    }};template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 2, Order, 1, order::big>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 2 ];        tmp[0] = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;        tmp[1] = p[0];        return boost::endian::endian_load<T, 2, order::big>( tmp );    }};// expanding load 1 -> 4template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 4, Order, 1, order::little>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 4 ];        unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;        tmp[0] = p[0];        tmp[1] = fill;        tmp[2] = fill;        tmp[3] = fill;        return boost::endian::endian_load<T, 4, order::little>( tmp );    }};template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 4, Order, 1, order::big>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 4 ];        unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;        tmp[0] = fill;        tmp[1] = fill;        tmp[2] = fill;        tmp[3] = p[0];        return boost::endian::endian_load<T, 4, order::big>( tmp );    }};// expanding load 2 -> 4template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 4, Order, 2, order::little>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 4 ];        unsigned char fill = boost::is_signed<T>::value && ( p[1] & 0x80 )? 0xFF: 0x00;        tmp[0] = p[0];        tmp[1] = p[1];        tmp[2] = fill;        tmp[3] = fill;        return boost::endian::endian_load<T, 4, order::little>( tmp );    }};template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 4, Order, 2, order::big>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 4 ];        unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;        tmp[0] = fill;        tmp[1] = fill;        tmp[2] = p[0];        tmp[3] = p[1];        return boost::endian::endian_load<T, 4, order::big>( tmp );    }};// expanding load 3 -> 4template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 4, Order, 3, order::little>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 4 ];        tmp[0] = p[0];        tmp[1] = p[1];        tmp[2] = p[2];        tmp[3] = boost::is_signed<T>::value && ( p[2] & 0x80 )? 0xFF: 0x00;        return boost::endian::endian_load<T, 4, order::little>( tmp );    }};template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 4, Order, 3, order::big>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 4 ];        tmp[0] = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;        tmp[1] = p[0];        tmp[2] = p[1];        tmp[3] = p[2];        return boost::endian::endian_load<T, 4, order::big>( tmp );    }};// expanding load 1 -> 8template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 1, order::little>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 8 ];        unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;        tmp[0] = p[0];        tmp[1] = fill;        tmp[2] = fill;        tmp[3] = fill;        tmp[4] = fill;        tmp[5] = fill;        tmp[6] = fill;        tmp[7] = fill;        return boost::endian::endian_load<T, 8, order::little>( tmp );    }};template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 1, order::big>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 8 ];        unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;        tmp[0] = fill;        tmp[1] = fill;        tmp[2] = fill;        tmp[3] = fill;        tmp[4] = fill;        tmp[5] = fill;        tmp[6] = fill;        tmp[7] = p[0];        return boost::endian::endian_load<T, 8, order::big>( tmp );    }};// expanding load 2 -> 8template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 2, order::little>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 8 ];        unsigned char fill = boost::is_signed<T>::value && ( p[1] & 0x80 )? 0xFF: 0x00;        tmp[0] = p[0];        tmp[1] = p[1];        tmp[2] = fill;        tmp[3] = fill;        tmp[4] = fill;        tmp[5] = fill;        tmp[6] = fill;        tmp[7] = fill;        return boost::endian::endian_load<T, 8, order::little>( tmp );    }};template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 2, order::big>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 8 ];        unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;        tmp[0] = fill;        tmp[1] = fill;        tmp[2] = fill;        tmp[3] = fill;        tmp[4] = fill;        tmp[5] = fill;        tmp[6] = p[0];        tmp[7] = p[1];        return boost::endian::endian_load<T, 8, order::big>( tmp );    }};// expanding load 3 -> 8template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 3, order::little>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 8 ];        unsigned char fill = boost::is_signed<T>::value && ( p[2] & 0x80 )? 0xFF: 0x00;        tmp[0] = p[0];        tmp[1] = p[1];        tmp[2] = p[2];        tmp[3] = fill;        tmp[4] = fill;        tmp[5] = fill;        tmp[6] = fill;        tmp[7] = fill;        return boost::endian::endian_load<T, 8, order::little>( tmp );    }};template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 3, order::big>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 8 ];        unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;        tmp[0] = fill;        tmp[1] = fill;        tmp[2] = fill;        tmp[3] = fill;        tmp[4] = fill;        tmp[5] = p[0];        tmp[6] = p[1];        tmp[7] = p[2];        return boost::endian::endian_load<T, 8, order::big>( tmp );    }};// expanding load 4 -> 8template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 4, order::little>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 8 ];        unsigned char fill = boost::is_signed<T>::value && ( p[3] & 0x80 )? 0xFF: 0x00;        tmp[0] = p[0];        tmp[1] = p[1];        tmp[2] = p[2];        tmp[3] = p[3];        tmp[4] = fill;        tmp[5] = fill;        tmp[6] = fill;        tmp[7] = fill;        return boost::endian::endian_load<T, 8, order::little>( tmp );    }};template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 4, order::big>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 8 ];        unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;        tmp[0] = fill;        tmp[1] = fill;        tmp[2] = fill;        tmp[3] = fill;        tmp[4] = p[0];        tmp[5] = p[1];        tmp[6] = p[2];        tmp[7] = p[3];        return boost::endian::endian_load<T, 8, order::big>( tmp );    }};// expanding load 5 -> 8template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 5, order::little>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 8 ];        unsigned char fill = boost::is_signed<T>::value && ( p[4] & 0x80 )? 0xFF: 0x00;        tmp[0] = p[0];        tmp[1] = p[1];        tmp[2] = p[2];        tmp[3] = p[3];        tmp[4] = p[4];        tmp[5] = fill;        tmp[6] = fill;        tmp[7] = fill;        return boost::endian::endian_load<T, 8, order::little>( tmp );    }};template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 5, order::big>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 8 ];        unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;        tmp[0] = fill;        tmp[1] = fill;        tmp[2] = fill;        tmp[3] = p[0];        tmp[4] = p[1];        tmp[5] = p[2];        tmp[6] = p[3];        tmp[7] = p[4];        return boost::endian::endian_load<T, 8, order::big>( tmp );    }};// expanding load 6 -> 8template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 6, order::little>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 8 ];        unsigned char fill = boost::is_signed<T>::value && ( p[5] & 0x80 )? 0xFF: 0x00;        tmp[0] = p[0];        tmp[1] = p[1];        tmp[2] = p[2];        tmp[3] = p[3];        tmp[4] = p[4];        tmp[5] = p[5];        tmp[6] = fill;        tmp[7] = fill;        return boost::endian::endian_load<T, 8, order::little>( tmp );    }};template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 6, order::big>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 8 ];        unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;        tmp[0] = fill;        tmp[1] = fill;        tmp[2] = p[0];        tmp[3] = p[1];        tmp[4] = p[2];        tmp[5] = p[3];        tmp[6] = p[4];        tmp[7] = p[5];        return boost::endian::endian_load<T, 8, order::big>( tmp );    }};// expanding load 7 -> 8template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 7, order::little>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 8 ];        unsigned char fill = boost::is_signed<T>::value && ( p[6] & 0x80 )? 0xFF: 0x00;        tmp[0] = 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] = fill;        return boost::endian::endian_load<T, 8, order::little>( tmp );    }};template<class T, BOOST_SCOPED_ENUM(order) Order> struct endian_load_impl<T, 8, Order, 7, order::big>{    inline T operator()( unsigned char const * p ) const BOOST_NOEXCEPT    {        BOOST_STATIC_ASSERT( is_integral<T>::value || is_enum<T>::value );        unsigned char tmp[ 8 ];        unsigned char fill = boost::is_signed<T>::value && ( p[0] & 0x80 )? 0xFF: 0x00;        tmp[0] = fill;        tmp[1] = p[0];        tmp[2] = p[1];        tmp[3] = p[2];        tmp[4] = p[3];        tmp[5] = p[4];        tmp[6] = p[5];        tmp[7] = p[6];        return boost::endian::endian_load<T, 8, order::big>( tmp );    }};} // namespace detail} // namespace endian} // namespace boost#endif  // BOOST_ENDIAN_DETAIL_ENDIAN_LOAD_HPP_INCLUDED
 |