Remove dumb parent-pointer in raii-objects.

This commit is contained in:
asuessenbach 2021-11-08 17:31:36 +01:00
parent 6da2240053
commit 0beeac873a
3 changed files with 1311 additions and 1566 deletions

View File

@ -2254,26 +2254,21 @@ std::string VulkanHppGenerator::generateCallArgumentsEnhanced( CommandData const
encounteredArgument = true; encounteredArgument = true;
break; break;
case 2: case 2:
assert( isHandleType( commandData.params[0].type.type ) && commandData.params[0].type.isValue() );
assert( commandData.params[0].arraySizes.empty() && commandData.params[0].len.empty() );
assert( commandData.params[0].type.type == commandData.handle );
if ( skipLeadingGrandParent( *m_handles.find( commandData.params[1].type.type ) ) )
{ {
auto [parentType, parentName] = getParentTypeAndName( *m_handles.find( commandData.params[1].type.type ) ); assert( isHandleType( commandData.params[0].type.type ) && commandData.params[0].type.isValue() );
arguments = "static_cast<" + commandData.handle + ">( **m_" + parentName + "->get" + assert( commandData.params[0].arraySizes.empty() && commandData.params[0].len.empty() );
stripPrefix( commandData.handle, "Vk" ) + "() )"; assert( commandData.params[0].type.type == commandData.handle );
} auto handleIt = m_handles.find( commandData.params[1].type.type );
else assert( handleIt != m_handles.end() );
{ arguments = "static_cast<" + commandData.handle + ">( m_" +
arguments = "static_cast<" + commandData.handle + ">( **m_" +
startLowerCase( stripPrefix( commandData.handle, "Vk" ) ) + " )"; startLowerCase( stripPrefix( commandData.handle, "Vk" ) ) + " )";
}
assert( isHandleType( commandData.params[1].type.type ) && commandData.params[1].type.isValue() ); assert( commandData.params[1].type.isValue() && commandData.params[1].arraySizes.empty() &&
assert( commandData.params[1].arraySizes.empty() && commandData.params[1].len.empty() ); commandData.params[1].len.empty() );
arguments += ", static_cast<" + commandData.params[1].type.type + ">( m_" + arguments += ", static_cast<" + commandData.params[1].type.type + ">( m_" +
startLowerCase( stripPrefix( commandData.params[1].type.type, "Vk" ) ) + " )"; generateRAIIHandleConstructorParamName( handleIt->first, handleIt->second.destructorIt ) + " )";
encounteredArgument = true; encounteredArgument = true;
}
break; break;
} }
} }
@ -7169,16 +7164,15 @@ std::string VulkanHppGenerator::generateRAIIHandle( std::pair<std::string, Handl
{ {
rescheduleRAIIHandle( str, handle, listedHandles, specialFunctions ); rescheduleRAIIHandle( str, handle, listedHandles, specialFunctions );
auto [enter, leave] = generateProtection( handle.first, !handle.second.alias.empty() ); auto [enter, leave] = generateProtection( handle.first, !handle.second.alias.empty() );
std::string handleType = stripPrefix( handle.first, "Vk" ); std::string handleType = stripPrefix( handle.first, "Vk" );
std::string handleName = startLowerCase( handleType ); std::string handleName = generateRAIIHandleConstructorParamName( handle.first, handle.second.destructorIt );
auto [parentType, parentName] = getParentTypeAndName( handle );
auto [singularConstructors, arrayConstructors] = generateRAIIHandleConstructors( handle ); auto [singularConstructors, arrayConstructors] = generateRAIIHandleConstructors( handle );
auto [destructor, destructorCall] = auto [destructor, destructorCall] =
( handle.second.destructorIt == m_commands.end() ) ( handle.second.destructorIt == m_commands.end() )
? std::make_pair( "", "" ) ? std::make_pair( "", "" )
: generateRAIIHandleDestructor( "Vk" + parentType, handle.first, handle.second.destructorIt, enter ); : generateRAIIHandleDestructor( handle.first, handle.second.destructorIt, enter );
auto [getConstructorSuccessCode, memberVariables, moveConstructorInitializerList, moveAssignmentInstructions] = auto [getConstructorSuccessCode, memberVariables, moveConstructorInitializerList, moveAssignmentInstructions] =
generateRAIIHandleDetails( handle, destructorCall ); generateRAIIHandleDetails( handle, destructorCall );
@ -7212,6 +7206,18 @@ std::string VulkanHppGenerator::generateRAIIHandle( std::pair<std::string, Handl
? "VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::DeviceDispatcher" ? "VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::DeviceDispatcher"
: "VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::InstanceDispatcher"; : "VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::InstanceDispatcher";
std::string getParent;
if ( ( handle.first != "VkInstance" ) && ( handle.first != "VkDevice" ) &&
( handle.second.destructorIt != m_commands.end() ) )
{
assert( !handle.second.destructorIt->second.params.empty() );
std::string parentType = stripPrefix( handle.second.destructorIt->second.params.front().type.type, "Vk" );
getParent = " VULKAN_HPP_NAMESPACE::" + parentType + " get" + parentType + "() const\n";
getParent += " {\n";
getParent += " return m_" + handle.second.destructorIt->second.params.front().name + ";\n";
getParent += " }\n";
}
const std::string handleTemplate = R"( const std::string handleTemplate = R"(
${enter} class ${handleType} ${enter} class ${handleType}
{ {
@ -7247,11 +7253,7 @@ ${moveAssignmentInstructions}
} }
${getConstructorSuccessCode} ${getConstructorSuccessCode}
VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::${parentType} const * get${parentType}() const VULKAN_HPP_NOEXCEPT ${getParent}
{
return m_${parentName};
}
${dispatcherType} const * getDispatcher() const ${dispatcherType} const * getDispatcher() const
{ {
VULKAN_HPP_ASSERT( m_dispatcher->getVkHeaderVersion() == VK_HEADER_VERSION ); VULKAN_HPP_ASSERT( m_dispatcher->getVkHeaderVersion() == VK_HEADER_VERSION );
@ -7272,6 +7274,7 @@ ${leave})";
{ "enter", enter }, { "enter", enter },
{ "getConstructorSuccessCode", getConstructorSuccessCode }, { "getConstructorSuccessCode", getConstructorSuccessCode },
{ "getDispatcherReturn", ( handleType == "Device" ) || ( handleType == "Instance" ) ? "&*" : "" }, { "getDispatcherReturn", ( handleType == "Device" ) || ( handleType == "Instance" ) ? "&*" : "" },
{ "getParent", getParent },
{ "handleName", handleName }, { "handleName", handleName },
{ "handleType", handleType }, { "handleType", handleType },
{ "leave", leave }, { "leave", leave },
@ -7280,8 +7283,6 @@ ${leave})";
{ "moveAssignmentInstructions", moveAssignmentInstructions }, { "moveAssignmentInstructions", moveAssignmentInstructions },
{ "moveConstructorInitializerList", moveConstructorInitializerList }, { "moveConstructorInitializerList", moveConstructorInitializerList },
{ "objTypeEnum", objTypeEnum }, { "objTypeEnum", objTypeEnum },
{ "parentType", parentType },
{ "parentName", parentName },
{ "singularConstructors", singularConstructors } } ); { "singularConstructors", singularConstructors } } );
if ( !arrayConstructors.empty() ) if ( !arrayConstructors.empty() )
@ -9628,7 +9629,8 @@ std::string VulkanHppGenerator::generateRAIIHandleConstructorArguments(
std::string arguments = "VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::" + parentType + " const & " + parentName; std::string arguments = "VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::" + parentType + " const & " + parentName;
if ( takesOwnership ) if ( takesOwnership )
{ {
arguments += ", " + handle.first + " " + startLowerCase( stripPrefix( handle.first, "Vk" ) ); arguments +=
", " + handle.first + " " + generateRAIIHandleConstructorParamName( handle.first, handle.second.destructorIt );
} }
if ( constructorIt != m_commands.end() ) if ( constructorIt != m_commands.end() )
@ -9688,7 +9690,7 @@ std::string VulkanHppGenerator::generateRAIIHandleConstructorCallArguments(
assert( ( 1 < constructorIt->second.params.size() ) && assert( ( 1 < constructorIt->second.params.size() ) &&
( m_handles.find( constructorIt->second.params[0].type.type ) != m_handles.end() ) && ( m_handles.find( constructorIt->second.params[0].type.type ) != m_handles.end() ) &&
( m_handles.find( constructorIt->second.params[1].type.type ) != m_handles.end() ) ); ( m_handles.find( constructorIt->second.params[1].type.type ) != m_handles.end() ) );
arguments += "static_cast<" + constructorIt->second.params[0].type.type + ">( **" + arguments += "static_cast<" + constructorIt->second.params[0].type.type + ">( " +
constructorIt->second.params[1].name + ".get" + constructorIt->second.params[1].name + ".get" +
stripPrefix( constructorIt->second.params[0].type.type, "Vk" ) + "() )"; stripPrefix( constructorIt->second.params[0].type.type, "Vk" ) + "() )";
encounteredArgument = true; encounteredArgument = true;
@ -9708,8 +9710,8 @@ std::string VulkanHppGenerator::generateRAIIHandleConstructorCallArguments(
{ {
assert( !param.optional ); assert( !param.optional );
assert( singularParams.empty() || ( param.len == constructorIt->second.params[*singularParams.begin()].name ) ); assert( singularParams.empty() || ( param.len == constructorIt->second.params[*singularParams.begin()].name ) );
arguments += std::string paramName = generateRAIIHandleConstructorParamName( handle.first, handle.second.destructorIt );
"reinterpret_cast<" + handle.first + "*>( &m_" + startLowerCase( stripPrefix( handle.first, "Vk" ) ) + " )"; arguments += "reinterpret_cast<" + handle.first + "*>( &m_" + paramName + " )";
} }
else if ( nonConstPointerAsNullptr ) else if ( nonConstPointerAsNullptr )
{ {
@ -9725,8 +9727,7 @@ std::string VulkanHppGenerator::generateRAIIHandleConstructorCallArguments(
assert( param.optional ); assert( param.optional );
if ( allocatorIsMemberVariable ) if ( allocatorIsMemberVariable )
{ {
// VkAllocationCallbacks is stored as a member of the handle class ! arguments += "reinterpret_cast<const VkAllocationCallbacks *>( m_allocator )";
arguments += "m_allocator";
} }
else else
{ {
@ -9830,81 +9831,140 @@ std::string VulkanHppGenerator::generateRAIIHandleConstructorInitializationList(
bool takesOwnership ) const bool takesOwnership ) const
{ {
auto [parentType, parentName] = getParentTypeAndName( handle ); auto [parentType, parentName] = getParentTypeAndName( handle );
std::string handleName = startLowerCase( stripPrefix( handle.first, "Vk" ) ); std::string handleName = generateRAIIHandleConstructorParamName( handle.first, destructorIt );
std::string initializationList = "m_" + parentName + "( &" + parentName + " )";
if ( takesOwnership )
{
initializationList += ", m_" + handleName + "( " + handleName + " )";
}
std::string initializationList;
if ( destructorIt != m_commands.end() ) if ( destructorIt != m_commands.end() )
{ {
for ( auto destructorParam : destructorIt->second.params ) for ( auto destructorParam : destructorIt->second.params )
{ {
if ( ( destructorParam.type.type != ( "Vk" + parentType ) ) && ( destructorParam.type.type != handle.first ) ) if ( destructorParam.type.type == "Vk" + parentType )
{ {
if ( isHandleType( destructorParam.type.type ) ) initializationList += "m_" + parentName + "( *" + parentName + " ), ";
}
else if ( destructorParam.type.type == handle.first )
{
if ( takesOwnership )
{ {
assert( destructorParam.type.isValue() && destructorParam.arraySizes.empty() && destructorParam.len.empty() && initializationList += "m_" + handleName + "( " + handleName + " ), ";
!destructorParam.optional );
initializationList += ", m_" + startLowerCase( stripPrefix( destructorParam.type.type, "Vk" ) ) + "( ";
auto constructorParamIt = std::find_if( constructorIt->second.params.begin(),
constructorIt->second.params.end(),
[&destructorParam]( ParamData const & pd )
{ return pd.type.type == destructorParam.type.type; } );
if ( constructorParamIt != constructorIt->second.params.end() )
{
assert( constructorParamIt->type.isValue() && constructorParamIt->arraySizes.empty() &&
constructorParamIt->len.empty() && !constructorParamIt->optional );
initializationList += constructorParamIt->name;
}
else
{
#if !defined( NDEBUG )
bool found = false;
#endif
for ( auto constructorParam : constructorIt->second.params )
{
auto structureIt = m_structures.find( constructorParam.type.type );
if ( structureIt != m_structures.end() )
{
auto structureMemberIt = std::find_if( structureIt->second.members.begin(),
structureIt->second.members.end(),
[&destructorParam]( MemberData const & md )
{ return md.type.type == destructorParam.type.type; } );
if ( structureMemberIt != structureIt->second.members.end() )
{
assert( constructorParam.type.isConstPointer() && constructorParam.arraySizes.empty() &&
constructorParam.len.empty() && !constructorParam.optional );
initializationList +=
startLowerCase( stripPrefix( constructorParam.name, "p" ) ) + "." + structureMemberIt->name;
#if !defined( NDEBUG )
found = true;
#endif
break;
}
}
}
assert( found );
}
initializationList += " )";
} }
else if ( destructorParam.type.type == "VkAllocationCallbacks" ) }
else if ( destructorParam.type.type == "VkAllocationCallbacks" )
{
assert( destructorParam.type.isConstPointer() && destructorParam.arraySizes.empty() &&
destructorParam.len.empty() && destructorParam.optional );
initializationList +=
"m_allocator( static_cast<const VULKAN_HPP_NAMESPACE::AllocationCallbacks *>( allocator ) ), ";
}
else if ( isHandleType( destructorParam.type.type ) )
{
assert( destructorParam.type.isValue() && destructorParam.arraySizes.empty() && destructorParam.len.empty() &&
!destructorParam.optional );
initializationList += "m_" + destructorParam.name + "( ";
auto constructorParamIt = std::find_if( constructorIt->second.params.begin(),
constructorIt->second.params.end(),
[&destructorParam]( ParamData const & pd )
{ return pd.type.type == destructorParam.type.type; } );
if ( constructorParamIt != constructorIt->second.params.end() )
{ {
assert( destructorParam.type.isConstPointer() && destructorParam.arraySizes.empty() && assert( constructorParamIt->type.isValue() && constructorParamIt->arraySizes.empty() &&
destructorParam.len.empty() && destructorParam.optional ); constructorParamIt->len.empty() && !constructorParamIt->optional );
initializationList += if ( constructorParamIt->type.type == "Vk" + parentType )
", m_allocator( reinterpret_cast<const VkAllocationCallbacks *>( static_cast<const VULKAN_HPP_NAMESPACE::AllocationCallbacks *>( allocator ) ) )"; {
initializationList += "*";
}
initializationList += constructorParamIt->name;
} }
else else
{ {
// we can ignore all other parameters here ! #if !defined( NDEBUG )
bool found = false;
#endif
for ( auto constructorParam : constructorIt->second.params )
{
auto structureIt = m_structures.find( constructorParam.type.type );
if ( structureIt != m_structures.end() )
{
auto structureMemberIt = std::find_if( structureIt->second.members.begin(),
structureIt->second.members.end(),
[&destructorParam]( MemberData const & md )
{ return md.type.type == destructorParam.type.type; } );
if ( structureMemberIt != structureIt->second.members.end() )
{
assert( constructorParam.type.isConstPointer() && constructorParam.arraySizes.empty() &&
constructorParam.len.empty() && !constructorParam.optional );
initializationList +=
startLowerCase( stripPrefix( constructorParam.name, "p" ) ) + "." + structureMemberIt->name;
#if !defined( NDEBUG )
found = true;
#endif
break;
}
}
}
assert( found );
} }
initializationList += " ), ";
}
else
{
// we can ignore all other parameters here !
} }
} }
} }
return initializationList; else
{
if ( !handle.second.secondLevelCommands.empty() )
{
assert( !handle.second.constructorIts.empty() );
auto constructorCommandIt = m_commands.find( handle.second.constructorIts.front()->first );
assert( ( constructorCommandIt != m_commands.end() ) && ( 1 < constructorCommandIt->second.params.size() ) );
assert( std::next( constructorCommandIt->second.params.begin() )->type.type == "Vk" + parentType );
auto commandIt = m_commands.find( *handle.second.secondLevelCommands.begin() );
assert( ( commandIt != m_commands.end() ) && ( 1 < commandIt->second.params.size() ) );
assert( commandIt->second.params.front().type.type == constructorCommandIt->second.params.front().type.type );
assert( std::next( commandIt->second.params.begin() )->type.type == handle.first );
std::string grandParentType = stripPrefix( commandIt->second.params.front().type.type, "Vk" );
initializationList +=
"m_" + startLowerCase( grandParentType ) + "( " + parentName + ".get" + grandParentType + "() ), ";
}
if ( takesOwnership )
{
initializationList += "m_" + handleName + "( " + handleName + " ), ";
}
}
return initializationList.empty() ? initializationList
: initializationList.substr( 0, initializationList.size() - 2 );
}
std::string VulkanHppGenerator::generateRAIIHandleConstructorParamName(
std::string const & type, std::map<std::string, CommandData>::const_iterator destructorIt ) const
{
if ( destructorIt != m_commands.end() )
{
auto destructorParamIt =
std::find_if( destructorIt->second.params.begin(),
destructorIt->second.params.end(),
[&type]( ParamData const & destructorParam ) { return destructorParam.type.type == type; } );
if ( destructorParamIt != destructorIt->second.params.end() )
{
assert( std::find_if( std::next( destructorParamIt ),
destructorIt->second.params.end(),
[&type]( ParamData const & destructorParam )
{ return destructorParam.type.type == type; } ) == destructorIt->second.params.end() );
if ( !destructorParamIt->type.isValue() )
{
return startLowerCase( stripPrefix( stripPluralS( destructorParamIt->name ), "p" ) );
}
else
{
return destructorParamIt->name;
}
}
}
return startLowerCase( stripPrefix( type, "Vk" ) );
} }
std::string VulkanHppGenerator::generateRAIIHandleConstructorResult( std::string VulkanHppGenerator::generateRAIIHandleConstructorResult(
@ -9919,7 +9979,7 @@ std::string VulkanHppGenerator::generateRAIIHandleConstructorResult(
std::string dispatcherInitializer, dispatcherInit; std::string dispatcherInitializer, dispatcherInit;
if ( ( handle.first != "VkInstance" ) && ( handle.first != "VkDevice" ) ) if ( ( handle.first != "VkInstance" ) && ( handle.first != "VkDevice" ) )
{ {
dispatcherInitializer = ", m_dispatcher( " + getDispatcher + " )"; dispatcherInitializer = "m_dispatcher( " + getDispatcher + " )";
} }
else else
{ {
@ -9937,6 +9997,10 @@ std::string VulkanHppGenerator::generateRAIIHandleConstructorResult(
std::string initializationList = std::string initializationList =
generateRAIIHandleConstructorInitializationList( handle, constructorIt, handle.second.destructorIt, false ); generateRAIIHandleConstructorInitializationList( handle, constructorIt, handle.second.destructorIt, false );
if ( !initializationList.empty() && !dispatcherInitializer.empty() )
{
initializationList += ", ";
}
const std::string constructorTemplate = const std::string constructorTemplate =
R"( R"(
@ -9973,12 +10037,6 @@ std::string VulkanHppGenerator::generateRAIIHandleConstructorTakeOwnership(
auto [parentType, parentName] = getParentTypeAndName( handle ); auto [parentType, parentName] = getParentTypeAndName( handle );
std::string dispatcherInitializer;
if ( ( handle.first != "VkInstance" ) && ( handle.first != "VkDevice" ) )
{
dispatcherInitializer = ", m_dispatcher( " + parentName + ".getDispatcher() )";
}
std::string constructorArguments = std::string constructorArguments =
generateRAIIHandleConstructorArguments( handle, handle.second.destructorIt, false, true ); generateRAIIHandleConstructorArguments( handle, handle.second.destructorIt, false, true );
std::string initializationList = generateRAIIHandleConstructorInitializationList( std::string initializationList = generateRAIIHandleConstructorInitializationList(
@ -9997,6 +10055,16 @@ std::string VulkanHppGenerator::generateRAIIHandleConstructorTakeOwnership(
initializationList += ", m_constructorSuccessCode( successCode )"; initializationList += ", m_constructorSuccessCode( successCode )";
} }
std::string dispatcherInitializer;
if ( ( handle.first != "VkInstance" ) && ( handle.first != "VkDevice" ) )
{
dispatcherInitializer = "m_dispatcher( " + parentName + ".getDispatcher() )";
}
if ( !initializationList.empty() && !dispatcherInitializer.empty() )
{
initializationList += ", ";
}
std::string dispatcherInit; std::string dispatcherInit;
if ( ( handle.first == "VkDevice" ) || ( handle.first == "VkInstance" ) ) if ( ( handle.first == "VkDevice" ) || ( handle.first == "VkInstance" ) )
{ {
@ -10160,12 +10228,15 @@ std::string VulkanHppGenerator::generateRAIIHandleConstructorVoid(
std::string constructorArguments = generateRAIIHandleConstructorArguments( handle, constructorIt, false, false ); std::string constructorArguments = generateRAIIHandleConstructorArguments( handle, constructorIt, false, false );
std::string initializationList = std::string initializationList =
generateRAIIHandleConstructorInitializationList( handle, constructorIt, handle.second.destructorIt, false ); generateRAIIHandleConstructorInitializationList( handle, constructorIt, handle.second.destructorIt, false );
assert( !initializationList.empty() ); if ( !initializationList.empty() )
{
initializationList += ", ";
}
const std::string constructorTemplate = const std::string constructorTemplate =
R"( R"(
${enter} ${handleType}( ${constructorArguments} ) ${enter} ${handleType}( ${constructorArguments} )
: ${initializationList}, m_dispatcher( ${firstArgument}.getDispatcher() ) : ${initializationList}m_dispatcher( ${firstArgument}.getDispatcher() )
{ {
getDispatcher()->${constructorCall}( ${callArguments} ); getDispatcher()->${constructorCall}( ${callArguments} );
} }
@ -10190,7 +10261,8 @@ std::string VulkanHppGenerator::generateRAIIHandleContext( std::pair<std::string
{ {
public: public:
Context() Context()
: m_dispatcher( new VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::ContextDispatcher( m_dynamicLoader.getProcAddress<PFN_vkGetInstanceProcAddr>( "vkGetInstanceProcAddr" ) ) ) : m_dispatcher( new VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::ContextDispatcher(
m_dynamicLoader.getProcAddress<PFN_vkGetInstanceProcAddr>( "vkGetInstanceProcAddr" ) ) )
{} {}
~Context() = default; ~Context() = default;
@ -10232,8 +10304,7 @@ ${memberFunctionDeclarations}
} }
std::pair<std::string, std::string> std::pair<std::string, std::string>
VulkanHppGenerator::generateRAIIHandleDestructor( std::string const & parentType, VulkanHppGenerator::generateRAIIHandleDestructor( std::string const & handleType,
std::string const & handleType,
std::map<std::string, CommandData>::const_iterator destructorIt, std::map<std::string, CommandData>::const_iterator destructorIt,
std::string const & enter ) const std::string const & enter ) const
{ {
@ -10245,8 +10316,7 @@ std::pair<std::string, std::string>
destructorLeave.clear(); destructorLeave.clear();
} }
std::string destructorCall = std::string destructorCall =
destructorIt->first + "( " + destructorIt->first + "( " + generateRAIIHandleDestructorCallArguments( handleType, destructorIt ) + " )";
generateRAIIHandleDestructorCallArguments( parentType, handleType, destructorIt->second.params ) + " )";
const std::string destructorTemplate = R"( const std::string destructorTemplate = R"(
${enter}~${handleType}() ${enter}~${handleType}()
@ -10258,55 +10328,61 @@ ${enter}~${handleType}()
} }
${leave})"; ${leave})";
std::string destructor = replaceWithMap( destructorTemplate, std::string destructor =
{ { "destructorCall", destructorCall }, replaceWithMap( destructorTemplate,
{ "enter", destructorEnter }, { { "destructorCall", destructorCall },
{ "handleName", startLowerCase( stripPrefix( handleType, "Vk" ) ) }, { "enter", destructorEnter },
{ "handleType", stripPrefix( handleType, "Vk" ) }, { "handleName", generateRAIIHandleConstructorParamName( handleType, destructorIt ) },
{ "leave", destructorLeave } } ); { "handleType", stripPrefix( handleType, "Vk" ) },
{ "leave", destructorLeave } } );
return std::make_pair( destructor, destructorCall ); return std::make_pair( destructor, destructorCall );
} }
std::string VulkanHppGenerator::generateRAIIHandleDestructorCallArguments( std::string const & parentType, std::string VulkanHppGenerator::generateRAIIHandleDestructorCallArguments(
std::string const & handleType, std::string const & handleType, std::map<std::string, CommandData>::const_iterator destructorIt ) const
std::vector<ParamData> const & params ) const
{ {
std::string arguments; std::string arguments;
bool encounteredArgument = false; bool encounteredArgument = false;
for ( auto param : params ) for ( auto param : destructorIt->second.params )
{ {
if ( encounteredArgument ) if ( encounteredArgument )
{ {
arguments += ", "; arguments += ", ";
} }
if ( param.type.type == "VkAllocationCallbacks" ) if ( param.type.type == handleType )
{ {
// VkAllocationCallbacks is stored as a member of the handle class std::string handleName = param.name;
arguments += "m_allocator"; if ( param.type.isValue() )
{
arguments += "static_cast<" + handleType + ">( m_" + handleName + " )";
}
else
{
arguments += "reinterpret_cast<" + handleType + " const *>( &m_" +
stripPluralS( startLowerCase( stripPrefix( handleName, "p" ) ) ) + " )";
}
}
else if ( param.type.type == "VkAllocationCallbacks" )
{
// vk::AllocationCallbacks is stored as a member of the handle class
arguments += "reinterpret_cast<const VkAllocationCallbacks *>( m_allocator )";
} }
else if ( isHandleType( param.type.type ) ) else if ( isHandleType( param.type.type ) )
{ {
assert( param.arraySizes.empty() ); assert( param.arraySizes.empty() );
std::string argument = "m_" + startLowerCase( stripPrefix( param.type.type, "Vk" ) ); std::string argument = "m_" + param.name;
if ( param.type.isValue() ) if ( param.type.isValue() )
{ {
if ( param.type.type == parentType ) arguments += "static_cast<" + param.type.type + ">( " + argument + " )";
{
argument = "static_cast<" + param.type.type + ">( **" + argument + " )";
}
else if ( param.type.type == handleType )
{
argument = "static_cast<" + param.type.type + ">( " + argument + " )";
}
arguments += argument;
} }
else else
{ {
assert( param.type.isConstPointer() ); assert( param.type.isConstPointer() );
assert( !param.len.empty() && assert( !param.len.empty() && ( std::find_if( destructorIt->second.params.begin(),
( std::find_if( params.begin(), destructorIt->second.params.end(),
params.end(), [&param]( ParamData const & pd ) {
[&param]( ParamData const & pd ) { return pd.name == param.len; } ) != params.end() ) ); return pd.name == param.len;
} ) != destructorIt->second.params.end() ) );
arguments += "reinterpret_cast<" + param.type.type + " const *>( &" + argument + " )"; arguments += "reinterpret_cast<" + param.type.type + " const *>( &" + argument + " )";
} }
} }
@ -10314,9 +10390,10 @@ std::string VulkanHppGenerator::generateRAIIHandleDestructorCallArguments( std::
{ {
assert( ( param.type.type == "uint32_t" ) && param.type.isValue() && param.arraySizes.empty() && assert( ( param.type.type == "uint32_t" ) && param.type.isValue() && param.arraySizes.empty() &&
param.len.empty() && !param.optional ); param.len.empty() && !param.optional );
assert( std::find_if( params.begin(), assert( std::find_if( destructorIt->second.params.begin(),
params.end(), destructorIt->second.params.end(),
[&param]( ParamData const & pd ) { return pd.len == param.name; } ) != params.end() ); [&param]( ParamData const & pd )
{ return pd.len == param.name; } ) != destructorIt->second.params.end() );
arguments += "1"; arguments += "1";
} }
encounteredArgument = true; encounteredArgument = true;
@ -10342,25 +10419,10 @@ std::tuple<std::string, std::string, std::string, std::string>
auto [parentType, parentName] = getParentTypeAndName( handle ); auto [parentType, parentName] = getParentTypeAndName( handle );
std::string moveConstructorInitializerList = "m_" + parentName +
"( VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::exchange( rhs.m_" +
parentName + ", nullptr ) ), ";
std::string moveAssignmentInstructions = " m_" + parentName +
" = VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::exchange( rhs.m_" +
parentName + ", nullptr );\n";
std::string memberVariables =
" VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::" + parentType + " const * m_" + parentName + " = nullptr;";
std::string handleType = stripPrefix( handle.first, "Vk" ); std::string handleType = stripPrefix( handle.first, "Vk" );
std::string handleName = startLowerCase( handleType ); std::string handleName = generateRAIIHandleConstructorParamName( handle.first, handle.second.destructorIt );
moveConstructorInitializerList +=
"m_" + handleName + "( VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::exchange( rhs.m_" + handleName + ", {} ) )";
moveAssignmentInstructions += " m_" + handleName +
" = VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::exchange( rhs.m_" + handleName +
", {} );";
memberVariables += " VULKAN_HPP_NAMESPACE::" + handleType + " m_" + handleName + ";";
std::string moveConstructorInitializerList, moveAssignmentInstructions, memberVariables;
if ( handle.second.destructorIt != m_commands.end() ) if ( handle.second.destructorIt != m_commands.end() )
{ {
moveAssignmentInstructions = " if ( m_" + handleName + moveAssignmentInstructions = " if ( m_" + handleName +
@ -10369,42 +10431,79 @@ std::tuple<std::string, std::string, std::string, std::string>
" getDispatcher()->" + " getDispatcher()->" +
destructorCall + destructorCall +
";\n" ";\n"
" }\n" " }";
" " +
moveAssignmentInstructions;
for ( auto const & destructorParam : handle.second.destructorIt->second.params ) for ( auto const & destructorParam : handle.second.destructorIt->second.params )
{ {
if ( ( destructorParam.type.type != "Vk" + parentType ) && ( destructorParam.type.type != handle.first ) && if ( destructorParam.type.type == "Vk" + parentType )
( std::find_if( handle.second.destructorIt->second.params.begin(), {
handle.second.destructorIt->second.params.end(), moveConstructorInitializerList = "m_" + parentName +
[&destructorParam]( ParamData const & pd ) { return pd.len == destructorParam.name; } ) == "( VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::exchange( rhs.m_" +
handle.second.destructorIt->second.params.end() ) ) parentName + ", {} ) ), ";
moveAssignmentInstructions = "\n m_" + parentName +
" = VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::exchange( rhs.m_" +
parentName + ", {} );";
memberVariables = "\n VULKAN_HPP_NAMESPACE::" + parentType + " m_" + parentName + " = {};";
}
else if ( destructorParam.type.type == handle.first )
{
moveConstructorInitializerList += "m_" + handleName +
"( VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::exchange( rhs.m_" +
handleName + ", {} ) ), ";
moveAssignmentInstructions += "\n m_" + handleName +
" = VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::exchange( rhs.m_" +
handleName + ", {} );";
memberVariables += "\n VULKAN_HPP_NAMESPACE::" + handleType + " m_" + handleName + " = {};";
}
else if ( std::find_if( handle.second.destructorIt->second.params.begin(),
handle.second.destructorIt->second.params.end(),
[&destructorParam]( ParamData const & pd ) { return pd.len == destructorParam.name; } ) ==
handle.second.destructorIt->second.params.end() )
{ {
std::string name = destructorParam.name; std::string name = destructorParam.name;
if ( !destructorParam.type.isValue() ) if ( !destructorParam.type.isValue() )
{ {
name = startLowerCase( stripPrefix( name, "p" ) ); name = startLowerCase( stripPrefix( name, "p" ) );
} }
memberVariables += "\n " + destructorParam.type.prefix + " " + destructorParam.type.type + " " + memberVariables += "\n " + destructorParam.type.compose( "VULKAN_HPP_NAMESPACE" ) + " m_" + name + " = " +
destructorParam.type.postfix + " m_" + name + ( destructorParam.type.postfix.empty() ? "{}" : "nullptr" ) + ";";
( destructorParam.type.postfix.empty() ? "" : " = nullptr" ) + ";"; moveConstructorInitializerList +=
moveConstructorInitializerList += ", m_" + name + "( rhs.m_" + name + " )"; "m_" + name + "( VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::exchange( rhs.m_" + name + ", {} ) ), ";
moveAssignmentInstructions += "\n m_" + name + " = rhs.m_" + name + ";"; moveAssignmentInstructions += "\n m_" + name +
" = VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::exchange( rhs.m_" + name +
", {} );";
} }
} }
} }
#if !defined( NDEBUG ) else
else if ( !handle.second.secondLevelCommands.empty() )
{ {
assert( !handle.second.constructorIts.empty() ); if ( !handle.second.secondLevelCommands.empty() )
assert( !handle.second.constructorIts.front()->second.params.empty() ); {
auto const & frontType = handle.second.constructorIts.front()->second.params.front().type.type; assert( !handle.second.constructorIts.empty() );
assert( isHandleType( frontType ) ); assert( !handle.second.constructorIts.front()->second.params.empty() );
auto handleIt = m_handles.find( "Vk" + parentType ); auto const & frontType = handle.second.constructorIts.front()->second.params.front().type.type;
assert( handleIt != m_handles.end() ); assert( isHandleType( frontType ) );
assert( handleIt->second.parent == frontType ); auto handleIt = m_handles.find( "Vk" + parentType );
assert( handleIt != m_handles.end() );
assert( handleIt->second.parent == frontType );
std::string frontName = handle.second.constructorIts.front()->second.params.front().name;
moveConstructorInitializerList = "m_" + frontName +
"( VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::exchange( rhs.m_" +
frontName + ", {} ) ), ";
moveAssignmentInstructions = "\n m_" + frontName +
" = VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::exchange( rhs.m_" + frontName +
", {} );";
memberVariables = "\n VULKAN_HPP_NAMESPACE::" + stripPrefix( frontType, "Vk" ) + " m_" + frontName + " = {};";
}
moveConstructorInitializerList += "m_" + handleName +
"( VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::exchange( rhs.m_" +
handleName + ", {} ) ), ";
moveAssignmentInstructions += "\n m_" + handleName +
" = VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::exchange( rhs.m_" + handleName +
", {} );";
memberVariables += "\n VULKAN_HPP_NAMESPACE::" + handleType + " m_" + handleName + " = {};";
} }
#endif
if ( multiSuccessCodeContructor ) if ( multiSuccessCodeContructor )
{ {
memberVariables += "\n VULKAN_HPP_NAMESPACE::Result m_constructorSuccessCode;"; memberVariables += "\n VULKAN_HPP_NAMESPACE::Result m_constructorSuccessCode;";
@ -10433,13 +10532,13 @@ std::tuple<std::string, std::string, std::string, std::string>
if ( ( handle.first == "VkInstance" ) || ( handle.first == "VkDevice" ) ) if ( ( handle.first == "VkInstance" ) || ( handle.first == "VkDevice" ) )
{ {
moveConstructorInitializerList += ", m_dispatcher( rhs.m_dispatcher.release() )"; moveConstructorInitializerList += "m_dispatcher( rhs.m_dispatcher.release() )";
moveAssignmentInstructions += "\n m_dispatcher.reset( rhs.m_dispatcher.release() );"; moveAssignmentInstructions += "\n m_dispatcher.reset( rhs.m_dispatcher.release() );";
} }
else else
{ {
moveConstructorInitializerList += moveConstructorInitializerList +=
", m_dispatcher( VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::exchange( rhs.m_dispatcher, nullptr ) )"; "m_dispatcher( VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::exchange( rhs.m_dispatcher, nullptr ) )";
moveAssignmentInstructions += moveAssignmentInstructions +=
"\n m_dispatcher = VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::exchange( rhs.m_dispatcher, nullptr );"; "\n m_dispatcher = VULKAN_HPP_NAMESPACE::VULKAN_HPP_RAII_NAMESPACE::exchange( rhs.m_dispatcher, nullptr );";
} }

View File

@ -1104,10 +1104,13 @@ private:
std::string const & enter, std::string const & enter,
std::string const & leave ) const; std::string const & leave ) const;
std::string std::string
generateRAIIHandleConstructorInitializationList( std::pair<std::string, HandleData> const & handle, generateRAIIHandleConstructorInitializationList( std::pair<std::string, HandleData> const & handle,
std::map<std::string, CommandData>::const_iterator constructorIt, std::map<std::string, CommandData>::const_iterator constructorIt,
std::map<std::string, CommandData>::const_iterator destructorIt, std::map<std::string, CommandData>::const_iterator destructorIt,
bool takesOwnership ) const; bool takesOwnership ) const;
std::string
generateRAIIHandleConstructorParamName( std::string const & type,
std::map<std::string, CommandData>::const_iterator destructorIt ) const;
std::string generateRAIIHandleConstructorResult( std::pair<std::string, HandleData> const & handle, std::string generateRAIIHandleConstructorResult( std::pair<std::string, HandleData> const & handle,
std::map<std::string, CommandData>::const_iterator constructorIt, std::map<std::string, CommandData>::const_iterator constructorIt,
std::string const & enter, std::string const & enter,
@ -1131,13 +1134,12 @@ private:
std::string generateRAIIHandleContext( std::pair<std::string, HandleData> const & handle, std::string generateRAIIHandleContext( std::pair<std::string, HandleData> const & handle,
std::set<std::string> const & specialFunctions ) const; std::set<std::string> const & specialFunctions ) const;
std::pair<std::string, std::string> std::pair<std::string, std::string>
generateRAIIHandleDestructor( std::string const & parentType, generateRAIIHandleDestructor( std::string const & handleType,
std::string const & handleType,
std::map<std::string, CommandData>::const_iterator destructorIt, std::map<std::string, CommandData>::const_iterator destructorIt,
std::string const & enter ) const; std::string const & enter ) const;
std::string generateRAIIHandleDestructorCallArguments( std::string const & parentType, std::string
std::string const & handleType, generateRAIIHandleDestructorCallArguments( std::string const & handleType,
std::vector<ParamData> const & params ) const; std::map<std::string, CommandData>::const_iterator destructorIt ) const;
std::tuple<std::string, std::string, std::string, std::string> std::tuple<std::string, std::string, std::string, std::string>
generateRAIIHandleDetails( std::pair<std::string, HandleData> const & handle, generateRAIIHandleDetails( std::pair<std::string, HandleData> const & handle,
std::string const & destructorCall ) const; std::string const & destructorCall ) const;

File diff suppressed because it is too large Load Diff