mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2026-01-09 06:41:07 +08:00
Merge pull request #14232 from iwubcode/const_flags
Common: update Flags to allow const object usage
This commit is contained in:
commit
329ab1f518
@ -13,6 +13,7 @@
|
||||
#include <type_traits>
|
||||
|
||||
#include "Common/CommonTypes.h"
|
||||
#include "Common/EnumUtils.h"
|
||||
|
||||
namespace Common
|
||||
{
|
||||
@ -208,23 +209,20 @@ template <typename T>
|
||||
class FlagBit
|
||||
{
|
||||
public:
|
||||
FlagBit(std::underlying_type_t<T>& bits, T bit) : m_bits(bits), m_bit(bit) {}
|
||||
explicit operator bool() const
|
||||
{
|
||||
return (m_bits & static_cast<std::underlying_type_t<T>>(m_bit)) != 0;
|
||||
}
|
||||
FlagBit& operator=(const bool rhs)
|
||||
FlagBit(T* bits, std::type_identity_t<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<T>)
|
||||
{
|
||||
if (rhs)
|
||||
m_bits |= static_cast<std::underlying_type_t<T>>(m_bit);
|
||||
m_bits |= m_bit;
|
||||
else
|
||||
m_bits &= ~static_cast<std::underlying_type_t<T>>(m_bit);
|
||||
m_bits &= ~m_bit;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
std::underlying_type_t<T>& m_bits;
|
||||
T m_bit;
|
||||
T& m_bits;
|
||||
const T m_bit;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
@ -239,7 +237,8 @@ public:
|
||||
m_hex |= static_cast<std::underlying_type_t<T>>(bit);
|
||||
}
|
||||
}
|
||||
FlagBit<T> 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<T> m_hex = 0;
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user