Core/Flags: Make aware of enumeration max value

Preventing it to enable bits that have no corresponding enum value
This commit is contained in:
Lynix 2016-12-12 15:10:37 +01:00
parent e9061a6cf8
commit 876fec6f5e
3 changed files with 11 additions and 4 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -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>;