Add FourCC/_u32 helper function/literals

Portable code for turning string-based four-character-codes like
`"BLAH"_u32` into a 32-bit integer at compile-time.
This commit is contained in:
Wunkolo 2023-06-10 01:42:35 -07:00
parent 75c41a3841
commit 5124b54027
3 changed files with 24 additions and 2 deletions

View file

@ -141,6 +141,28 @@ constexpr size_t operator""_GB(unsigned long long int x) {
return 1024_MB * x;
}
// Utility user-literal/metaprogramming-type for turning four-character-codes
// into 32-bit integers at compile-time
// Ex: "BLAH"_u32, "HEAD"_u32, "END "_u32
template <std::size_t N>
struct FourCC {
u32 value;
constexpr FourCC(const char (&identifier)[N]) : value(0) {
static_assert(N == 5, "FourCC must be 4 characters");
if constexpr (std::endian::native == std::endian::big) {
value = ((identifier[3] << 0) | (identifier[2] << 8) | (identifier[1] << 16) | (identifier[0] << 24));
} else {
value = ((identifier[3] << 24) | (identifier[2] << 16) | (identifier[1] << 8) | (identifier[0] << 0));
}
}
};
template <FourCC code>
constexpr std::uint32_t operator""_u32() {
return code.value;
}
// useful macros
// likely/unlikely
#ifdef __GNUC__