SSX3LobbyServer/lib/base/fourcc.hpp

37 lines
1.3 KiB
C++
Raw Normal View History

#pragma once
#include <base/fixed_string.hpp>
#include <bit>
namespace base {
/// Type system magic
enum class FourCC32_t : u32 {};
template <FixedString fccString, std::endian Endian = std::endian::little>
consteval FourCC32_t FourCC32() {
static_assert(fccString.Length() == 4, "Provided string is not a FourCC");
switch(Endian) {
case std::endian::little:
return static_cast<FourCC32_t>((fccString[0]) | (fccString[1] << 8) | (fccString[2] << 16) | (fccString[3] << 24));
case std::endian::big:
return static_cast<FourCC32_t>((fccString[0] << 24) | (fccString[1] << 16) | (fccString[2] << 8) | fccString[3]);
// endian::native is practically implemented in most standard libraries
// by aliasing the native endian enumerator, so that it will match
// one of the two cases here. therefore this code is literally useless
// and i have no idea why i even wrote it 4 years ago :')
// default:
// throw "Invalid endian provided? How'd you do that?"; // NOLINT
}
}
inline std::string FourCC32ToString(FourCC32_t fcc) {
auto* fccAsBytes = std::bit_cast<u8*>(&fcc);
return std::format("{:c}{:c}{:c}{:c}", fccAsBytes[0], fccAsBytes[1], fccAsBytes[2], fccAsBytes[3]);
}
// TODO: 64-bit version which returns a u64 (if required?)
} // namespace base