Generated C++ headers now have include guards.

Bug: 15700355
Change-Id: Iceccb5b344e394e399092ffaa81f9cad2f0418ab
Tested: on Windows
parent d58da1db
...@@ -302,7 +302,8 @@ extern void GenerateText(const Parser &parser, ...@@ -302,7 +302,8 @@ extern void GenerateText(const Parser &parser,
// Generate a C++ header from the definitions in the Parser object. // Generate a C++ header from the definitions in the Parser object.
// See idl_gen_cpp. // See idl_gen_cpp.
extern std::string GenerateCPP(const Parser &parser); extern std::string GenerateCPP(const Parser &parser,
const std::string &include_guard_ident);
extern bool GenerateCPP(const Parser &parser, extern bool GenerateCPP(const Parser &parser,
const std::string &path, const std::string &path,
const std::string &file_name); const std::string &file_name);
......
...@@ -298,7 +298,7 @@ static void GenStruct(StructDef &struct_def, std::string *code_ptr) { ...@@ -298,7 +298,7 @@ static void GenStruct(StructDef &struct_def, std::string *code_ptr) {
// Iterate through all definitions we haven't generate code for (enums, structs, // Iterate through all definitions we haven't generate code for (enums, structs,
// and tables) and output them to a single file. // and tables) and output them to a single file.
std::string GenerateCPP(const Parser &parser) { std::string GenerateCPP(const Parser &parser, const std::string &include_guard_ident) {
using namespace cpp; using namespace cpp;
// Generate code for all the enum declarations. // Generate code for all the enum declarations.
...@@ -331,28 +331,54 @@ std::string GenerateCPP(const Parser &parser) { ...@@ -331,28 +331,54 @@ std::string GenerateCPP(const Parser &parser) {
// Only output file-level code if there were any declarations. // Only output file-level code if there were any declarations.
if (enum_code.length() || forward_decl_code.length() || decl_code.length()) { if (enum_code.length() || forward_decl_code.length() || decl_code.length()) {
std::string code; std::string code;
code = "// automatically generated, do not modify\n\n"; code = "// automatically generated by the FlatBuffers compiler,"
" do not modify\n\n";
// Generate include guard.
std::string include_guard = "FLATBUFFERS_GENERATED_" + include_guard_ident;
include_guard += "_";
for (auto it = parser.name_space_.begin();
it != parser.name_space_.end(); ++it) {
include_guard += *it + "_";
}
include_guard += "H_";
std::transform(include_guard.begin(), include_guard.end(),
include_guard.begin(), ::toupper);
code += "#ifndef " + include_guard + "\n";
code += "#define " + include_guard + "\n\n";
code += "#include \"flatbuffers/flatbuffers.h\"\n\n"; code += "#include \"flatbuffers/flatbuffers.h\"\n\n";
// Generate nested namespaces.
for (auto it = parser.name_space_.begin(); for (auto it = parser.name_space_.begin();
it != parser.name_space_.end(); ++it) { it != parser.name_space_.end(); ++it) {
code += "namespace " + *it + " {\n"; code += "namespace " + *it + " {\n";
} }
// Output the main declaration code from above.
code += "\n"; code += "\n";
code += enum_code; code += enum_code;
code += forward_decl_code; code += forward_decl_code;
code += "\n"; code += "\n";
code += decl_code; code += decl_code;
// Generate convenient root datatype accessor.
if (parser.root_struct_def) { if (parser.root_struct_def) {
code += "inline const " + parser.root_struct_def->name + " *Get"; code += "inline const " + parser.root_struct_def->name + " *Get";
code += parser.root_struct_def->name; code += parser.root_struct_def->name;
code += "(const void *buf) { return flatbuffers::GetRoot<"; code += "(const void *buf) { return flatbuffers::GetRoot<";
code += parser.root_struct_def->name + ">(buf); }\n\n"; code += parser.root_struct_def->name + ">(buf); }\n\n";
} }
// Close the namespaces.
for (auto it = parser.name_space_.begin(); for (auto it = parser.name_space_.begin();
it != parser.name_space_.end(); ++it) { it != parser.name_space_.end(); ++it) {
code += "}; // namespace " + *it + "\n"; code += "}; // namespace " + *it + "\n";
} }
// Close the include guard.
code += "\n#endif // " + include_guard + "\n";
return code; return code;
} }
...@@ -362,7 +388,7 @@ std::string GenerateCPP(const Parser &parser) { ...@@ -362,7 +388,7 @@ std::string GenerateCPP(const Parser &parser) {
bool GenerateCPP(const Parser &parser, bool GenerateCPP(const Parser &parser,
const std::string &path, const std::string &path,
const std::string &file_name) { const std::string &file_name) {
auto code = GenerateCPP(parser); auto code = GenerateCPP(parser, file_name);
return !code.length() || return !code.length() ||
SaveFile((path + file_name + "_generated.h").c_str(), code, false); SaveFile((path + file_name + "_generated.h").c_str(), code, false);
} }
......
// automatically generated, do not modify // automatically generated by the FlatBuffers compiler, do not modify
#ifndef FLATBUFFERS_GENERATED_MONSTER_TEST_MYGAME_EXAMPLE_H_
#define FLATBUFFERS_GENERATED_MONSTER_TEST_MYGAME_EXAMPLE_H_
#include "flatbuffers/flatbuffers.h" #include "flatbuffers/flatbuffers.h"
...@@ -119,5 +122,7 @@ inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder ...@@ -119,5 +122,7 @@ inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder
inline const Monster *GetMonster(const void *buf) { return flatbuffers::GetRoot<Monster>(buf); } inline const Monster *GetMonster(const void *buf) { return flatbuffers::GetRoot<Monster>(buf); }
}; // namespace MyGame }; // namespace MyGame
}; // namespace Example }; // namespace Example
#endif // FLATBUFFERS_GENERATED_MONSTER_TEST_MYGAME_EXAMPLE_H_
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