#pragma once #define re16(val) _byteswap_ushort(val) #define re32(val) _byteswap_ulong(val) #define re64(val) _byteswap_uint64(val) #define re128(val) u128::byteswap(val) template struct se_t; template struct se_t { static __forceinline void func(T& dst, const T src) { (u8&)dst = (u8&)src; } }; template struct se_t { static __forceinline void func(T& dst, const T src) { (u16&)dst = _byteswap_ushort((u16&)src); } }; template struct se_t { static __forceinline void func(T& dst, const T src) { (u32&)dst = _byteswap_ulong((u32&)src); } }; template struct se_t { static __forceinline void func(T& dst, const T src) { (u64&)dst = _byteswap_uint64((u64&)src); } }; template T re(const T val) { T res; se_t::func(res, val); return res; } template void re(T1& dst, const T2 val) { se_t::func(dst, val); } template struct const_se_t; template struct const_se_t { static const T value = (T)_value; }; template struct const_se_t { static const T value = ((_value >> 8) & 0xff) | ((_value << 8) & 0xff00); }; template struct const_se_t { static const T value = ((_value >> 24) & 0x000000ff) | ((_value >> 8) & 0x0000ff00) | ((_value << 8) & 0x00ff0000) | ((_value << 24) & 0xff000000); }; template struct const_se_t { static const T value = ((_value >> 56) & 0x00000000000000ff) | ((_value >> 40) & 0x000000000000ff00) | ((_value >> 24) & 0x0000000000ff0000) | ((_value >> 8) & 0x00000000ff000000) | ((_value << 8) & 0x000000ff00000000) | ((_value << 24) & 0x0000ff0000000000) | ((_value << 40) & 0x00ff000000000000) | ((_value << 56) & 0xff00000000000000); }; template class be_t { static_assert(sizeof(T2) == 1 || sizeof(T2) == 2 || sizeof(T2) == 4 || sizeof(T2) == 8, "Bad be_t type"); T m_data; public: typedef T type; const T& ToBE() const { return m_data; } T ToLE() const { T res; se_t::func(res, m_data); return res; } void FromBE(const T& value) { m_data = value; } void FromLE(const T& value) { se_t::func(m_data, value); } static be_t MakeFromLE(const T value) { be_t res; res.FromLE(value); return res; } static be_t MakeFromBE(const T value) { be_t res; res.FromBE(value); return res; } //template operator const T() const { return ToLE(); } template operator const be_t() const { be_t res; if (sizeof(T1) < sizeof(T)) { res.FromBE(ToBE() >> ((sizeof(T)-sizeof(T1)) * 8)); } else if (sizeof(T1) > sizeof(T)) { res.FromLE(ToLE()); } else { res.FromBE(ToBE()); } return res; } be_t& operator = (const T& right) { FromLE(right); return *this; } be_t& operator = (const be_t& right) = default; 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 (T1)ToLE() == right; } template bool operator != (T1 right) const { return !(*this == right); } template bool operator > (T1 right) const { return (T1)ToLE() > right; } template bool operator < (T1 right) const { return (T1)ToLE() < right; } template bool operator >= (T1 right) const { return (T1)ToLE() >= right; } template bool operator <= (T1 right) const { return (T1)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 (T1)ToLE() > right.ToLE(); } template bool operator < (const be_t& right) const { return (T1)ToLE() < right.ToLE(); } template bool operator >= (const be_t& right) const { return (T1)ToLE() >= right.ToLE(); } template bool operator <= (const be_t& right) const { return (T1)ToLE() <= right.ToLE(); } be_t operator++ (int) { be_t res = *this; *this += 1; return res; } be_t operator-- (int) { be_t res = *this; *this -= 1; return res; } be_t& operator++ () { *this += 1; return *this; } be_t& operator-- () { *this -= 1; return *this; } }; template struct is_be_t : public std::integral_constant {}; template struct is_be_t, T2> : public std::integral_constant { }; template struct remove_be_t { typedef T type; }; template struct remove_be_t> { typedef T type; }; template class to_be_t { template struct _be_type_selector { typedef TT type; }; template struct _be_type_selector { typedef be_t type; }; public: //true if need swap endianes for be static const bool value = (sizeof(T2) > 1) && std::is_arithmetic::value; //be_t if need swap endianes, T otherwise typedef typename _be_type_selector< T, T2, value >::type type; }; template class to_be_t { public: //true if need swap endianes for be static const bool value = false; //be_t if need swap endianes, T otherwise typedef void type; }; template class to_be_t { public: //true if need swap endianes for be static const bool value = false; //be_t if need swap endianes, T otherwise typedef void type; }; template struct _se : public const_se_t {}; template struct _se, T1, value> : public const_se_t {}; #define se(t, x) _se::value #define se16(x) _se::value #define se32(x) _se::value #define se64(x) _se::value template __forceinline static u8 Read8(T& f) { u8 ret; f.Read(&ret, sizeof(ret)); return ret; } template __forceinline static u16 Read16(T& f) { be_t ret; f.Read(&ret, sizeof(ret)); return ret; } template __forceinline static u32 Read32(T& f) { be_t ret; f.Read(&ret, sizeof(ret)); return ret; } template __forceinline static u64 Read64(T& f) { be_t ret; f.Read(&ret, sizeof(ret)); return ret; } template __forceinline static u16 Read16LE(T& f) { u16 ret; f.Read(&ret, sizeof(ret)); return ret; } template __forceinline static u32 Read32LE(T& f) { u32 ret; f.Read(&ret, sizeof(ret)); return ret; } template __forceinline static u64 Read64LE(T& f) { u64 ret; f.Read(&ret, sizeof(ret)); return ret; } template __forceinline static void Write8(T& f, const u8 data) { f.Write(&data, sizeof(data)); } template __forceinline static void Write16LE(T& f, const u16 data) { f.Write(&data, sizeof(data)); } template __forceinline static void Write32LE(T& f, const u32 data) { f.Write(&data, sizeof(data)); } template __forceinline static void Write64LE(T& f, const u64 data) { f.Write(&data, sizeof(data)); } template __forceinline static void Write16(T& f, const u16 data) { Write16LE(f, re16(data)); } template __forceinline static void Write32(T& f, const u32 data) { Write32LE(f, re32(data)); } template __forceinline static void Write64(T& f, const u64 data) { Write64LE(f, re64(data)); }