Merge pull request #1301 from asuessenbach/cleanup

Cleanup on generateDataDeclarations.
This commit is contained in:
Andreas Süßenbach 2022-05-09 15:39:42 +02:00 committed by GitHub
commit 7d2ed82957
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4656,173 +4656,176 @@ std::string VulkanHppGenerator::generateDataDeclarations( CommandData const &
std::string const & returnVariable ) const std::string const & returnVariable ) const
{ {
assert( dataTypes.size() == returnParams.size() ); assert( dataTypes.size() == returnParams.size() );
std::string dataDeclarations; std::string dataDeclarations;
switch ( returnParams.size() ) switch ( returnParams.size() )
{ {
case 0: break; case 0: break;
case 1: case 1:
if ( chained )
{ {
assert( ( commandData.returnType == "void" ) || ( commandData.returnType == "VkResult" ) ); auto vectorParamIt = vectorParams.find( returnParams[0] );
assert( vectorParams.empty() ); if ( !chained )
{
if ( ( vectorParamIt == vectorParams.end() ) || singular )
{
std::string const dataDeclarationsTemplate = R"(${returnType} ${returnVariable};)";
dataDeclarations = replaceWithMap( dataDeclarationsTemplate, { { "returnType", dataType }, { "returnVariable", returnVariable } } );
}
else
{
std::string allocator = stripPrefix( dataTypes[0], "VULKAN_HPP_NAMESPACE::" ) + "Allocator";
std::string dataTypeAllocator = !unique ? ( ", " + startUpperCase( allocator ) ) : "";
std::string vectorAllocator = ( withAllocator && !unique ) ? ( ", " + startLowerCase( allocator ) ) : "";
std::string vectorSize = getVectorSize( commandData.params, vectorParams, returnParams[0], dataTypes[0], templatedParams );
std::string const dataDeclarationsTemplate =
R"(std::vector<${dataType}${dataTypeAllocator}> ${returnVariable}( ${vectorSize}${vectorAllocator} );)";
dataDeclarations = replaceWithMap( dataDeclarationsTemplate,
{ { "dataType", dataTypes[0] },
{ "dataTypeAllocator", dataTypeAllocator },
{ "returnVariable", returnVariable },
{ "vectorAllocator", vectorAllocator },
{ "vectorSize", vectorSize } } );
}
}
else
{
assert( ( vectorParamIt == vectorParams.end() ) || singular );
std::string dataVariable = startLowerCase( stripPrefix( commandData.params[returnParams[0]].name, "p" ) );
std::string const dataDeclarationsTemplate = R"(${returnType} ${returnVariable}; std::string const dataDeclarationsTemplate = R"(${returnType} ${returnVariable};
${dataType} & ${dataVariable} = ${returnVariable}.template get<${dataType}>();)"; ${dataType} & ${dataVariable} = ${returnVariable}.template get<${dataType}>();)";
dataDeclarations = replaceWithMap( dataDeclarationsTemplate, dataDeclarations = replaceWithMap( dataDeclarationsTemplate,
{ { "dataType", dataTypes[0] }, { { "dataType", dataTypes[0] },
{ "dataVariable", startLowerCase( stripPrefix( commandData.params[returnParams[0]].name, "p" ) ) }, { "dataVariable", dataVariable },
{ "returnType", ( commandData.returnType == "void" ) ? returnType : "StructureChain<X, Y, Z...>" }, { "returnType", ( commandData.returnType == "void" ) ? returnType : "StructureChain<X, Y, Z...>" },
{ "returnVariable", returnVariable } } ); { "returnVariable", returnVariable } } );
} }
else
{
if ( singular || ( vectorParams.find( returnParams[0] ) == vectorParams.end() ) )
{
std::string const dataDeclarationsTemplate = R"(${dataType} ${dataVariable};)";
dataDeclarations = replaceWithMap( dataDeclarationsTemplate, { { "dataType", dataTypes[0] }, { "dataVariable", returnVariable } } );
}
else
{
std::string dataTypeAllocator = startUpperCase( stripPrefix( dataTypes[0], "VULKAN_HPP_NAMESPACE::" ) ) + "Allocator";
std::string vectorSize = getVectorSize( commandData.params, vectorParams, returnParams[0], dataTypes[0], templatedParams );
std::string const dataDeclarationsTemplate = R"(std::vector<${dataType}${dataTypeAllocator}> ${dataVariable}( ${vectorSize}${vectorAllocator} );)";
dataDeclarations = replaceWithMap( dataDeclarationsTemplate,
{ { "dataType", dataTypes[0] },
{ "dataTypeAllocator", unique ? "" : ( ", " + dataTypeAllocator ) },
{ "dataVariable", returnVariable },
{ "vectorAllocator", ( withAllocator && !unique ) ? ( ", " + startLowerCase( dataTypeAllocator ) ) : "" },
{ "vectorSize", vectorSize } } );
}
} }
break; break;
case 2: case 2:
if ( chained )
{ {
assert( !singular ); if ( vectorParams.size() == 1 )
assert( returnParams.size() == 2 ); {
assert( vectorParams.find( returnParams[0] ) == vectorParams.end() ); assert( ( returnParams[0] == vectorParams.begin()->second ) && ( returnParams[1] == vectorParams.begin()->first ) && !singular && !unique );
auto vectorParamIt = vectorParams.find( returnParams[1] );
assert( vectorParamIt != vectorParams.end() );
assert( vectorParamIt->second == returnParams[0] );
std::string const dataDeclarationTemplate = R"(std::vector<StructureChain, StructureChainAllocator> structureChains${structureChainAllocator}; std::string counterVariable = startLowerCase( stripPrefix( commandData.params[returnParams[0]].name, "p" ) );
std::vector<${vectorElementType}> ${vectorName}; if ( !chained )
${counterType} ${counterName};)";
dataDeclarations = replaceWithMap( dataDeclarationTemplate,
{ {
{ "counterName", startLowerCase( stripPrefix( commandData.params[returnParams[0]].name, "p" ) ) },
{ "counterType", dataTypes[0] },
{ "structureChainAllocator", withAllocator ? ( "( structureChainAllocator )" ) : "" },
{ "vectorElementType", dataTypes[1] },
{ "vectorName", startLowerCase( stripPrefix( commandData.params[returnParams[1]].name, "p" ) ) },
} );
}
else
{
auto vectorParamIt = vectorParams.find( returnParams[0] );
if ( vectorParamIt == vectorParams.end() || singular )
{
vectorParamIt = vectorParams.find( returnParams[1] );
if ( vectorParamIt == vectorParams.end() )
{
assert( vectorParams.empty() || singular );
std::string dataNames[2];
for ( size_t i = 0; i < returnParams.size(); ++i )
{
dataNames[i] = startLowerCase( stripPrefix( commandData.params[returnParams[i]].name, "p" ) );
if ( vectorParams.find( returnParams[i] ) != vectorParams.end() )
{
assert( singular );
dataNames[i] = stripPluralS( dataNames[i] );
}
}
std::string const dataDeclarationTemplate = R"(std::pair<${firstDataType},${secondDataType}> data;
${firstDataType} & ${firstDataName} = data.first;
${secondDataType} & ${secondDataName} = data.second;)";
dataDeclarations = replaceWithMap( dataDeclarationTemplate,
{ { "firstDataName", dataNames[0] },
{ "firstDataType", dataTypes[0] },
{ "secondDataName", dataNames[1] },
{ "secondDataType", dataTypes[1] } } );
}
else
{
assert( !singular );
assert( vectorParamIt->second == returnParams[0] );
assert( commandData.params[returnParams[0]].type.isNonConstPointer() );
std::string vectorAllocator = std::string vectorAllocator =
withAllocator ? ( "( " + startLowerCase( stripPrefix( dataTypes[1], "VULKAN_HPP_NAMESPACE::" ) ) + "Allocator )" ) : ""; withAllocator ? ( "( " + startLowerCase( stripPrefix( dataTypes[1], "VULKAN_HPP_NAMESPACE::" ) ) + "Allocator )" ) : "";
std::string const dataDeclarationTemplate = R"(${returnType} ${vectorName}${vectorAllocator}; std::string const dataDeclarationTemplate = R"(${returnType} ${returnVariable}${vectorAllocator};
${counterType} ${counterName};)"; ${counterType} ${counterVariable};)";
dataDeclarations = replaceWithMap( dataDeclarationTemplate, dataDeclarations = replaceWithMap( dataDeclarationTemplate,
{ { "counterName", startLowerCase( stripPrefix( commandData.params[returnParams[0]].name, "p" ) ) }, { { "counterType", dataTypes[0] },
{ "counterType", dataTypes[0] }, { "counterVariable", counterVariable },
{ "returnType", dataType }, { "returnType", dataType },
{ "vectorAllocator", vectorAllocator }, { "returnVariable", returnVariable },
{ "vectorName", returnVariable } } ); { "vectorAllocator", vectorAllocator } } );
}
else
{
std::string structureChainAllocator = withAllocator ? ( "( structureChainAllocator )" ) : "";
std::string vectorVariable = startLowerCase( stripPrefix( commandData.params[returnParams[1]].name, "p" ) );
std::string const dataDeclarationTemplate = R"(std::vector<StructureChain, StructureChainAllocator> structureChains${structureChainAllocator};
std::vector<${vectorElementType}> ${vectorVariable};
${counterType} ${counterVariable};)";
dataDeclarations = replaceWithMap( dataDeclarationTemplate,
{
{ "counterType", dataTypes[0] },
{ "counterVariable", counterVariable },
{ "structureChainAllocator", structureChainAllocator },
{ "vectorElementType", dataTypes[1] },
{ "vectorVariable", vectorVariable },
} );
} }
} }
else else
{ {
assert( vectorParams.find( returnParams[1] ) == vectorParams.end() ); assert( ( vectorParams.size() == 2 ) && ( returnParams[0] == std::next( vectorParams.begin() )->first ) &&
assert( vectorParamIt != vectorParams.begin() ); ( vectorParams.find( returnParams[1] ) == vectorParams.end() ) && !chained && !unique );
assert( vectorParamIt->second != returnParams[1] );
std::string firstDataVariable = startLowerCase( stripPrefix( commandData.params[returnParams[0]].name, "p" ) );
std::string secondDataVariable = startLowerCase( stripPrefix( commandData.params[returnParams[1]].name, "p" ) );
if ( singular )
{
firstDataVariable = stripPluralS( firstDataVariable );
std::string const dataDeclarationTemplate = R"(std::pair<${firstDataType},${secondDataType}> data;
${firstDataType} & ${firstDataVariable} = data.first;
${secondDataType} & ${secondDataVariable} = data.second;)";
dataDeclarations = replaceWithMap( dataDeclarationTemplate,
{ { "firstDataType", dataTypes[0] },
{ "firstDataVariable", firstDataVariable },
{ "secondDataType", dataTypes[1] },
{ "secondDataVariable", secondDataVariable } } );
}
else
{
std::string allocatorType = startUpperCase( stripPrefix( dataTypes[0], "VULKAN_HPP_NAMESPACE::" ) ) + "Allocator"; std::string allocatorType = startUpperCase( stripPrefix( dataTypes[0], "VULKAN_HPP_NAMESPACE::" ) ) + "Allocator";
std::string allocateInitializer = withAllocator ? ( ", " + startLowerCase( allocatorType ) ) : "";
std::string vectorSize = startLowerCase( stripPrefix( commandData.params[vectorParams.begin()->first].name, "p" ) ) + ".size()"; std::string vectorSize = startLowerCase( stripPrefix( commandData.params[vectorParams.begin()->first].name, "p" ) ) + ".size()";
std::string const dataDeclarationTemplate = std::string const dataDeclarationTemplate =
R"(std::pair<std::vector<${vectorElementType}, ${allocatorType}>,${valueType}> data( std::piecewise_construct, std::forward_as_tuple( ${vectorSize}${allocateInitializer} ), std::forward_as_tuple( 0 ) ); R"(std::pair<std::vector<${firstDataType}, ${allocatorType}>,${secondDataType}> data( std::piecewise_construct, std::forward_as_tuple( ${vectorSize}${allocateInitializer} ), std::forward_as_tuple( 0 ) );
std::vector<${vectorElementType}, ${allocatorType}> & ${vectorName} = data.first; std::vector<${firstDataType}, ${allocatorType}> & ${firstDataVariable} = data.first;
${valueType} & ${valueName} = data.second;)"; ${secondDataType} & ${secondDataVariable} = data.second;)";
dataDeclarations = replaceWithMap( dataDeclarationTemplate, dataDeclarations = replaceWithMap( dataDeclarationTemplate,
{ { "allocateInitializer", withAllocator ? ( ", " + startLowerCase( allocatorType ) ) : "" }, { { "allocateInitializer", allocateInitializer },
{ "allocatorType", allocatorType }, { "allocatorType", allocatorType },
{ "valueName", startLowerCase( stripPrefix( commandData.params[returnParams[1]].name, "p" ) ) }, { "firstDataType", dataTypes[0] },
{ "valueType", dataTypes[1] }, { "firstDataVariable", firstDataVariable },
{ "vectorElementType", dataTypes[0] }, { "secondDataType", dataTypes[1] },
{ "vectorName", startLowerCase( stripPrefix( commandData.params[returnParams[0]].name, "p" ) ) }, { "secondDataVariable", secondDataVariable },
{ "vectorSize", vectorSize } } ); { "vectorSize", vectorSize } } );
} }
} }
}
break; break;
default: case 3:
{ {
assert( ( vectorParams.size() == 2 ) && ( vectorParams.begin()->first == returnParams[1] ) && ( vectorParams.begin()->second == returnParams[0] ) && assert( ( vectorParams.size() == 2 ) && ( returnParams[0] == vectorParams.begin()->second ) && ( returnParams[1] == vectorParams.begin()->first ) &&
( std::next( vectorParams.begin() )->first == returnParams[2] ) && ( std::next( vectorParams.begin() )->second == returnParams[0] ) ); ( returnParams[2] == std::next( vectorParams.begin() )->first ) && ( returnParams[0] == std::next( vectorParams.begin() )->second ) &&
templatedParams.empty() && !singular && !chained && !unique );
std::string counterVariable = startLowerCase( stripPrefix( commandData.params[returnParams[0]].name, "p" ) );
std::string firstVectorAllocatorType = startUpperCase( stripPrefix( dataTypes[1], "VULKAN_HPP_NAMESPACE::" ) ) + "Allocator"; std::string firstVectorAllocatorType = startUpperCase( stripPrefix( dataTypes[1], "VULKAN_HPP_NAMESPACE::" ) ) + "Allocator";
std::string firstVectorVariable = startLowerCase( stripPrefix( commandData.params[returnParams[1]].name, "p" ) );
std::string secondVectorAllocatorType = startUpperCase( stripPrefix( dataTypes[2], "VULKAN_HPP_NAMESPACE::" ) ) + "Allocator"; std::string secondVectorAllocatorType = startUpperCase( stripPrefix( dataTypes[2], "VULKAN_HPP_NAMESPACE::" ) ) + "Allocator";
std::string secondVectorVariable = startLowerCase( stripPrefix( commandData.params[returnParams[2]].name, "p" ) );
std::string pairConstructor = withAllocator ? ( "( std::piecewise_construct, std::forward_as_tuple( " + startLowerCase( firstVectorAllocatorType ) + std::string pairConstructor = withAllocator ? ( "( std::piecewise_construct, std::forward_as_tuple( " + startLowerCase( firstVectorAllocatorType ) +
" ), std::forward_as_tuple( " + startLowerCase( secondVectorAllocatorType ) + " ) )" ) " ), std::forward_as_tuple( " + startLowerCase( secondVectorAllocatorType ) + " ) )" )
: ""; : "";
std::string const dataDeclarationsTemplate = std::string const dataDeclarationsTemplate =
R"(std::pair<std::vector<${firstVectorElementType}, ${firstVectorAllocatorType}>, std::vector<${secondVectorElementType}, ${secondVectorAllocatorType}>> data${pairConstructor}; R"(std::pair<std::vector<${firstVectorElementType}, ${firstVectorAllocatorType}>, std::vector<${secondVectorElementType}, ${secondVectorAllocatorType}>> data${pairConstructor};
std::vector<${firstVectorElementType}, ${firstVectorAllocatorType}> & ${firstVectorName} = data.first; std::vector<${firstVectorElementType}, ${firstVectorAllocatorType}> & ${firstVectorVariable} = data.first;
std::vector<${secondVectorElementType}, ${secondVectorAllocatorType}> & ${secondVectorName} = data.second; std::vector<${secondVectorElementType}, ${secondVectorAllocatorType}> & ${secondVectorVariable} = data.second;
${counterType} ${counterName};)"; ${counterType} ${counterVariable};)";
dataDeclarations = replaceWithMap( dataDeclarationsTemplate, dataDeclarations = replaceWithMap( dataDeclarationsTemplate,
{ { "counterName", startLowerCase( stripPrefix( commandData.params[returnParams[0]].name, "p" ) ) }, { { "counterType", dataTypes[0] },
{ "counterType", dataTypes[0] }, { "counterVariable", counterVariable },
{ "firstVectorAllocatorType", firstVectorAllocatorType }, { "firstVectorAllocatorType", firstVectorAllocatorType },
{ "firstVectorElementType", dataTypes[1] }, { "firstVectorElementType", dataTypes[1] },
{ "firstVectorName", startLowerCase( stripPrefix( commandData.params[returnParams[1]].name, "p" ) ) }, { "firstVectorVariable", firstVectorVariable },
{ "pairConstructor", pairConstructor }, { "pairConstructor", pairConstructor },
{ "secondVectorAllocatorType", secondVectorAllocatorType }, { "secondVectorAllocatorType", secondVectorAllocatorType },
{ "secondVectorElementType", dataTypes[2] }, { "secondVectorElementType", dataTypes[2] },
{ "secondVectorName", startLowerCase( stripPrefix( commandData.params[returnParams[2]].name, "p" ) ) } } ); { "secondVectorVariable", secondVectorVariable } } );
} }
break; break;
default: assert( false ); break;
} }
return dataDeclarations; return dataDeclarations;
} }