summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndreas Kabel <akabel@users.noreply.github.com>2021-01-25 09:22:01 -0800
committerGitHub <noreply@github.com>2021-01-25 09:22:01 -0800
commit0984d4328dfa25eb1e79d25d92e134debcedfd7b (patch)
treed95c5d4b542137599b47692c20c68cc916d268be /src
parent786f69b248f731b7df009046e8b688b67dbd1da8 (diff)
downloadflatbuffers-0984d4328dfa25eb1e79d25d92e134debcedfd7b.tar.gz
flatbuffers-0984d4328dfa25eb1e79d25d92e134debcedfd7b.tar.bz2
flatbuffers-0984d4328dfa25eb1e79d25d92e134debcedfd7b.zip
[c++] Apply NativeName before WrapInNameSpace in idl_gen_cpp.cpp (#6419)
* Apply NativeName before WrapInNameSpace in idl_gen_cpp.cpp * remove actual_type argument from GetUnionElement -- it's always true * Merge GetUnionElement's native_type and wrap_native argument -- they always have the same value. * Use convenience method WrapNativeNameInNameSpace * Remove wrap_namespace argument from GetUnionElement * Move declaration closer to first use.
Diffstat (limited to 'src')
-rw-r--r--src/idl_gen_cpp.cpp67
1 files changed, 33 insertions, 34 deletions
diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp
index 629b2dc6..bd194b24 100644
--- a/src/idl_gen_cpp.cpp
+++ b/src/idl_gen_cpp.cpp
@@ -550,8 +550,7 @@ class CppGenerator : public BaseGenerator {
if (opts_.generate_object_based_api) {
// A convenient root unpack function.
- auto native_name =
- NativeName(WrapInNameSpace(struct_def), &struct_def, opts_);
+ auto native_name = WrapNativeNameInNameSpace(struct_def, opts_);
code_.SetValue("UNPACK_RETURN",
GenTypeNativePtr(native_name, nullptr, false));
code_.SetValue("UNPACK_TYPE",
@@ -709,6 +708,12 @@ class CppGenerator : public BaseGenerator {
: name;
}
+ std::string WrapNativeNameInNameSpace(const StructDef &struct_def,
+ const IDLOptions &opts) {
+ return WrapInNameSpace(struct_def.defined_namespace,
+ NativeName(Name(struct_def), &struct_def, opts));
+ }
+
const std::string &PtrType(const FieldDef *field) {
auto attr = field ? field->attributes.Lookup("cpp_ptr_type") : nullptr;
return attr ? attr->constant : opts_.cpp_object_api_pointer_type;
@@ -786,8 +791,9 @@ class CppGenerator : public BaseGenerator {
return GenTypeNativePtr(type_name, &field, false);
}
} else {
- return GenTypeNativePtr(NativeName(type_name, type.struct_def, opts_),
- &field, false);
+ return GenTypeNativePtr(
+ WrapNativeNameInNameSpace(*type.struct_def, opts_), &field,
+ false);
}
}
case BASE_TYPE_UNION: {
@@ -878,16 +884,16 @@ class CppGenerator : public BaseGenerator {
return name.substr(0, name.size() - strlen(UnionTypeFieldSuffix()));
}
- std::string GetUnionElement(const EnumVal &ev, bool wrap, bool actual_type,
- bool native_type = false) {
+ std::string GetUnionElement(const EnumVal &ev, bool native_type,
+ const IDLOptions &opts) {
if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
- auto name = actual_type ? ev.union_type.struct_def->name : Name(ev);
- return wrap ? WrapInNameSpace(ev.union_type.struct_def->defined_namespace,
- name)
- : name;
+ auto name = ev.union_type.struct_def->name;
+ if (native_type) {
+ name = NativeName(name, ev.union_type.struct_def, opts);
+ }
+ return WrapInNameSpace(ev.union_type.struct_def->defined_namespace, name);
} else if (IsString(ev.union_type)) {
- return actual_type ? (native_type ? "std::string" : "flatbuffers::String")
- : Name(ev);
+ return native_type ? "std::string" : "flatbuffers::String";
} else {
FLATBUFFERS_ASSERT(false);
return Name(ev);
@@ -1258,7 +1264,7 @@ class CppGenerator : public BaseGenerator {
if (it == enum_def.Vals().begin()) {
code_ += "template<typename T> struct {{ENUM_NAME}}Traits {";
} else {
- auto name = GetUnionElement(ev, true, true);
+ auto name = GetUnionElement(ev, false, opts_);
code_ += "template<> struct {{ENUM_NAME}}Traits<" + name + "> {";
}
@@ -1321,9 +1327,7 @@ class CppGenerator : public BaseGenerator {
const auto &ev = **it;
if (ev.IsZero()) { continue; }
- const auto native_type =
- NativeName(GetUnionElement(ev, true, true, true),
- ev.union_type.struct_def, opts_);
+ const auto native_type = GetUnionElement(ev, true, opts_);
code_.SetValue("NATIVE_TYPE", native_type);
code_.SetValue("NATIVE_NAME", Name(ev));
code_.SetValue("NATIVE_ID", GetEnumValUse(enum_def, ev));
@@ -1355,9 +1359,7 @@ class CppGenerator : public BaseGenerator {
const auto &ev = **it;
code_.SetValue("NATIVE_ID", GetEnumValUse(enum_def, ev));
if (ev.IsNonZero()) {
- const auto native_type =
- NativeName(GetUnionElement(ev, true, true, true),
- ev.union_type.struct_def, opts_);
+ const auto native_type = GetUnionElement(ev, true, opts_);
code_.SetValue("NATIVE_TYPE", native_type);
code_ += " case {{NATIVE_ID}}: {";
code_ +=
@@ -1411,7 +1413,7 @@ class CppGenerator : public BaseGenerator {
code_.SetValue("LABEL", GetEnumValUse(enum_def, ev));
if (ev.IsNonZero()) {
- code_.SetValue("TYPE", GetUnionElement(ev, true, true));
+ code_.SetValue("TYPE", GetUnionElement(ev, false, opts_));
code_ += " case {{LABEL}}: {";
auto getptr =
" auto ptr = reinterpret_cast<const {{TYPE}} *>(obj);";
@@ -1466,7 +1468,7 @@ class CppGenerator : public BaseGenerator {
if (ev.IsZero()) { continue; }
code_.SetValue("LABEL", GetEnumValUse(enum_def, ev));
- code_.SetValue("TYPE", GetUnionElement(ev, true, true));
+ code_.SetValue("TYPE", GetUnionElement(ev, false, opts_));
code_ += " case {{LABEL}}: {";
code_ += " auto ptr = reinterpret_cast<const {{TYPE}} *>(obj);";
if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
@@ -1496,15 +1498,14 @@ class CppGenerator : public BaseGenerator {
if (ev.IsZero()) { continue; }
code_.SetValue("LABEL", GetEnumValUse(enum_def, ev));
- code_.SetValue("TYPE", NativeName(GetUnionElement(ev, true, true, true),
- ev.union_type.struct_def, opts_));
- code_.SetValue("NAME", GetUnionElement(ev, false, true));
+ code_.SetValue("TYPE", GetUnionElement(ev, true, opts_));
code_ += " case {{LABEL}}: {";
code_ += " auto ptr = reinterpret_cast<const {{TYPE}} *>(value);";
if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
if (ev.union_type.struct_def->fixed) {
code_ += " return _fbb.CreateStruct(*ptr).Union();";
} else {
+ code_.SetValue("NAME", ev.union_type.struct_def->name);
code_ +=
" return Create{{NAME}}(_fbb, ptr, _rehasher).Union();";
}
@@ -1530,8 +1531,7 @@ class CppGenerator : public BaseGenerator {
const auto &ev = **it;
if (ev.IsZero()) { continue; }
code_.SetValue("LABEL", GetEnumValUse(enum_def, ev));
- code_.SetValue("TYPE", NativeName(GetUnionElement(ev, true, true, true),
- ev.union_type.struct_def, opts_));
+ code_.SetValue("TYPE", GetUnionElement(ev, true, opts_));
code_ += " case {{LABEL}}: {";
bool copyable = true;
if (ev.union_type.base_type == BASE_TYPE_STRUCT) {
@@ -1575,8 +1575,7 @@ class CppGenerator : public BaseGenerator {
const auto &ev = **it;
if (ev.IsZero()) { continue; }
code_.SetValue("LABEL", GetEnumValUse(enum_def, ev));
- code_.SetValue("TYPE", NativeName(GetUnionElement(ev, true, true, true),
- ev.union_type.struct_def, opts_));
+ code_.SetValue("TYPE", GetUnionElement(ev, true, opts_));
code_ += " case {{LABEL}}: {";
code_ += " auto ptr = reinterpret_cast<{{TYPE}} *>(value);";
code_ += " delete ptr;";
@@ -1978,7 +1977,7 @@ class CppGenerator : public BaseGenerator {
for (auto u_it = u->Vals().begin(); u_it != u->Vals().end(); ++u_it) {
auto &ev = **u_it;
if (ev.union_type.base_type == BASE_TYPE_NONE) { continue; }
- auto full_struct_name = GetUnionElement(ev, true, true);
+ auto full_struct_name = GetUnionElement(ev, false, opts_);
// @TODO: Mby make this decisions more universal? How?
code_.SetValue("U_GET_TYPE",
@@ -2223,7 +2222,7 @@ class CppGenerator : public BaseGenerator {
auto &ev = **u_it;
if (ev.union_type.base_type == BASE_TYPE_NONE) { continue; }
- auto full_struct_name = GetUnionElement(ev, true, true);
+ auto full_struct_name = GetUnionElement(ev, false, opts_);
code_.SetValue(
"U_ELEMENT_TYPE",
@@ -2494,7 +2493,6 @@ class CppGenerator : public BaseGenerator {
}
}
case BASE_TYPE_STRUCT: {
- const auto name = WrapInNameSpace(*type.struct_def);
if (IsStruct(type)) {
auto native_type = type.struct_def->attributes.Lookup("native_type");
if (native_type) {
@@ -2502,12 +2500,14 @@ class CppGenerator : public BaseGenerator {
} else if (invector || afield.native_inline) {
return "*" + val;
} else {
+ const auto name = WrapInNameSpace(*type.struct_def);
const auto ptype = GenTypeNativePtr(name, &afield, true);
return ptype + "(new " + name + "(*" + val + "))";
}
} else {
const auto ptype = GenTypeNativePtr(
- NativeName(name, type.struct_def, opts_), &afield, true);
+ WrapNativeNameInNameSpace(*type.struct_def, opts_), &afield,
+ true);
return ptype + "(" + val + "->UnPack(_resolver))";
}
}
@@ -2843,8 +2843,7 @@ class CppGenerator : public BaseGenerator {
"inline " + TableUnPackSignature(struct_def, false, opts_) + " {";
if (opts_.g_cpp_std == cpp::CPP_STD_X0) {
- auto native_name =
- NativeName(WrapInNameSpace(struct_def), &struct_def, parser_.opts);
+ auto native_name = WrapNativeNameInNameSpace(struct_def, parser_.opts);
code_.SetValue("POINTER_TYPE",
GenTypeNativePtr(native_name, nullptr, false));
code_ +=