Skip to content

Commit cef8f92

Browse files
T045Taardappel
authored andcommitted
Struct typetable (google#5291)
* C++: Generate MiniReflectTypeTable for Structs as well as Tables * Update generated code * add test
1 parent 98b9b5a commit cef8f92

File tree

6 files changed

+41
-0
lines changed

6 files changed

+41
-0
lines changed

samples/monster_generated.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Vec3 FLATBUFFERS_FINAL_CLASS {
174174
float z_;
175175

176176
public:
177+
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
178+
return Vec3TypeTable();
179+
}
177180
Vec3() {
178181
memset(static_cast<void *>(this), 0, sizeof(Vec3));
179182
}

src/idl_gen_cpp.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2684,6 +2684,15 @@ class CppGenerator : public BaseGenerator {
26842684
// Generate GetFullyQualifiedName
26852685
code_ += "";
26862686
code_ += " public:";
2687+
2688+
// Make TypeTable accessible via the generated struct.
2689+
if (parser_.opts.mini_reflect != IDLOptions::kNone) {
2690+
code_ +=
2691+
" static const flatbuffers::TypeTable *MiniReflectTypeTable() {";
2692+
code_ += " return {{STRUCT_NAME}}TypeTable();";
2693+
code_ += " }";
2694+
}
2695+
26872696
GenFullyQualifiedNameGetter(struct_def, Name(struct_def));
26882697

26892698
// Generate a default constructor.

tests/monster_test_generated.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(2) Test FLATBUFFERS_FINAL_CLASS {
542542
int8_t padding0__;
543543

544544
public:
545+
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
546+
return TestTypeTable();
547+
}
545548
Test() {
546549
memset(static_cast<void *>(this), 0, sizeof(Test));
547550
}
@@ -590,6 +593,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Vec3 FLATBUFFERS_FINAL_CLASS {
590593
int16_t padding2__;
591594

592595
public:
596+
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
597+
return Vec3TypeTable();
598+
}
593599
Vec3() {
594600
memset(static_cast<void *>(this), 0, sizeof(Vec3));
595601
}
@@ -667,6 +673,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Ability FLATBUFFERS_FINAL_CLASS {
667673
uint32_t distance_;
668674

669675
public:
676+
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
677+
return AbilityTypeTable();
678+
}
670679
Ability() {
671680
memset(static_cast<void *>(this), 0, sizeof(Ability));
672681
}

tests/namespace_test/namespace_test1_generated.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) StructInNestedNS FLATBUFFERS_FINAL_CLASS
5656
int32_t b_;
5757

5858
public:
59+
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
60+
return StructInNestedNSTypeTable();
61+
}
5962
StructInNestedNS() {
6063
memset(static_cast<void *>(this), 0, sizeof(StructInNestedNS));
6164
}

tests/test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,17 @@ void MiniReflectFlatBuffersTest(uint8_t *flatbuf) {
894894
"test5: [ { a: 10, b: 20 }, { a: 30, b: 40 } ], "
895895
"vector_of_enums: [ Blue, Green ] "
896896
"}");
897+
898+
Test test(16, 32);
899+
Vec3 vec(1,2,3, 1.5, Color_Red, test);
900+
flatbuffers::FlatBufferBuilder vec_builder;
901+
vec_builder.Finish(vec_builder.CreateStruct(vec));
902+
auto vec_buffer = vec_builder.Release();
903+
auto vec_str = flatbuffers::FlatBufferToString(vec_buffer.data(),
904+
Vec3::MiniReflectTypeTable());
905+
TEST_EQ_STR(
906+
vec_str.c_str(),
907+
"{ x: 1.0, y: 2.0, z: 3.0, test1: 1.5, test2: Red, test3: { a: 16, b: 32 } }");
897908
}
898909

899910
// Parse a .proto schema, output as .fbs

tests/union_vector/union_vector_generated.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Rapunzel FLATBUFFERS_FINAL_CLASS {
197197
int32_t hair_length_;
198198

199199
public:
200+
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
201+
return RapunzelTypeTable();
202+
}
200203
Rapunzel() {
201204
memset(static_cast<void *>(this), 0, sizeof(Rapunzel));
202205
}
@@ -227,6 +230,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) BookReader FLATBUFFERS_FINAL_CLASS {
227230
int32_t books_read_;
228231

229232
public:
233+
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
234+
return BookReaderTypeTable();
235+
}
230236
BookReader() {
231237
memset(static_cast<void *>(this), 0, sizeof(BookReader));
232238
}

0 commit comments

Comments
 (0)