From 6f067eecac341418847ad35e3050f9c2ef03762d Mon Sep 17 00:00:00 2001 From: asuessenbach Date: Wed, 28 Jul 2021 09:13:25 +0200 Subject: [PATCH] Filter constructors for unions with multiple elements of the same type. --- VulkanHppGenerator.cpp | 81 ++++++++++++++++++++++++++++-------------- VulkanHppGenerator.hpp | 8 +++++ 2 files changed, 63 insertions(+), 26 deletions(-) diff --git a/VulkanHppGenerator.cpp b/VulkanHppGenerator.cpp index 62bccd6..3d76cdc 100644 --- a/VulkanHppGenerator.cpp +++ b/VulkanHppGenerator.cpp @@ -6959,7 +6959,8 @@ void VulkanHppGenerator::checkEnumCorrectness( std::vector const & check( !requireTypeIt->second.referencedIn.empty(), bitmaskIt->second.xmlLine, "bitmask <" + bitmaskIt->first + ">, listed for <" + typeIt->second.referencedIn + - ">, requires <" + bitmaskIt->second.requirements + "> which is not listed for any feature or extension!" ); + ">, requires <" + bitmaskIt->second.requirements + + "> which is not listed for any feature or extension!" ); } } else @@ -11063,6 +11064,22 @@ std::string VulkanHppGenerator::generateLenInitializer( return initializer; } +std::string VulkanHppGenerator::generateName( TypeInfo const & typeInfo ) const +{ + std::string name = stripPrefix( typeInfo.type, "Vk" ); + assert( typeInfo.prefix.find( '*' ) == std::string::npos ); + if ( typeInfo.postfix.find( '*' ) != std::string::npos ) + { + assert( typeInfo.postfix.find_first_of( '*' ) == typeInfo.postfix.find_last_of( '*' ) ); + name = "p" + name; + } + else + { + name = startLowerCase( name ); + } + return name; +} + std::pair VulkanHppGenerator::generateProtection( std::string const & referencedIn, std::string const & protect ) const { @@ -12191,39 +12208,51 @@ std::string VulkanHppGenerator::generateUnion( std::pair listedTypes; // create just one constructor per different type ! + for (auto memberIt = structure.second.members.begin(); memberIt != structure.second.members.end(); ++memberIt ) { - // VkBool32 is aliased to uint32_t. Don't create a VkBool32 constructor if the union also contains a uint32_t - // constructor. - auto compareBool32Alias = []( MemberData const & member ) + if ( listedTypes.insert( memberIt->type ).second ) { - return member.type.type == std::string( "uint32_t" ); - }; - if ( member.type.type == "VkBool32" ) - { - if ( std::find_if( structure.second.members.begin(), structure.second.members.end(), compareBool32Alias ) != - structure.second.members.end() ) + // VkBool32 is aliased to uint32_t. Don't create a VkBool32 constructor if the union also contains a uint32_t + // constructor. + if ( memberIt->type.type == "VkBool32" ) { - continue; + if ( std::find_if( structure.second.members.begin(), + structure.second.members.end(), + []( MemberData const & member ) { + return member.type.type == std::string( "uint32_t" ); + } ) != + structure.second.members.end() ) + { + continue; + } } - } - static const std::string constructorTemplate = R"( - ${unionName}( ${memberType} ${memberName}_${defaultAssignment} ) - : ${memberName}( ${memberName}_ ) + bool multipleType = ( std::find_if( std::next( memberIt ), + structure.second.members.end(), + [memberIt]( MemberData const & member ) { + return member.type == memberIt->type; + } ) != structure.second.members.end() ); + + static const std::string constructorTemplate = R"( + ${unionName}( ${memberType} ${argumentName}_${defaultAssignment} ) + : ${memberName}( ${argumentName}_ ) {} )"; - std::string memberType = ( member.arraySizes.empty() ) - ? member.type.compose() - : ( "const " + generateStandardArray( member.type.compose(), member.arraySizes ) + "&" ); - str += replaceWithMap( constructorTemplate, - { { "defaultAssignment", firstMember ? " = {}" : "" }, - { "memberName", member.name }, - { "memberType", memberType }, - { "unionName", stripPrefix( structure.first, "Vk" ) } } ); - firstMember = false; + std::string memberType = + ( memberIt->arraySizes.empty() ) + ? memberIt->type.compose() + : ( "const " + generateStandardArray( memberIt->type.compose(), memberIt->arraySizes ) + "&" ); + str += replaceWithMap( constructorTemplate, + { { "argumentName", multipleType ? generateName( memberIt->type ) : memberIt->name }, + { "defaultAssignment", firstMember ? " = {}" : "" }, + { "memberName", memberIt->name }, + { "memberType", memberType }, + { "unionName", stripPrefix( structure.first, "Vk" ) } } ); + firstMember = false; + } } str += "#endif /*VULKAN_HPP_NO_UNION_CONSTRUCTORS*/\n"; diff --git a/VulkanHppGenerator.hpp b/VulkanHppGenerator.hpp index cd22a62..0a71069 100644 --- a/VulkanHppGenerator.hpp +++ b/VulkanHppGenerator.hpp @@ -56,6 +56,13 @@ private: return ( prefix == rhs.prefix ) && ( type == rhs.type ) && ( postfix == rhs.postfix ); } + bool operator<( TypeInfo const & rhs ) const + { + return ( prefix < rhs.prefix ) || + ( ( prefix == rhs.prefix ) && + ( ( type < rhs.type ) || ( ( type == rhs.type ) && ( postfix < rhs.postfix ) ) ) ); + } + bool isConstPointer() const { return ( prefix.find( "const" ) != std::string::npos ) && ( postfix.find( '*' ) != std::string::npos ); @@ -1058,6 +1065,7 @@ private: std::map::const_iterator, std::vector::const_iterator>>::const_iterator litit, bool mutualExclusiveLens ) const; + std::string generateName( TypeInfo const & typeInfo ) const; std::pair generateProtection( std::string const & referencedIn, std::string const & protect ) const; std::pair generateProtection( std::string const & type, bool isAliased ) const;