From 8b4bd2adfcc27aaf24546ff95bcf1ac8938a19b2 Mon Sep 17 00:00:00 2001 From: Beman Date: Wed, 15 May 2013 08:51:05 -0400 Subject: [PATCH] Add synonyms based on names popularized by BSD, e.g. OS X, Linux. --- include/boost/endian/converters.hpp | 19 +++++++++++++++- test/converter_test.cpp | 34 +++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/include/boost/endian/converters.hpp b/include/boost/endian/converters.hpp index bfa1df1..023bd25 100644 --- a/include/boost/endian/converters.hpp +++ b/include/boost/endian/converters.hpp @@ -70,9 +70,17 @@ namespace endian inline ReversibleValue little_endian_value(ReversibleValue x) BOOST_NOEXCEPT; // Return: x if native endian order is little, otherwise reverse_value(x); + // synonyms based on names popularized by BSD, e.g. OS X, Linux + // "h" stands for "host" (i.e. native), "be" for "big endian", "le" for "little endian" + template T bswap(T x) {return reverse_value(x);} + template T htobe(T host_) {return big_endian_value(host_);} + template T htole(T host_) {return little_endian_value(host_);} + template T betoh(T big_endian_) {return big_endian_value(big_endian_);} + template T letoh(T little_endian_) {return little_endian_value(little_endian_);} + // compile-time generic byte order conversion template - ReversibleValue convert_value(ReversibleValue from) BOOST_NOEXCEPT; + 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; @@ -108,6 +116,15 @@ namespace endian inline void little_endian(Reversible& x) BOOST_NOEXCEPT; // Effects: none if native endian order is little, otherwise reverse(x); + // synonyms based on names popularized by BSD, e.g. OS X, Linux. + // "h" stands for "host" (i.e. native), "be" for "big endian", + // "le" for "little endian", "m" for "modify in place" + template void mbswap(T& x) {reverse(x);} + template void mhtobe(T& host_) {big_endian(host_);} + template void mhtole(T& host_) {little_endian(host_);} + template void mbetoh(T& big_endian_) {big_endian(big_endian_);} + template void mletoh(T& little_endian_) {little_endian(little_endian_);} + // compile-time generic byte order conversion template void convert(Reversible& x) BOOST_NOEXCEPT; diff --git a/test/converter_test.cpp b/test/converter_test.cpp index f48e032..b920e04 100644 --- a/test/converter_test.cpp +++ b/test/converter_test.cpp @@ -200,6 +200,40 @@ namespace 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), little); + BOOST_TEST_EQ(be::htole(native), big); + 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, little); + x = native; be::mhtole(x); BOOST_TEST_EQ(x, big); + 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