Add synonyms based on names popularized by BSD, e.g. OS X, Linux.

This commit is contained in:
Beman
2013-05-15 08:51:05 -04:00
parent 09b361e161
commit 8b4bd2adfc
2 changed files with 52 additions and 1 deletions

View File

@ -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 <class T> T bswap(T x) {return reverse_value(x);}
template <class T> T htobe(T host_) {return big_endian_value(host_);}
template <class T> T htole(T host_) {return little_endian_value(host_);}
template <class T> T betoh(T big_endian_) {return big_endian_value(big_endian_);}
template <class T> T letoh(T little_endian_) {return little_endian_value(little_endian_);}
// compile-time generic byte order conversion
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, class ReversibleValue >
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 <class T> void mbswap(T& x) {reverse(x);}
template <class T> void mhtobe(T& host_) {big_endian(host_);}
template <class T> void mhtole(T& host_) {little_endian(host_);}
template <class T> void mbetoh(T& big_endian_) {big_endian(big_endian_);}
template <class T> void mletoh(T& little_endian_) {little_endian(little_endian_);}
// compile-time generic byte order conversion
template <BOOST_SCOPED_ENUM(order) From, BOOST_SCOPED_ENUM(order) To, class Reversible>
void convert(Reversible& x) BOOST_NOEXCEPT;

View File

@ -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