Commit 35f6bb50 authored by Nnamdi's avatar Nnamdi

Added --gen-name-strings command line option.

To support the use case described in issue google/flatbuffers#3826, a new command line option --gen-name-strings
has been added, which will cause a static GetFullyQualifiedName function to be added
to the C++ output for tables/structs.
parent a649cb7d
...@@ -91,6 +91,14 @@ ...@@ -91,6 +91,14 @@
#else #else
#define FLATBUFFERS_FINAL_CLASS #define FLATBUFFERS_FINAL_CLASS
#endif #endif
#if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \
(!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 406))
#define FLATBUFFERS_CONSTEXPR constexpr
#else
#define FLATBUFFERS_CONSTEXPR
#endif
/// @endcond /// @endcond
/// @file /// @file
......
...@@ -321,6 +321,7 @@ struct IDLOptions { ...@@ -321,6 +321,7 @@ struct IDLOptions {
bool proto_mode; bool proto_mode;
bool generate_all; bool generate_all;
bool skip_unexpected_fields_in_json; bool skip_unexpected_fields_in_json;
bool generate_name_strings;
// Possible options for the more general generator below. // Possible options for the more general generator below.
enum Language { kJava, kCSharp, kGo, kMAX }; enum Language { kJava, kCSharp, kGo, kMAX };
...@@ -339,6 +340,7 @@ struct IDLOptions { ...@@ -339,6 +340,7 @@ struct IDLOptions {
proto_mode(false), proto_mode(false),
generate_all(false), generate_all(false),
skip_unexpected_fields_in_json(false), skip_unexpected_fields_in_json(false),
generate_name_strings(false),
lang(IDLOptions::kJava) {} lang(IDLOptions::kJava) {}
}; };
......
...@@ -117,6 +117,7 @@ static void Error(const std::string &err, bool usage, bool show_exe_name) { ...@@ -117,6 +117,7 @@ static void Error(const std::string &err, bool usage, bool show_exe_name) {
" schemas the generated file depends on (C++).\n" " schemas the generated file depends on (C++).\n"
" --gen-mutable Generate accessors that can mutate buffers in-place.\n" " --gen-mutable Generate accessors that can mutate buffers in-place.\n"
" --gen-onefile Generate single output file for C#\n" " --gen-onefile Generate single output file for C#\n"
" --gen-name-strings Generate type name functions for C++.\n"
" --raw-binary Allow binaries without file_indentifier to be read.\n" " --raw-binary Allow binaries without file_indentifier to be read.\n"
" This may crash flatc given a mismatched schema.\n" " This may crash flatc given a mismatched schema.\n"
" --proto Input is a .proto, translate to .fbs.\n" " --proto Input is a .proto, translate to .fbs.\n"
...@@ -171,6 +172,8 @@ int main(int argc, const char *argv[]) { ...@@ -171,6 +172,8 @@ int main(int argc, const char *argv[]) {
opts.scoped_enums = true; opts.scoped_enums = true;
} else if(arg == "--gen-mutable") { } else if(arg == "--gen-mutable") {
opts.mutable_buffer = true; opts.mutable_buffer = true;
} else if(arg == "--gen-name-strings") {
opts.generate_name_strings = true;
} else if(arg == "--gen-all") { } else if(arg == "--gen-all") {
opts.generate_all = true; opts.generate_all = true;
opts.include_dependence_headers = false; opts.include_dependence_headers = false;
......
...@@ -267,6 +267,14 @@ std::string GenFieldOffsetName(const FieldDef &field) { ...@@ -267,6 +267,14 @@ std::string GenFieldOffsetName(const FieldDef &field) {
return "VT_" + uname; return "VT_" + uname;
} }
static void GenFullyQualifiedNameGetter(const Parser &parser, const std::string& name, std::string &code) {
if (parser.opts.generate_name_strings) {
code += " static FLATBUFFERS_CONSTEXPR const char *GetFullyQualifiedName() {\n";
code += " return \"" + parser.namespaces_.back()->GetFullyQualifiedName(name) + "\";\n";
code += " }\n";
}
}
// Generate an accessor struct, builder structs & function for a table. // Generate an accessor struct, builder structs & function for a table.
static void GenTable(const Parser &parser, StructDef &struct_def, static void GenTable(const Parser &parser, StructDef &struct_def,
std::string *code_ptr) { std::string *code_ptr) {
...@@ -277,6 +285,8 @@ static void GenTable(const Parser &parser, StructDef &struct_def, ...@@ -277,6 +285,8 @@ static void GenTable(const Parser &parser, StructDef &struct_def,
code += "struct " + struct_def.name; code += "struct " + struct_def.name;
code += " FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table"; code += " FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table";
code += " {\n"; code += " {\n";
// Generate GetFullyQualifiedName
GenFullyQualifiedNameGetter(parser, struct_def.name, code);
// Generate field id constants. // Generate field id constants.
if (struct_def.fields.vec.size() > 0) { if (struct_def.fields.vec.size() > 0) {
code += " enum {\n"; code += " enum {\n";
...@@ -583,8 +593,12 @@ static void GenStruct(const Parser &parser, StructDef &struct_def, ...@@ -583,8 +593,12 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
GenPadding(field, code, padding_id, PaddingDefinition); GenPadding(field, code, padding_id, PaddingDefinition);
} }
// Generate GetFullyQualifiedName
code += "\n public:\n";
GenFullyQualifiedNameGetter(parser, struct_def.name, code);
// Generate a constructor that takes all fields as arguments. // Generate a constructor that takes all fields as arguments.
code += "\n public:\n " + struct_def.name + "("; code += " " + struct_def.name + "(";
for (auto it = struct_def.fields.vec.begin(); for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end(); it != struct_def.fields.vec.end();
++it) { ++it) {
......
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