c++: Add command line option to add extra includes to gen files (#5360)

* c++: Add command line option to add extra includes to gen files

Fixes #5351
We have an ability to pass custom types for strings, allocators, etc
but have no way to tell the generator to include the classes in gen code

* c++: remove std::strtok for std::string methods. passes msvc compile

* generate_code.sh: add --cpp-includes to the test gen script

* tests:generate.bat: update code gen scripts w/ --cpp-includes

* cpp: use command line parsing for extra includes

s/--cpp-includes/--cpp-include/g
Simplify command line parsing of includes by using a std::vector.

* cpp: idl.h: move std::vector for cpp_includes as the last member

msvc does not understand initalization list on our CI server

* cpp:msvc: CI fails on for-range loops

* cpp:codegen: fix error reporting on flatcc

* as per code review: remove unwated --cpp-include in the
tests/generate_code.{sh,bat}
This commit is contained in:
Alexander Gallego 2019-05-31 13:43:30 -07:00 committed by Wouter van Oortmerssen
parent b652fcc3a7
commit bc7ede8fb3
4 changed files with 17 additions and 0 deletions

View File

@ -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)

View File

@ -499,6 +499,7 @@ struct IDLOptions {
bool size_prefixed;
std::string root_type;
bool force_defaults;
std::vector<std::string> cpp_includes;
// Possible options for the more general generator below.
enum Language {

View File

@ -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];

View File

@ -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_);