From c9ef05c4b37e837f5c18fca6068681066fb63d53 Mon Sep 17 00:00:00 2001 From: iwubcode Date: Wed, 24 Dec 2025 15:32:42 -0600 Subject: [PATCH] Common: update Flags to allow const object usage Co-authored-by: Jordan Woyak --- Source/Core/Common/BitUtils.h | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Source/Core/Common/BitUtils.h b/Source/Core/Common/BitUtils.h index e052c8aeff..9385b9d8d9 100644 --- a/Source/Core/Common/BitUtils.h +++ b/Source/Core/Common/BitUtils.h @@ -13,6 +13,7 @@ #include #include "Common/CommonTypes.h" +#include "Common/EnumUtils.h" namespace Common { @@ -208,23 +209,20 @@ template class FlagBit { public: - FlagBit(std::underlying_type_t& bits, T bit) : m_bits(bits), m_bit(bit) {} - explicit operator bool() const - { - return (m_bits & static_cast>(m_bit)) != 0; - } - FlagBit& operator=(const bool rhs) + FlagBit(T* bits, std::type_identity_t bit) : m_bits(*bits), m_bit(bit) {} + explicit operator bool() const { return (m_bits & m_bit) != 0; } + FlagBit& operator=(const bool rhs) requires(!std::is_const_v) { if (rhs) - m_bits |= static_cast>(m_bit); + m_bits |= m_bit; else - m_bits &= ~static_cast>(m_bit); + m_bits &= ~m_bit; return *this; } private: - std::underlying_type_t& m_bits; - T m_bit; + T& m_bits; + const T m_bit; }; template @@ -239,7 +237,8 @@ public: m_hex |= static_cast>(bit); } } - FlagBit operator[](T bit) { return FlagBit(m_hex, bit); } + auto operator[](T bit) { return FlagBit(&m_hex, Common::ToUnderlying(bit)); } + auto operator[](T bit) const { return FlagBit(&m_hex, Common::ToUnderlying(bit)); } std::underlying_type_t m_hex = 0; };