mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-03 04:21:13 +00:00
C++: mini_reflect: Add DefaultTypeTable (#4614)
* mini_reflect: Add DefaultTypeTable
Currently it's very easy to make a mistake when it comes to
instantiating the TypeTable to print a buffer because it is not type
safe.
This will allow us to write safer cpp code:
flatbuffers::FlatBufferToString(reinterpret_cast<const uint8_t *>(&t),
decltype(t)::DefaultTypeTable());
* c++: mini_reflect: update generated code
* Ensure types and names are set for mini_reflect
* c++: mini_refelct: update unit tests with new typed TypeTable
* Adding PR feedback of sylte and naming convention
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
daf0a420be
commit
36f8564846
@@ -231,6 +231,18 @@ class CppGenerator : public BaseGenerator {
|
||||
code_ += "";
|
||||
}
|
||||
}
|
||||
// Generate preablmle code for mini reflection.
|
||||
if (parser_.opts.mini_reflect != IDLOptions::kNone) {
|
||||
// To break cyclic dependencies, first pre-declare all tables/structs.
|
||||
for (auto it = parser_.structs_.vec.begin();
|
||||
it != parser_.structs_.vec.end(); ++it) {
|
||||
const auto &struct_def = **it;
|
||||
if (!struct_def.generated) {
|
||||
SetNameSpace(struct_def.defined_namespace);
|
||||
GenMiniReflectPre(&struct_def);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Generate code for all the enum declarations.
|
||||
for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
|
||||
@@ -280,15 +292,6 @@ class CppGenerator : public BaseGenerator {
|
||||
|
||||
// Generate code for mini reflection.
|
||||
if (parser_.opts.mini_reflect != IDLOptions::kNone) {
|
||||
// To break cyclic dependencies, first pre-declare all tables/structs.
|
||||
for (auto it = parser_.structs_.vec.begin();
|
||||
it != parser_.structs_.vec.end(); ++it) {
|
||||
const auto &struct_def = **it;
|
||||
if (!struct_def.generated) {
|
||||
SetNameSpace(struct_def.defined_namespace);
|
||||
GenMiniReflectPre(&struct_def);
|
||||
}
|
||||
}
|
||||
// Then the unions/enums that may refer to them.
|
||||
for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
|
||||
++it) {
|
||||
@@ -1503,6 +1506,12 @@ class CppGenerator : public BaseGenerator {
|
||||
if (parser_.opts.generate_object_based_api) {
|
||||
code_ += " typedef {{NATIVE_NAME}} NativeTableType;";
|
||||
}
|
||||
if (parser_.opts.mini_reflect != IDLOptions::kNone) {
|
||||
code_ += " static flatbuffers::TypeTable *MiniReflectTypeTable() {";
|
||||
code_ += " return {{STRUCT_NAME}}TypeTable();";
|
||||
code_ += " }";
|
||||
}
|
||||
|
||||
|
||||
GenFullyQualifiedNameGetter(struct_def, Name(struct_def));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user