diff --git a/src/xenia/base/byte_order.h b/src/xenia/base/byte_order.h index 9ca9505ce..62a500bfc 100644 --- a/src/xenia/base/byte_order.h +++ b/src/xenia/base/byte_order.h @@ -27,6 +27,27 @@ #include #endif +#if !__cpp_lib_endian +// Polyfill +#ifdef __BYTE_ORDER__ +namespace std { +enum class endian { + little = __ORDER_LITTLE_ENDIAN__, + big = __ORDER_BIG_ENDIAN__, + native = __BYTE_ORDER__ +}; +} +#else +// Hardcode to little endian for now +namespace std { +enum class endian { little = 0, big = 1, native = 0 }; +} +#endif +#endif +// Check for mixed endian +static_assert((std::endian::native == std::endian::big) || + (std::endian::native == std::endian::little)); + namespace xe { #if XE_PLATFORM_WIN32 @@ -88,34 +109,46 @@ inline T byte_swap(T value) { } } -template -struct be { - be() = default; - be(const T& src) : value(xe::byte_swap(src)) {} // NOLINT(runtime/explicit) - be(const be& other) { value = other.value; } // NOLINT(runtime/explicit) - operator T() const { return xe::byte_swap(value); } +template +struct endian_store { + endian_store() = default; + endian_store(const T& src) { + if constexpr (std::endian::native == E) { + value = src; + } else { + value = xe::byte_swap(src); + } + } + endian_store(const endian_store& other) { value = other.value; } + operator T() const { + if constexpr (std::endian::native == E) { + return value; + } else { + return xe::byte_swap(value); + } + } - be& operator+=(int a) { + endian_store& operator+=(int a) { *this = *this + a; return *this; } - be& operator-=(int a) { + endian_store& operator-=(int a) { *this = *this - a; return *this; } - be& operator++() { + endian_store& operator++() { *this += 1; return *this; } // ++a - be operator++(int) { + endian_store operator++(int) { *this += 1; return (*this - 1); } // a++ - be& operator--() { + endian_store& operator--() { *this -= 1; return *this; } // --a - be operator--(int) { + endian_store operator--(int) { *this -= 1; return (*this + 1); } // a-- @@ -123,6 +156,11 @@ struct be { T value; }; +template +using be = endian_store; +template +using le = endian_store; + } // namespace xe #endif // XENIA_BASE_BYTE_ORDER_H_