Optimize to_string functions for enums (#2096)

* Optimize `to_string` functions for enums

* Fix `-Wunused-parameter` warning
This commit is contained in:
Victor Chernyakin 2025-03-06 09:30:20 -07:00 committed by GitHub
parent da9db0c8a0
commit fe203f86d0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 1835 additions and 1698 deletions

View File

@ -2558,31 +2558,6 @@ std::string VulkanHppGenerator::generateBitmaskToString( std::map<std::string, B
std::string enumName = stripPrefix( bitmaskBitsIt->first, "Vk" ); std::string enumName = stripPrefix( bitmaskBitsIt->first, "Vk" );
std::string str; std::string str;
if ( bitmaskBitsIt->second.values.empty() ||
std::none_of( bitmaskBitsIt->second.values.begin(), bitmaskBitsIt->second.values.end(), []( auto const & evd ) { return evd.supported; } ) )
{
static std::string bitmaskToStringTemplate = R"(
VULKAN_HPP_INLINE std::string to_string( ${bitmaskName} )
{
return "{}";
}
)";
str += replaceWithMap( bitmaskToStringTemplate, { { "bitmaskName", bitmaskName } } );
}
else
{
static const std::string bitmaskToStringTemplate = R"(
VULKAN_HPP_INLINE std::string to_string( ${bitmaskName} value )
{
if ( !value )
return "${emptyValue}";
std::string result;
${toStringChecks}
return "{ " + result.substr( 0, result.size() - 3 ) + " }";
}
)";
std::string emptyValue = "{}"; std::string emptyValue = "{}";
std::string toStringChecks; std::string toStringChecks;
std::string previousEnter, previousLeave; std::string previousEnter, previousLeave;
@ -2613,6 +2588,30 @@ ${toStringChecks}
previousLeave.resize( previousLeave.size() - strlen( "\n" ) ); previousLeave.resize( previousLeave.size() - strlen( "\n" ) );
} }
if ( toStringChecks.empty() )
{
static const std::string bitmaskToStringTemplate = R"(
VULKAN_HPP_INLINE std::string to_string( ${bitmaskName} )
{
return "${emptyValue}";
}
)";
str += replaceWithMap( bitmaskToStringTemplate, { { "bitmaskName", bitmaskName }, { "emptyValue", emptyValue } } );
}
else
{
static const std::string bitmaskToStringTemplate = R"(
VULKAN_HPP_INLINE std::string to_string( ${bitmaskName} value )
{
std::string result = "{";
${toStringChecks}
if ( result.size() > 1 )
result.back() = '}';
else
result = "${emptyValue}";
return result;
}
)";
str += replaceWithMap( bitmaskToStringTemplate, { { "bitmaskName", bitmaskName }, { "emptyValue", emptyValue }, { "toStringChecks", toStringChecks } } ); str += replaceWithMap( bitmaskToStringTemplate, { { "bitmaskName", bitmaskName }, { "emptyValue", emptyValue }, { "toStringChecks", toStringChecks } } );
} }

File diff suppressed because it is too large Load Diff