Add constructors for readonly structures (#153)

This commit is contained in:
Markus Tavenrath 2017-11-29 14:53:57 +01:00 committed by Andreas Süßenbach
parent 509a3df5bb
commit 1a32a6c072
2 changed files with 900 additions and 46 deletions

View File

@ -3604,40 +3604,43 @@ void writeStructConstructor( std::ostream & os, std::string const& name, StructD
// the constructor with all the elements as arguments, with defaults // the constructor with all the elements as arguments, with defaults
os << " " << name << "( "; os << " " << name << "( ";
bool listedArgument = false; bool listedArgument = false;
for (size_t i = 0; i<structData.members.size(); i++) if (!structData.returnedOnly)
{ {
if (listedArgument) for (size_t i = 0; i < structData.members.size(); i++)
{ {
os << ", "; if (listedArgument)
} {
// skip members 'pNext' and 'sType', as they are never explicitly set os << ", ";
if ((structData.members[i].name != "pNext") && (structData.members[i].name != "sType")) }
{ // skip members 'pNext' and 'sType', as they are never explicitly set
// find a default value for the given pure type if ((structData.members[i].name != "pNext") && (structData.members[i].name != "sType"))
std::map<std::string, std::string>::const_iterator defaultIt = defaultValues.find(structData.members[i].pureType); {
assert(defaultIt != defaultValues.end()); // find a default value for the given pure type
std::map<std::string, std::string>::const_iterator defaultIt = defaultValues.find(structData.members[i].pureType);
assert(defaultIt != defaultValues.end());
if (structData.members[i].arraySize.empty()) if (structData.members[i].arraySize.empty())
{
// the arguments name get a trailing '_', to distinguish them from the actual struct members
// pointer arguments get a nullptr as default
os << structData.members[i].type << " " << structData.members[i].name << "_ = " << (structData.members[i].type.back() == '*' ? "nullptr" : defaultIt->second);
}
else
{
// array members are provided as const reference to a std::array
// the arguments name get a trailing '_', to distinguish them from the actual struct members
// list as many default values as there are elements in the array
os << "std::array<" << structData.members[i].type << "," << structData.members[i].arraySize << "> const& " << structData.members[i].name << "_ = { { " << defaultIt->second;
size_t n = atoi(structData.members[i].arraySize.c_str());
assert(0 < n);
for (size_t j = 1; j < n; j++)
{ {
os << ", " << defaultIt->second; // the arguments name get a trailing '_', to distinguish them from the actual struct members
// pointer arguments get a nullptr as default
os << structData.members[i].type << " " << structData.members[i].name << "_ = " << (structData.members[i].type.back() == '*' ? "nullptr" : defaultIt->second);
} }
os << " } }"; else
{
// array members are provided as const reference to a std::array
// the arguments name get a trailing '_', to distinguish them from the actual struct members
// list as many default values as there are elements in the array
os << "std::array<" << structData.members[i].type << "," << structData.members[i].arraySize << "> const& " << structData.members[i].name << "_ = { { " << defaultIt->second;
size_t n = atoi(structData.members[i].arraySize.c_str());
assert(0 < n);
for (size_t j = 1; j < n; j++)
{
os << ", " << defaultIt->second;
}
os << " } }";
}
listedArgument = true;
} }
listedArgument = true;
} }
} }
os << " )" << std::endl; os << " )" << std::endl;
@ -3668,8 +3671,15 @@ void writeStructConstructor( std::ostream & os, std::string const& name, StructD
} }
else else
{ {
// the other elements are initialized by the corresponding argument (with trailing '_', as mentioned above) if (!structData.returnedOnly)
value = structData.members[i].name + "_"; {
// the other elements are initialized by the corresponding argument (with trailing '_', as mentioned above)
value = structData.members[i].name + "_";
}
else
{
templateString = "";
}
} }
os << replaceWithMap(templateString, { {"sep", sep}, {"member", member}, {"value", value} }); os << replaceWithMap(templateString, { {"sep", sep}, {"member", member}, {"value", value} });
firstArgument = false; firstArgument = false;
@ -3678,21 +3688,23 @@ void writeStructConstructor( std::ostream & os, std::string const& name, StructD
// the body of the constructor, copying over data from argument list into wrapped struct // the body of the constructor, copying over data from argument list into wrapped struct
os << " {" << std::endl; os << " {" << std::endl;
for ( size_t i=0 ; i<structData.members.size() ; i++ ) if (!structData.returnedOnly)
{ {
if (!structData.members[i].arraySize.empty()) for (size_t i = 0; i < structData.members.size(); i++)
{ {
// here we can handle the arrays, copying over from argument (with trailing '_') to member if (!structData.members[i].arraySize.empty())
// size is arraySize times sizeof type {
std::string member = structData.members[i].name; // here we can handle the arrays, copying over from argument (with trailing '_') to member
std::string arraySize = structData.members[i].arraySize; // size is arraySize times sizeof type
std::string type = structData.members[i].type; std::string member = structData.members[i].name;
os << replaceWithMap(" memcpy( &${member}, ${member}_.data(), ${arraySize} * sizeof( ${type} ) );\n", std::string arraySize = structData.members[i].arraySize;
{ {"member", member}, {"arraySize", arraySize }, {"type", type} }); std::string type = structData.members[i].type;
os << replaceWithMap(" memcpy( &${member}, ${member}_.data(), ${arraySize} * sizeof( ${type} ) );\n",
{ {"member", member}, {"arraySize", arraySize }, {"type", type} });
}
} }
} }
os << " }" << std::endl os << " }\n\n";
<< std::endl;
std::string templateString = std::string templateString =
R"( ${name}( Vk${name} const & rhs ) R"( ${name}( Vk${name} const & rhs )
@ -4344,11 +4356,7 @@ void writeTypeStruct( std::ostream & os, VkData const& vkData, DependencyData co
os << " struct " << dependencyData.name << std::endl os << " struct " << dependencyData.name << std::endl
<< " {" << std::endl; << " {" << std::endl;
// only structs that are not returnedOnly get a constructor! writeStructConstructor( os, dependencyData.name, it->second, vkData.vkTypes, vkData.nameMap, defaultValues );
if ( !it->second.returnedOnly )
{
writeStructConstructor( os, dependencyData.name, it->second, vkData.vkTypes, vkData.nameMap, defaultValues );
}
// create the setters // create the setters
if (!it->second.returnedOnly) if (!it->second.returnedOnly)

File diff suppressed because it is too large Load Diff