Struct typetable (#5291)

* C++: Generate MiniReflectTypeTable for Structs as well as Tables

* Update generated code

* add test
This commit is contained in:
Nils Berg 2019-04-15 20:38:00 +02:00 committed by Wouter van Oortmerssen
parent 98b9b5a933
commit cef8f928bb
6 changed files with 41 additions and 0 deletions

View File

@ -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));
}

View File

@ -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.

View File

@ -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));
}

View File

@ -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));
}

View File

@ -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

View File

@ -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));
}