Introduce usage of constexpr for constructors of structs. (#385)

This commit is contained in:
Andreas Süßenbach 2019-09-23 15:57:48 +02:00 committed by Markus Tavenrath
parent 720c8aec77
commit e2268eadb6
4 changed files with 2273 additions and 2210 deletions

View File

@ -47,3 +47,4 @@ script:
- echo "}" >> /tmp/test.cpp - echo "}" >> /tmp/test.cpp
- cat /tmp/test.cpp - cat /tmp/test.cpp
- ${CXX} -std=c++11 -Wall -Ivulkan -IVulkan-Headers/include -c /tmp/test.cpp - ${CXX} -std=c++11 -Wall -Ivulkan -IVulkan-Headers/include -c /tmp/test.cpp
- ${CXX} -std=c++14 -Wall -Ivulkan -IVulkan-Headers/include -c /tmp/test.cpp

View File

@ -2340,8 +2340,11 @@ void VulkanHppGenerator::appendStructCompareOperators(std::string & str, std::pa
std::string VulkanHppGenerator::appendStructConstructor(std::pair<std::string, StructureData> const& structData, std::string const& prefix, bool withLayoutStructure) const std::string VulkanHppGenerator::appendStructConstructor(std::pair<std::string, StructureData> const& structData, std::string const& prefix, bool withLayoutStructure) const
{ {
// the constructor with all the elements as arguments, with defaults // the constructor with all the elements as arguments, with defaults
std::string ctorOpening = prefix + stripPrefix(structData.first, "Vk") + "( "; // returnedOnly structs and structs with a union (and VkBaseInStructure and VkBaseOutStructure) can't be a constexpr!
std::string indentation(ctorOpening.size(), ' '); bool isConstExpression = !structData.second.returnedOnly && !containsUnion(structData.first) && (structData.first != "VkBaseInStructure") && (structData.first != "VkBaseOutStructure");
std::string constexprString = isConstExpression ? (std::string("VULKAN_HPP_CONSTEXPR") + (containsArray(structData.first) ? "_14 " : " ")) : "";
std::string ctorOpening = prefix + constexprString + stripPrefix(structData.first, "Vk");
std::string indentation(ctorOpening.size() + 2, ' ');
std::string arguments, initializers, copyOps; std::string arguments, initializers, copyOps;
bool listedArgument = false; bool listedArgument = false;
@ -2375,19 +2378,17 @@ std::string VulkanHppGenerator::appendStructConstructor(std::pair<std::string, S
else else
{ {
// here we can handle the arrays, copying over from argument (with trailing '_') to member // here we can handle the arrays, copying over from argument (with trailing '_') to member
// size is arraySize times sizeof type initializers += prefix + " " + (firstArgument ? ":" : ",") + " " + member.name + "{}\n"; // need to initialize the array
copyOps += "\n" firstArgument = false;
+ prefix + " memcpy( &" + member.name + ", " + member.name + "_.data(), " + member.arraySize + " * sizeof( " + member.type.compose() + " ) );";
std::string type = (member.type.type.substr(0, 2) == "Vk") ? ("vk::" + stripPrefix(member.type.type, "Vk")) : member.type.type;
copyOps += prefix + " vk::ConstExpressionArrayCopy<" + type + "," + member.arraySize + "," + member.arraySize + ">::copy( " + member.name + ", " + member.name + "_ );\n";
} }
} }
} }
if (!copyOps.empty())
{
copyOps += "\n" + prefix;
}
} }
std::string structConstructor = prefix + stripPrefix(structData.first, "Vk") + (arguments.empty() ? "()" : std::string("( " + arguments + " )")) + "\n"; std::string structConstructor = ctorOpening + (arguments.empty() ? "()" : std::string("( " + arguments + " )")) + "\n";
if (withLayoutStructure) if (withLayoutStructure)
{ {
structConstructor += prefix + " : layout::" + stripPrefix(structData.first, "Vk") + (initializers.empty() ? "()" : std::string("( " + initializers + " )")) + "\n" + prefix + "{}\n"; structConstructor += prefix + " : layout::" + stripPrefix(structData.first, "Vk") + (initializers.empty() ? "()" : std::string("( " + initializers + " )")) + "\n" + prefix + "{}\n";
@ -2401,7 +2402,7 @@ std::string VulkanHppGenerator::appendStructConstructor(std::pair<std::string, S
} }
else else
{ {
structConstructor += prefix + "{\n" + copyOps + "\n" + prefix + "}\n"; structConstructor += prefix + "{\n" + copyOps + prefix + "}\n";
} }
if (!structData.second.subStruct.empty()) if (!structData.second.subStruct.empty())
@ -2854,6 +2855,21 @@ void VulkanHppGenerator::EnumData::addEnumValue(std::string const &valueName, bo
} }
} }
bool VulkanHppGenerator::containsArray(std::string const& type) const
{
// a simple recursive check if a type is or contains an array
auto structureIt = m_structures.find(type);
bool found = false;
if (structureIt != m_structures.end())
{
for (auto memberIt = structureIt->second.members.begin(); memberIt != structureIt->second.members.end() && !found; ++memberIt)
{
found = !memberIt->arraySize.empty();
}
}
return found;
}
bool VulkanHppGenerator::containsUnion(std::string const& type) const bool VulkanHppGenerator::containsUnion(std::string const& type) const
{ {
// a simple recursive check if a type is or contains a union // a simple recursive check if a type is or contains a union
@ -4290,23 +4306,19 @@ int main( int argc, char **argv )
public: public:
VULKAN_HPP_CONSTEXPR Flags() VULKAN_HPP_CONSTEXPR Flags()
: m_mask(0) : m_mask(0)
{ {}
}
Flags(BitType bit) VULKAN_HPP_CONSTEXPR Flags(BitType bit)
: m_mask(static_cast<MaskType>(bit)) : m_mask(static_cast<MaskType>(bit))
{ {}
}
Flags(Flags<BitType> const& rhs) VULKAN_HPP_CONSTEXPR Flags(Flags<BitType> const& rhs)
: m_mask(rhs.m_mask) : m_mask(rhs.m_mask)
{ {}
}
explicit Flags(MaskType flags) VULKAN_HPP_CONSTEXPR explicit Flags(MaskType flags)
: m_mask(flags) : m_mask(flags)
{ {}
}
Flags<BitType> & operator=(Flags<BitType> const& rhs) Flags<BitType> & operator=(Flags<BitType> const& rhs)
{ {
@ -4782,6 +4794,26 @@ int main( int argc, char **argv )
#endif #endif
)"; )";
static const std::string constExpressionArrayCopy = R"(
template <typename T, size_t N, size_t I>
class ConstExpressionArrayCopy
{
public:
VULKAN_HPP_CONSTEXPR static void copy(T dst[N], std::array<T,N> const& src)
{
dst[I] = src[I];
ConstExpressionArrayCopy<T, N, I - 1>::copy(dst, src);
}
};
template <typename T, size_t N>
class ConstExpressionArrayCopy<T, N, 0>
{
public:
VULKAN_HPP_CONSTEXPR static void copy(T /*dst*/[N], std::array<T,N> const& /*src*/) {}
};
)";
static const std::string defines = R"( static const std::string defines = R"(
// <tuple> includes <sys/sysmacros.h> through some other header // <tuple> includes <sys/sysmacros.h> through some other header
// this results in major(x) being resolved to gnu_dev_major(x) // this results in major(x) being resolved to gnu_dev_major(x)
@ -4838,12 +4870,18 @@ int main( int argc, char **argv )
# define VULKAN_HPP_TYPESAFE_EXPLICIT explicit # define VULKAN_HPP_TYPESAFE_EXPLICIT explicit
#endif #endif
#if defined(_MSC_VER) && (_MSC_VER <= 1800) #if defined(__cpp_constexpr)
# define VULKAN_HPP_CONSTEXPR
# define VULKAN_HPP_CONST_OR_CONSTEXPR const
#else
# define VULKAN_HPP_CONSTEXPR constexpr # define VULKAN_HPP_CONSTEXPR constexpr
# if __cpp_constexpr >= 201304
# define VULKAN_HPP_CONSTEXPR_14 constexpr
# else
# define VULKAN_HPP_CONSTEXPR_14
# endif
# define VULKAN_HPP_CONST_OR_CONSTEXPR constexpr # define VULKAN_HPP_CONST_OR_CONSTEXPR constexpr
#else
# define VULKAN_HPP_CONSTEXPR
# define VULKAN_HPP_CONSTEXPR_14
# define VULKAN_HPP_CONST_OR_CONSTEXPR const
#endif #endif
#if !defined(VULKAN_HPP_NAMESPACE) #if !defined(VULKAN_HPP_NAMESPACE)
@ -5178,6 +5216,7 @@ namespace std
str += classObjectDestroy str += classObjectDestroy
+ classObjectFree + classObjectFree
+ classPoolFree + classPoolFree
+ constExpressionArrayCopy
+ "\n"; + "\n";
generator.appendBaseTypes(str); generator.appendBaseTypes(str);
generator.appendEnums(str); generator.appendEnums(str);

View File

@ -202,6 +202,7 @@ class VulkanHppGenerator
void appendStructure(std::string & str, std::pair<std::string, StructureData> const& structure) const; void appendStructure(std::string & str, std::pair<std::string, StructureData> const& structure) const;
void appendUnion(std::string & str, std::pair<std::string, StructureData> const& structure) const; void appendUnion(std::string & str, std::pair<std::string, StructureData> const& structure) const;
void appendUniqueTypes(std::string &str, std::string const& parentType, std::set<std::string> const& childrenTypes) const; void appendUniqueTypes(std::string &str, std::string const& parentType, std::set<std::string> const& childrenTypes) const;
bool containsArray(std::string const& type) const;
bool containsUnion(std::string const& type) const; bool containsUnion(std::string const& type) const;
std::string defaultValue(std::string const& type) const; std::string defaultValue(std::string const& type) const;
std::string determineEnhancedReturnType(CommandData const& commandData, size_t returnParamIndex, std::map<size_t, size_t> const& vectorParamIndices, bool twoStep, bool isStructureChain) const; std::string determineEnhancedReturnType(CommandData const& commandData, size_t returnParamIndex, std::map<size_t, size_t> const& vectorParamIndices, bool twoStep, bool isStructureChain) const;

File diff suppressed because it is too large Load Diff