From 6cf5464cd2b7d53a8471516fc54364fb3ffa26d2 Mon Sep 17 00:00:00 2001 From: Beman Date: Mon, 13 May 2013 14:16:54 -0400 Subject: [PATCH] Distinguish between ReversibleValue and Reversible in both name and behavior. --- include/boost/endian/converters.hpp | 141 ++++++++++++++++++++++------ 1 file changed, 114 insertions(+), 27 deletions(-) diff --git a/include/boost/endian/converters.hpp b/include/boost/endian/converters.hpp index b4ff033..97f7ade 100644 --- a/include/boost/endian/converters.hpp +++ b/include/boost/endian/converters.hpp @@ -16,16 +16,6 @@ #include #include -/* - - TODO: - - * Reversible means reverse_value(x) well-formed in some places, reverse(x) well-formed - in other places. - * Inplace converts should should be implemented in terms of reverse(), not convert_value() - -*/ - //------------------------------------- synopsis ---------------------------------------// namespace boost @@ -70,27 +60,27 @@ namespace endian // reverse bytes unless native endianness is big // possible names: reverse_unless_native_big, reverse_value_unless_big, reverse_unless_big - template - inline Reversible big_endian_value(Reversible x) BOOST_NOEXCEPT; + template + inline ReversibleValue big_endian_value(ReversibleValue x) BOOST_NOEXCEPT; // Return: x if native endian order is big, otherwise reverse_value(x) // reverse bytes unless native endianness is little // possible names: reverse_unless_native_little, reverse_value_unless_little, reverse_unless_little - template - inline Reversible little_endian_value(Reversible x) BOOST_NOEXCEPT; + template + inline ReversibleValue little_endian_value(ReversibleValue x) BOOST_NOEXCEPT; // Return: x if native endian order is little, otherwise reverse_value(x); // compile-time generic byte order conversion - template - Reversible convert_value(Reversible from) BOOST_NOEXCEPT; + template + ReversibleValue convert_value(ReversibleValue from) BOOST_NOEXCEPT; // runtime actual byte-order determination inline BOOST_SCOPED_ENUM(order) actual_order(BOOST_SCOPED_ENUM(order) o) BOOST_NOEXCEPT; // Return: o if o != native, otherwise big or little depending on native ordering // runtime byte-order conversion - template - Reversible convert_value(Reversible from, BOOST_SCOPED_ENUM(order) from_order, + template + ReversibleValue convert_value(ReversibleValue from, BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order) BOOST_NOEXCEPT; //--------------------------------------------------------------------------------------// @@ -246,8 +236,8 @@ namespace endian } } - template - inline Reversible big_endian_value(Reversible x) BOOST_NOEXCEPT + template + inline ReversibleValue big_endian_value(ReversibleValue x) BOOST_NOEXCEPT { # ifdef BOOST_BIG_ENDIAN return x; @@ -256,8 +246,8 @@ namespace endian # endif } - template - inline Reversible little_endian_value(Reversible x) BOOST_NOEXCEPT + template + inline ReversibleValue little_endian_value(ReversibleValue x) BOOST_NOEXCEPT { # ifdef BOOST_LITTLE_ENDIAN return x; @@ -365,8 +355,8 @@ namespace endian ; } - template - Reversible convert_value(Reversible from, BOOST_SCOPED_ENUM(order) from_order, + template + ReversibleValue convert_value(ReversibleValue from, BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order) BOOST_NOEXCEPT { if (actual_order(from_order) == order::big) @@ -411,13 +401,110 @@ namespace endian } // compile-time generic byte order conversion - template - void convert(Reversible& x) BOOST_NOEXCEPT {x = convert_value(x);} + template<> inline void convert(int16_t&) BOOST_NOEXCEPT {} + template<> inline void convert(int16_t&) BOOST_NOEXCEPT {} + template<> inline void convert(int16_t&) BOOST_NOEXCEPT {} + + template<> inline void convert(uint16_t&) BOOST_NOEXCEPT {} + template<> inline void convert(uint16_t&) BOOST_NOEXCEPT {} + template<> inline void convert(uint16_t&) BOOST_NOEXCEPT {} + + template<> inline void convert(int32_t&) BOOST_NOEXCEPT {} + template<> inline void convert(int32_t&) BOOST_NOEXCEPT {} + template<> inline void convert(int32_t&) BOOST_NOEXCEPT {} + + template<> inline void convert(uint32_t&) BOOST_NOEXCEPT {} + template<> inline void convert(uint32_t&) BOOST_NOEXCEPT {} + template<> inline void convert(uint32_t&) BOOST_NOEXCEPT {} + + template<> inline void convert(int64_t&) BOOST_NOEXCEPT {} + template<> inline void convert(int64_t&) BOOST_NOEXCEPT {} + template<> inline void convert(int64_t&) BOOST_NOEXCEPT {} + + template<> inline void convert(uint64_t&) BOOST_NOEXCEPT {} + template<> inline void convert(uint64_t&) BOOST_NOEXCEPT {} + template<> inline void convert(uint64_t&) BOOST_NOEXCEPT {} + + template<> inline void convert(float&) BOOST_NOEXCEPT {} + template<> inline void convert(float&) BOOST_NOEXCEPT {} + template<> inline void convert(float&) BOOST_NOEXCEPT {} + + template<> inline void convert(double&) BOOST_NOEXCEPT {} + template<> inline void convert(double&) BOOST_NOEXCEPT {} + template<> inline void convert(double&) BOOST_NOEXCEPT {} + + template<> inline void convert(int16_t& x) BOOST_NOEXCEPT {reverse(x);} + template<> inline void convert(int16_t& x) BOOST_NOEXCEPT {big_endian(x);} + template<> inline void convert(int16_t& x) BOOST_NOEXCEPT {reverse(x);} + template<> inline void convert(int16_t& x) BOOST_NOEXCEPT {little_endian(x);} + template<> inline void convert(int16_t& x) BOOST_NOEXCEPT {big_endian(x);} + template<> inline void convert(int16_t& x) BOOST_NOEXCEPT {little_endian(x);} + + template<> inline void convert(uint16_t& x) BOOST_NOEXCEPT {reverse(x);} + template<> inline void convert(uint16_t& x) BOOST_NOEXCEPT {big_endian(x);} + template<> inline void convert(uint16_t& x) BOOST_NOEXCEPT {reverse(x);} + template<> inline void convert(uint16_t& x) BOOST_NOEXCEPT {little_endian(x);} + template<> inline void convert(uint16_t& x) BOOST_NOEXCEPT {big_endian(x);} + template<> inline void convert(uint16_t& x) BOOST_NOEXCEPT {little_endian(x);} + + template<> inline void convert(int32_t& x) BOOST_NOEXCEPT {reverse(x);} + template<> inline void convert(int32_t& x) BOOST_NOEXCEPT {big_endian(x);} + template<> inline void convert(int32_t& x) BOOST_NOEXCEPT {reverse(x);} + template<> inline void convert(int32_t& x) BOOST_NOEXCEPT {little_endian(x);} + template<> inline void convert(int32_t& x) BOOST_NOEXCEPT {big_endian(x);} + template<> inline void convert(int32_t& x) BOOST_NOEXCEPT {little_endian(x);} + + template<> inline void convert(uint32_t& x) BOOST_NOEXCEPT {reverse(x);} + template<> inline void convert(uint32_t& x) BOOST_NOEXCEPT {big_endian(x);} + template<> inline void convert(uint32_t& x) BOOST_NOEXCEPT {reverse(x);} + template<> inline void convert(uint32_t& x) BOOST_NOEXCEPT {little_endian(x);} + template<> inline void convert(uint32_t& x) BOOST_NOEXCEPT {big_endian(x);} + template<> inline void convert(uint32_t& x) BOOST_NOEXCEPT {little_endian(x);} + + template<> inline void convert(int64_t& x) BOOST_NOEXCEPT {reverse(x);} + template<> inline void convert(int64_t& x) BOOST_NOEXCEPT {big_endian(x);} + template<> inline void convert(int64_t& x) BOOST_NOEXCEPT {reverse(x);} + template<> inline void convert(int64_t& x) BOOST_NOEXCEPT {little_endian(x);} + template<> inline void convert(int64_t& x) BOOST_NOEXCEPT {big_endian(x);} + template<> inline void convert(int64_t& x) BOOST_NOEXCEPT {little_endian(x);} + + template<> inline void convert(uint64_t& x) BOOST_NOEXCEPT {reverse(x);} + template<> inline void convert(uint64_t& x) BOOST_NOEXCEPT {big_endian(x);} + template<> inline void convert(uint64_t& x) BOOST_NOEXCEPT {reverse(x);} + template<> inline void convert(uint64_t& x) BOOST_NOEXCEPT {little_endian(x);} + template<> inline void convert(uint64_t& x) BOOST_NOEXCEPT {big_endian(x);} + template<> inline void convert(uint64_t& x) BOOST_NOEXCEPT {little_endian(x);} + + template<> inline void convert(float& x) BOOST_NOEXCEPT {reverse(x);} + template<> inline void convert(float& x) BOOST_NOEXCEPT {big_endian(x);} + template<> inline void convert(float& x) BOOST_NOEXCEPT {reverse(x);} + template<> inline void convert(float& x) BOOST_NOEXCEPT {little_endian(x);} + template<> inline void convert(float& x) BOOST_NOEXCEPT {big_endian(x);} + template<> inline void convert(float& x) BOOST_NOEXCEPT {little_endian(x);} + + template<> inline void convert(double& x) BOOST_NOEXCEPT {reverse(x);} + template<> inline void convert(double& x) BOOST_NOEXCEPT {big_endian(x);} + template<> inline void convert(double& x) BOOST_NOEXCEPT {reverse(x);} + template<> inline void convert(double& x) BOOST_NOEXCEPT {little_endian(x);} + template<> inline void convert(double& x) BOOST_NOEXCEPT {big_endian(x);} + template<> inline void convert(double& x) BOOST_NOEXCEPT {little_endian(x);} // runtime byte-order conversion template void convert(Reversible& x, BOOST_SCOPED_ENUM(order) from_order, - BOOST_SCOPED_ENUM(order) to_order) BOOST_NOEXCEPT {x = convert_value(x, from_order, to_order);} + BOOST_SCOPED_ENUM(order) to_order) BOOST_NOEXCEPT + { + if (actual_order(from_order) == order::big) + { + if (actual_order(to_order) != order::big) + reverse(x); + } + else // actual from_order is little + { + if (actual_order(to_order) != order::little) + reverse(x); + } + } } // namespace endian } // namespace boost