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 ---------------------------------------//
@ -139,229 +141,241 @@ namespace endian
//----------------------------------- end synopsis -------------------------------------// //----------------------------------- end synopsis -------------------------------------//
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 } // namespace detail
template <class T>
inline void big_reverse_copy(T from, char* to) BOOST_NOEXCEPT; template <class EndianReversible>
template <class T> inline EndianReversible big_to_native( EndianReversible x ) BOOST_NOEXCEPT
inline void big_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT; {
template <class T> BOOST_STATIC_ASSERT( detail::is_endian_reversible<EndianReversible>::value );
inline void little_reverse_copy(T from, char* to) BOOST_NOEXCEPT;
template <class T> #if BOOST_ENDIAN_BIG_BYTE
inline void little_reverse_copy(const char* from, T& to) BOOST_NOEXCEPT;
} // namespace detail
template <class EndianReversible >
inline EndianReversible big_to_native(EndianReversible x) BOOST_NOEXCEPT
{
# 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
{ {
# if BOOST_ENDIAN_BIG_BYTE BOOST_STATIC_ASSERT( detail::is_endian_reversible<EndianReversible>::value );
#if BOOST_ENDIAN_BIG_BYTE
return x; return x;
# else
return endian_reverse(x);
# endif
}
template <class EndianReversible > #else
inline EndianReversible little_to_native(EndianReversible x) BOOST_NOEXCEPT
{ return endian_reverse(x);
# if BOOST_ENDIAN_LITTLE_BYTE
#endif
}
template <class EndianReversible>
inline EndianReversible little_to_native( EndianReversible x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible<EndianReversible>::value );
#if BOOST_ENDIAN_LITTLE_BYTE
return x; return x;
# else
return endian_reverse(x);
# endif
}
template <class EndianReversible > #else
inline EndianReversible native_to_little(EndianReversible x) BOOST_NOEXCEPT
{ return endian_reverse(x);
# if BOOST_ENDIAN_LITTLE_BYTE
#endif
}
template <class EndianReversible>
inline EndianReversible native_to_little( EndianReversible x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible<EndianReversible>::value );
#if BOOST_ENDIAN_LITTLE_BYTE
return x; return x;
# else
#else
return endian_reverse(x); return endian_reverse(x);
# endif
}
namespace detail #endif
{ }
// Primary template and specializations to support endian_reverse().
// See rationale in endian_reverse() below.
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To,
class EndianReversible>
class value_converter ; // primary template
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);}};
}
// generic conditional reverse namespace detail
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, {
class EndianReversible>
inline EndianReversible conditional_reverse(EndianReversible from) BOOST_NOEXCEPT {
// work around lack of function template partial specialization by instantiating
// a function object of a class that is partially specialized on the two order
// template parameters, and then calling its operator().
detail::value_converter <From, To, EndianReversible> tmp;
return tmp(from);
}
// runtime conditional reverse template<class EndianReversible>
template <class EndianReversible > inline EndianReversible conditional_reverse_impl( EndianReversible x, boost::true_type ) BOOST_NOEXCEPT
inline EndianReversible conditional_reverse(EndianReversible from, {
BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order) BOOST_NOEXCEPT return x;
{ }
return from_order == to_order ? from : endian_reverse(from);
} template<class EndianReversible>
inline EndianReversible conditional_reverse_impl( EndianReversible x, boost::false_type ) BOOST_NOEXCEPT
{
return endian_reverse( x );
}
} // namespace detail
// generic conditional reverse
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, class EndianReversible>
inline EndianReversible conditional_reverse( EndianReversible x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible<EndianReversible>::value );
return detail::conditional_reverse_impl( x, boost::integral_constant<bool, From == To>() );
}
// runtime conditional reverse
template <class EndianReversible>
inline EndianReversible conditional_reverse( EndianReversible x,
BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order ) BOOST_NOEXCEPT
{
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);
}
# endif
template <class EndianReversibleInplace> template<class T> struct is_endian_reversible_inplace: boost::integral_constant<bool,
# if BOOST_ENDIAN_LITTLE_BYTE boost::is_class<T>::value || ( boost::is_integral<T>::value && !boost::is_same<T, bool>::value )>
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
{
endian_reverse_inplace(x);
}
# endif
namespace detail } // namespace detail
{
// Primary template and specializations support generic
// endian_reverse_inplace().
// See rationale in endian_reverse_inplace() below.
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>
{public: void operator()(T&) BOOST_NOEXCEPT {/*no effect*/}};
template <class T> class converter<order::little, order::little, T>
{public: void operator()(T&) BOOST_NOEXCEPT {/*no effect*/}};
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
// generic conditional reverse in place #if BOOST_ENDIAN_BIG_BYTE
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To,
class EndianReversibleInplace>
inline void conditional_reverse_inplace(EndianReversibleInplace& x) BOOST_NOEXCEPT
{
// work around lack of function template partial specialization by instantiating
// a function object of a class that is partially specialized on the two order
// template parameters, and then calling its operator().
detail::converter<From, To, EndianReversibleInplace> tmp;
tmp(x); // call operator ()
}
// runtime reverse in place template <class EndianReversibleInplace>
template <class EndianReversibleInplace> inline void big_to_native_inplace( EndianReversibleInplace& ) BOOST_NOEXCEPT
inline void conditional_reverse_inplace(EndianReversibleInplace& x, {
BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order) BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
BOOST_NOEXCEPT }
{
if (from_order != to_order)
endian_reverse_inplace(x);
}
#else
namespace detail template <class EndianReversibleInplace>
{ inline void big_to_native_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
template <class T> {
inline void big_reverse_copy(T from, char* to) BOOST_NOEXCEPT BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
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
namespace detail
{
template<class EndianReversibleInplace>
inline void conditional_reverse_inplace_impl( EndianReversibleInplace&, boost::true_type ) BOOST_NOEXCEPT
{
}
template<class EndianReversibleInplace>
inline void conditional_reverse_inplace_impl( EndianReversibleInplace& x, boost::false_type ) BOOST_NOEXCEPT
{
endian_reverse_inplace( x );
}
} // namespace detail
// generic conditional reverse in place
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, class EndianReversibleInplace>
inline void conditional_reverse_inplace( EndianReversibleInplace& x ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
detail::conditional_reverse_inplace_impl( x, boost::integral_constant<bool, From == To>() );
}
// runtime reverse in place
template <class EndianReversibleInplace>
inline void conditional_reverse_inplace( EndianReversibleInplace& x,
BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order ) BOOST_NOEXCEPT
{
BOOST_STATIC_ASSERT( detail::is_endian_reversible_inplace<EndianReversibleInplace>::value );
if( from_order != to_order )
{ {
# if BOOST_ENDIAN_BIG_BYTE endian_reverse_inplace( x );
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