#pragma once #pragma warning(disable: 4739) template class be_t { static_assert(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8, "Bad be_t type"); T m_data; public: be_t() { } be_t(const T& value) { FromLE(value); } template be_t(const be_t& value) { FromBE(value.ToBE()); } T ToBE() const { return m_data; } T ToLE() const { T res; switch(sizeof(T)) { case 1: (u8&)res = (u8&)m_data; break; case 2: (u16&)res = _byteswap_ushort((u16&)m_data); break; case 4: (u32&)res = _byteswap_ulong((u32&)m_data); break; case 8: (u64&)res = _byteswap_uint64((u64&)m_data); break; default: assert(0); break; } return res; } void FromBE(const T& value) { m_data = value; } void FromLE(const T& value) { switch(sizeof(T)) { case 1: (u8&)m_data = (u8&)value; return; case 2: (u16&)m_data = _byteswap_ushort((u16&)value); return; case 4: (u32&)m_data = _byteswap_ulong((u32&)value); return; case 8: (u64&)m_data = _byteswap_uint64((u64&)value); return; } assert(0); m_data = value; } //template operator const T() const { return ToLE(); } template operator const be_t() const { be_t res; res.FromBE(ToBE()); return res; } be_t& operator = (const T& right) { FromLE(right); return *this; } be_t& operator = (const be_t& right) { m_data = right.m_data; return *this; } template be_t& operator += (T1 right) { return *this = T(*this) + right; } template be_t& operator -= (T1 right) { return *this = T(*this) - right; } template be_t& operator *= (T1 right) { return *this = T(*this) * right; } template be_t& operator /= (T1 right) { return *this = T(*this) / right; } template be_t& operator %= (T1 right) { return *this = T(*this) % right; } template be_t& operator &= (T1 right) { return *this = T(*this) & right; } template be_t& operator |= (T1 right) { return *this = T(*this) | right; } template be_t& operator ^= (T1 right) { return *this = T(*this) ^ right; } template be_t& operator <<= (T1 right) { return *this = T(*this) << right; } template be_t& operator >>= (T1 right) { return *this = T(*this) >> right; } template be_t& operator += (const be_t& right) { return *this = ToLE() + right.ToLE(); } template be_t& operator -= (const be_t& right) { return *this = ToLE() - right.ToLE(); } template be_t& operator *= (const be_t& right) { return *this = ToLE() * right.ToLE(); } template be_t& operator /= (const be_t& right) { return *this = ToLE() / right.ToLE(); } template be_t& operator %= (const be_t& right) { return *this = ToLE() % right.ToLE(); } template be_t& operator &= (const be_t& right) { return *this = ToBE() & right.ToBE(); } template be_t& operator |= (const be_t& right) { return *this = ToBE() | right.ToBE(); } template be_t& operator ^= (const be_t& right) { return *this = ToBE() ^ right.ToBE(); } template be_t operator & (const be_t& right) const { be_t res; res.FromBE(ToBE() & right.ToBE()); return res; } template be_t operator | (const be_t& right) const { be_t res; res.FromBE(ToBE() | right.ToBE()); return res; } template be_t operator ^ (const be_t& right) const { be_t res; res.FromBE(ToBE() ^ right.ToBE()); return res; } template bool operator == (T1 right) const { return ToLE() == right; } template bool operator != (T1 right) const { return !(*this == right); } template bool operator > (T1 right) const { return ToLE() > right; } template bool operator < (T1 right) const { return ToLE() < right; } template bool operator >= (T1 right) const { return ToLE() >= right; } template bool operator <= (T1 right) const { return ToLE() <= right; } template bool operator == (const be_t& right) const { return ToBE() == right.ToBE(); } template bool operator != (const be_t& right) const { return !(*this == right); } template bool operator > (const be_t& right) const { return ToLE() > right.ToLE(); } template bool operator < (const be_t& right) const { return ToLE() < right.ToLE(); } template bool operator >= (const be_t& right) const { return ToLE() >= right.ToLE(); } template bool operator <= (const be_t& right) const { return ToLE() <= right.ToLE(); } };