[constexpr] BEInt

This commit is contained in:
Behdad Esfahbod 2020-06-29 00:20:45 -07:00
parent 2fbd34f89a
commit f8ebe1dacd
1 changed files with 23 additions and 41 deletions

View File

@ -530,25 +530,19 @@ template <typename Type>
struct BEInt<Type, 1> struct BEInt<Type, 1>
{ {
public: public:
BEInt<Type, 1>& operator = (Type V) BEInt () = default;
{ constexpr BEInt (Type V) : v {V} {}
v = V; constexpr operator Type () const { return v; }
return *this;
}
operator Type () const { return v; }
private: uint8_t v; private: uint8_t v;
}; };
template <typename Type> template <typename Type>
struct BEInt<Type, 2> struct BEInt<Type, 2>
{ {
public: public:
BEInt<Type, 2>& operator = (Type V) BEInt () = default;
{ constexpr BEInt (Type V) : v {(V >> 8) & 0xFF,
v[0] = (V >> 8) & 0xFF; (V ) & 0xFF} {}
v[1] = (V ) & 0xFF; constexpr operator Type () const
return *this;
}
operator Type () const
{ {
#if ((defined(__GNUC__) && __GNUC__ >= 5) || defined(__clang__)) && \ #if ((defined(__GNUC__) && __GNUC__ >= 5) || defined(__clang__)) && \
defined(__BYTE_ORDER) && \ defined(__BYTE_ORDER) && \
@ -571,40 +565,28 @@ template <typename Type>
struct BEInt<Type, 3> struct BEInt<Type, 3>
{ {
public: public:
BEInt<Type, 3>& operator = (Type V) BEInt () = default;
{ constexpr BEInt (Type V) : v {(V >> 16) & 0xFF,
v[0] = (V >> 16) & 0xFF; (V >> 8) & 0xFF,
v[1] = (V >> 8) & 0xFF; (V ) & 0xFF} {}
v[2] = (V ) & 0xFF; constexpr operator Type () const { return (v[0] << 16)
return *this; + (v[1] << 8)
} + (v[2] ); }
operator Type () const
{
return (v[0] << 16)
+ (v[1] << 8)
+ (v[2] );
}
private: uint8_t v[3]; private: uint8_t v[3];
}; };
template <typename Type> template <typename Type>
struct BEInt<Type, 4> struct BEInt<Type, 4>
{ {
public: public:
BEInt<Type, 4>& operator = (Type V) BEInt () = default;
{ constexpr BEInt (Type V) : v {(V >> 24) & 0xFF,
v[0] = (V >> 24) & 0xFF; (V >> 16) & 0xFF,
v[1] = (V >> 16) & 0xFF; (V >> 8) & 0xFF,
v[2] = (V >> 8) & 0xFF; (V ) & 0xFF} {}
v[3] = (V ) & 0xFF; constexpr operator Type () const { return (v[0] << 24)
return *this; + (v[1] << 16)
} + (v[2] << 8)
operator Type () const + (v[3] ); }
{
return (v[0] << 24)
+ (v[1] << 16)
+ (v[2] << 8)
+ (v[3] );
}
private: uint8_t v[4]; private: uint8_t v[4];
}; };