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.
parent 09521439
......@@ -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<typename T> T GetField(uoffset_t o) const {
return ReadScalar<T>(&data_[o]);
......
......@@ -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<Monster> 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<const Monster *>(union_obj));
......
......@@ -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<type>(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();
......
......@@ -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<const flatbuffers::String *>(4); }
int64_t val() const { return GetField<int64_t>(6, 0); }
bool Verify(flatbuffers::Verifier &verifier) const {
......@@ -120,7 +120,7 @@ inline flatbuffers::Offset<Stat> 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<const Vec3 *>(4); }
int16_t mana() const { return GetField<int16_t>(6, 150); }
int16_t hp() const { return GetField<int16_t>(8, 100); }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment