mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 12:05:50 +00:00
[C++] Use strong enum type for vectors when scoped-enums is on. (#5750)
* [C++] Use strong enum type for vectors when scoped-enums is on. These changes only apply when scoped-enums is on, and thus only the C++17 tests show diffs. This may break users who use 1) use scoped-enums and 2) use vectors of enums. However, it seems that this change should have been made originally when scoped-enums were added. Fixes #5285 * [C++] Put strong enum change also behind C++17 flag. It actually only needs C++11 technically, but this is being done to avoid breaking any existing users. Tests were rerun, but produced no additional diffs, as expected. * [C++] Forgot one location in which C++17 guard needs to go. This commit produces no additional diffs in generated code. * Use g_only_fixed_enums instead of scoped_enums.
This commit is contained in:
@@ -625,6 +625,12 @@ class CppGenerator : public BaseGenerator {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool VectorElementUserFacing(const Type& type) const {
|
||||
return opts_.g_cpp_std >= cpp::CPP_STD_17 &&
|
||||
opts_.g_only_fixed_enums &&
|
||||
IsEnum(type);
|
||||
}
|
||||
|
||||
void GenComment(const std::vector<std::string> &dc, const char *prefix = "") {
|
||||
std::string text;
|
||||
::flatbuffers::GenComment(dc, &text, nullptr, prefix);
|
||||
@@ -656,7 +662,8 @@ class CppGenerator : public BaseGenerator {
|
||||
return "flatbuffers::String";
|
||||
}
|
||||
case BASE_TYPE_VECTOR: {
|
||||
const auto type_name = GenTypeWire(type.VectorType(), "", false);
|
||||
const auto type_name = GenTypeWire(type.VectorType(), "",
|
||||
VectorElementUserFacing(type.VectorType()));
|
||||
return "flatbuffers::Vector<" + type_name + ">";
|
||||
}
|
||||
case BASE_TYPE_STRUCT: {
|
||||
@@ -1622,7 +1629,7 @@ class CppGenerator : public BaseGenerator {
|
||||
if (IsStruct(vtype)) {
|
||||
type = WrapInNameSpace(*vtype.struct_def);
|
||||
} else {
|
||||
type = GenTypeWire(vtype, "", false);
|
||||
type = GenTypeWire(vtype, "", VectorElementUserFacing(vtype));
|
||||
}
|
||||
if (TypeHasKey(vtype)) {
|
||||
code_.SetValue("PARAM_TYPE", "std::vector<" + type + "> *");
|
||||
@@ -2322,7 +2329,8 @@ class CppGenerator : public BaseGenerator {
|
||||
const auto type = WrapInNameSpace(*vtype.struct_def);
|
||||
code_ += "_fbb.CreateVectorOfSortedTables<" + type + ">\\";
|
||||
} else {
|
||||
const auto type = GenTypeWire(vtype, "", false);
|
||||
const auto type = GenTypeWire(
|
||||
vtype, "", VectorElementUserFacing(vtype));
|
||||
code_ += "_fbb.CreateVector<" + type + ">\\";
|
||||
}
|
||||
code_ +=
|
||||
@@ -2627,7 +2635,8 @@ class CppGenerator : public BaseGenerator {
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
if (field.value.type.enum_def) {
|
||||
if (field.value.type.enum_def &&
|
||||
!VectorElementUserFacing(vector_type)) {
|
||||
// For enumerations, we need to get access to the array data for
|
||||
// the underlying storage type (eg. uint8_t).
|
||||
const auto basetype = GenTypeBasic(
|
||||
|
||||
Reference in New Issue
Block a user