From 193492c51800d970b9433290dde7ad3f1d29880e Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 17 Feb 2018 00:47:28 -0600 Subject: [PATCH] Add ox::LittleEndian template --- deps/ox/src/ox/std/byteswap.hpp | 95 ++++++++++----------------------- 1 file changed, 29 insertions(+), 66 deletions(-) diff --git a/deps/ox/src/ox/std/byteswap.hpp b/deps/ox/src/ox/std/byteswap.hpp index bd86ffd8..08dcf9bb 100644 --- a/deps/ox/src/ox/std/byteswap.hpp +++ b/deps/ox/src/ox/std/byteswap.hpp @@ -60,36 +60,8 @@ inline uint64_t byteSwap(uint64_t i) { /** * Takes an int and byte swaps if the platform is big endian. */ -inline int8_t bigEndianAdapt(int8_t i) { - return i; -} - -/** - * Takes an int and byte swaps if the platform is big endian. - */ -inline int16_t bigEndianAdapt(int16_t i) { -#ifdef __BIG_ENDIAN__ - return byteSwap(i); -#else - return i; -#endif -} - -/** - * Takes an int and byte swaps if the platform is big endian. - */ -inline int32_t bigEndianAdapt(int32_t i) { -#ifdef __BIG_ENDIAN__ - return byteSwap(i); -#else - return i; -#endif -} - -/** - * Takes an int and byte swaps if the platform is big endian. - */ -inline int64_t bigEndianAdapt(int64_t i) { +template +inline T bigEndianAdapt(T i) { #ifdef __BIG_ENDIAN__ return byteSwap(i); #else @@ -98,44 +70,35 @@ inline int64_t bigEndianAdapt(int64_t i) { } -/** - * Takes an int and byte swaps if the platform is big endian. - */ -inline uint8_t bigEndianAdapt(uint8_t i) { - return i; -} +template +class LittleEndian { + private: + T m_value; -/** - * Takes an int and byte swaps if the platform is big endian. - */ -inline uint16_t bigEndianAdapt(uint16_t i) { -#ifdef __BIG_ENDIAN__ - return byteSwap(i); -#else - return i; -#endif -} + public: + inline LittleEndian() = default; -/** - * Takes an int and byte swaps if the platform is big endian. - */ -inline uint32_t bigEndianAdapt(uint32_t i) { -#ifdef __BIG_ENDIAN__ - return byteSwap(i); -#else - return i; -#endif -} + inline LittleEndian(T value) { + m_value = ox::bigEndianAdapt(value); + } -/** - * Takes an int and byte swaps if the platform is big endian. - */ -inline uint64_t bigEndianAdapt(uint64_t i) { -#ifdef __BIG_ENDIAN__ - return byteSwap(i); -#else - return i; -#endif -} + inline T operator=(T value) { + m_value = ox::bigEndianAdapt(value); + return value; + } + + inline operator T() { + return ox::bigEndianAdapt(m_value); + } + + inline bool operator==(T value) { + return value == ox::bigEndianAdapt(m_value); + } + + inline bool operator!=(T value) { + return value != ox::bigEndianAdapt(m_value); + } + +}; }