Commit cef8f928 authored by Nils Berg's avatar Nils Berg Committed by Wouter van Oortmerssen

Struct typetable (#5291)

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

* Update generated code

* add test
parent 98b9b5a9
...@@ -174,6 +174,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Vec3 FLATBUFFERS_FINAL_CLASS { ...@@ -174,6 +174,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Vec3 FLATBUFFERS_FINAL_CLASS {
float z_; float z_;
public: public:
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return Vec3TypeTable();
}
Vec3() { Vec3() {
memset(static_cast<void *>(this), 0, sizeof(Vec3)); memset(static_cast<void *>(this), 0, sizeof(Vec3));
} }
......
...@@ -2684,6 +2684,15 @@ class CppGenerator : public BaseGenerator { ...@@ -2684,6 +2684,15 @@ class CppGenerator : public BaseGenerator {
// Generate GetFullyQualifiedName // Generate GetFullyQualifiedName
code_ += ""; code_ += "";
code_ += " public:"; 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)); GenFullyQualifiedNameGetter(struct_def, Name(struct_def));
// Generate a default constructor. // Generate a default constructor.
......
...@@ -542,6 +542,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(2) Test FLATBUFFERS_FINAL_CLASS { ...@@ -542,6 +542,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(2) Test FLATBUFFERS_FINAL_CLASS {
int8_t padding0__; int8_t padding0__;
public: public:
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return TestTypeTable();
}
Test() { Test() {
memset(static_cast<void *>(this), 0, sizeof(Test)); memset(static_cast<void *>(this), 0, sizeof(Test));
} }
...@@ -590,6 +593,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Vec3 FLATBUFFERS_FINAL_CLASS { ...@@ -590,6 +593,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Vec3 FLATBUFFERS_FINAL_CLASS {
int16_t padding2__; int16_t padding2__;
public: public:
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return Vec3TypeTable();
}
Vec3() { Vec3() {
memset(static_cast<void *>(this), 0, sizeof(Vec3)); memset(static_cast<void *>(this), 0, sizeof(Vec3));
} }
...@@ -667,6 +673,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Ability FLATBUFFERS_FINAL_CLASS { ...@@ -667,6 +673,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Ability FLATBUFFERS_FINAL_CLASS {
uint32_t distance_; uint32_t distance_;
public: public:
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return AbilityTypeTable();
}
Ability() { Ability() {
memset(static_cast<void *>(this), 0, sizeof(Ability)); memset(static_cast<void *>(this), 0, sizeof(Ability));
} }
......
...@@ -56,6 +56,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) StructInNestedNS FLATBUFFERS_FINAL_CLASS ...@@ -56,6 +56,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) StructInNestedNS FLATBUFFERS_FINAL_CLASS
int32_t b_; int32_t b_;
public: public:
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return StructInNestedNSTypeTable();
}
StructInNestedNS() { StructInNestedNS() {
memset(static_cast<void *>(this), 0, sizeof(StructInNestedNS)); memset(static_cast<void *>(this), 0, sizeof(StructInNestedNS));
} }
......
...@@ -894,6 +894,17 @@ void MiniReflectFlatBuffersTest(uint8_t *flatbuf) { ...@@ -894,6 +894,17 @@ void MiniReflectFlatBuffersTest(uint8_t *flatbuf) {
"test5: [ { a: 10, b: 20 }, { a: 30, b: 40 } ], " "test5: [ { a: 10, b: 20 }, { a: 30, b: 40 } ], "
"vector_of_enums: [ Blue, Green ] " "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 // Parse a .proto schema, output as .fbs
......
...@@ -197,6 +197,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Rapunzel FLATBUFFERS_FINAL_CLASS { ...@@ -197,6 +197,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Rapunzel FLATBUFFERS_FINAL_CLASS {
int32_t hair_length_; int32_t hair_length_;
public: public:
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return RapunzelTypeTable();
}
Rapunzel() { Rapunzel() {
memset(static_cast<void *>(this), 0, sizeof(Rapunzel)); memset(static_cast<void *>(this), 0, sizeof(Rapunzel));
} }
...@@ -227,6 +230,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) BookReader FLATBUFFERS_FINAL_CLASS { ...@@ -227,6 +230,9 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) BookReader FLATBUFFERS_FINAL_CLASS {
int32_t books_read_; int32_t books_read_;
public: public:
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
return BookReaderTypeTable();
}
BookReader() { BookReader() {
memset(static_cast<void *>(this), 0, sizeof(BookReader)); memset(static_cast<void *>(this), 0, sizeof(BookReader));
} }
......
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