conversion.hpp: remove unused detail functions, static-assert that types are EndianReversible

This commit is contained in:
Peter Dimov
2019-04-29 01:32:07 +03:00
parent b29b3dfc10
commit f0f8e398d6
2 changed files with 220 additions and 198 deletions

View File

@ -10,12 +10,14 @@
#include <boost/endian/detail/endian_reverse.hpp> #include <boost/endian/detail/endian_reverse.hpp>
#include <boost/endian/detail/order.hpp> #include <boost/endian/detail/order.hpp>
#include <boost/type_traits/is_class.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/integral_constant.hpp>
#include <boost/predef/other/endian.h> #include <boost/predef/other/endian.h>
#include <boost/static_assert.hpp> #include <boost/static_assert.hpp>
#include <boost/cstdint.hpp> #include <boost/cstdint.hpp>
#include <boost/config.hpp> #include <boost/config.hpp>
#include <algorithm>
#include <cstring> // for memcpy
//------------------------------------- synopsis ---------------------------------------// //------------------------------------- synopsis ---------------------------------------//
@ -141,227 +143,239 @@ namespace endian
namespace detail namespace detail
{ {
// generic reverse function template implementation approach using std::reverse
// suggested by Mathias Gaunard. Primary motivation for inclusion is to have an
// independent implementation to test against.
template <class T> template<class T> struct is_endian_reversible: boost::integral_constant<bool,
inline T std_endian_reverse(T x) BOOST_NOEXCEPT boost::is_class<T>::value || ( boost::is_integral<T>::value && !boost::is_same<T, bool>::value )>
{ {
T tmp(x); };
std::reverse(
reinterpret_cast<unsigned char*>(&tmp),
reinterpret_cast<unsigned char*>(&tmp) + sizeof(T));
return tmp;
}
// conditional unaligned reverse copy, patterned after std::reverse_copy
template <class T>
inline void big_reverse_copy(T from, char* to) BOOST_NOEXCEPT;
template <class T>
inline void big_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT;
template <class T>
inline void little_reverse_copy(T from, char* to) BOOST_NOEXCEPT;
template <class T>
inline void little_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT;
} // namespace detail } // namespace detail
template <class EndianReversible> template <class EndianReversible>
inline EndianReversible big_to_native( EndianReversible x ) BOOST_NOEXCEPT inline EndianReversible big_to_native( EndianReversible x ) BOOST_NOEXCEPT
{ {
BOOST_STATIC_ASSERT( detail::is_endian_reversible<EndianReversible>::value );
#if BOOST_ENDIAN_BIG_BYTE #if BOOST_ENDIAN_BIG_BYTE
return x; return x;
#else #else
return endian_reverse(x); return endian_reverse(x);
#endif #endif
} }
template <class EndianReversible> template <class EndianReversible>
inline EndianReversible native_to_big( EndianReversible x ) BOOST_NOEXCEPT inline EndianReversible native_to_big( EndianReversible x ) BOOST_NOEXCEPT
{ {
BOOST_STATIC_ASSERT( detail::is_endian_reversible<EndianReversible>::value );
#if BOOST_ENDIAN_BIG_BYTE #if BOOST_ENDIAN_BIG_BYTE
return x; return x;
#else #else
return endian_reverse(x); return endian_reverse(x);
#endif #endif
} }
template <class EndianReversible> template <class EndianReversible>
inline EndianReversible little_to_native( EndianReversible x ) BOOST_NOEXCEPT inline EndianReversible little_to_native( EndianReversible x ) BOOST_NOEXCEPT
{ {
BOOST_STATIC_ASSERT( detail::is_endian_reversible<EndianReversible>::value );
#if BOOST_ENDIAN_LITTLE_BYTE #if BOOST_ENDIAN_LITTLE_BYTE
return x; return x;
#else #else
return endian_reverse(x); return endian_reverse(x);
#endif #endif
} }
template <class EndianReversible> template <class EndianReversible>
inline EndianReversible native_to_little( EndianReversible x ) BOOST_NOEXCEPT inline EndianReversible native_to_little( EndianReversible x ) BOOST_NOEXCEPT
{ {
BOOST_STATIC_ASSERT( detail::is_endian_reversible<EndianReversible>::value );
#if BOOST_ENDIAN_LITTLE_BYTE #if BOOST_ENDIAN_LITTLE_BYTE
return x; return x;
#else #else
return endian_reverse(x); return endian_reverse(x);
#endif #endif
} }
namespace detail namespace detail
{ {
// Primary template and specializations to support endian_reverse().
// See rationale in endian_reverse() below. template<class EndianReversible>
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, inline EndianReversible conditional_reverse_impl( EndianReversible x, boost::true_type ) BOOST_NOEXCEPT
class EndianReversible> {
class value_converter ; // primary template return x;
template <class T> class value_converter <order::big, order::big, T>
{public: T operator()(T x) BOOST_NOEXCEPT {return x;}};
template <class T> class value_converter <order::little, order::little, T>
{public: T operator()(T x) BOOST_NOEXCEPT {return x;}};
template <class T> class value_converter <order::big, order::little, T>
{public: T operator()(T x) BOOST_NOEXCEPT {return endian_reverse(x);}};
template <class T> class value_converter <order::little, order::big, T>
{public: T operator()(T x) BOOST_NOEXCEPT {return endian_reverse(x);}};
} }
template<class EndianReversible>
inline EndianReversible conditional_reverse_impl( EndianReversible x, boost::false_type ) BOOST_NOEXCEPT
{
return endian_reverse( x );
}
} // namespace detail
// generic conditional reverse // generic conditional reverse
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, class EndianReversible>
class EndianReversible> inline EndianReversible conditional_reverse( EndianReversible x ) BOOST_NOEXCEPT
inline EndianReversible conditional_reverse(EndianReversible from) BOOST_NOEXCEPT { {
// work around lack of function template partial specialization by instantiating BOOST_STATIC_ASSERT( detail::is_endian_reversible<EndianReversible>::value );
// a function object of a class that is partially specialized on the two order return detail::conditional_reverse_impl( x, boost::integral_constant<bool, From == To>() );
// template parameters, and then calling its operator().
detail::value_converter <From, To, EndianReversible> tmp;
return tmp(from);
} }
// runtime conditional reverse // runtime conditional reverse
template <class EndianReversible> template <class EndianReversible>
inline EndianReversible conditional_reverse(EndianReversible from, inline EndianReversible conditional_reverse( EndianReversible x,
BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order ) BOOST_NOEXCEPT BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order ) BOOST_NOEXCEPT
{ {
return from_order == to_order ? from : endian_reverse(from); BOOST_STATIC_ASSERT( detail::is_endian_reversible<EndianReversible>::value );
return from_order == to_order? x: endian_reverse( x );
} }
//--------------------------------------------------------------------------------------// //--------------------------------------------------------------------------------------//
// reverse-in-place implementation // // reverse-in-place implementation //
//--------------------------------------------------------------------------------------// //--------------------------------------------------------------------------------------//
template <class EndianReversibleInplace> namespace detail
# if BOOST_ENDIAN_BIG_BYTE
inline void big_to_native_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {}
# else
inline void big_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT
{ endian_reverse_inplace(x); }
# endif
template <class EndianReversibleInplace>
# if BOOST_ENDIAN_BIG_BYTE
inline void native_to_big_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {}
# else
inline void native_to_big_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT
{ {
endian_reverse_inplace(x);
} template<class T> struct is_endian_reversible_inplace: boost::integral_constant<bool,
# endif boost::is_class<T>::value || ( boost::is_integral<T>::value && !boost::is_same<T, bool>::value )>
{
};
} // namespace detail
#if BOOST_ENDIAN_BIG_BYTE
template <class EndianReversibleInplace> template <class EndianReversibleInplace>
# if BOOST_ENDIAN_LITTLE_BYTE inline void big_to_native_inplace( EndianReversibleInplace& ) BOOST_NOEXCEPT
inline void little_to_native_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {}
# else
inline void little_to_native_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT
{ endian_reverse_inplace(x); }
# endif
template <class EndianReversibleInplace>
# if BOOST_ENDIAN_LITTLE_BYTE
inline void native_to_little_inplace(EndianReversibleInplace&) BOOST_NOEXCEPT {}
# else
inline void native_to_little_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT
{ {
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
}
#else
template <class EndianReversibleInplace>
inline void big_to_native_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
endian_reverse_inplace( x ); endian_reverse_inplace( x );
} }
#endif
#if BOOST_ENDIAN_BIG_BYTE
template <class EndianReversibleInplace>
inline void native_to_big_inplace( EndianReversibleInplace& ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
}
#else
template <class EndianReversibleInplace>
inline void native_to_big_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
endian_reverse_inplace( x );
}
#endif
#if BOOST_ENDIAN_LITTLE_BYTE
template <class EndianReversibleInplace>
inline void little_to_native_inplace( EndianReversibleInplace& ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
}
#else
template <class EndianReversibleInplace>
inline void little_to_native_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
endian_reverse_inplace( x );
}
#endif
#if BOOST_ENDIAN_LITTLE_BYTE
template <class EndianReversibleInplace>
inline void native_to_little_inplace( EndianReversibleInplace& ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
}
#else
template <class EndianReversibleInplace>
inline void native_to_little_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
endian_reverse_inplace( x );
}
#endif #endif
namespace detail namespace detail
{ {
// Primary template and specializations support generic
// endian_reverse_inplace(). template<class EndianReversibleInplace>
// See rationale in endian_reverse_inplace() below. inline void conditional_reverse_inplace_impl( EndianReversibleInplace&, boost::true_type ) BOOST_NOEXCEPT
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, {
class EndianReversibleInplace> }
class converter; // primary template
template <class T> class converter<order::big, order::big, T> template<class EndianReversibleInplace>
{public: void operator()(T&) BOOST_NOEXCEPT {/*no effect*/}}; inline void conditional_reverse_inplace_impl( EndianReversibleInplace& x, boost::false_type ) BOOST_NOEXCEPT
template <class T> class converter<order::little, order::little, T> {
{public: void operator()(T&) BOOST_NOEXCEPT {/*no effect*/}}; endian_reverse_inplace( x );
template <class T> class converter<order::big, order::little, T> }
{public: void operator()(T& x) BOOST_NOEXCEPT { endian_reverse_inplace(x); }};
template <class T> class converter<order::little, order::big, T>
{public: void operator()(T& x) BOOST_NOEXCEPT { endian_reverse_inplace(x); }};
} // namespace detail } // namespace detail
// generic conditional reverse in place // generic conditional reverse in place
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, class EndianReversibleInplace>
class EndianReversibleInplace>
inline void conditional_reverse_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT inline void conditional_reverse_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
{ {
// work around lack of function template partial specialization by instantiating BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
// a function object of a class that is partially specialized on the two order detail::conditional_reverse_inplace_impl( x, boost::integral_constant<bool, From == To>() );
// template parameters, and then calling its operator().
detail::converter<From, To, EndianReversibleInplace> tmp;
tmp(x); // call operator ()
} }
// runtime reverse in place // runtime reverse in place
template <class EndianReversibleInplace> template <class EndianReversibleInplace>
inline void conditional_reverse_inplace( EndianReversibleInplace& x, inline void conditional_reverse_inplace( EndianReversibleInplace& x,
BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order) BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order ) BOOST_NOEXCEPT
BOOST_NOEXCEPT
{ {
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
if( from_order != to_order ) if( from_order != to_order )
{
endian_reverse_inplace( x ); endian_reverse_inplace( x );
} }
}
namespace detail
{
template <class T>
inline void big_reverse_copy(T from, char* to) BOOST_NOEXCEPT
{
# if BOOST_ENDIAN_BIG_BYTE
std::memcpy(to, reinterpret_cast<const char*>(&from), sizeof(T));
# else
std::reverse_copy(reinterpret_cast<const char*>(&from),
reinterpret_cast<const char*>(&from) + sizeof(T), to);
# endif
}
template <class T>
inline void big_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT
{
# if BOOST_ENDIAN_BIG_BYTE
std::memcpy(reinterpret_cast<char*>(&to), from, sizeof(T));
# else
std::reverse_copy(from, from + sizeof(T), reinterpret_cast<char*>(&to));
# endif
}
template <class T>
inline void little_reverse_copy(T from, char* to) BOOST_NOEXCEPT
{
# if BOOST_ENDIAN_LITTLE_BYTE
std::memcpy(to, reinterpret_cast<const char*>(&from), sizeof(T));
# else
std::reverse_copy(reinterpret_cast<const char*>(&from),
reinterpret_cast<const char*>(&from) + sizeof(T), to);
# endif
}
template <class T>
inline void little_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT
{
# if BOOST_ENDIAN_LITTLE_BYTE
std::memcpy(reinterpret_cast<char*>(&to), from, sizeof(T));
# else
std::reverse_copy(from, from + sizeof(T), reinterpret_cast<char*>(&to));
# endif
}
} // namespace detail
} // namespace endian } // namespace endian
} // namespace boost } // namespace boost

View File

@ -14,6 +14,7 @@
#include <boost/core/lightweight_test.hpp> #include <boost/core/lightweight_test.hpp>
#include <iostream> #include <iostream>
#include <cstring> #include <cstring>
#include <algorithm>
namespace be = boost::endian; namespace be = boost::endian;
using std::cout; using std::cout;
@ -27,6 +28,13 @@ using boost::uint32_t;
using boost::int64_t; using boost::int64_t;
using boost::uint64_t; using boost::uint64_t;
template <class T> inline T std_endian_reverse(T x) BOOST_NOEXCEPT
{
T tmp(x);
std::reverse( reinterpret_cast<unsigned char*>(&tmp), reinterpret_cast<unsigned char*>(&tmp) + sizeof(T) );
return tmp;
}
namespace namespace
{ {
@ -102,9 +110,9 @@ namespace
# if BOOST_ENDIAN_BIG_BYTE # if BOOST_ENDIAN_BIG_BYTE
BOOST_TEST_EQ(native, big); BOOST_TEST_EQ(native, big);
BOOST_TEST_EQ(be::detail::std_endian_reverse(native), little); BOOST_TEST_EQ(::std_endian_reverse(native), little);
# else # else
BOOST_TEST_EQ(be::detail::std_endian_reverse(native), big); BOOST_TEST_EQ(::std_endian_reverse(native), big);
BOOST_TEST_EQ(native, little); BOOST_TEST_EQ(native, little);
# endif # endif