From 285501f7bef048c21d0424653637f6f295b7d4fb Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Wed, 19 Nov 2014 11:03:32 -0800 Subject: [PATCH] Added "final" to generated types to block inheritance. People sometimes accidentally inherit from these types. Bug: 18224703 Change-Id: Ia09489a834ac4941f9b4a46f240cbdcf456f03a1 Tested: on Windows and Linux. --- include/flatbuffers/flatbuffers.h | 13 ++++++++++--- samples/monster_generated.h | 4 ++-- src/idl_gen_cpp.cpp | 5 +++-- tests/monster_test_generated.h | 8 ++++---- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 634fd77..75db44a 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -61,6 +61,13 @@ #define FLATBUFFERS_STRING_EXPAND(X) #X #define FLATBUFFERS_STRING(X) FLATBUFFERS_STRING_EXPAND(X) +#if (!defined(_MSC_VER) || _MSC_VER > 1600) && \ + (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 407)) + #define FLATBUFFERS_FINAL_CLASS final +#else + #define FLATBUFFERS_FINAL_CLASS +#endif + namespace flatbuffers { // Our default offset / size type, 32bit on purpose on 64bit systems. @@ -394,7 +401,7 @@ inline size_t PaddingBytes(size_t buf_size, size_t scalar_size) { // AddElement/EndTable, or the builtin CreateString/CreateVector functions. // Do this is depth-first order to build up a tree to the root. // Finish() wraps up the buffer ready for transport. -class FlatBufferBuilder { +class FlatBufferBuilder FLATBUFFERS_FINAL_CLASS { public: explicit FlatBufferBuilder(uoffset_t initial_size = 1024, const simple_allocator *allocator = nullptr) @@ -715,7 +722,7 @@ inline bool BufferHasIdentifier(const void *buf, const char *identifier) { } // Helper class to verify the integrity of a FlatBuffer -class Verifier { +class Verifier FLATBUFFERS_FINAL_CLASS { public: Verifier(const uint8_t *buf, size_t buf_len, size_t _max_depth = 64, size_t _max_tables = 1000000) @@ -833,7 +840,7 @@ class Verifier { // always have all members present and do not support forwards/backwards // compatible extensions. -class Struct { +class Struct FLATBUFFERS_FINAL_CLASS { public: template T GetField(uoffset_t o) const { return ReadScalar(&data_[o]); diff --git a/samples/monster_generated.h b/samples/monster_generated.h index 4fb6a17..17d95d3 100755 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -37,7 +37,7 @@ inline const char **EnumNamesAny() { inline const char *EnumNameAny(Any e) { return EnumNamesAny()[e]; } -inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, uint8_t type); +inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, Any type); MANUALLY_ALIGNED_STRUCT(4) Vec3 { private: @@ -110,7 +110,7 @@ inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder return builder_.Finish(); } -inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, uint8_t type) { +inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, Any type) { switch (type) { case Any_NONE: return true; case Any_Monster: return verifier.VerifyTable(reinterpret_cast(union_obj)); diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 8bf44a8..3a43240 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -208,7 +208,8 @@ static void GenTable(const Parser &parser, StructDef &struct_def, // Generate an accessor struct, with methods of the form: // type name() const { return GetField(offset, defaultval); } GenComment(struct_def.doc_comment, code_ptr); - code += "struct " + struct_def.name + " : private flatbuffers::Table"; + code += "struct " + struct_def.name; + code += " FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table"; code += " {\n"; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); @@ -414,7 +415,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def, // platforms. GenComment(struct_def.doc_comment, code_ptr); code += "MANUALLY_ALIGNED_STRUCT(" + NumToString(struct_def.minalign) + ") "; - code += struct_def.name + " {\n private:\n"; + code += struct_def.name + " FLATBUFFERS_FINAL_CLASS {\n private:\n"; int padding_id = 0; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index e114ce3..62c43c3 100755 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -46,7 +46,7 @@ inline const char *EnumNameAny(Any e) { return EnumNamesAny()[e]; } inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, Any type); -MANUALLY_ALIGNED_STRUCT(2) Test { +MANUALLY_ALIGNED_STRUCT(2) Test FLATBUFFERS_FINAL_CLASS { private: int16_t a_; int8_t b_; @@ -61,7 +61,7 @@ MANUALLY_ALIGNED_STRUCT(2) Test { }; STRUCT_END(Test, 4); -MANUALLY_ALIGNED_STRUCT(16) Vec3 { +MANUALLY_ALIGNED_STRUCT(16) Vec3 FLATBUFFERS_FINAL_CLASS { private: float x_; float y_; @@ -86,7 +86,7 @@ MANUALLY_ALIGNED_STRUCT(16) Vec3 { }; STRUCT_END(Vec3, 32); -struct Stat : private flatbuffers::Table { +struct Stat FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { const flatbuffers::String *id() const { return GetPointer(4); } int64_t val() const { return GetField(6, 0); } bool Verify(flatbuffers::Verifier &verifier) const { @@ -120,7 +120,7 @@ inline flatbuffers::Offset CreateStat(flatbuffers::FlatBufferBuilder &_fbb return builder_.Finish(); } -struct Monster : private flatbuffers::Table { +struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { const Vec3 *pos() const { return GetStruct(4); } int16_t mana() const { return GetField(6, 150); } int16_t hp() const { return GetField(8, 100); } -- 2.7.4