diff --git a/Source/Core/Common/BitField.h b/Source/Core/Common/BitField.h index 045509ef07..caaf91dc3d 100644 --- a/Source/Core/Common/BitField.h +++ b/Source/Core/Common/BitField.h @@ -32,6 +32,7 @@ #pragma once #include +#include #include #include @@ -99,6 +100,8 @@ * explicit cast must be performed on the BitField object to make sure it gets * passed correctly, e.g.: * printf("Value: %d", (s32)some_register.some_signed_fields); + * Note that this does not apply when using fmt, as a formatter is provided that + * handles this conversion automatically. * * 2) * Not really a caveat, but potentially irritating: This class is used in some @@ -186,3 +189,16 @@ private: static_assert(bits > 0, "Invalid number of bits"); }; #pragma pack() + +// Use the underlying type's formatter for BitFields, if one exists +template +struct fmt::formatter> +{ + fmt::formatter m_formatter; + constexpr auto parse(format_parse_context& ctx) { return m_formatter.parse(ctx); } + template + auto format(const BitField& bitfield, FormatContext& ctx) + { + return m_formatter.format(bitfield.Value(), ctx); + } +}; diff --git a/Source/UnitTests/Common/BitFieldTest.cpp b/Source/UnitTests/Common/BitFieldTest.cpp index cb82a33911..d68b18b4c6 100644 --- a/Source/UnitTests/Common/BitFieldTest.cpp +++ b/Source/UnitTests/Common/BitFieldTest.cpp @@ -6,6 +6,15 @@ #include "Common/BitField.h" #include "Common/CommonTypes.h" +#include "Common/EnumFormatter.h" + +enum class TestEnum : u64 +{ + A, + B, + C, + D, +}; union TestUnion { @@ -23,6 +32,9 @@ union TestUnion BitField<15, 1, s64> signed_1bit; // allowed values: -1 and 0 BitField<63, 1, bool, u64> flag; + + BitField<16, 2, TestEnum> enum_1; + BitField<48, 2, TestEnum> enum_2; }; // table of raw numbers to test with @@ -54,6 +66,8 @@ TEST(BitField, Storage) EXPECT_EQ(sizeof(TestUnion), sizeof(object.at_dword_boundary)); EXPECT_EQ(sizeof(TestUnion), sizeof(object.signed_1bit)); EXPECT_EQ(sizeof(TestUnion), sizeof(object.flag)); + EXPECT_EQ(sizeof(TestUnion), sizeof(object.enum_1)); + EXPECT_EQ(sizeof(TestUnion), sizeof(object.enum_2)); // Now write some values to one field and check if this reflects properly // in the others. @@ -86,6 +100,8 @@ TEST(BitField, Read) EXPECT_EQ(object.at_dword_boundary, (s64)object.at_dword_boundary); EXPECT_EQ(object.signed_1bit, (s64)object.signed_1bit); EXPECT_EQ(object.flag, (bool)object.flag); + EXPECT_EQ(object.enum_1, static_cast(object.enum_1)); + EXPECT_EQ(object.enum_2, static_cast(object.enum_2)); // Now make sure the value is indeed correct EXPECT_EQ(val, object.full_u64); @@ -96,6 +112,8 @@ TEST(BitField, Read) EXPECT_EQ(((s64)(object.hex << 30)) >> 60, object.at_dword_boundary); EXPECT_EQ(((object.hex >> 15) & 1) ? -1 : 0, object.signed_1bit); EXPECT_EQ((bool)object.flag, ((object.hex >> 63) & 1)); + EXPECT_EQ(static_cast((object.hex >> 16) & 3), object.enum_1); + EXPECT_EQ(static_cast((object.hex >> 48) & 3), object.enum_2); } } @@ -180,3 +198,56 @@ TEST(BitField, Alignment) EXPECT_EQ(object.flag, (val & 2) != 0); } } + +template <> +struct fmt::formatter : EnumFormatter +{ + formatter() : EnumFormatter({"A", "B", "C", "D"}) {} +}; + +// Test behavior of using BitFields with fmt +TEST(BitField, Fmt) +{ + TestUnion object; + + for (u64 val : table) + { + object.hex = val; + + // Formatting the BitField should be the same as formatting its value + EXPECT_EQ(fmt::to_string(object.full_u64), fmt::to_string(object.full_u64.Value())); + EXPECT_EQ(fmt::to_string(object.full_s64), fmt::to_string(object.full_s64.Value())); + EXPECT_EQ(fmt::to_string(object.regular_field_unsigned), + fmt::to_string(object.regular_field_unsigned.Value())); + EXPECT_EQ(fmt::to_string(object.regular_field_unsigned2), + fmt::to_string(object.regular_field_unsigned2.Value())); + EXPECT_EQ(fmt::to_string(object.regular_field_signed), + fmt::to_string(object.regular_field_signed.Value())); + EXPECT_EQ(fmt::to_string(object.at_dword_boundary), + fmt::to_string(object.at_dword_boundary.Value())); + EXPECT_EQ(fmt::to_string(object.signed_1bit), fmt::to_string(object.signed_1bit.Value())); + EXPECT_EQ(fmt::to_string(object.flag), fmt::to_string(object.flag.Value())); + // The custom enum formatter should be used properly. + EXPECT_EQ(fmt::to_string(object.enum_1), fmt::to_string(object.enum_1.Value())); + EXPECT_EQ(fmt::to_string(object.enum_2), fmt::to_string(object.enum_2.Value())); + + // Formatting the BitField should respect the format spec + EXPECT_EQ(fmt::format("{:02x}", object.full_u64), + fmt::format("{:02x}", object.full_u64.Value())); + EXPECT_EQ(fmt::format("{:02x}", object.full_s64), + fmt::format("{:02x}", object.full_s64.Value())); + EXPECT_EQ(fmt::format("{:02x}", object.regular_field_unsigned), + fmt::format("{:02x}", object.regular_field_unsigned.Value())); + EXPECT_EQ(fmt::format("{:02x}", object.regular_field_unsigned2), + fmt::format("{:02x}", object.regular_field_unsigned2.Value())); + EXPECT_EQ(fmt::format("{:02x}", object.regular_field_signed), + fmt::format("{:02x}", object.regular_field_signed.Value())); + EXPECT_EQ(fmt::format("{:02x}", object.at_dword_boundary), + fmt::format("{:02x}", object.at_dword_boundary.Value())); + EXPECT_EQ(fmt::format("{:02x}", object.signed_1bit), + fmt::format("{:02x}", object.signed_1bit.Value())); + EXPECT_EQ(fmt::format("{:02x}", object.flag), fmt::format("{:02x}", object.flag.Value())); + EXPECT_EQ(fmt::format("{:s}", object.enum_1), fmt::format("{:s}", object.enum_1.Value())); + EXPECT_EQ(fmt::format("{:s}", object.enum_2), fmt::format("{:s}", object.enum_2.Value())); + } +}