feat(C++): Support underlying_type for union (#7954)

* feat(C++): support underlying type for union

* chore: add conform checks for underlying type changes
This commit is contained in:
sssooonnnggg
2023-05-15 12:22:38 +08:00
committed by GitHub
parent fe5e4c71c5
commit 1d3afb90c5
10 changed files with 1004 additions and 17 deletions

View File

@@ -779,7 +779,12 @@ class CppGenerator : public BaseGenerator {
if (type.enum_def) return WrapInNameSpace(*type.enum_def);
if (type.base_type == BASE_TYPE_BOOL) return "bool";
}
return StringOf(type.base_type);
// Get real underlying type for union type
auto base_type = type.base_type;
if (type.base_type == BASE_TYPE_UTYPE && type.enum_def != nullptr) {
base_type = type.enum_def->underlying_type.base_type;
}
return StringOf(base_type);
}
// Return a C++ pointer type, specialized to the actual struct/table types,
@@ -1048,7 +1053,7 @@ class CppGenerator : public BaseGenerator {
std::string UnionVectorVerifySignature(const EnumDef &enum_def) {
const std::string name = Name(enum_def);
const std::string &type = opts_.scoped_enums ? name : "uint8_t";
const std::string &type = opts_.scoped_enums ? name : GenTypeBasic(enum_def.underlying_type, false);
return "bool Verify" + name + "Vector" +
"(::flatbuffers::Verifier &verifier, " +
"const ::flatbuffers::Vector<::flatbuffers::Offset<void>> "
@@ -3496,12 +3501,13 @@ class CppGenerator : public BaseGenerator {
}
case BASE_TYPE_UTYPE: {
value = StripUnionType(value);
auto underlying_type = GenTypeBasic(vector_type, false);
const std::string &type = opts_.scoped_enums
? Name(*field.value.type.enum_def)
: "uint8_t";
: underlying_type;
auto enum_value = "__va->_" + value + "[i].type";
if (!opts_.scoped_enums)
enum_value = "static_cast<uint8_t>(" + enum_value + ")";
enum_value = "static_cast<" + underlying_type + ">(" + enum_value + ")";
code += "_fbb.CreateVector<" + type + ">(" + value +
".size(), [](size_t i, _VectorArgs *__va) { return " +