[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:
David P. Sicilia
2020-02-10 20:45:32 -05:00
committed by GitHub
parent 173e10fdf1
commit d54af8cd43
2 changed files with 22 additions and 13 deletions

View File

@@ -625,6 +625,12 @@ class CppGenerator : public BaseGenerator {
return false; 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 = "") { void GenComment(const std::vector<std::string> &dc, const char *prefix = "") {
std::string text; std::string text;
::flatbuffers::GenComment(dc, &text, nullptr, prefix); ::flatbuffers::GenComment(dc, &text, nullptr, prefix);
@@ -656,7 +662,8 @@ class CppGenerator : public BaseGenerator {
return "flatbuffers::String"; return "flatbuffers::String";
} }
case BASE_TYPE_VECTOR: { 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 + ">"; return "flatbuffers::Vector<" + type_name + ">";
} }
case BASE_TYPE_STRUCT: { case BASE_TYPE_STRUCT: {
@@ -1622,7 +1629,7 @@ class CppGenerator : public BaseGenerator {
if (IsStruct(vtype)) { if (IsStruct(vtype)) {
type = WrapInNameSpace(*vtype.struct_def); type = WrapInNameSpace(*vtype.struct_def);
} else { } else {
type = GenTypeWire(vtype, "", false); type = GenTypeWire(vtype, "", VectorElementUserFacing(vtype));
} }
if (TypeHasKey(vtype)) { if (TypeHasKey(vtype)) {
code_.SetValue("PARAM_TYPE", "std::vector<" + type + "> *"); code_.SetValue("PARAM_TYPE", "std::vector<" + type + "> *");
@@ -2322,7 +2329,8 @@ class CppGenerator : public BaseGenerator {
const auto type = WrapInNameSpace(*vtype.struct_def); const auto type = WrapInNameSpace(*vtype.struct_def);
code_ += "_fbb.CreateVectorOfSortedTables<" + type + ">\\"; code_ += "_fbb.CreateVectorOfSortedTables<" + type + ">\\";
} else { } else {
const auto type = GenTypeWire(vtype, "", false); const auto type = GenTypeWire(
vtype, "", VectorElementUserFacing(vtype));
code_ += "_fbb.CreateVector<" + type + ">\\"; code_ += "_fbb.CreateVector<" + type + ">\\";
} }
code_ += code_ +=
@@ -2627,7 +2635,8 @@ class CppGenerator : public BaseGenerator {
break; break;
} }
default: { 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 // For enumerations, we need to get access to the array data for
// the underlying storage type (eg. uint8_t). // the underlying storage type (eg. uint8_t).
const auto basetype = GenTypeBasic( const auto basetype = GenTypeBasic(

View File

@@ -1411,11 +1411,11 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
void *mutable_any_ambiguous() { void *mutable_any_ambiguous() {
return GetPointer<void *>(VT_ANY_AMBIGUOUS); return GetPointer<void *>(VT_ANY_AMBIGUOUS);
} }
const flatbuffers::Vector<uint8_t> *vector_of_enums() const { const flatbuffers::Vector<MyGame::Example::Color> *vector_of_enums() const {
return GetPointer<const flatbuffers::Vector<uint8_t> *>(VT_VECTOR_OF_ENUMS); return GetPointer<const flatbuffers::Vector<MyGame::Example::Color> *>(VT_VECTOR_OF_ENUMS);
} }
flatbuffers::Vector<uint8_t> *mutable_vector_of_enums() { flatbuffers::Vector<MyGame::Example::Color> *mutable_vector_of_enums() {
return GetPointer<flatbuffers::Vector<uint8_t> *>(VT_VECTOR_OF_ENUMS); return GetPointer<flatbuffers::Vector<MyGame::Example::Color> *>(VT_VECTOR_OF_ENUMS);
} }
MyGame::Example::Race signed_enum() const { MyGame::Example::Race signed_enum() const {
return static_cast<MyGame::Example::Race>(GetField<int8_t>(VT_SIGNED_ENUM, -1)); return static_cast<MyGame::Example::Race>(GetField<int8_t>(VT_SIGNED_ENUM, -1));
@@ -1676,7 +1676,7 @@ struct MonsterBuilder {
void add_any_ambiguous(flatbuffers::Offset<void> any_ambiguous) { void add_any_ambiguous(flatbuffers::Offset<void> any_ambiguous) {
fbb_.AddOffset(Monster::VT_ANY_AMBIGUOUS, any_ambiguous); fbb_.AddOffset(Monster::VT_ANY_AMBIGUOUS, any_ambiguous);
} }
void add_vector_of_enums(flatbuffers::Offset<flatbuffers::Vector<uint8_t>> vector_of_enums) { void add_vector_of_enums(flatbuffers::Offset<flatbuffers::Vector<MyGame::Example::Color>> vector_of_enums) {
fbb_.AddOffset(Monster::VT_VECTOR_OF_ENUMS, vector_of_enums); fbb_.AddOffset(Monster::VT_VECTOR_OF_ENUMS, vector_of_enums);
} }
void add_signed_enum(MyGame::Example::Race signed_enum) { void add_signed_enum(MyGame::Example::Race signed_enum) {
@@ -1743,7 +1743,7 @@ inline flatbuffers::Offset<Monster> CreateMonster(
flatbuffers::Offset<void> any_unique = 0, flatbuffers::Offset<void> any_unique = 0,
MyGame::Example::AnyAmbiguousAliases any_ambiguous_type = MyGame::Example::AnyAmbiguousAliases::NONE, MyGame::Example::AnyAmbiguousAliases any_ambiguous_type = MyGame::Example::AnyAmbiguousAliases::NONE,
flatbuffers::Offset<void> any_ambiguous = 0, flatbuffers::Offset<void> any_ambiguous = 0,
flatbuffers::Offset<flatbuffers::Vector<uint8_t>> vector_of_enums = 0, flatbuffers::Offset<flatbuffers::Vector<MyGame::Example::Color>> vector_of_enums = 0,
MyGame::Example::Race signed_enum = MyGame::Example::Race::None) { MyGame::Example::Race signed_enum = MyGame::Example::Race::None) {
MonsterBuilder builder_(_fbb); MonsterBuilder builder_(_fbb);
builder_.add_non_owning_reference(non_owning_reference); builder_.add_non_owning_reference(non_owning_reference);
@@ -1850,7 +1850,7 @@ inline flatbuffers::Offset<Monster> CreateMonsterDirect(
flatbuffers::Offset<void> any_unique = 0, flatbuffers::Offset<void> any_unique = 0,
MyGame::Example::AnyAmbiguousAliases any_ambiguous_type = MyGame::Example::AnyAmbiguousAliases::NONE, MyGame::Example::AnyAmbiguousAliases any_ambiguous_type = MyGame::Example::AnyAmbiguousAliases::NONE,
flatbuffers::Offset<void> any_ambiguous = 0, flatbuffers::Offset<void> any_ambiguous = 0,
const std::vector<uint8_t> *vector_of_enums = nullptr, const std::vector<MyGame::Example::Color> *vector_of_enums = nullptr,
MyGame::Example::Race signed_enum = MyGame::Example::Race::None) { MyGame::Example::Race signed_enum = MyGame::Example::Race::None) {
auto name__ = name ? _fbb.CreateString(name) : 0; auto name__ = name ? _fbb.CreateString(name) : 0;
auto inventory__ = inventory ? _fbb.CreateVector<uint8_t>(*inventory) : 0; auto inventory__ = inventory ? _fbb.CreateVector<uint8_t>(*inventory) : 0;
@@ -1870,7 +1870,7 @@ inline flatbuffers::Offset<Monster> CreateMonsterDirect(
auto vector_of_strong_referrables__ = vector_of_strong_referrables ? _fbb.CreateVectorOfSortedTables<MyGame::Example::Referrable>(vector_of_strong_referrables) : 0; auto vector_of_strong_referrables__ = vector_of_strong_referrables ? _fbb.CreateVectorOfSortedTables<MyGame::Example::Referrable>(vector_of_strong_referrables) : 0;
auto vector_of_co_owning_references__ = vector_of_co_owning_references ? _fbb.CreateVector<uint64_t>(*vector_of_co_owning_references) : 0; auto vector_of_co_owning_references__ = vector_of_co_owning_references ? _fbb.CreateVector<uint64_t>(*vector_of_co_owning_references) : 0;
auto vector_of_non_owning_references__ = vector_of_non_owning_references ? _fbb.CreateVector<uint64_t>(*vector_of_non_owning_references) : 0; auto vector_of_non_owning_references__ = vector_of_non_owning_references ? _fbb.CreateVector<uint64_t>(*vector_of_non_owning_references) : 0;
auto vector_of_enums__ = vector_of_enums ? _fbb.CreateVector<uint8_t>(*vector_of_enums) : 0; auto vector_of_enums__ = vector_of_enums ? _fbb.CreateVector<MyGame::Example::Color>(*vector_of_enums) : 0;
return MyGame::Example::CreateMonster( return MyGame::Example::CreateMonster(
_fbb, _fbb,
pos, pos,
@@ -2448,7 +2448,7 @@ inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder
auto _any_unique = _o->any_unique.Pack(_fbb); auto _any_unique = _o->any_unique.Pack(_fbb);
auto _any_ambiguous_type = _o->any_ambiguous.type; auto _any_ambiguous_type = _o->any_ambiguous.type;
auto _any_ambiguous = _o->any_ambiguous.Pack(_fbb); auto _any_ambiguous = _o->any_ambiguous.Pack(_fbb);
auto _vector_of_enums = _o->vector_of_enums.size() ? _fbb.CreateVectorScalarCast<uint8_t>(flatbuffers::data(_o->vector_of_enums), _o->vector_of_enums.size()) : 0; auto _vector_of_enums = _o->vector_of_enums.size() ? _fbb.CreateVector(_o->vector_of_enums) : 0;
auto _signed_enum = _o->signed_enum; auto _signed_enum = _o->signed_enum;
return MyGame::Example::CreateMonster( return MyGame::Example::CreateMonster(
_fbb, _fbb,