From 89d13e9d026cc9e5dc0e4738689a3328000f23b8 Mon Sep 17 00:00:00 2001 From: Beman Date: Sun, 12 May 2013 10:16:35 -0400 Subject: [PATCH] Add modify in place interface, implementation. No tests yet. --- include/boost/endian/converters.hpp | 77 +++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/include/boost/endian/converters.hpp b/include/boost/endian/converters.hpp index ee3ed95..af6c43a 100644 --- a/include/boost/endian/converters.hpp +++ b/include/boost/endian/converters.hpp @@ -83,6 +83,40 @@ namespace endian T convert_value(T from, BOOST_SCOPED_ENUM(order) from_order, BOOST_SCOPED_ENUM(order) to_order) BOOST_NOEXCEPT; +//--------------------------------------------------------------------------------------// +// modify in place interface // +//--------------------------------------------------------------------------------------// + + // reverse byte order (i.e. endianness) + // + inline void reverse(int16_t& x) BOOST_NOEXCEPT; + inline void reverse(int32_t& x) BOOST_NOEXCEPT; + inline void reverse(int64_t& x) BOOST_NOEXCEPT; + inline void reverse(uint16_t& x) BOOST_NOEXCEPT; + inline void reverse(uint32_t& x) BOOST_NOEXCEPT; + inline void reverse(uint64_t& x) BOOST_NOEXCEPT; + inline void reverse(float& x) BOOST_NOEXCEPT; + inline void reverse(double& x) BOOST_NOEXCEPT; + + // reverse unless native endianness is big + template + inline void big_endianx(T& x) BOOST_NOEXCEPT; + // Effects: none if native endian order is big, otherwise reverse(x) + + // reverse unless native endianness is little + // possible names: reverse_unless_native_little, reverse_bytes_unless_little, reverse_unless_little + template + inline void little_endian(T& x) BOOST_NOEXCEPT; + // Effects: none if native endian order is little, otherwise reverse(x); + + // compile-time generic byte order conversion + template + void convert(T& x) BOOST_NOEXCEPT; + + // runtime byte-order conversion + template + void convert(T& x, BOOST_SCOPED_ENUM(order) from_order, + BOOST_SCOPED_ENUM(order) to_order) BOOST_NOEXCEPT; //----------------------------------- end synopsis -------------------------------------// @@ -332,6 +366,49 @@ namespace endian return actual_order(to_order) == order::little ? from : reverse_value(from); } +//--------------------------------------------------------------------------------------// +// modify in place implementation // +//--------------------------------------------------------------------------------------// + + // reverse byte order (i.e. endianness) + // + inline void reverse(int16_t& x) BOOST_NOEXCEPT {x = reverse_value(x);} + inline void reverse(int32_t& x) BOOST_NOEXCEPT {x = reverse_value(x);} + inline void reverse(int64_t& x) BOOST_NOEXCEPT {x = reverse_value(x);} + inline void reverse(uint16_t& x) BOOST_NOEXCEPT {x = reverse_value(x);} + inline void reverse(uint32_t& x) BOOST_NOEXCEPT {x = reverse_value(x);} + inline void reverse(uint64_t& x) BOOST_NOEXCEPT {x = reverse_value(x);} + inline void reverse(float& x) BOOST_NOEXCEPT {x = reverse_value(x);} + inline void reverse(double& x) BOOST_NOEXCEPT {x = reverse_value(x);} + + // reverse unless native endianness is big + template + inline void big_endianx(T& x) BOOST_NOEXCEPT + { +# ifndef BOOST_BIG_ENDIAN + reverse(x); +# endif + } + + // reverse bytes unless native endianness is little + template + inline void little_endian(T& x) BOOST_NOEXCEPT + // Effects: none if native endian order is little, otherwise reverse(x) + { +# ifndef BOOST_LITTLE_ENDIAN + x = reverse_value(x); +# endif + } + + // compile-time generic byte order conversion + template + void convert(T& x) BOOST_NOEXCEPT {x = convert_value(x);} + + // runtime byte-order conversion + template + void convert(T& x, BOOST_SCOPED_ENUM(order) from_order, + BOOST_SCOPED_ENUM(order) to_order) BOOST_NOEXCEPT {x = convert_value(x, from_order, to_order);} + } // namespace endian } // namespace boost