Add support of alias'ed enums for IndexType/CppType traits generation (#1627)

This commit is contained in:
Andreas Süßenbach 2023-07-26 10:17:13 +02:00 committed by GitHub
parent 48b4b6f985
commit 24045ff3f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7436,41 +7436,31 @@ ${indexTypeTraits}
auto indexType = m_enums.find( "VkIndexType" ); auto indexType = m_enums.find( "VkIndexType" );
assert( indexType != m_enums.end() ); assert( indexType != m_enums.end() );
std::string indexTypeTraits; std::string indexTypeTraits;
std::set<std::string> listedCppTypes;
for ( auto const & value : indexType->second.values ) for ( auto const & value : indexType->second.values )
{ {
std::string valueName = generateEnumValueName( indexType->first, value.name, false ); assert( value.name.starts_with( "VK_INDEX_TYPE_UINT" ) || value.name.starts_with( "VK_INDEX_TYPE_NONE" ) );
std::string cppType; if ( value.alias.empty() && value.name.starts_with( "VK_INDEX_TYPE_UINT" ) )
if ( !valueName.starts_with( "eNone" ) )
{ {
// get the bit count out of the value Name (8, 16, 32, ... ) and generate the cppType (uint8_t,...) std::string valueName = generateEnumValueName( indexType->first, value.name, false );
assert( valueName.starts_with( "eUint" ) ); assert( valueName.starts_with( "eUint" ) );
auto beginDigit = valueName.begin() + strlen( "eUint" ); auto beginDigit = valueName.begin() + strlen( "eUint" );
assert( isdigit( *beginDigit ) ); assert( isdigit( *beginDigit ) );
auto endDigit = std::find_if( beginDigit, valueName.end(), []( std::string::value_type c ) { return !isdigit( c ); } ); auto endDigit = std::find_if( beginDigit, valueName.end(), []( std::string::value_type c ) { return !isdigit( c ); } );
cppType = "uint" + valueName.substr( strlen( "eUint" ), endDigit - beginDigit ) + "_t"; std::string cppType = "uint" + valueName.substr( strlen( "eUint" ), endDigit - beginDigit ) + "_t";
}
if ( !cppType.empty() ) // from type to enum value
{ const std::string typeToEnumTemplate = R"(
if ( listedCppTypes.insert( cppType ).second )
{
// IndexType traits aren't necessarily invertible.
// The Type -> Enum translation will only occur for the first prefixed enum value.
// A hypothetical extension to this enum with a conflicting prefix will use the core spec value.
const std::string typeToEnumTemplate = R"(
template <> template <>
struct IndexTypeValue<${cppType}> struct IndexTypeValue<${cppType}>
{ {
static VULKAN_HPP_CONST_OR_CONSTEXPR IndexType value = IndexType::${valueName}; static VULKAN_HPP_CONST_OR_CONSTEXPR IndexType value = IndexType::${valueName};
}; };
)"; )";
indexTypeTraits += replaceWithMap( typeToEnumTemplate, { { "cppType", cppType }, { "valueName", valueName } } ); indexTypeTraits += replaceWithMap( typeToEnumTemplate, { { "cppType", cppType }, { "valueName", valueName } } );
}
// Enum -> Type translations are always able to occur. // from enum value to type
const std::string enumToTypeTemplate = R"( const std::string enumToTypeTemplate = R"(
template <> template <>
struct CppType<IndexType, IndexType::${valueName}> struct CppType<IndexType, IndexType::${valueName}>
{ {