Core/Flags: Allow handling of more than 32 enum options

This commit is contained in:
Lynix 2016-12-12 15:12:15 +01:00
parent 876fec6f5e
commit e98a02b190
2 changed files with 24 additions and 24 deletions

View File

@ -26,29 +26,29 @@ namespace Nz
static_assert(std::is_enum<E>::value, "Type must be an enumeration"); static_assert(std::is_enum<E>::value, "Type must be an enumeration");
public: public:
constexpr Flags(UInt32 value); using BitField = typename std::conditional<(EnumAsFlags<E>::max > 32), UInt64, UInt32>::type;
constexpr Flags(BitField value);
constexpr Flags(E enumVal); constexpr Flags(E enumVal);
explicit constexpr operator bool() const; explicit constexpr operator bool() const;
explicit constexpr operator UInt32() const; explicit constexpr operator BitField() const;
constexpr Flags operator~() const; constexpr Flags operator~() const;
constexpr Flags operator&(Flags rhs) const; constexpr Flags operator&(const Flags& rhs) const;
constexpr Flags operator|(Flags rhs) const; constexpr Flags operator|(const Flags& rhs) const;
constexpr Flags operator^(Flags rhs) const; constexpr Flags operator^(const Flags& rhs) const;
constexpr bool operator==(Flags rhs) const; constexpr bool operator==(const Flags& rhs) const;
constexpr bool operator!=(Flags rhs) const; constexpr bool operator!=(const Flags& rhs) const;
/*constexpr*/ Flags& operator|=(Flags rhs); /*constexpr*/ Flags& operator|=(const Flags& rhs);
/*constexpr*/ Flags& operator&=(Flags rhs); /*constexpr*/ Flags& operator&=(const Flags& rhs);
/*constexpr*/ Flags& operator^=(Flags rhs); /*constexpr*/ Flags& operator^=(const Flags& rhs);
static constexpr UInt32 GetFlagValue(E enumValue); static constexpr BitField GetFlagValue(E enumValue);
private: static constexpr BitField ValueMask = ((BitField(1) << (EnumAsFlags<E>::max + 1)) - 1);
UInt32 m_value;
static constexpr UInt32 ValueMask = ((UInt32(1) << (EnumAsFlags<E>::max + 1)) - 1);
private: private:
BitField m_value; BitField m_value;

View File

@ -84,7 +84,7 @@ namespace Nz
* This will returns a copy of the Flags object with only enabled flags in common with the parameter * This will returns a copy of the Flags object with only enabled flags in common with the parameter
*/ */
template<typename E> template<typename E>
constexpr Flags<E> Flags<E>::operator&(Flags rhs) const constexpr Flags<E> Flags<E>::operator&(const Flags& rhs) const
{ {
return Flags(m_value & rhs.m_value); return Flags(m_value & rhs.m_value);
} }
@ -98,7 +98,7 @@ namespace Nz
* \param rhs Flags to combine with. * \param rhs Flags to combine with.
*/ */
template<typename E> template<typename E>
constexpr Flags<E> Flags<E>::operator|(Flags rhs) const constexpr Flags<E> Flags<E>::operator|(const Flags& rhs) const
{ {
return Flags(m_value | rhs.m_value); return Flags(m_value | rhs.m_value);
} }
@ -113,7 +113,7 @@ namespace Nz
* This will returns a copy of the object with disabled common flags and enabled unique ones. * This will returns a copy of the object with disabled common flags and enabled unique ones.
*/ */
template<typename E> template<typename E>
constexpr Flags<E> Flags<E>::operator^(Flags rhs) const constexpr Flags<E> Flags<E>::operator^(const Flags& rhs) const
{ {
return Flags((m_value ^ rhs.m_value) & ValueMask); return Flags((m_value ^ rhs.m_value) & ValueMask);
} }
@ -127,7 +127,7 @@ namespace Nz
* Compare two Flags object and returns true if the flag states are identical. * Compare two Flags object and returns true if the flag states are identical.
*/ */
template<typename E> template<typename E>
constexpr bool Flags<E>::operator==(Flags rhs) const constexpr bool Flags<E>::operator==(const Flags& rhs) const
{ {
return m_value == rhs.m_value; return m_value == rhs.m_value;
} }
@ -141,7 +141,7 @@ namespace Nz
* Compare two Flags object and returns true if the flag states are identical. * Compare two Flags object and returns true if the flag states are identical.
*/ */
template<typename E> template<typename E>
constexpr bool Flags<E>::operator!=(Flags rhs) const constexpr bool Flags<E>::operator!=(const Flags& rhs) const
{ {
return !operator==(rhs); return !operator==(rhs);
} }
@ -155,7 +155,7 @@ namespace Nz
* This will enable flags which are enabled in parameter object and not in Flag object. * This will enable flags which are enabled in parameter object and not in Flag object.
*/ */
template<typename E> template<typename E>
/*constexpr*/ Flags<E>& Flags<E>::operator|=(Flags rhs) /*constexpr*/ Flags<E>& Flags<E>::operator|=(const Flags& rhs)
{ {
m_value |= rhs.m_value; m_value |= rhs.m_value;
@ -171,7 +171,7 @@ namespace Nz
* This will disable flags which are disabled in parameter object and enabled in Flag object (and vice-versa). * This will disable flags which are disabled in parameter object and enabled in Flag object (and vice-versa).
*/ */
template<typename E> template<typename E>
/*constexpr*/ Flags<E>& Flags<E>::operator&=(Flags rhs) /*constexpr*/ Flags<E>& Flags<E>::operator&=(const Flags& rhs)
{ {
m_value &= rhs.m_value; m_value &= rhs.m_value;
@ -188,7 +188,7 @@ namespace Nz
* This will disable flags enabled in both Flags objects and enable those enabled in only one of the Flags objects. * This will disable flags enabled in both Flags objects and enable those enabled in only one of the Flags objects.
*/ */
template<typename E> template<typename E>
/*constexpr*/ Flags<E>& Flags<E>::operator^=(Flags rhs) /*constexpr*/ Flags<E>& Flags<E>::operator^=(const Flags& rhs)
{ {
m_value ^= rhs.m_value; m_value ^= rhs.m_value;
m_value &= ValueMask; m_value &= ValueMask;
@ -205,9 +205,9 @@ namespace Nz
* Internally, every enum option is turned into a bit, this function allows to get a bitfield with only the bit of the enumeration value enabled. * Internally, every enum option is turned into a bit, this function allows to get a bitfield with only the bit of the enumeration value enabled.
*/ */
template<typename E> template<typename E>
constexpr UInt32 Flags<E>::GetFlagValue(E enumValue) constexpr typename Flags<E>::BitField Flags<E>::GetFlagValue(E enumValue)
{ {
return 1U << static_cast<UInt32>(enumValue); return 1U << static_cast<BitField>(enumValue);
} }