diff --git a/samples/monster_generated.h b/samples/monster_generated.h index b31a8ed5c..7026116d9 100644 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -174,6 +174,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Vec3 FLATBUFFERS_FINAL_CLASS { float z_; public: + static const flatbuffers::TypeTable *MiniReflectTypeTable() { + return Vec3TypeTable(); + } Vec3() { memset(static_cast(this), 0, sizeof(Vec3)); } diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index d3dfeb10e..a50d28acc 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -2684,6 +2684,15 @@ class CppGenerator : public BaseGenerator { // Generate GetFullyQualifiedName code_ += ""; code_ += " public:"; + + // Make TypeTable accessible via the generated struct. + if (parser_.opts.mini_reflect != IDLOptions::kNone) { + code_ += + " static const flatbuffers::TypeTable *MiniReflectTypeTable() {"; + code_ += " return {{STRUCT_NAME}}TypeTable();"; + code_ += " }"; + } + GenFullyQualifiedNameGetter(struct_def, Name(struct_def)); // Generate a default constructor. diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 3a440073f..3deadd930 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -542,6 +542,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(2) Test FLATBUFFERS_FINAL_CLASS { int8_t padding0__; public: + static const flatbuffers::TypeTable *MiniReflectTypeTable() { + return TestTypeTable(); + } Test() { memset(static_cast(this), 0, sizeof(Test)); } @@ -590,6 +593,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Vec3 FLATBUFFERS_FINAL_CLASS { int16_t padding2__; public: + static const flatbuffers::TypeTable *MiniReflectTypeTable() { + return Vec3TypeTable(); + } Vec3() { memset(static_cast(this), 0, sizeof(Vec3)); } @@ -667,6 +673,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Ability FLATBUFFERS_FINAL_CLASS { uint32_t distance_; public: + static const flatbuffers::TypeTable *MiniReflectTypeTable() { + return AbilityTypeTable(); + } Ability() { memset(static_cast(this), 0, sizeof(Ability)); } diff --git a/tests/namespace_test/namespace_test1_generated.h b/tests/namespace_test/namespace_test1_generated.h index 12b344d13..239e6e4b4 100644 --- a/tests/namespace_test/namespace_test1_generated.h +++ b/tests/namespace_test/namespace_test1_generated.h @@ -56,6 +56,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) StructInNestedNS FLATBUFFERS_FINAL_CLASS int32_t b_; public: + static const flatbuffers::TypeTable *MiniReflectTypeTable() { + return StructInNestedNSTypeTable(); + } StructInNestedNS() { memset(static_cast(this), 0, sizeof(StructInNestedNS)); } diff --git a/tests/test.cpp b/tests/test.cpp index 81eb91d2c..4af297440 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -894,6 +894,17 @@ void MiniReflectFlatBuffersTest(uint8_t *flatbuf) { "test5: [ { a: 10, b: 20 }, { a: 30, b: 40 } ], " "vector_of_enums: [ Blue, Green ] " "}"); + + Test test(16, 32); + Vec3 vec(1,2,3, 1.5, Color_Red, test); + flatbuffers::FlatBufferBuilder vec_builder; + vec_builder.Finish(vec_builder.CreateStruct(vec)); + auto vec_buffer = vec_builder.Release(); + auto vec_str = flatbuffers::FlatBufferToString(vec_buffer.data(), + Vec3::MiniReflectTypeTable()); + TEST_EQ_STR( + vec_str.c_str(), + "{ x: 1.0, y: 2.0, z: 3.0, test1: 1.5, test2: Red, test3: { a: 16, b: 32 } }"); } // Parse a .proto schema, output as .fbs diff --git a/tests/union_vector/union_vector_generated.h b/tests/union_vector/union_vector_generated.h index 962c98b91..9da819a33 100644 --- a/tests/union_vector/union_vector_generated.h +++ b/tests/union_vector/union_vector_generated.h @@ -197,6 +197,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Rapunzel FLATBUFFERS_FINAL_CLASS { int32_t hair_length_; public: + static const flatbuffers::TypeTable *MiniReflectTypeTable() { + return RapunzelTypeTable(); + } Rapunzel() { memset(static_cast(this), 0, sizeof(Rapunzel)); } @@ -227,6 +230,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) BookReader FLATBUFFERS_FINAL_CLASS { int32_t books_read_; public: + static const flatbuffers::TypeTable *MiniReflectTypeTable() { + return BookReaderTypeTable(); + } BookReader() { memset(static_cast(this), 0, sizeof(BookReader)); }