albyte.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #ifndef AL_BYTE_H
  2. #define AL_BYTE_H
  3. #include <cstddef>
  4. #include <cstdint>
  5. #include <limits>
  6. #include <type_traits>
  7. using uint = unsigned int;
  8. namespace al {
  9. /* The "canonical" way to store raw byte data. Like C++17's std::byte, it's not
  10. * treated as a character type and does not work with arithmatic ops. Only
  11. * bitwise ops are allowed.
  12. */
  13. enum class byte : unsigned char { };
  14. template<typename T>
  15. constexpr std::enable_if_t<std::is_integral<T>::value,T>
  16. to_integer(al::byte b) noexcept { return T(b); }
  17. template<typename T>
  18. constexpr std::enable_if_t<std::is_integral<T>::value,al::byte>
  19. operator<<(al::byte lhs, T rhs) noexcept { return al::byte(to_integer<uint>(lhs) << rhs); }
  20. template<typename T>
  21. constexpr std::enable_if_t<std::is_integral<T>::value,al::byte>
  22. operator>>(al::byte lhs, T rhs) noexcept { return al::byte(to_integer<uint>(lhs) >> rhs); }
  23. template<typename T>
  24. constexpr std::enable_if_t<std::is_integral<T>::value,al::byte&>
  25. operator<<=(al::byte &lhs, T rhs) noexcept { lhs = lhs << rhs; return lhs; }
  26. template<typename T>
  27. constexpr std::enable_if_t<std::is_integral<T>::value,al::byte&>
  28. operator>>=(al::byte &lhs, T rhs) noexcept { lhs = lhs >> rhs; return lhs; }
  29. #define AL_DECL_OP(op, opeq) \
  30. template<typename T> \
  31. constexpr std::enable_if_t<std::is_integral<T>::value,al::byte> \
  32. operator op (al::byte lhs, T rhs) noexcept \
  33. { return al::byte(to_integer<uint>(lhs) op static_cast<uint>(rhs)); } \
  34. \
  35. template<typename T> \
  36. constexpr std::enable_if_t<std::is_integral<T>::value,al::byte&> \
  37. operator opeq (al::byte &lhs, T rhs) noexcept { lhs = lhs op rhs; return lhs; } \
  38. \
  39. constexpr al::byte operator op (al::byte lhs, al::byte rhs) noexcept \
  40. { return al::byte(lhs op to_integer<uint>(rhs)); } \
  41. \
  42. constexpr al::byte& operator opeq (al::byte &lhs, al::byte rhs) noexcept \
  43. { lhs = lhs op rhs; return lhs; }
  44. AL_DECL_OP(|, |=)
  45. AL_DECL_OP(&, &=)
  46. AL_DECL_OP(^, ^=)
  47. #undef AL_DECL_OP
  48. constexpr al::byte operator~(al::byte b) noexcept
  49. { return al::byte(~to_integer<uint>(b)); }
  50. } // namespace al
  51. #endif /* AL_BYTE_H */