Get the to_string function of an enum inside of the corresponding platform protection (#311)

This commit is contained in:
Andreas Süßenbach 2019-03-21 11:53:30 +01:00 committed by Markus Tavenrath
parent fe614bd960
commit 7905145361
2 changed files with 74 additions and 58 deletions

View File

@ -60,7 +60,6 @@ std::string trimEnd(std::string const& input);
void writeArgumentCount(std::ostream & os, size_t vectorIndex, std::string const& vectorName, std::string const& counterName, size_t returnParamIndex, size_t templateParamIndex, bool twoStep, bool singular); void writeArgumentCount(std::ostream & os, size_t vectorIndex, std::string const& vectorName, std::string const& counterName, size_t returnParamIndex, size_t templateParamIndex, bool twoStep, bool singular);
void writeBitmask(std::ostream & os, std::string const& bitmaskName, std::string const& bitmaskAlias, std::string const& enumName, std::vector<std::pair<std::string, std::string>> const& enumValues); void writeBitmask(std::ostream & os, std::string const& bitmaskName, std::string const& bitmaskAlias, std::string const& enumName, std::vector<std::pair<std::string, std::string>> const& enumValues);
void writeBitmaskToStringFunction(std::ostream & os, std::string const& flagsName, std::string const& enumName, std::vector<std::pair<std::string, std::string>> const& enumValues); void writeBitmaskToStringFunction(std::ostream & os, std::string const& flagsName, std::string const& enumName, std::vector<std::pair<std::string, std::string>> const& enumValues);
void writeEnumToString(std::ostream & os, std::string const& enumName, std::vector<std::pair<std::string, std::string>> const& enumValues);
std::string writeFunctionBodyEnhancedLocalReturnVariableSingular(std::ostream & os, std::string const& indentation, std::string const& typeName, std::string const&returnName, bool isStructureChain); std::string writeFunctionBodyEnhancedLocalReturnVariableSingular(std::ostream & os, std::string const& indentation, std::string const& typeName, std::string const&returnName, bool isStructureChain);
std::pair<bool, std::string> writeFunctionBodyStandardReturn(std::string const& returnType); std::pair<bool, std::string> writeFunctionBodyStandardReturn(std::string const& returnType);
@ -684,43 +683,6 @@ ${cases}
os << replaceWithMap(toStringTemplate, { { "typeName", bitmaskName },{ "argumentName", enumValues.empty() ? " " : " value " },{ "functionBody", functionBody } }); os << replaceWithMap(toStringTemplate, { { "typeName", bitmaskName },{ "argumentName", enumValues.empty() ? " " : " value " },{ "functionBody", functionBody } });
} }
void writeEnumToString(std::ostream & os, std::string const& enumName, std::vector<std::pair<std::string, std::string>> const& enumValues)
{
std::string functionBody;
if (enumValues.empty())
{
functionBody = "\n return \"(void)\";";
}
else
{
static const std::string caseTemplate = R"( case ${type}::${value} : return "${valueText}";)";
std::ostringstream casesString;
for (auto const& value : enumValues)
{
casesString << replaceWithMap(caseTemplate, { { "type", enumName },{ "value", value.second },{ "valueText", value.second.substr(1) } }) << std::endl;
}
static const std::string switchTemplate = R"(
switch ( value )
{
${cases} default: return "invalid";
})";
functionBody = replaceWithMap(switchTemplate, { { "cases", casesString.str() } });
}
static const std::string enumToString = R"(
VULKAN_HPP_INLINE std::string to_string( ${typeName}${argumentName} )
{${functionBody}
}
)";
os << replaceWithMap(enumToString,
{
{ "typeName", enumName },
{ "argumentName", enumValues.empty() ? "" : " value" },
{ "functionBody", functionBody }
});
}
std::string writeFunctionBodyEnhancedLocalReturnVariableSingular(std::ostream & os, std::string const& indentation, std::string const& typeName, std::string const& returnName, bool isStructureChain) std::string writeFunctionBodyEnhancedLocalReturnVariableSingular(std::ostream & os, std::string const& indentation, std::string const& typeName, std::string const& returnName, bool isStructureChain)
{ {
std::string adjustedReturnName = stripPluralS(returnName); std::string adjustedReturnName = stripPluralS(returnName);
@ -2276,23 +2238,18 @@ void VulkanHppGenerator::writeBitmasks(std::ostream & os) const
{ {
for (auto const& bitmask : m_bitmasks) for (auto const& bitmask : m_bitmasks)
{ {
std::string protect = bitmask.second.platform.empty() ? "" : m_platforms.find(bitmask.second.platform)->second;
if (!protect.empty())
{
os << std::endl
<< "#ifdef " << protect;
}
auto bitmaskBits = m_bitmaskBits.find(bitmask.second.requires); auto bitmaskBits = m_bitmaskBits.find(bitmask.second.requires);
assert(bitmaskBits != m_bitmaskBits.end()); assert(bitmaskBits != m_bitmaskBits.end());
os << std::endl;
writePlatformEnter(os, bitmask.second.platform);
writeEnum(os, *bitmaskBits); writeEnum(os, *bitmaskBits);
std::string strippedEnumName = stripPrefix(bitmaskBits->first, "Vk"); writeEnumToString(os, *bitmaskBits);
writeEnumToString(os, strippedEnumName, bitmaskBits->second.values);
std::string strippedBitmaskName = stripPrefix(bitmask.first, "Vk"); std::string strippedBitmaskName = stripPrefix(bitmask.first, "Vk");
std::string strippedEnumName = stripPrefix(bitmaskBits->first, "Vk");
writeBitmask(os, strippedBitmaskName, bitmask.second.alias, strippedEnumName, bitmaskBits->second.values); writeBitmask(os, strippedBitmaskName, bitmask.second.alias, strippedEnumName, bitmaskBits->second.values);
writeBitmaskToStringFunction(os, strippedBitmaskName, strippedEnumName, bitmaskBits->second.values); writeBitmaskToStringFunction(os, strippedBitmaskName, strippedEnumName, bitmaskBits->second.values);
writePlatformLeave(os, bitmask.second.platform);
leaveProtect(os, protect);
} }
} }
@ -2557,18 +2514,13 @@ void VulkanHppGenerator::writeEnum(std::ostream & os, std::pair<std::string,Enum
values += "\n "; values += "\n ";
} }
assert(m_platforms.find(enumData.second.platform) != m_platforms.end()); static const std::string enumTemplate =
std::string const& protect = m_platforms.find(enumData.second.platform)->second; R"( enum class ${name}
static const std::string enumTemplate = R"(${enterProtect}
enum class ${name}
{${values}}; {${values}};
${leaveProtect})"; )";
os << replaceWithMap(enumTemplate, os << replaceWithMap(enumTemplate,
{ {
{ "enterProtect", protect.empty() ? "" : ("\n#ifdef " + protect) },
{ "leaveProtect", protect.empty() ? "" : ("#endif /*" + protect + "*/\n") },
{ "name", stripPrefix(enumData.first, "Vk") }, { "name", stripPrefix(enumData.first, "Vk") },
{ "values", values }, { "values", values },
}); });
@ -2578,11 +2530,52 @@ void VulkanHppGenerator::writeEnums(std::ostream & os) const
{ {
for (auto const& e : m_enums) for (auto const& e : m_enums)
{ {
os << std::endl;
writePlatformEnter(os, e.second.platform);
writeEnum(os, e); writeEnum(os, e);
writeEnumToString(os, stripPrefix(e.first, "Vk"), e.second.values); writeEnumToString(os, e);
writePlatformLeave(os, e.second.platform);
} }
} }
void VulkanHppGenerator::writeEnumToString(std::ostream & os, std::pair<std::string, EnumData> const& enumData) const
{
std::string enumName = stripPrefix(enumData.first, "Vk");
std::string functionBody;
if (enumData.second.values.empty())
{
functionBody = "\n return \"(void)\";";
}
else
{
static const std::string caseTemplate = R"( case ${type}::${value} : return "${valueText}";)";
std::ostringstream casesString;
for (auto const& value : enumData.second.values)
{
casesString << replaceWithMap(caseTemplate, { { "type", enumName },{ "value", value.second },{ "valueText", value.second.substr(1) } }) << std::endl;
}
static const std::string switchTemplate = R"(
switch ( value )
{
${cases} default: return "invalid";
})";
functionBody = replaceWithMap(switchTemplate, { { "cases", casesString.str() } });
}
static const std::string enumToString = R"(
VULKAN_HPP_INLINE std::string to_string( ${typeName}${argumentName} )
{${functionBody}
}
)";
os << replaceWithMap(enumToString,
{
{ "typeName", enumName },
{ "argumentName", enumData.second.values.empty() ? "" : " value" },
{ "functionBody", functionBody }
});
}
void VulkanHppGenerator::writeForwardDeclarations(std::ostream & os) const void VulkanHppGenerator::writeForwardDeclarations(std::ostream & os) const
{ {
os << std::endl; os << std::endl;
@ -3601,6 +3594,26 @@ void VulkanHppGenerator::writeResultExceptions(std::ostream & os) const
os << std::endl; os << std::endl;
} }
void VulkanHppGenerator::writePlatformEnter(std::ostream & os, std::string const& platform) const
{
if (!platform.empty())
{
auto it = m_platforms.find(platform);
assert((it != m_platforms.end()) && !it->second.empty());
os << "#ifdef " << it->second << std::endl;
}
}
void VulkanHppGenerator::writePlatformLeave(std::ostream & os, std::string const& platform) const
{
if (!platform.empty())
{
auto it = m_platforms.find(platform);
assert((it != m_platforms.end()) && !it->second.empty());
os << "#endif /*" << it->second << "*/" << std::endl;
}
}
void VulkanHppGenerator::writeStruct(std::ostream & os, std::pair<std::string, StructureData> const& structure, std::set<std::string> & listedStructures) const void VulkanHppGenerator::writeStruct(std::ostream & os, std::pair<std::string, StructureData> const& structure, std::set<std::string> & listedStructures) const
{ {
if (listedStructures.find(structure.first) == listedStructures.end()) if (listedStructures.find(structure.first) == listedStructures.end())

View File

@ -189,6 +189,7 @@ class VulkanHppGenerator
void writeArgumentVulkanType(std::ostream & os, ParamData const& paramData) const; void writeArgumentVulkanType(std::ostream & os, ParamData const& paramData) const;
void writeCommand(std::ostream &os, std::string const& indentation, std::string const& name, std::pair<std::string, CommandData> const& commandData, bool definition) const; void writeCommand(std::ostream &os, std::string const& indentation, std::string const& name, std::pair<std::string, CommandData> const& commandData, bool definition) const;
void writeEnum(std::ostream & os, std::pair<std::string,EnumData> const& enumData) const; void writeEnum(std::ostream & os, std::pair<std::string,EnumData> const& enumData) const;
void writeEnumToString(std::ostream & os, std::pair<std::string, EnumData> const& enumData) const;
void writeFunction(std::ostream & os, std::string const& indentation, std::string const& name, std::pair<std::string, CommandData> const& commandData, size_t returnParamIndex, size_t templateParamIndex, std::map<size_t, size_t> const& vectorParamIndices, bool twoStep, std::string const& enhancedReturnType, bool definition, bool enhanced, bool singular, bool unique, bool isStructureChain, bool withAllocator) const; void writeFunction(std::ostream & os, std::string const& indentation, std::string const& name, std::pair<std::string, CommandData> const& commandData, size_t returnParamIndex, size_t templateParamIndex, std::map<size_t, size_t> const& vectorParamIndices, bool twoStep, std::string const& enhancedReturnType, bool definition, bool enhanced, bool singular, bool unique, bool isStructureChain, bool withAllocator) const;
void writeFunctionBodyEnhanced(std::ostream & os, std::string const& indentation, std::string const& commandName, std::pair<std::string, CommandData> const& commandData, size_t returnParamIndex, size_t templateParamIndex, std::map<size_t, size_t> const& vectorParamIndices, bool twoStep, std::string const& enhancedReturnType, bool singular, bool unique, bool isStructureChain, bool withAllocator) const; void writeFunctionBodyEnhanced(std::ostream & os, std::string const& indentation, std::string const& commandName, std::pair<std::string, CommandData> const& commandData, size_t returnParamIndex, size_t templateParamIndex, std::map<size_t, size_t> const& vectorParamIndices, bool twoStep, std::string const& enhancedReturnType, bool singular, bool unique, bool isStructureChain, bool withAllocator) const;
std::string writeFunctionBodyEnhancedLocalReturnVariable(std::ostream & os, std::string const& indentation, CommandData const& commandData, size_t returnParamIndex, std::map<size_t, size_t> const& vectorParamIndices, bool twoStep, std::string const& enhancedReturnType, bool singular, bool isStructureChain, bool withAllocator) const; std::string writeFunctionBodyEnhancedLocalReturnVariable(std::ostream & os, std::string const& indentation, CommandData const& commandData, size_t returnParamIndex, std::map<size_t, size_t> const& vectorParamIndices, bool twoStep, std::string const& enhancedReturnType, bool singular, bool isStructureChain, bool withAllocator) const;
@ -211,6 +212,8 @@ class VulkanHppGenerator
void writeFunctionHeaderReturnType(std::ostream & os, CommandData const& commandData, size_t returnParamIndex, std::map<size_t, size_t> const& vectorParamIndices, std::string const& enhancedReturnType, bool enhanced, bool singular, bool unique, bool isStructureChain) const; void writeFunctionHeaderReturnType(std::ostream & os, CommandData const& commandData, size_t returnParamIndex, std::map<size_t, size_t> const& vectorParamIndices, std::string const& enhancedReturnType, bool enhanced, bool singular, bool unique, bool isStructureChain) const;
void writeFunctionHeaderTemplate(std::ostream & os, std::string const& indentation, size_t returnParamIndex, size_t templateParamIndex, std::string const& enhancedReturnType, bool enhanced, bool singular, bool unique, bool withDefault, bool isStructureChain) const; void writeFunctionHeaderTemplate(std::ostream & os, std::string const& indentation, size_t returnParamIndex, size_t templateParamIndex, std::string const& enhancedReturnType, bool enhanced, bool singular, bool unique, bool withDefault, bool isStructureChain) const;
void writeHandle(std::ostream & os, std::pair<std::string, HandleData> const& handle, std::set<std::string> & listedHandles) const; void writeHandle(std::ostream & os, std::pair<std::string, HandleData> const& handle, std::set<std::string> & listedHandles) const;
void writePlatformEnter(std::ostream & os, std::string const& platform) const;
void writePlatformLeave(std::ostream & os, std::string const& platform) const;
void writeStruct(std::ostream & os, std::pair<std::string, StructureData> const& structure, std::set<std::string> & listedStructures) const; void writeStruct(std::ostream & os, std::pair<std::string, StructureData> const& structure, std::set<std::string> & listedStructures) const;
void writeStructCompareOperators(std::ostream & os, std::pair<std::string, StructureData> const& structure) const; void writeStructCompareOperators(std::ostream & os, std::pair<std::string, StructureData> const& structure) const;
void writeStructConstructor(std::ostream & os, std::pair<std::string, StructureData> const& structData) const; void writeStructConstructor(std::ostream & os, std::pair<std::string, StructureData> const& structData) const;