[constexpr] IntType

See https://github.com/harfbuzz/harfbuzz/pull/2875
This commit is contained in:
Behdad Esfahbod 2021-02-22 17:55:47 -07:00
parent 8b2f9adf29
commit cc16b26ef4
2 changed files with 16 additions and 9 deletions

View File

@ -83,7 +83,8 @@ static inline constexpr uint16_t hb_uint16_swap (uint16_t v)
static inline constexpr uint32_t hb_uint32_swap (uint32_t v)
{ return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16); }
template <typename Type, int Bytes = sizeof (Type)> struct BEInt;
template <typename Type, int Bytes = sizeof (Type)>
struct BEInt;
template <typename Type>
struct BEInt<Type, 1>
{
@ -124,11 +125,13 @@ struct BEInt<Type, 2>
template <typename Type>
struct BEInt<Type, 3>
{
static_assert (!hb_is_signed (Type), "");
public:
BEInt () = default;
constexpr BEInt (Type V) : v {uint8_t ((V >> 16) & 0xFF),
uint8_t ((V >> 8) & 0xFF),
uint8_t ((V ) & 0xFF)} {}
constexpr operator Type () const { return (v[0] << 16)
+ (v[1] << 8)
+ (v[2] ); }

View File

@ -53,14 +53,18 @@ namespace OT {
*/
/* Integer types in big-endian order and no alignment requirement */
template <typename Type, unsigned int Size = sizeof (Type)>
template <typename Type,
unsigned int Size = sizeof (Type),
typename Wide = hb_conditional<hb_is_signed (Type), signed, unsigned>>
struct IntType
{
typedef Type type;
typedef hb_conditional<hb_is_signed (Type), signed, unsigned> wide_type;
IntType& operator = (wide_type i) { v = i; return *this; }
operator wide_type () const { return v; }
IntType () = default;
explicit constexpr IntType (Wide V) : v {V} {}
IntType& operator = (Wide i) { v = i; return *this; }
operator Wide () const { return v; }
bool operator == (const IntType &o) const { return (Type) v == (Type) o.v; }
bool operator != (const IntType &o) const { return !(*this == o); }