Core/Flags: Add operator&|^ for enum and flags

This commit is contained in:
Jérôme Leclercq 2018-05-16 15:54:55 +02:00
parent f864fc8a52
commit 74773e9daa
3 changed files with 66 additions and 16 deletions

View File

@ -100,6 +100,7 @@ Nazara Engine:
- SubMesh class now has a OnSubMeshInvalidateAABB signal, triggered when a new AABB is set to the submesh
- Mesh class now has a OnMeshInvalidateAABB signal, triggered when a mesh invalidates its AABB, which is also submesh updates its AABB
- Model now invalidate properly their bounding volume when their mesh AABB is updated
- Added operator&/|/^ taking an enumeration value and a Flags object using the same enumeration type.
Nazara Development Kit:
- Added ImageWidget (#139)

View File

@ -73,12 +73,16 @@ namespace Nz
BitField m_value;
};
template<typename E> constexpr Flags<E> operator&(E lhs, Flags<E> rhs);
template<typename E> constexpr Flags<E> operator|(E lhs, Flags<E> rhs);
template<typename E> constexpr Flags<E> operator^(E lhs, Flags<E> rhs);
// Little hack to have them in both Nz and global scope
namespace FlagsOperators
{
template<typename E> constexpr std::enable_if_t<IsEnumFlag<E>::value, Flags<E>> operator~(E lhs);
template<typename E> constexpr std::enable_if_t<IsEnumFlag<E>::value, Flags<E>> operator|(E lhs, E rhs);
template<typename E> constexpr std::enable_if_t<IsEnumFlag<E>::value, Flags<E>> operator&(E lhs, E rhs);
template<typename E> constexpr std::enable_if_t<IsEnumFlag<E>::value, Flags<E>> operator|(E lhs, E rhs);
template<typename E> constexpr std::enable_if_t<IsEnumFlag<E>::value, Flags<E>> operator^(E lhs, E rhs);
}

View File

@ -221,6 +221,51 @@ namespace Nz
return 1U << static_cast<BitField>(enumValue);
}
/*!
* \brief Compare flag states
* \return Compared flags
*
* This will returns a copy of the Flags object compared with the enum state.
*
* \param lhs Enum to compare with flags.
* \param rhs Flags object.
*/
template<typename E>
constexpr Flags<E> operator&(E lhs, Flags<E> rhs)
{
return rhs & lhs;
}
/*!
* \brief Combine flag states
* \return Combined flags
*
* This will returns a copy of the Flags object combined with the enum state.
*
* \param lhs Enum to combine with flags.
* \param rhs Flags object.
*/
template<typename E>
constexpr Flags<E> operator|(E lhs, Flags<E> rhs)
{
return rhs | lhs;
}
/*!
* \brief XOR flag states
* \return XORed flags
*
* This will returns a copy of the Flags object XORed with the enum state.
*
* \param lhs Enum to XOR with flags.
* \param rhs Flags object.
*/
template<typename E>
constexpr Flags<E> operator^(E lhs, Flags<E> rhs)
{
return rhs ^ lhs;
}
namespace FlagsOperators
{
@ -238,21 +283,6 @@ namespace Nz
return ~Flags<E>(lhs);
}
/*!
* \brief Override binary OR operator on enum to turns into a Flags object.
* \return A Flags object with combined enum states.
*
* \param lhs First enumeration value to combine.
* \param rhs Second enumeration value to combine.
*
* Returns a Flags object with combined states from the two enumeration values.
*/
template<typename E>
constexpr std::enable_if_t<IsEnumFlag<E>::value, Flags<E>> operator|(E lhs, E rhs)
{
return Flags<E>(lhs) | rhs;
}
/*!
* \brief Override binary AND operator on enum to turns into a Flags object.
* \return A Flags object with compare enum states.
@ -269,6 +299,21 @@ namespace Nz
return Flags<E>(lhs) & rhs;
}
/*!
* \brief Override binary OR operator on enum to turns into a Flags object.
* \return A Flags object with combined enum states.
*
* \param lhs First enumeration value to combine.
* \param rhs Second enumeration value to combine.
*
* Returns a Flags object with combined states from the two enumeration values.
*/
template<typename E>
constexpr std::enable_if_t<IsEnumFlag<E>::value, Flags<E>> operator|(E lhs, E rhs)
{
return Flags<E>(lhs) | rhs;
}
/*!
* \brief Override binary XOR operator on enum to turns into a Flags object.
* \return A Flags object with XORed enum states.