From e6bcf48b97038f985e43886f82fbd48364fe2fa2 Mon Sep 17 00:00:00 2001 From: Lynix Date: Fri, 9 Dec 2016 09:08:34 +0100 Subject: [PATCH 1/9] Plugins/Assimp: Fix compilation --- plugins/Assimp/Plugin.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/Assimp/Plugin.cpp b/plugins/Assimp/Plugin.cpp index 746549568..e65d460db 100644 --- a/plugins/Assimp/Plugin.cpp +++ b/plugins/Assimp/Plugin.cpp @@ -184,7 +184,7 @@ bool Load(Mesh* mesh, Stream& stream, const MeshParams& parameters) // Index buffer bool largeIndices = (vertexCount > std::numeric_limits::max()); - IndexBufferRef indexBuffer = IndexBuffer::New(largeIndices, indexCount, parameters.storage); + IndexBufferRef indexBuffer = IndexBuffer::New(largeIndices, indexCount, parameters.storage, 0); IndexMapper indexMapper(indexBuffer, BufferAccess_DiscardAndWrite); IndexIterator index = indexMapper.begin(); @@ -202,7 +202,7 @@ bool Load(Mesh* mesh, Stream& stream, const MeshParams& parameters) indexMapper.Unmap(); // Vertex buffer - VertexBufferRef vertexBuffer = VertexBuffer::New(VertexDeclaration::Get(VertexLayout_XYZ_Normal_UV_Tangent), vertexCount, parameters.storage); + VertexBufferRef vertexBuffer = VertexBuffer::New(VertexDeclaration::Get(VertexLayout_XYZ_Normal_UV_Tangent), vertexCount, parameters.storage, 0); BufferMapper vertexMapper(vertexBuffer, BufferAccess_WriteOnly); MeshVertex* vertex = static_cast(vertexMapper.GetPointer()); From 4e239236b119fc76a5002eef2257dcd2b44abdc2 Mon Sep 17 00:00:00 2001 From: Lynix Date: Fri, 9 Dec 2016 09:11:22 +0100 Subject: [PATCH 2/9] Utility/SoftwareBuffer: Move to public API --- {src => include}/Nazara/Utility/SoftwareBuffer.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename {src => include}/Nazara/Utility/SoftwareBuffer.hpp (93%) diff --git a/src/Nazara/Utility/SoftwareBuffer.hpp b/include/Nazara/Utility/SoftwareBuffer.hpp similarity index 93% rename from src/Nazara/Utility/SoftwareBuffer.hpp rename to include/Nazara/Utility/SoftwareBuffer.hpp index 4e48dd29e..1bd4eb3db 100644 --- a/src/Nazara/Utility/SoftwareBuffer.hpp +++ b/include/Nazara/Utility/SoftwareBuffer.hpp @@ -15,7 +15,7 @@ namespace Nz { class Buffer; - class SoftwareBuffer : public AbstractBuffer + class NAZARA_UTILITY_API SoftwareBuffer : public AbstractBuffer { public: SoftwareBuffer(Buffer* parent, BufferType type); From b3549f122912372baa37ec118ee08f7db31288af Mon Sep 17 00:00:00 2001 From: Lynix Date: Sun, 11 Dec 2016 10:00:22 +0100 Subject: [PATCH 3/9] GitHub/ Add nightlies link to the readme --- readme.md | 6 +++--- readme_fr.md | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index ea054e608..955031256 100644 --- a/readme.md +++ b/readme.md @@ -1,7 +1,7 @@ -Platform | Build Status +Platform | Build Status | Nightlies ------------ | ------------- -Windows | [![AppVeyor Build status](https://ci.appveyor.com/api/projects/status/dj5qx7axym4uakmy/branch/master?svg=true)](https://ci.appveyor.com/project/DPSLynix/nazaraengine/branch/master) -Linux | [![Travis CI Build Status](https://travis-ci.org/DigitalPulseSoftware/NazaraEngine.svg?branch=master)](https://travis-ci.org/DigitalPulseSoftware/NazaraEngine) +Windows | [![AppVeyor Build status](https://ci.appveyor.com/api/projects/status/dj5qx7axym4uakmy/branch/master?svg=true)](https://ci.appveyor.com/project/DPSLynix/nazaraengine/branch/master) | MSVC14: [x86](https://ci.appveyor.com/api/projects/DPSLynix/NazaraEngine/artifacts/package%2FNazaraEngine.7z?branch=master&job=Environment%3A%20TOOLSET%3Dvs2015%3B%20Configuration%3A%20ReleaseDynamic%3B%20Platform%3A%20Win32) [x86_64](https://ci.appveyor.com/api/projects/DPSLynix/NazaraEngine/artifacts/package%2FNazaraEngine.7z?branch=master&job=Environment%3A%20TOOLSET%3Dvs2015%3B%20Configuration%3A%20ReleaseDynamic%3B%20Platform%3A%20x64) +Linux | [![Travis CI Build Status](https://travis-ci.org/DigitalPulseSoftware/NazaraEngine.svg?branch=master)](https://travis-ci.org/DigitalPulseSoftware/NazaraEngine) | No # Nazara Engine diff --git a/readme_fr.md b/readme_fr.md index 422be6a0e..e9a619766 100644 --- a/readme_fr.md +++ b/readme_fr.md @@ -1,7 +1,7 @@ -Platforme | Build Status +Platforme | Build Status | Nightlies ------------ | ------------- -Windows | [![AppVeyor Build status](https://ci.appveyor.com/api/projects/status/dj5qx7axym4uakmy/branch/master?svg=true)](https://ci.appveyor.com/project/DPSLynix/nazaraengine/branch/master) -Linux | [![Travis CI Build Status](https://travis-ci.org/DigitalPulseSoftware/NazaraEngine.svg?branch=master)](https://travis-ci.org/DigitalPulseSoftware/NazaraEngine) +Windows | [![AppVeyor Build status](https://ci.appveyor.com/api/projects/status/dj5qx7axym4uakmy/branch/master?svg=true)](https://ci.appveyor.com/project/DPSLynix/nazaraengine/branch/master) | MSVC14: [x86](https://ci.appveyor.com/api/projects/DPSLynix/NazaraEngine/artifacts/package%2FNazaraEngine.7z?branch=master&job=Environment%3A%20TOOLSET%3Dvs2015%3B%20Configuration%3A%20ReleaseDynamic%3B%20Platform%3A%20Win32) [x86_64](https://ci.appveyor.com/api/projects/DPSLynix/NazaraEngine/artifacts/package%2FNazaraEngine.7z?branch=master&job=Environment%3A%20TOOLSET%3Dvs2015%3B%20Configuration%3A%20ReleaseDynamic%3B%20Platform%3A%20x64) +Linux | [![Travis CI Build Status](https://travis-ci.org/DigitalPulseSoftware/NazaraEngine.svg?branch=master)](https://travis-ci.org/DigitalPulseSoftware/NazaraEngine) | Non # Nazara Engine From d29730067f722a5b029254dd5431dd33c487cec1 Mon Sep 17 00:00:00 2001 From: Lynix Date: Sun, 11 Dec 2016 10:07:29 +0100 Subject: [PATCH 4/9] GitHub: Markdown fix --- readme.md | 2 +- readme_fr.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 955031256..753a00523 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,5 @@ Platform | Build Status | Nightlies ------------- | ------------- +------------ | ------------- | ------------- Windows | [![AppVeyor Build status](https://ci.appveyor.com/api/projects/status/dj5qx7axym4uakmy/branch/master?svg=true)](https://ci.appveyor.com/project/DPSLynix/nazaraengine/branch/master) | MSVC14: [x86](https://ci.appveyor.com/api/projects/DPSLynix/NazaraEngine/artifacts/package%2FNazaraEngine.7z?branch=master&job=Environment%3A%20TOOLSET%3Dvs2015%3B%20Configuration%3A%20ReleaseDynamic%3B%20Platform%3A%20Win32) [x86_64](https://ci.appveyor.com/api/projects/DPSLynix/NazaraEngine/artifacts/package%2FNazaraEngine.7z?branch=master&job=Environment%3A%20TOOLSET%3Dvs2015%3B%20Configuration%3A%20ReleaseDynamic%3B%20Platform%3A%20x64) Linux | [![Travis CI Build Status](https://travis-ci.org/DigitalPulseSoftware/NazaraEngine.svg?branch=master)](https://travis-ci.org/DigitalPulseSoftware/NazaraEngine) | No diff --git a/readme_fr.md b/readme_fr.md index e9a619766..808788b0d 100644 --- a/readme_fr.md +++ b/readme_fr.md @@ -1,5 +1,5 @@ Platforme | Build Status | Nightlies ------------- | ------------- +------------ | ------------- | ------------- Windows | [![AppVeyor Build status](https://ci.appveyor.com/api/projects/status/dj5qx7axym4uakmy/branch/master?svg=true)](https://ci.appveyor.com/project/DPSLynix/nazaraengine/branch/master) | MSVC14: [x86](https://ci.appveyor.com/api/projects/DPSLynix/NazaraEngine/artifacts/package%2FNazaraEngine.7z?branch=master&job=Environment%3A%20TOOLSET%3Dvs2015%3B%20Configuration%3A%20ReleaseDynamic%3B%20Platform%3A%20Win32) [x86_64](https://ci.appveyor.com/api/projects/DPSLynix/NazaraEngine/artifacts/package%2FNazaraEngine.7z?branch=master&job=Environment%3A%20TOOLSET%3Dvs2015%3B%20Configuration%3A%20ReleaseDynamic%3B%20Platform%3A%20x64) Linux | [![Travis CI Build Status](https://travis-ci.org/DigitalPulseSoftware/NazaraEngine.svg?branch=master)](https://travis-ci.org/DigitalPulseSoftware/NazaraEngine) | Non From bc073de94a02e8debd12b6d06e443ea69790a37a Mon Sep 17 00:00:00 2001 From: Lynix Date: Mon, 12 Dec 2016 14:41:34 +0100 Subject: [PATCH 5/9] Core/Flags: Add documentation --- include/Nazara/Core/Flags.inl | 147 +++++++++++++++++++++++++++++++++- 1 file changed, 144 insertions(+), 3 deletions(-) diff --git a/include/Nazara/Core/Flags.inl b/include/Nazara/Core/Flags.inl index 7a23861a2..d9558fb99 100644 --- a/include/Nazara/Core/Flags.inl +++ b/include/Nazara/Core/Flags.inl @@ -10,71 +10,150 @@ namespace Nz /*! * \ingroup core * \class Nz::Flags - * \brief Core class that represents a set of bits - * - * This class meets the requirements of Container, AllocatorAwareContainer, SequenceContainer + * \brief Core class used to combine enumeration values into flags bitfield */ + /*! + * \brief Constructs a Flags object using a bitfield + * + * \param value Bitfield to be used + * + * 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) : m_value(value) { } + /*! + * \brief Constructs a Flags object using an Enum value + * + * \param value enumVal + * + * Setup a Flags object with only one flag active (corresponding to the enum value passed as argument). + */ template constexpr Flags::Flags(E enumVal) : Flags(GetFlagValue(enumVal)) { } + /*! + * \brief Tests a Flags + * \return True if any flag is enabled. + * + * This will convert to a boolean value allowing to check if any flag is set. + */ template constexpr Flags::operator bool() const { return m_value != 0; } + /*! + * \brief Converts to a bitfield + * \return Enabled flags as a bitfield. + * + * This will convert to a bitfield value. + */ template constexpr Flags::operator UInt32() const { return m_value; } + /*! + * \brief Reverse flag states + * \return Opposite enabled flags + * + * This will returns a copy of the Flags object with reversed flags states. + */ template constexpr Flags Flags::operator~() const { return Flags(~m_value); } + /*! + * \brief Compare flag states + * \return Shared flags + * + * \param rhs Flags to compare with. + * + * This will returns a copy of the Flags object with only enabled flags in common with the parameter + */ template constexpr Flags Flags::operator&(Flags rhs) const { return Flags(m_value & rhs.m_value); } + /*! + * \brief Combine flag states + * \return Combined flags + * + * This will returns a copy of the Flags object with combined flags from the parameter. + * + * \param rhs Flags to combine with. + */ template constexpr Flags Flags::operator|(Flags rhs) const { return Flags(m_value | rhs.m_value); } + /*! + * \brief XOR flag states + * \return XORed flags. + * + * \param rhs Flags to XOR with. + * + * This performs a XOR (Exclusive OR) on a copy of the flag object. + * This will returns a copy of the object with disabled common flags and enabled unique ones. + */ template constexpr Flags Flags::operator^(Flags rhs) const { return Flags(m_value ^ rhs.m_value); } + /*! + * \brief Check equality with flag object + * \return True if both flags objects have the same states. + * + * \param rhs Flags to compare with. + * + * Compare two Flags object and returns true if the flag states are identical. + */ template constexpr bool Flags::operator==(Flags rhs) const { return m_value == rhs.m_value; } + /*! + * \brief Check inequality with flag object + * \return True if both flags objects have different states. + * + * \param rhs Flags to compare with. + * + * Compare two Flags object and returns true if the flag states are identical. + */ template constexpr bool Flags::operator!=(Flags rhs) const { return !operator==(rhs); } + /*! + * \brief Combine flag states + * \return A reference to the object. + * + * \param rhs Flags to combine with. + * + * This will enable flags which are enabled in parameter object and not in Flag object. + */ template /*constexpr*/ Flags& Flags::operator|=(Flags rhs) { @@ -83,6 +162,14 @@ namespace Nz return *this; } + /*! + * \brief Compare flag states + * \return A reference to the object. + * + * \param rhs Flags to compare with. + * + * This will disable flags which are disabled in parameter object and enabled in Flag object (and vice-versa). + */ template /*constexpr*/ Flags& Flags::operator&=(Flags rhs) { @@ -91,6 +178,15 @@ namespace Nz return *this; } + /*! + * \brief XOR flag states + * \return A reference to the object. + * + * \param rhs Flags to XOR with. + * + * This performs a XOR (Exclusive OR) on the flag object. + * This will disable flags enabled in both Flags objects and enable those enabled in only one of the Flags objects. + */ template /*constexpr*/ Flags& Flags::operator^=(Flags rhs) { @@ -99,6 +195,14 @@ namespace Nz return *this; } + /*! + * \brief Returns a bitfield corresponding to an enum value. + * \return Bitfield representation of the enum value + * + * \param enumValue Enumeration value to get as a bitfield. + * + * 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 constexpr UInt32 Flags::GetFlagValue(E enumValue) { @@ -106,24 +210,61 @@ namespace Nz } + /*! + * \brief Override binary NOT operator on enum to turns into a Flags object. + * \return A Flags object with reversed bits. + * + * \param lhs Enumeration value to reverse. + * + * Returns a Flags object with all state enabled except for the enum one. + */ template constexpr std::enable_if_t::value, Flags> operator~(E lhs) { return ~Flags(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 constexpr std::enable_if_t::value, Flags> operator|(E lhs, E rhs) { return Flags(lhs) | rhs; } + /*! + * \brief Override binary AND operator on enum to turns into a Flags object. + * \return A Flags object with compare enum states. + * + * \param lhs First enumeration value to compare. + * \param rhs Second enumeration value to compare. + * + * Returns a Flags object with compared states from the two enumeration values. + * In this case, only one flag will be enabled if both enumeration values are the same. + */ template constexpr std::enable_if_t::value, Flags> operator&(E lhs, E rhs) { return Flags(lhs) & rhs; } + /*! + * \brief Override binary XOR operator on enum to turns into a Flags object. + * \return A Flags object with XORed enum states. + * + * \param lhs First enumeration value to compare. + * \param rhs Second enumeration value to compare. + * + * Returns a Flags object with XORed states from the two enumeration values. + * In this case, two flags will be enabled if both the enumeration values are different. + */ template constexpr std::enable_if_t::value, Flags> operator^(E lhs, E rhs) { From e9061a6cf8d8e9d3c45a73a516c921ab12c45420 Mon Sep 17 00:00:00 2001 From: Lynix Date: Mon, 12 Dec 2016 15:07:16 +0100 Subject: [PATCH 6/9] Core/Flags: Rename EnableFlagsOperators to EnumAsFlags --- include/Nazara/Core/Enums.hpp | 6 ++++-- include/Nazara/Core/Flags.hpp | 23 ++++++++++++----------- include/Nazara/Core/Flags.inl | 8 ++++---- include/Nazara/Lua/LuaInstance.inl | 12 ++++++------ include/Nazara/Utility/Enums.hpp | 4 ++-- 5 files changed, 28 insertions(+), 25 deletions(-) diff --git a/include/Nazara/Core/Enums.hpp b/include/Nazara/Core/Enums.hpp index 765505575..1020cd2b6 100644 --- a/include/Nazara/Core/Enums.hpp +++ b/include/Nazara/Core/Enums.hpp @@ -91,9 +91,10 @@ namespace Nz }; template<> - struct EnableFlagsOperators + struct EnumAsFlags { static constexpr bool value = true; + static constexpr int max = OpenMode_Max; }; using OpenModeFlags = Flags; @@ -194,9 +195,10 @@ namespace Nz }; template<> - struct EnableFlagsOperators + struct EnumAsFlags { static constexpr bool value = true; + static constexpr int max = StreamOption_Max; }; using StreamOptionFlags = Flags; diff --git a/include/Nazara/Core/Flags.hpp b/include/Nazara/Core/Flags.hpp index e56d723e5..90e7ed2be 100644 --- a/include/Nazara/Core/Flags.hpp +++ b/include/Nazara/Core/Flags.hpp @@ -12,6 +12,13 @@ namespace Nz { + // From: https://www.justsoftwaresolutions.co.uk/cplusplus/using-enum-classes-as-bitfields.html + template + struct EnumAsFlags + { + static constexpr bool value = false; + }; + template class Flags { @@ -40,19 +47,13 @@ namespace Nz private: UInt32 m_value; + }; - // From: https://www.justsoftwaresolutions.co.uk/cplusplus/using-enum-classes-as-bitfields.html - template - struct EnableFlagsOperators - { - static constexpr bool value = false; - }; - - template constexpr std::enable_if_t::value, Flags> operator~(E lhs); - template constexpr std::enable_if_t::value, Flags> operator|(E lhs, E rhs); - template constexpr std::enable_if_t::value, Flags> operator&(E lhs, E rhs); - template constexpr std::enable_if_t::value, Flags> operator^(E lhs, E rhs); + template constexpr std::enable_if_t::value, Flags> operator~(E lhs); + template constexpr std::enable_if_t::value, Flags> operator|(E lhs, E rhs); + template constexpr std::enable_if_t::value, Flags> operator&(E lhs, E rhs); + template constexpr std::enable_if_t::value, Flags> operator^(E lhs, E rhs); } #include diff --git a/include/Nazara/Core/Flags.inl b/include/Nazara/Core/Flags.inl index d9558fb99..8bf122b69 100644 --- a/include/Nazara/Core/Flags.inl +++ b/include/Nazara/Core/Flags.inl @@ -219,7 +219,7 @@ namespace Nz * Returns a Flags object with all state enabled except for the enum one. */ template - constexpr std::enable_if_t::value, Flags> operator~(E lhs) + constexpr std::enable_if_t::value, Flags> operator~(E lhs) { return ~Flags(lhs); } @@ -234,7 +234,7 @@ namespace Nz * Returns a Flags object with combined states from the two enumeration values. */ template - constexpr std::enable_if_t::value, Flags> operator|(E lhs, E rhs) + constexpr std::enable_if_t::value, Flags> operator|(E lhs, E rhs) { return Flags(lhs) | rhs; } @@ -250,7 +250,7 @@ namespace Nz * In this case, only one flag will be enabled if both enumeration values are the same. */ template - constexpr std::enable_if_t::value, Flags> operator&(E lhs, E rhs) + constexpr std::enable_if_t::value, Flags> operator&(E lhs, E rhs) { return Flags(lhs) & rhs; } @@ -266,7 +266,7 @@ namespace Nz * In this case, two flags will be enabled if both the enumeration values are different. */ template - constexpr std::enable_if_t::value, Flags> operator^(E lhs, E rhs) + constexpr std::enable_if_t::value, Flags> operator^(E lhs, E rhs) { return Flags(lhs) ^ rhs; } diff --git a/include/Nazara/Lua/LuaInstance.inl b/include/Nazara/Lua/LuaInstance.inl index 99ef94ea4..6c3a4ff7e 100644 --- a/include/Nazara/Lua/LuaInstance.inl +++ b/include/Nazara/Lua/LuaInstance.inl @@ -100,21 +100,21 @@ namespace Nz } template - std::enable_if_t::value && !EnableFlagsOperators::value, unsigned int> LuaImplQueryArg(const LuaInstance& instance, int index, T* arg, TypeTag) + std::enable_if_t::value && !EnumAsFlags::value, unsigned int> LuaImplQueryArg(const LuaInstance& instance, int index, T* arg, TypeTag) { using UnderlyingT = std::underlying_type_t; return LuaImplQueryArg(instance, index, reinterpret_cast(arg), TypeTag()); } template - std::enable_if_t::value && !EnableFlagsOperators::value, unsigned int> LuaImplQueryArg(const LuaInstance& instance, int index, T* arg, T defValue, TypeTag) + std::enable_if_t::value && !EnumAsFlags::value, unsigned int> LuaImplQueryArg(const LuaInstance& instance, int index, T* arg, T defValue, TypeTag) { using UnderlyingT = std::underlying_type_t; return LuaImplQueryArg(instance, index, reinterpret_cast(arg), static_cast(defValue), TypeTag()); } template - std::enable_if_t::value && EnableFlagsOperators::value, unsigned int> LuaImplQueryArg(const LuaInstance& instance, int index, T* arg, TypeTag) + std::enable_if_t::value && EnumAsFlags::value, unsigned int> LuaImplQueryArg(const LuaInstance& instance, int index, T* arg, TypeTag) { using UnderlyingT = std::underlying_type_t; @@ -126,7 +126,7 @@ namespace Nz } template - std::enable_if_t::value && EnableFlagsOperators::value, unsigned int> LuaImplQueryArg(const LuaInstance& instance, int index, T* arg, T defValue, TypeTag) + std::enable_if_t::value && EnumAsFlags::value, unsigned int> LuaImplQueryArg(const LuaInstance& instance, int index, T* arg, T defValue, TypeTag) { using UnderlyingT = std::underlying_type_t; @@ -216,14 +216,14 @@ namespace Nz } template - std::enable_if_t::value && !EnableFlagsOperators::value, int> LuaImplReplyVal(const LuaInstance& instance, T val, TypeTag) + std::enable_if_t::value && !EnumAsFlags::value, int> LuaImplReplyVal(const LuaInstance& instance, T val, TypeTag) { using EnumT = typename std::underlying_type::type; return LuaImplReplyVal(instance, static_cast(val), TypeTag()); } template - std::enable_if_t::value && EnableFlagsOperators::value, int> LuaImplReplyVal(const LuaInstance& instance, T val, TypeTag) + std::enable_if_t::value && EnumAsFlags::value, int> LuaImplReplyVal(const LuaInstance& instance, T val, TypeTag) { Flags flags(val); return LuaImplReplyVal(instance, flags, TypeTag()); diff --git a/include/Nazara/Utility/Enums.hpp b/include/Nazara/Utility/Enums.hpp index 4edce5fef..69df340bd 100644 --- a/include/Nazara/Utility/Enums.hpp +++ b/include/Nazara/Utility/Enums.hpp @@ -62,7 +62,7 @@ namespace Nz }; template<> - struct EnableFlagsOperators + struct EnumAsFlags { static constexpr bool value = true; }; @@ -453,7 +453,7 @@ namespace Nz }; template<> - struct EnableFlagsOperators + struct EnumAsFlags { static constexpr bool value = true; }; From 876fec6f5e74dea481c1a97ad7c18d776e4b3501 Mon Sep 17 00:00:00 2001 From: Lynix Date: Mon, 12 Dec 2016 15:10:37 +0100 Subject: [PATCH 7/9] 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; From e98a02b19013af6784c4ed78f8ede9c970df44c6 Mon Sep 17 00:00:00 2001 From: Lynix Date: Mon, 12 Dec 2016 15:12:15 +0100 Subject: [PATCH 8/9] Core/Flags: Allow handling of more than 32 enum options --- include/Nazara/Core/Flags.hpp | 28 ++++++++++++++-------------- include/Nazara/Core/Flags.inl | 20 ++++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/include/Nazara/Core/Flags.hpp b/include/Nazara/Core/Flags.hpp index 0e4623849..ff55e0d74 100644 --- a/include/Nazara/Core/Flags.hpp +++ b/include/Nazara/Core/Flags.hpp @@ -26,29 +26,29 @@ namespace Nz static_assert(std::is_enum::value, "Type must be an enumeration"); public: - constexpr Flags(UInt32 value); + using BitField = typename std::conditional<(EnumAsFlags::max > 32), UInt64, UInt32>::type; + + constexpr Flags(BitField value); constexpr Flags(E enumVal); explicit constexpr operator bool() const; - explicit constexpr operator UInt32() const; + explicit constexpr operator BitField() const; constexpr Flags operator~() const; - constexpr Flags operator&(Flags rhs) const; - constexpr Flags operator|(Flags rhs) const; - constexpr Flags operator^(Flags rhs) const; + constexpr Flags operator&(const Flags& rhs) const; + constexpr Flags operator|(const Flags& rhs) const; + constexpr Flags operator^(const Flags& rhs) const; - constexpr bool operator==(Flags rhs) const; - constexpr bool operator!=(Flags rhs) const; + constexpr bool operator==(const Flags& rhs) const; + constexpr bool operator!=(const Flags& rhs) const; - /*constexpr*/ Flags& operator|=(Flags rhs); - /*constexpr*/ Flags& operator&=(Flags rhs); - /*constexpr*/ Flags& operator^=(Flags rhs); + /*constexpr*/ Flags& operator|=(const Flags& rhs); + /*constexpr*/ Flags& operator&=(const Flags& rhs); + /*constexpr*/ Flags& operator^=(const Flags& rhs); - static constexpr UInt32 GetFlagValue(E enumValue); + static constexpr BitField GetFlagValue(E enumValue); - private: - UInt32 m_value; - static constexpr UInt32 ValueMask = ((UInt32(1) << (EnumAsFlags::max + 1)) - 1); + static constexpr BitField ValueMask = ((BitField(1) << (EnumAsFlags::max + 1)) - 1); private: BitField m_value; diff --git a/include/Nazara/Core/Flags.inl b/include/Nazara/Core/Flags.inl index 6e8e25d27..26e82c56c 100644 --- a/include/Nazara/Core/Flags.inl +++ b/include/Nazara/Core/Flags.inl @@ -84,7 +84,7 @@ namespace Nz * This will returns a copy of the Flags object with only enabled flags in common with the parameter */ template - constexpr Flags Flags::operator&(Flags rhs) const + constexpr Flags Flags::operator&(const Flags& rhs) const { return Flags(m_value & rhs.m_value); } @@ -98,7 +98,7 @@ namespace Nz * \param rhs Flags to combine with. */ template - constexpr Flags Flags::operator|(Flags rhs) const + constexpr Flags Flags::operator|(const Flags& rhs) const { 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. */ template - constexpr Flags Flags::operator^(Flags rhs) const + constexpr Flags Flags::operator^(const Flags& rhs) const { 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. */ template - constexpr bool Flags::operator==(Flags rhs) const + constexpr bool Flags::operator==(const Flags& rhs) const { 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. */ template - constexpr bool Flags::operator!=(Flags rhs) const + constexpr bool Flags::operator!=(const Flags& rhs) const { return !operator==(rhs); } @@ -155,7 +155,7 @@ namespace Nz * This will enable flags which are enabled in parameter object and not in Flag object. */ template - /*constexpr*/ Flags& Flags::operator|=(Flags rhs) + /*constexpr*/ Flags& Flags::operator|=(const Flags& rhs) { 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). */ template - /*constexpr*/ Flags& Flags::operator&=(Flags rhs) + /*constexpr*/ Flags& Flags::operator&=(const Flags& rhs) { 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. */ template - /*constexpr*/ Flags& Flags::operator^=(Flags rhs) + /*constexpr*/ Flags& Flags::operator^=(const Flags& rhs) { m_value ^= rhs.m_value; 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. */ template - constexpr UInt32 Flags::GetFlagValue(E enumValue) + constexpr typename Flags::BitField Flags::GetFlagValue(E enumValue) { - return 1U << static_cast(enumValue); + return 1U << static_cast(enumValue); } From cb447e4db7864571175acec270fdcdad5f9a535e Mon Sep 17 00:00:00 2001 From: Lynix Date: Mon, 12 Dec 2016 15:12:34 +0100 Subject: [PATCH 9/9] Core/Flags: Add assertion in case EnumAsFlags has not been specialized --- include/Nazara/Core/Flags.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/include/Nazara/Core/Flags.hpp b/include/Nazara/Core/Flags.hpp index ff55e0d74..d4f536356 100644 --- a/include/Nazara/Core/Flags.hpp +++ b/include/Nazara/Core/Flags.hpp @@ -24,6 +24,7 @@ namespace Nz class Flags { static_assert(std::is_enum::value, "Type must be an enumeration"); + static_assert(EnumAsFlags::value, "Enum has not been enabled as flags by an EnumAsFlags specialization"); public: using BitField = typename std::conditional<(EnumAsFlags::max > 32), UInt64, UInt32>::type;