Templatized bitwise operators on FlagBits.

This commit is contained in:
asuessenbach 2022-10-10 11:04:27 +02:00
parent 41bcbe48d8
commit 302389d550
5 changed files with 981 additions and 2844 deletions

View File

@ -112,12 +112,6 @@ namespace VULKAN_HPP_NAMESPACE
template <typename EnumType, EnumType value> template <typename EnumType, EnumType value>
struct CppType struct CppType
{}; {};
template <typename Type>
struct isVulkanHandleType
{
static VULKAN_HPP_CONST_OR_CONSTEXPR bool value = false;
};
${enums} ${enums}
${indexTypeTraits} ${indexTypeTraits}
${bitmasks}} // namespace VULKAN_HPP_NAMESPACE ${bitmasks}} // namespace VULKAN_HPP_NAMESPACE
@ -2099,49 +2093,15 @@ std::string VulkanHppGenerator::generateBitmask( std::map<std::string, BitmaskDa
std::string bitmaskName = stripPrefix( bitmaskIt->first, "Vk" ); std::string bitmaskName = stripPrefix( bitmaskIt->first, "Vk" );
std::string enumName = stripPrefix( bitmaskBitsIt->first, "Vk" ); std::string enumName = stripPrefix( bitmaskBitsIt->first, "Vk" );
std::string alias = bitmaskIt->second.alias.empty() ? "" : ( " using " + stripPrefix( bitmaskIt->second.alias, "Vk" ) + " = " + bitmaskName + ";\n" );
// each Flags class is using the class 'Flags' with the corresponding FlagBits enum as the template parameter std::string allFlags;
std::string str = "\n using " + bitmaskName + " = Flags<" + enumName + ">;\n";
std::string alias = bitmaskIt->second.alias.empty() ? "" : ( "\n using " + stripPrefix( bitmaskIt->second.alias, "Vk" ) + " = " + bitmaskName + ";\n" );
if ( bitmaskBitsIt->second.values.empty() ) if ( bitmaskBitsIt->second.values.empty() )
{ {
str += alias; allFlags = "0";
} }
else else
{ {
static const std::string bitmaskTemplate = R"(
template <> struct FlagTraits<${enumName}>
{
enum : ${bitmaskType}
{
allFlags = ${allFlags}
};
};
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR ${bitmaskName} operator|( ${enumName} bit0, ${enumName} bit1 ) VULKAN_HPP_NOEXCEPT
{
return ${bitmaskName}( bit0 ) | bit1;
}
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR ${bitmaskName} operator&( ${enumName} bit0, ${enumName} bit1 ) VULKAN_HPP_NOEXCEPT
{
return ${bitmaskName}( bit0 ) & bit1;
}
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR ${bitmaskName} operator^( ${enumName} bit0, ${enumName} bit1 ) VULKAN_HPP_NOEXCEPT
{
return ${bitmaskName}( bit0 ) ^ bit1;
}
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR ${bitmaskName} operator~( ${enumName} bits ) VULKAN_HPP_NOEXCEPT
{
return ~( ${bitmaskName}( bits ) );
}
${alias})";
std::string allFlags;
bool encounteredFlag = false; bool encounteredFlag = false;
std::string previousEnter, previousLeave; std::string previousEnter, previousLeave;
for ( auto const & value : bitmaskBitsIt->second.values ) for ( auto const & value : bitmaskBitsIt->second.values )
@ -2168,13 +2128,24 @@ ${alias})";
previousLeave.resize( previousLeave.size() - strlen( "\n" ) ); previousLeave.resize( previousLeave.size() - strlen( "\n" ) );
allFlags += "\n" + previousLeave; allFlags += "\n" + previousLeave;
} }
str += replaceWithMap(
bitmaskTemplate,
{ { "alias", alias }, { "allFlags", allFlags }, { "bitmaskName", bitmaskName }, { "bitmaskType", bitmaskIt->second.type }, { "enumName", enumName } } );
} }
return str; static const std::string bitmaskTemplate = R"(
template <> struct FlagTraits<${enumName}>
{
static VULKAN_HPP_CONST_OR_CONSTEXPR bool isBitmask = true;
enum : ${bitmaskType}
{
allFlags = ${allFlags}
};
};
using ${bitmaskName} = Flags<${enumName}>;
${alias})";
return replaceWithMap(
bitmaskTemplate,
{ { "alias", alias }, { "allFlags", allFlags }, { "bitmaskName", bitmaskName }, { "bitmaskType", bitmaskIt->second.type }, { "enumName", enumName } } );
} }
std::string VulkanHppGenerator::generateBitmasks() const std::string VulkanHppGenerator::generateBitmasks() const
@ -5939,6 +5910,12 @@ std::string VulkanHppGenerator::generateHandles() const
//=============== //===============
//=== HANDLEs === //=== HANDLEs ===
//=============== //===============
template <typename Type>
struct isVulkanHandleType
{
static VULKAN_HPP_CONST_OR_CONSTEXPR bool value = false;
};
)"; )";
std::set<std::string> listedHandles; std::set<std::string> listedHandles;

View File

@ -1,6 +1,8 @@
template <typename FlagBitsType> template <typename FlagBitsType>
struct FlagTraits struct FlagTraits
{}; {
static VULKAN_HPP_CONST_OR_CONSTEXPR bool isBitmask = false;
};
template <typename BitType> template <typename BitType>
class Flags class Flags
@ -182,3 +184,29 @@
{ {
return flags.operator^( bit ); return flags.operator^( bit );
} }
// bitwise operators on BitType
template <typename BitType, typename std::enable_if<FlagTraits<BitType>::isBitmask, bool>::type = true>
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR Flags<BitType> operator&(BitType lhs, BitType rhs) VULKAN_HPP_NOEXCEPT
{
return Flags<BitType>( lhs ) & rhs;
}
template <typename BitType, typename std::enable_if<FlagTraits<BitType>::isBitmask, bool>::type = true>
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR Flags<BitType> operator|(BitType lhs, BitType rhs) VULKAN_HPP_NOEXCEPT
{
return Flags<BitType>( lhs ) | rhs;
}
template <typename BitType, typename std::enable_if<FlagTraits<BitType>::isBitmask, bool>::type = true>
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR Flags<BitType> operator^(BitType lhs, BitType rhs) VULKAN_HPP_NOEXCEPT
{
return Flags<BitType>( lhs ) ^ rhs;
}
template <typename BitType, typename std::enable_if<FlagTraits<BitType>::isBitmask, bool>::type = true>
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR Flags<BitType> operator~( BitType bit ) VULKAN_HPP_NOEXCEPT
{
return ~( Flags<BitType>( bit ) );
}

View File

@ -377,6 +377,7 @@ namespace VULKAN_HPP_NAMESPACE
template <typename FlagBitsType> template <typename FlagBitsType>
struct FlagTraits struct FlagTraits
{ {
static VULKAN_HPP_CONST_OR_CONSTEXPR bool isBitmask = false;
}; };
template <typename BitType> template <typename BitType>
@ -554,6 +555,31 @@ namespace VULKAN_HPP_NAMESPACE
return flags.operator^( bit ); return flags.operator^( bit );
} }
// bitwise operators on BitType
template <typename BitType, typename std::enable_if<FlagTraits<BitType>::isBitmask, bool>::type = true>
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR Flags<BitType> operator&( BitType lhs, BitType rhs ) VULKAN_HPP_NOEXCEPT
{
return Flags<BitType>( lhs ) & rhs;
}
template <typename BitType, typename std::enable_if<FlagTraits<BitType>::isBitmask, bool>::type = true>
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR Flags<BitType> operator|( BitType lhs, BitType rhs ) VULKAN_HPP_NOEXCEPT
{
return Flags<BitType>( lhs ) | rhs;
}
template <typename BitType, typename std::enable_if<FlagTraits<BitType>::isBitmask, bool>::type = true>
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR Flags<BitType> operator^( BitType lhs, BitType rhs ) VULKAN_HPP_NOEXCEPT
{
return Flags<BitType>( lhs ) ^ rhs;
}
template <typename BitType, typename std::enable_if<FlagTraits<BitType>::isBitmask, bool>::type = true>
VULKAN_HPP_INLINE VULKAN_HPP_CONSTEXPR Flags<BitType> operator~( BitType bit ) VULKAN_HPP_NOEXCEPT
{
return ~( Flags<BitType>( bit ) );
}
#if !defined( VULKAN_HPP_DISABLE_ENHANCED_MODE ) #if !defined( VULKAN_HPP_DISABLE_ENHANCED_MODE )
template <typename T> template <typename T>
class ArrayProxy class ArrayProxy

File diff suppressed because it is too large Load Diff

View File

@ -1514,6 +1514,12 @@ namespace VULKAN_HPP_NAMESPACE
//=== HANDLEs === //=== HANDLEs ===
//=============== //===============
template <typename Type>
struct isVulkanHandleType
{
static VULKAN_HPP_CONST_OR_CONSTEXPR bool value = false;
};
class SurfaceKHR class SurfaceKHR
{ {
public: public: