From 0423225290f3ffac915a71d12b922c25a1ca5cff Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Mon, 20 Nov 2023 17:01:09 +0000 Subject: [PATCH] LibGfx: Make FourCC more useful A few small changes that didn't seem to deserve separate commits: - Mark it as packed to remove compiler complaints when it's a member of a packed struct. - Add a default constructor for places where we fill in a struct gradually. - Restrict the constructor to exactly 4-character string literals. - Add a to_u32() method for the one place that needs that. --- Userland/Libraries/LibGfx/FourCC.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibGfx/FourCC.h b/Userland/Libraries/LibGfx/FourCC.h index 2b2bdb9a3ac..6747cc33d89 100644 --- a/Userland/Libraries/LibGfx/FourCC.h +++ b/Userland/Libraries/LibGfx/FourCC.h @@ -8,8 +8,10 @@ namespace Gfx { -struct FourCC { - constexpr FourCC(char const* name) +struct [[gnu::packed]] FourCC { + FourCC() = default; + + constexpr FourCC(char const name[4]) { cc[0] = name[0]; cc[1] = name[1]; @@ -20,6 +22,14 @@ struct FourCC { bool operator==(FourCC const&) const = default; bool operator!=(FourCC const&) const = default; + u32 to_u32() const + { + return (static_cast(cc[0]) << 24) + | (static_cast(cc[1]) << 16) + | (static_cast(cc[2]) << 8) + | static_cast(cc[3]); + } + char cc[4]; };