Generate type traits for unions to map a type to the corresponding u… (#4032)

*  Generate type traits for unions to map a type to the corresponding union enum value.

* Fixed break with union enum type traits when type is in a namespace.

* Fixed spacing and variable names in type traits generation to match style guidelines.

*  Fixed spacing in type traits generation to match style guidelines.

* Regenerated test schema header.
This commit is contained in:
jbrads
2016-10-03 22:14:25 +01:00
committed by Wouter van Oortmerssen
parent f6c1a1ebcf
commit b075b8c49d
3 changed files with 34 additions and 1 deletions

View File

@@ -483,7 +483,24 @@ class CppGenerator : public BaseGenerator {
}
code += "]; }\n\n";
}
// Generate type traits for unions to map from a type to union enum value.
if (enum_def.is_union) {
for (auto it = enum_def.vals.vec.begin();
it != enum_def.vals.vec.end();
++it) {
auto &ev = **it;
if (it == enum_def.vals.vec.begin()) {
code += "template<typename T> struct " + enum_def.name + "Traits {\n";
}
else {
code += "template<> struct " + enum_def.name + "Traits<" + WrapInNameSpace(*ev.struct_def) + "> {\n";
}
code += " static const " + enum_def.name + " enum_value = " + GenEnumValDecl(enum_def, ev.name, parser_.opts) + ";\n";
code += "};\n\n";
}
}
if (enum_def.is_union) {
code += UnionVerifySignature(enum_def) + ";\n\n";
}