diff options
author | Nils Berg <berg.nils@gmail.com> | 2019-04-15 20:38:00 +0200 |
---|---|---|
committer | Wouter van Oortmerssen <aardappel@gmail.com> | 2019-04-15 11:38:00 -0700 |
commit | cef8f928bb64c05d12e7a9f67ed1246d43559f33 (patch) | |
tree | dcd1d226b6aaa66fbab18d4c07e9adbbe14330ef | |
parent | 98b9b5a933f58df6162442162341debed94f2daa (diff) | |
download | flatbuffers-cef8f928bb64c05d12e7a9f67ed1246d43559f33.tar.gz flatbuffers-cef8f928bb64c05d12e7a9f67ed1246d43559f33.tar.bz2 flatbuffers-cef8f928bb64c05d12e7a9f67ed1246d43559f33.zip |
Struct typetable (#5291)
* C++: Generate MiniReflectTypeTable for Structs as well as Tables
* Update generated code
* add test
-rw-r--r-- | samples/monster_generated.h | 3 | ||||
-rw-r--r-- | src/idl_gen_cpp.cpp | 9 | ||||
-rw-r--r-- | tests/monster_test_generated.h | 9 | ||||
-rw-r--r-- | tests/namespace_test/namespace_test1_generated.h | 3 | ||||
-rw-r--r-- | tests/test.cpp | 11 | ||||
-rw-r--r-- | tests/union_vector/union_vector_generated.h | 6 |
6 files changed, 41 insertions, 0 deletions
diff --git a/samples/monster_generated.h b/samples/monster_generated.h index b31a8ed5..7026116d 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<void *>(this), 0, sizeof(Vec3)); } diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index d3dfeb10..a50d28ac 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 3a440073..3deadd93 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<void *>(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<void *>(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<void *>(this), 0, sizeof(Ability)); } diff --git a/tests/namespace_test/namespace_test1_generated.h b/tests/namespace_test/namespace_test1_generated.h index 12b344d1..239e6e4b 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<void *>(this), 0, sizeof(StructInNestedNS)); } diff --git a/tests/test.cpp b/tests/test.cpp index 81eb91d2..4af29744 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 962c98b9..9da819a3 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<void *>(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<void *>(this), 0, sizeof(BookReader)); } |