More conversion.hpp fixes. Some, but not all, conversion_test tests passing.

This commit is contained in:
Beman
2014-11-23 10:38:21 -05:00
parent ce73f59741
commit 4197f996ac
3 changed files with 137 additions and 132 deletions

View File

@@ -262,7 +262,7 @@ namespace endian
inline T generic_reverse_endianness(T x) BOOST_NOEXCEPT inline T generic_reverse_endianness(T x) BOOST_NOEXCEPT
{ {
T tmp(x); T tmp(x);
std::reverse_endianness( std::reverse(
reinterpret_cast<char*>(&tmp), reinterpret_cast<char*>(&tmp),
reinterpret_cast<char*>(&tmp) + sizeof(T)); reinterpret_cast<char*>(&tmp) + sizeof(T));
return tmp; return tmp;
@@ -325,8 +325,8 @@ namespace endian
namespace detail namespace detail
{ {
// Primary template and specializations to support convert_value(). See rationale in // Primary template and specializations to support reverse_endianness().
// convert_value() below. // See rationale in reverse_endianness() below.
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, class Reversible> template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, class Reversible>
class value_converter ; // primary template class value_converter ; // primary template
template <class T> class value_converter <order::big, order::big, T> template <class T> class value_converter <order::big, order::big, T>
@@ -334,14 +334,14 @@ namespace endian
template <class T> class value_converter <order::little, order::little, T> template <class T> class value_converter <order::little, order::little, T>
{public: T operator()(T x) BOOST_NOEXCEPT {return x;}}; {public: T operator()(T x) BOOST_NOEXCEPT {return x;}};
template <class T> class value_converter <order::big, order::little, T> template <class T> class value_converter <order::big, order::little, T>
{public: T operator()(T x) BOOST_NOEXCEPT {return reverse_value(x);}}; {public: T operator()(T x) BOOST_NOEXCEPT {return reverse_endianness(x);}};
template <class T> class value_converter <order::little, order::big, T> template <class T> class value_converter <order::little, order::big, T>
{public: T operator()(T x) BOOST_NOEXCEPT {return reverse_value(x);}}; {public: T operator()(T x) BOOST_NOEXCEPT {return reverse_endianness(x);}};
} }
// compile-time generic convert return by value // compile-time generic convert return by value
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, class Reversible> template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, class Reversible>
Reversible convert_value(Reversible x) BOOST_NOEXCEPT Reversible reverse_endianness(Reversible x) BOOST_NOEXCEPT
{ {
// work around lack of function template partial specialization by instantiating // work around lack of function template partial specialization by instantiating
// a function object of a class that is partially specialized on the two order // a function object of a class that is partially specialized on the two order
@@ -363,7 +363,7 @@ namespace endian
} }
template <class Reversible > template <class Reversible >
Reversible convert_value(Reversible from, Reversible reverse_endianness(Reversible from,
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 effective_order(from_order) == effective_order(to_order) return effective_order(from_order) == effective_order(to_order)
@@ -375,50 +375,67 @@ namespace endian
//--------------------------------------------------------------------------------------// //--------------------------------------------------------------------------------------//
// reverse byte-order in place (i.e. flip endianness) // reverse byte-order in place (i.e. flip endianness)
inline void reverse_endianness_in_place(int8_t& x) BOOST_NOEXCEPT { x = reverse_endianness(x); } inline void reverse_endianness_in_place(int8_t& x) BOOST_NOEXCEPT
inline void reverse_endianness_in_place(int16_t& x) BOOST_NOEXCEPT { x = reverse_endianness(x); } { x = reverse_endianness(x); }
inline void reverse_endianness_in_place(int32_t& x) BOOST_NOEXCEPT { x = reverse_endianness(x); } inline void reverse_endianness_in_place(int16_t& x) BOOST_NOEXCEPT
inline void reverse_endianness_in_place(int64_t& x) BOOST_NOEXCEPT { x = reverse_endianness(x); } { x = reverse_endianness(x); }
inline void reverse_endianness_in_place(uint8_t& x) BOOST_NOEXCEPT { x = reverse_endianness(x); } inline void reverse_endianness_in_place(int32_t& x) BOOST_NOEXCEPT
inline void reverse_endianness_in_place(uint16_t& x) BOOST_NOEXCEPT { x = reverse_endianness(x); } { x = reverse_endianness(x); }
inline void reverse_endianness_in_place(uint32_t& x) BOOST_NOEXCEPT { x = reverse_endianness(x); } inline void reverse_endianness_in_place(int64_t& x) BOOST_NOEXCEPT
inline void reverse_endianness_in_place(uint64_t& x) BOOST_NOEXCEPT { x = reverse_endianness(x); } { x = reverse_endianness(x); }
inline void reverse_endianness_in_place(float& x) BOOST_NOEXCEPT { x = reverse_endianness(x); } inline void reverse_endianness_in_place(uint8_t& x) BOOST_NOEXCEPT
inline void reverse_endianness_in_place(double& x) BOOST_NOEXCEPT { x = reverse_endianness(x); } { x = reverse_endianness(x); }
inline void reverse_endianness_in_place(uint16_t& x) BOOST_NOEXCEPT
{ x = reverse_endianness(x); }
inline void reverse_endianness_in_place(uint32_t& x) BOOST_NOEXCEPT
{ x = reverse_endianness(x); }
inline void reverse_endianness_in_place(uint64_t& x) BOOST_NOEXCEPT
{ x = reverse_endianness(x); }
inline void reverse_endianness_in_place(float& x) BOOST_NOEXCEPT
{ x = reverse_endianness(x); }
inline void reverse_endianness_in_place(double& x) BOOST_NOEXCEPT
{ x = reverse_endianness(x); }
// reverse in place unless native endianness is big // reverse in place unless native endianness is big
// Effects: none if native endian order is big, otherwise reverse_endianness_in_place(x) // Effects: none if native endian order is big,
// otherwise reverse_endianness_in_place(x)
template <class Reversible> template <class Reversible>
# ifdef BOOST_BIG_ENDIAN # ifdef BOOST_BIG_ENDIAN
inline void big_to_native_in_place(Reversible&) BOOST_NOEXCEPT {} inline void big_to_native_in_place(Reversible&) BOOST_NOEXCEPT {}
# else # else
inline void big_to_native_in_place(Reversible& x) BOOST_NOEXCEPT { reverse_endianness_in_place(x); } inline void big_to_native_in_place(Reversible& x) BOOST_NOEXCEPT
{ reverse_endianness_in_place(x); }
# endif # endif
template <class Reversible> template <class Reversible>
# ifdef BOOST_BIG_ENDIAN # ifdef BOOST_BIG_ENDIAN
inline void native_to_big_in_place(Reversible&) BOOST_NOEXCEPT {} inline void native_to_big_in_place(Reversible&) BOOST_NOEXCEPT {}
# else # else
inline void native_to_big_in_place(Reversible& x) BOOST_NOEXCEPT { reverse_endianness_in_place(x); } inline void native_to_big_in_place(Reversible& x) BOOST_NOEXCEPT
{ reverse_endianness_in_place(x); }
# endif # endif
// reverse in place unless native endianness is little // reverse in place unless native endianness is little
// Effects: none if native endian order is little, otherwise reverse_endianness_in_place(x) // Effects: none if native endian order is little,
// otherwise reverse_endianness_in_place(x)
template <class Reversible> template <class Reversible>
# ifdef BOOST_LITTLE_ENDIAN # ifdef BOOST_LITTLE_ENDIAN
inline void little_to_native_in_place(Reversible&) BOOST_NOEXCEPT {} inline void little_to_native_in_place(Reversible&) BOOST_NOEXCEPT {}
# else # else
inline void little_to_native_in_place(Reversible& x) BOOST_NOEXCEPT { reverse_endianness_in_place(x); } inline void little_to_native_in_place(Reversible& x) BOOST_NOEXCEPT
{ reverse_endianness_in_place(x); }
# endif # endif
template <class Reversible> template <class Reversible>
# ifdef BOOST_LITTLE_ENDIAN # ifdef BOOST_LITTLE_ENDIAN
inline void native_to_little_in_place(Reversible&) BOOST_NOEXCEPT {} inline void native_to_little_in_place(Reversible&) BOOST_NOEXCEPT {}
# else # else
inline void native_to_little_in_place(Reversible& x) BOOST_NOEXCEPT { reverse_endianness_in_place(x); } inline void native_to_little_in_place(Reversible& x) BOOST_NOEXCEPT
{ reverse_endianness_in_place(x); }
# endif # endif
namespace detail namespace detail
{ {
// Primary template and specializations to support generic reverse_endianness_in_place(). // Primary template and specializations support generic
// reverse_endianness_in_place().
// See rationale in reverse_endianness_in_place() below. // See rationale in reverse_endianness_in_place() below.
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, class Reversible> template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, class Reversible>
class converter; // primary template class converter; // primary template

View File

@@ -1,4 +1,4 @@
// converter_test.cpp ----------------------------------------------------------------// // conversion_test.cpp ---------------------------------------------------------------//
// Copyright Beman Dawes 2010 // Copyright Beman Dawes 2010
@@ -134,122 +134,110 @@ namespace
big_value(big); big_value(big);
little_value(little); little_value(little);
BOOST_TEST_EQ(be::reverse_value(big), little); // one-way tests
BOOST_TEST_EQ(be::reverse_value(little), big);
BOOST_TEST_EQ(be::detail::reverse_value<T>(big), little);
BOOST_TEST_EQ(be::detail::reverse_value<T>(little), big);
BOOST_TEST_EQ(be::big_endian_value(native), big); BOOST_TEST_EQ(be::reverse_endianness(big), little);
BOOST_TEST_EQ(be::big_endian_value<T>(native), big); BOOST_TEST_EQ(be::reverse_endianness(little), big);
BOOST_TEST_EQ(be::little_endian_value(native), little); // BOOST_TEST_EQ(be::reverse<T>(big), little);
BOOST_TEST_EQ(be::little_endian_value<T>(native), little); // BOOST_TEST_EQ(be::reverse<T>(little), big);
BOOST_TEST_EQ(be::big_endian_value(be::big_endian_value(native)), native);
BOOST_TEST_EQ(be::big_endian_value(be::big_endian_value(big)), big); BOOST_TEST_EQ(be::native_to_big(native), big);
BOOST_TEST_EQ(be::big_endian_value(be::big_endian_value(little)), little); // BOOST_TEST_EQ(be::native_to_big<T>(native), big);
BOOST_TEST_EQ(be::little_endian_value(be::little_endian_value(native)), native); BOOST_TEST_EQ(be::native_to_little(native), little);
BOOST_TEST_EQ(be::little_endian_value(be::little_endian_value(big)), big); // BOOST_TEST_EQ(be::native_to_little<T>(native), little);
BOOST_TEST_EQ(be::little_endian_value(be::little_endian_value(little)), little);
// round-trip tests
BOOST_TEST_EQ(be::big_to_native(be::native_to_big(native)), native);
BOOST_TEST_EQ(be::native_to_big(be::big_to_native(big)), big);
BOOST_TEST_EQ(be::big_to_native(be::native_to_big(little)), little);
BOOST_TEST_EQ(be::little_to_native(be::native_to_little(native)), native);
BOOST_TEST_EQ(be::little_to_native(be::native_to_little(big)), big);
BOOST_TEST_EQ(be::little_to_native(be::native_to_little(little)), little);
# ifdef BOOST_BIG_ENDIAN # ifdef BOOST_BIG_ENDIAN
BOOST_TEST_EQ(be::reverse_value(native), little); BOOST_TEST_EQ(be::reverse_endianness(native), little);
BOOST_TEST_EQ(be::detail::reverse_value<T>(native), little); // BOOST_TEST_EQ(be::detail::reverse<T>(native), little);
BOOST_TEST_EQ(be::big_endian_value(big), big); BOOST_TEST_EQ(be::big_to_native(big), big);
BOOST_TEST_EQ(be::big_endian_value<T>(big), big); // BOOST_TEST_EQ(be::big_endian<T>(big), big);
BOOST_TEST_EQ(be::big_endian_value(little), little); BOOST_TEST_EQ(be::little_to_native(little), little);
BOOST_TEST_EQ(be::big_endian_value<T>(little), little); // BOOST_TEST_EQ(be::big_endian<T>(little), little);
# else # else
BOOST_TEST_EQ(be::reverse_value(native), big); BOOST_TEST_EQ(be::reverse_endianness(native), big);
BOOST_TEST_EQ(be::detail::reverse_value<T>(native), big); // BOOST_TEST_EQ(be::detail::reverse<T>(native), big);
BOOST_TEST_EQ(be::big_endian_value(big), little); BOOST_TEST_EQ(be::big_to_native(big), little);
BOOST_TEST_EQ(be::big_endian_value<T>(big), little); // BOOST_TEST_EQ(be::big_endian<T>(big), little);
BOOST_TEST_EQ(be::big_endian_value(little), big); BOOST_TEST_EQ(be::native_to_big(little), big);
BOOST_TEST_EQ(be::big_endian_value<T>(little), big); // BOOST_TEST_EQ(be::big_endian<T>(little), big);
# endif # endif
// compile time order determination // compile time order determination test
BOOST_TEST_EQ((be::convert_value<be::order::big, be::order::big>(big)), big); BOOST_TEST_EQ((be::reverse_endianness<be::order::big, be::order::big>(big)), big);
BOOST_TEST_EQ((be::convert_value<be::order::little, be::order::little>(little)), little); BOOST_TEST_EQ((be::reverse_endianness<be::order::little,
BOOST_TEST_EQ((be::convert_value<be::order::native, be::order::native>(native)), native); be::order::little>(little)), little);
BOOST_TEST_EQ((be::reverse_endianness<be::order::native,
be::order::native>(native)), native);
BOOST_TEST_EQ((be::convert_value<be::order::big, be::order::little>(big)), little); BOOST_TEST_EQ((be::reverse_endianness<be::order::big,
BOOST_TEST_EQ((be::convert_value<be::order::big, be::order::native>(big)), native); be::order::little>(big)), little);
BOOST_TEST_EQ((be::convert_value<be::order::little, be::order::big>(little)), big); BOOST_TEST_EQ((be::reverse_endianness<be::order::big,
BOOST_TEST_EQ((be::convert_value<be::order::little, be::order::native>(little)), native); be::order::native>(big)), native);
BOOST_TEST_EQ((be::convert_value<be::order::native, be::order::big>(native)), big); BOOST_TEST_EQ((be::reverse_endianness<be::order::little,
BOOST_TEST_EQ((be::convert_value<be::order::native, be::order::little>(native)), little); be::order::big>(little)), big);
BOOST_TEST_EQ((be::reverse_endianness<be::order::little,
be::order::native>(little)), native);
BOOST_TEST_EQ((be::reverse_endianness<be::order::native,
be::order::big>(native)), big);
BOOST_TEST_EQ((be::reverse_endianness<be::order::native,
be::order::little>(native)), little);
// runtime order determination // runtime order determination test
BOOST_TEST_EQ((be::convert_value(big, be::order::big, be::order::big)), big); BOOST_TEST_EQ((be::reverse_endianness(big, be::order::big, be::order::big)), big);
BOOST_TEST_EQ((be::convert_value(little, be::order::little, be::order::little)), little); BOOST_TEST_EQ((be::reverse_endianness(little, be::order::little,
BOOST_TEST_EQ((be::convert_value(native, be::order::native, be::order::native)), native); be::order::little)), little);
BOOST_TEST_EQ((be::reverse_endianness(native, be::order::native,
be::order::native)), native);
BOOST_TEST_EQ((be::convert_value(big, be::order::big, be::order::little)), little); BOOST_TEST_EQ((be::reverse_endianness(big, be::order::big, be::order::little)),
BOOST_TEST_EQ((be::convert_value(big, be::order::big, be::order::native)), native); little);
BOOST_TEST_EQ((be::convert_value(little, be::order::little, be::order::big)), big); BOOST_TEST_EQ((be::reverse_endianness(big, be::order::big, be::order::native)),
BOOST_TEST_EQ((be::convert_value(little, be::order::little, be::order::native)), native); native);
BOOST_TEST_EQ((be::convert_value(native, be::order::native, be::order::big)), big); BOOST_TEST_EQ((be::reverse_endianness(little, be::order::little, be::order::big)),
BOOST_TEST_EQ((be::convert_value(native, be::order::native, be::order::little)), little); big);
BOOST_TEST_EQ((be::reverse_endianness(little, be::order::little, be::order::native)),
native);
BOOST_TEST_EQ((be::reverse_endianness(native, be::order::native, be::order::big)),
big);
BOOST_TEST_EQ((be::reverse_endianness(native, be::order::native, be::order::little)),
little);
// light test of modify-in-place functions // // light test of modify-in-place functions
//
// T x;
//
// x = big; be::reverse_endianness(x); BOOST_TEST_EQ(x, little);
// x = big; be::convert<be::order::big, be::order::little>(x); BOOST_TEST_EQ(x, little);
// x = big; be::convert(x, be::order::big, be::order::little); BOOST_TEST_EQ(x, little);
//
//# ifdef BOOST_BIG_ENDIAN
// x = native; be::big_endian(x); BOOST_TEST_EQ(x, big);
// x = big; be::big_endian(x); BOOST_TEST_EQ(x, big);
// x = little; be::big_endian(x); BOOST_TEST_EQ(x, little);
// x = native; be::little_endian(x); BOOST_TEST_EQ(x, little);
// x = big; be::little_endian(x); BOOST_TEST_EQ(x, little);
// x = little; be::little_endian(x); BOOST_TEST_EQ(x, big);
//# else
// x = native; be::big_endian(x); BOOST_TEST_EQ(x, big);
// x = big; be::big_endian(x); BOOST_TEST_EQ(x, little);
// x = little; be::big_endian(x); BOOST_TEST_EQ(x, big);
// x = native; be::little_endian(x); BOOST_TEST_EQ(x, little);
// x = big; be::little_endian(x); BOOST_TEST_EQ(x, big);
// x = little; be::little_endian(x); BOOST_TEST_EQ(x, little);
//# endif
T x;
x = big; be::reverse(x); BOOST_TEST_EQ(x, little);
x = big; be::convert<be::order::big, be::order::little>(x); BOOST_TEST_EQ(x, little);
x = big; be::convert(x, be::order::big, be::order::little); BOOST_TEST_EQ(x, little);
# ifdef BOOST_BIG_ENDIAN
x = native; be::big_endian(x); BOOST_TEST_EQ(x, big);
x = big; be::big_endian(x); BOOST_TEST_EQ(x, big);
x = little; be::big_endian(x); BOOST_TEST_EQ(x, little);
x = native; be::little_endian(x); BOOST_TEST_EQ(x, little);
x = big; be::little_endian(x); BOOST_TEST_EQ(x, little);
x = little; be::little_endian(x); BOOST_TEST_EQ(x, big);
# else
x = native; be::big_endian(x); BOOST_TEST_EQ(x, big);
x = big; be::big_endian(x); BOOST_TEST_EQ(x, little);
x = little; be::big_endian(x); BOOST_TEST_EQ(x, big);
x = native; be::little_endian(x); BOOST_TEST_EQ(x, little);
x = big; be::little_endian(x); BOOST_TEST_EQ(x, big);
x = little; be::little_endian(x); BOOST_TEST_EQ(x, little);
# endif
// synonym test
x = big; x = be::bswap(x); BOOST_TEST_EQ(x, little);
x = big; be::mbswap(x); BOOST_TEST_EQ(x, little);
# ifdef BOOST_BIG_ENDIAN
BOOST_TEST_EQ(be::htobe(native), big);
BOOST_TEST_EQ(be::htole(native), little);
BOOST_TEST_EQ(be::betoh(big), big);
BOOST_TEST_EQ(be::letoh(big), little);
BOOST_TEST_EQ(be::betoh(little), little);
BOOST_TEST_EQ(be::letoh(little), big);
x = native; be::mhtobe(x); BOOST_TEST_EQ(x, big);
x = native; be::mhtole(x); BOOST_TEST_EQ(x, little);
x = big; be::mbetoh(x); BOOST_TEST_EQ(x, big);
x = big; be::mletoh(x); BOOST_TEST_EQ(x, little);
x = little; be::mbetoh(x); BOOST_TEST_EQ(x, little);
x = little; be::mletoh(x); BOOST_TEST_EQ(x, big);
# else
BOOST_TEST_EQ(be::htobe(native), big);
BOOST_TEST_EQ(be::htole(native), little);
BOOST_TEST_EQ(be::betoh(big), little);
BOOST_TEST_EQ(be::letoh(big), big);
BOOST_TEST_EQ(be::betoh(little), big);
BOOST_TEST_EQ(be::letoh(little), little);
x = native; be::mhtobe(x); BOOST_TEST_EQ(x, big);
x = native; be::mhtole(x); BOOST_TEST_EQ(x, little);
x = big; be::mbetoh(x); BOOST_TEST_EQ(x, little);
x = big; be::mletoh(x); BOOST_TEST_EQ(x, big);
x = little; be::mbetoh(x); BOOST_TEST_EQ(x, big);
x = little; be::mletoh(x); BOOST_TEST_EQ(x, little);
# endif
} }
} // unnamed namespace } // unnamed namespace

View File

@@ -18,6 +18,9 @@
<Platform>x64</Platform> <Platform>x64</Platform>
</ProjectConfiguration> </ProjectConfiguration>
</ItemGroup> </ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\conversion_test.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<ProjectGuid>{EAE18F4D-AAF2-4C19-86FB-1144B5BD5993}</ProjectGuid> <ProjectGuid>{EAE18F4D-AAF2-4C19-86FB-1144B5BD5993}</ProjectGuid>
<Keyword>Win32Proj</Keyword> <Keyword>Win32Proj</Keyword>
@@ -158,9 +161,6 @@
<Message>Executing test...</Message> <Message>Executing test...</Message>
</PostBuildEvent> </PostBuildEvent>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\converter_test.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>