From 876fec6f5e74dea481c1a97ad7c18d776e4b3501 Mon Sep 17 00:00:00 2001 From: Lynix Date: Mon, 12 Dec 2016 15:10:37 +0100 Subject: [PATCH] Core/Flags: Make aware of enumeration max value Preventing it to enable bits that have no corresponding enum value --- include/Nazara/Core/Flags.hpp | 4 ++++ include/Nazara/Core/Flags.inl | 9 +++++---- include/Nazara/Utility/Enums.hpp | 2 ++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/include/Nazara/Core/Flags.hpp b/include/Nazara/Core/Flags.hpp index 90e7ed2be..0e4623849 100644 --- a/include/Nazara/Core/Flags.hpp +++ b/include/Nazara/Core/Flags.hpp @@ -17,6 +17,7 @@ namespace Nz struct EnumAsFlags { static constexpr bool value = false; + static constexpr int max = 0; }; template @@ -47,7 +48,10 @@ namespace Nz private: UInt32 m_value; + static constexpr UInt32 ValueMask = ((UInt32(1) << (EnumAsFlags::max + 1)) - 1); + private: + BitField m_value; }; template constexpr std::enable_if_t::value, Flags> operator~(E lhs); diff --git a/include/Nazara/Core/Flags.inl b/include/Nazara/Core/Flags.inl index 8bf122b69..6e8e25d27 100644 --- a/include/Nazara/Core/Flags.inl +++ b/include/Nazara/Core/Flags.inl @@ -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 - constexpr Flags::Flags(UInt32 value) : + constexpr Flags::Flags(BitField value) : m_value(value) { } @@ -58,7 +58,7 @@ namespace Nz * This will convert to a bitfield value. */ template - constexpr Flags::operator UInt32() const + constexpr Flags::operator BitField() const { return m_value; } @@ -72,7 +72,7 @@ namespace Nz template constexpr Flags Flags::operator~() const { - return Flags(~m_value); + return Flags((~m_value) & ValueMask); } /*! @@ -115,7 +115,7 @@ namespace Nz template constexpr Flags Flags::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& Flags::operator^=(Flags rhs) { m_value ^= rhs.m_value; + m_value &= ValueMask; return *this; } diff --git a/include/Nazara/Utility/Enums.hpp b/include/Nazara/Utility/Enums.hpp index 69df340bd..fd9001d44 100644 --- a/include/Nazara/Utility/Enums.hpp +++ b/include/Nazara/Utility/Enums.hpp @@ -65,6 +65,7 @@ namespace Nz struct EnumAsFlags { static constexpr bool value = true; + static constexpr int max = BufferUsage_Max; }; using BufferUsageFlags = Flags; @@ -456,6 +457,7 @@ namespace Nz struct EnumAsFlags { static constexpr bool value = true; + static constexpr int max = WindowStyle_Max; }; using WindowStyleFlags = Flags;