Core/Flags: Make aware of enumeration max value
Preventing it to enable bits that have no corresponding enum value
This commit is contained in:
parent
e9061a6cf8
commit
876fec6f5e
|
|
@ -17,6 +17,7 @@ namespace Nz
|
|||
struct EnumAsFlags
|
||||
{
|
||||
static constexpr bool value = false;
|
||||
static constexpr int max = 0;
|
||||
};
|
||||
|
||||
template<typename E>
|
||||
|
|
@ -47,7 +48,10 @@ namespace Nz
|
|||
|
||||
private:
|
||||
UInt32 m_value;
|
||||
static constexpr UInt32 ValueMask = ((UInt32(1) << (EnumAsFlags<E>::max + 1)) - 1);
|
||||
|
||||
private:
|
||||
BitField m_value;
|
||||
};
|
||||
|
||||
template<typename E> constexpr std::enable_if_t<EnumAsFlags<E>::value, Flags<E>> operator~(E lhs);
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ namespace Nz
|
|||
* Uses a bitfield to builds the flag value. (e.g. if bit 0 is active, then Enum value 0 will be set as active).
|
||||
*/
|
||||
template<typename E>
|
||||
constexpr Flags<E>::Flags(UInt32 value) :
|
||||
constexpr Flags<E>::Flags(BitField value) :
|
||||
m_value(value)
|
||||
{
|
||||
}
|
||||
|
|
@ -58,7 +58,7 @@ namespace Nz
|
|||
* This will convert to a bitfield value.
|
||||
*/
|
||||
template<typename E>
|
||||
constexpr Flags<E>::operator UInt32() const
|
||||
constexpr Flags<E>::operator BitField() const
|
||||
{
|
||||
return m_value;
|
||||
}
|
||||
|
|
@ -72,7 +72,7 @@ namespace Nz
|
|||
template<typename E>
|
||||
constexpr Flags<E> Flags<E>::operator~() const
|
||||
{
|
||||
return Flags(~m_value);
|
||||
return Flags((~m_value) & ValueMask);
|
||||
}
|
||||
|
||||
/*!
|
||||
|
|
@ -115,7 +115,7 @@ namespace Nz
|
|||
template<typename E>
|
||||
constexpr Flags<E> Flags<E>::operator^(Flags rhs) const
|
||||
{
|
||||
return Flags(m_value ^ rhs.m_value);
|
||||
return Flags((m_value ^ rhs.m_value) & ValueMask);
|
||||
}
|
||||
|
||||
/*!
|
||||
|
|
@ -191,6 +191,7 @@ namespace Nz
|
|||
/*constexpr*/ Flags<E>& Flags<E>::operator^=(Flags rhs)
|
||||
{
|
||||
m_value ^= rhs.m_value;
|
||||
m_value &= ValueMask;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -65,6 +65,7 @@ namespace Nz
|
|||
struct EnumAsFlags<BufferUsage>
|
||||
{
|
||||
static constexpr bool value = true;
|
||||
static constexpr int max = BufferUsage_Max;
|
||||
};
|
||||
|
||||
using BufferUsageFlags = Flags<BufferUsage>;
|
||||
|
|
@ -456,6 +457,7 @@ namespace Nz
|
|||
struct EnumAsFlags<WindowStyle>
|
||||
{
|
||||
static constexpr bool value = true;
|
||||
static constexpr int max = WindowStyle_Max;
|
||||
};
|
||||
|
||||
using WindowStyleFlags = Flags<WindowStyle>;
|
||||
|
|
|
|||
Loading…
Reference in New Issue