#ifndef AL_BYTE_H #define AL_BYTE_H #include #include #include #include namespace al { /* The "canonical" way to store raw byte data. Like C++17's std::byte, it's not * treated as a character type and does not work with arithmatic ops. Only * bitwise ops are allowed. */ enum class byte : unsigned char { }; #define REQUIRES(...) std::enable_if_t<(__VA_ARGS__),bool> = true template::value)> inline constexpr T to_integer(al::byte b) noexcept { return T(b); } template::value)> inline constexpr al::byte operator<<(al::byte lhs, T rhs) noexcept { return al::byte(to_integer(lhs) << rhs); } template::value)> inline constexpr al::byte operator>>(al::byte lhs, T rhs) noexcept { return al::byte(to_integer(lhs) >> rhs); } template::value)> inline al::byte& operator<<=(al::byte &lhs, T rhs) noexcept { lhs = lhs << rhs; return lhs; } template::value)> inline al::byte& operator>>=(al::byte &lhs, T rhs) noexcept { lhs = lhs >> rhs; return lhs; } #define AL_DECL_OP(op, opeq) \ template::value)> \ inline constexpr al::byte operator op (al::byte lhs, T rhs) noexcept \ { return al::byte(to_integer(lhs) op static_cast(rhs)); } \ template::value)> \ inline al::byte& operator opeq (al::byte &lhs, T rhs) noexcept \ { lhs = lhs op rhs; return lhs; } \ inline constexpr al::byte operator op (al::byte lhs, al::byte rhs) noexcept \ { return al::byte(lhs op to_integer(rhs)); } \ inline al::byte& operator opeq (al::byte &lhs, al::byte rhs) noexcept \ { lhs = lhs op rhs; return lhs; } AL_DECL_OP(|, |=) AL_DECL_OP(&, &=) AL_DECL_OP(^, ^=) #undef AL_DECL_OP inline constexpr al::byte operator~(al::byte b) noexcept { return al::byte(~to_integer(b)); } namespace detail_ { template struct Elem { }; template<> struct Elem<1> { using type = uint8_t; }; template<> struct Elem<2> { using type = uint16_t; }; template<> struct Elem<3> { using type = uint32_t; }; template<> struct Elem<4> { using type = uint32_t; }; template using ElemT = typename Elem::type; } // namespace detail_ template class bitfield { static constexpr size_t bits_per_byte{std::numeric_limits::digits}; static constexpr size_t NumElems{(N+bits_per_byte-1) / bits_per_byte}; using storage_type = detail_::ElemT; storage_type vals{}; public: template inline void set() noexcept { static_assert(b < N, "Bit index out of range"); vals |= 1 << b; } template inline void unset() noexcept { static_assert(b < N, "Bit index out of range"); vals &= static_cast(~(1 << b)); } template inline bool get() const noexcept { static_assert(b < N, "Bit index out of range"); return (vals & (1 << b)) != 0; } template 0)> void set() noexcept { set(); set(); } }; #undef REQUIRES } // namespace al #endif /* AL_BYTE_H */