diff --git a/docs/source/Compiler.md b/docs/source/Compiler.md index 90f8d8b41..29979ce42 100644 --- a/docs/source/Compiler.md +++ b/docs/source/Compiler.md @@ -117,6 +117,8 @@ Additional options: output (by default the case for C++ and JS), all code will end up in this one file. +- `--cpp-include` : Adds an #include in generated file + - `--cpp-ptr-type T` : Set object API pointer type (default std::unique_ptr) - `--cpp-str-type T` : Set object API string type (default std::string) diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index 729917a8b..6f45a7ba3 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -499,6 +499,7 @@ struct IDLOptions { bool size_prefixed; std::string root_type; bool force_defaults; + std::vector cpp_includes; // Possible options for the more general generator below. enum Language { diff --git a/src/flatc.cpp b/src/flatc.cpp index 78e4c5bf9..d9df09513 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -107,6 +107,7 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const { " If the language uses a single file for output (by default\n" " the case for C++ and JS), all code will end up in this one\n" " file.\n" + " --cpp-include Adds an #include in generated file.\n" " --cpp-ptr-type T Set object API pointer type (default std::unique_ptr).\n" " --cpp-str-type T Set object API string type (default std::string).\n" " T::c_str(), T::length() and T::empty() must be supported.\n" @@ -247,6 +248,9 @@ int FlatCompiler::Compile(int argc, const char **argv) { opts.generate_object_based_api = true; } else if (arg == "--gen-compare") { opts.gen_compare = true; + } else if (arg == "--cpp-include") { + if (++argi >= argc) Error("missing include following" + arg, true); + opts.cpp_includes.push_back(argv[argi]); } else if (arg == "--cpp-ptr-type") { if (++argi >= argc) Error("missing type following" + arg, true); opts.cpp_object_api_pointer_type = argv[argi]; diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 33d7c14ad..3185eb3fc 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -204,6 +204,15 @@ class CppGenerator : public BaseGenerator { if (num_includes) code_ += ""; } + void GenExtraIncludes() { + for(std::size_t i = 0; i < parser_.opts.cpp_includes.size(); ++i) { + code_ += "#include <" + parser_.opts.cpp_includes[i] + ">"; + } + if (!parser_.opts.cpp_includes.empty()) { + code_ += ""; + } + } + std::string EscapeKeyword(const std::string &name) const { return keywords_.find(name) == keywords_.end() ? name : name + "_"; } @@ -236,6 +245,7 @@ class CppGenerator : public BaseGenerator { code_ += ""; if (parser_.opts.include_dependence_headers) { GenIncludeDependencies(); } + GenExtraIncludes(); FLATBUFFERS_ASSERT(!cur_name_space_);