From 2e3b4a1753ace6b0fdabe8acda7c7cf05f91747b Mon Sep 17 00:00:00 2001 From: vglavnyy Date: Sun, 8 Dec 2019 15:59:31 +0700 Subject: [PATCH 1/8] Add flatc '--cpp_std' switch and sandbox for C++17 code generator - Added 'flac --cpp_std legacy' for compatibility with old compilers (VS2010); - Added experimental switch 'flac --cpp_std c++17' for future development; - Added C++17 sandbox test_cpp17.cpp; - C++ code generator generates enums with explicit underlying type to avoid problems with the forward and backward schema compatibility; - Adjusted CMakeLists.txt, CI and generate code scripts to support of introduced '--cpp_std'; --- CMakeLists.txt | 49 +- appveyor.yml | 8 +- docs/source/Compiler.md | 5 + include/flatbuffers/flatbuffers.h | 23 + include/flatbuffers/flatc.h | 18 +- include/flatbuffers/idl.h | 1 + include/flatbuffers/reflection_generated.h | 2 +- reflection/generate_code.bat | 2 +- reflection/generate_code.sh | 2 +- samples/monster_generated.h | 8 +- src/flatc.cpp | 10 +- src/flatc_main.cpp | 12 +- src/idl_gen_cpp.cpp | 243 +- src/idl_parser.cpp | 6 - tests/arrays_test_generated.h | 2 +- .../generated_cpp17/monster_test_generated.h | 3296 +++++++++++++++++ tests/cpp17/test_cpp17.cpp | 38 + tests/generate_code.bat | 49 +- tests/generate_code.sh | 39 +- tests/monster_test_generated.h | 20 +- .../NamespaceA/NamespaceB/StructInNestedNS.go | 21 + .../NamespaceA/NamespaceB/StructInNestedNS.py | 31 + .../NamespaceA/NamespaceB/TableInNestedNS.go | 22 + .../NamespaceA/NamespaceB/TableInNestedNS.py | 32 + .../NamespaceA/SecondTableInA.go | 23 + .../NamespaceA/SecondTableInA.py | 41 +- .../NamespaceA/TableInFirstNS.go | 30 + .../NamespaceA/TableInFirstNS.py | 51 +- tests/namespace_test/NamespaceC/TableInC.go | 27 + tests/namespace_test/NamespaceC/TableInC.py | 49 +- .../namespace_test1_generated.h | 71 +- .../namespace_test2_generated.h | 183 +- ...namespace_test2_namespace_a_generated.dart | 1 - ...namespace_test2_namespace_c_generated.dart | 1 - tests/test.cpp | 2 +- tests/union_vector/union_vector_generated.h | 4 +- 36 files changed, 4237 insertions(+), 185 deletions(-) create mode 100644 tests/cpp17/generated_cpp17/monster_test_generated.h create mode 100644 tests/cpp17/test_cpp17.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e7e9980952b..ef6df363c9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 2.8) +cmake_minimum_required(VERSION 2.8.12) # generate compile_commands.json set(CMAKE_EXPORT_COMPILE_COMMANDS ON) include(CheckCXXSymbolExists) @@ -31,6 +31,13 @@ option(FLATBUFFERS_PACKAGE_REDHAT option(FLATBUFFERS_PACKAGE_DEBIAN "Build an deb using the 'package' target." OFF) +option(FLATBUFFERS_BUILD_CPP17 + "Enable the build of c++17 test target. \" + Requirements: Clang6, GCC7, MSVC2017 (_MSC_VER >= 1914) or higher." + OFF) +option(FLATBUFFERS_BUILD_LEGACY + "Run C++ code generator with '--cpp-std legacy' switch." + OFF) if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS) message(WARNING @@ -128,6 +135,11 @@ set(FlatBuffers_Tests_SRCS tests/native_type_test_impl.cpp # file generate by running compiler on tests/monster_test.fbs ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h + # file generate by running compiler on namespace_test/namespace_test1.fbs + ${CMAKE_CURRENT_BINARY_DIR}/tests/namespace_test/namespace_test1_generated.h + ${CMAKE_CURRENT_BINARY_DIR}/tests/namespace_test/namespace_test2_generated.h + # file generate by running compiler on union_vector/union_vector.fbs + ${CMAKE_CURRENT_BINARY_DIR}/tests/union_vector/union_vector_generated.h # file generate by running compiler on tests/arrays_test.fbs ${CMAKE_CURRENT_BINARY_DIR}/tests/arrays_test_generated.h # file generate by running compiler on tests/native_type_test.fbs @@ -136,6 +148,14 @@ set(FlatBuffers_Tests_SRCS ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_extra_generated.h ) +set(FlatBuffers_Tests_CPP17_SRCS + ${FlatBuffers_Library_SRCS} + tests/cpp17/test_cpp17.cpp + # file generate by running compiler on tests/monster_test.fbs + ${CMAKE_CURRENT_BINARY_DIR}/tests/cpp17/generated_cpp17/monster_test_generated.h + ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h +) + set(FlatBuffers_Sample_Binary_SRCS include/flatbuffers/flatbuffers.h samples/sample_binary.cpp @@ -278,7 +298,7 @@ include_directories(grpc) if(FLATBUFFERS_BUILD_FLATLIB) add_library(flatbuffers STATIC ${FlatBuffers_Library_SRCS}) - # CMake > 2.8.11: Attach header directory for when build via add_subdirectory(). + # Attach header directory for when build via add_subdirectory(). target_include_directories(flatbuffers INTERFACE $) target_compile_options(flatbuffers PRIVATE "${FLATBUFFERS_PRIVATE_CXX_FLAGS}") @@ -321,7 +341,10 @@ if(FLATBUFFERS_BUILD_SHAREDLIB) endif() function(compile_flatbuffers_schema_to_cpp_opt SRC_FBS OPT) - message(STATUS "Add the code-generation command for the `${SRC_FBS}` schema.") + if(FLATBUFFERS_BUILD_LEGACY) + set(OPT ${OPT};--cpp-std legacy) + endif() + message(STATUS "Add the code-generation command for the `${SRC_FBS}` schema with '${OPT}'") get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH) string(REGEX REPLACE "\\.fbs$" "_generated.h" GEN_HEADER ${SRC_FBS}) add_custom_command( @@ -336,7 +359,7 @@ function(compile_flatbuffers_schema_to_cpp_opt SRC_FBS OPT) endfunction() function(compile_flatbuffers_schema_to_cpp SRC_FBS) - compile_flatbuffers_schema_to_cpp_opt(${SRC_FBS} "--no-includes;--gen-compare") + compile_flatbuffers_schema_to_cpp_opt(${SRC_FBS} "--no-includes;--gen-compare") endfunction() function(compile_flatbuffers_schema_to_binary SRC_FBS) @@ -351,9 +374,12 @@ endfunction() if(FLATBUFFERS_BUILD_TESTS) compile_flatbuffers_schema_to_cpp(tests/monster_test.fbs) + compile_flatbuffers_schema_to_cpp(tests/namespace_test/namespace_test1.fbs) + compile_flatbuffers_schema_to_cpp(tests/namespace_test/namespace_test2.fbs) + compile_flatbuffers_schema_to_cpp(tests/union_vector/union_vector.fbs) compile_flatbuffers_schema_to_cpp_opt(tests/native_type_test.fbs "") compile_flatbuffers_schema_to_cpp_opt(tests/arrays_test.fbs "--scoped-enums;--gen-compare") - if(NOT (MSVC AND MSVC_VERSION LESS 1900)) + if(NOT (MSVC AND (MSVC_VERSION LESS 1900))) compile_flatbuffers_schema_to_cpp(tests/monster_extra.fbs) # Test floating-point NAN/INF. endif() include_directories(${CMAKE_CURRENT_BINARY_DIR}/tests) @@ -375,6 +401,19 @@ if(FLATBUFFERS_BUILD_TESTS) add_executable(flatsamplebinary ${FlatBuffers_Sample_Binary_SRCS}) add_executable(flatsampletext ${FlatBuffers_Sample_Text_SRCS}) add_executable(flatsamplebfbs ${FlatBuffers_Sample_BFBS_SRCS}) + + if(FLATBUFFERS_BUILD_CPP17) + add_executable(flattests_cpp17 ${FlatBuffers_Tests_CPP17_SRCS}) + target_compile_features(flattests_cpp17 PRIVATE cxx_std_17) + target_compile_definitions(flattests_cpp17 PRIVATE + FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE + FLATBUFFERS_DEBUG_VERIFICATION_FAILURE=1 + ) + + if(NOT WIN32 AND FLATBUFFERS_CODE_SANITIZE) + add_fsanitize_to_target(flattests_cpp17 ${FLATBUFFERS_CODE_SANITIZE}) + endif() + endif(FLATBUFFERS_BUILD_CPP17) endif() if(FLATBUFFERS_BUILD_GRPCTEST) diff --git a/appveyor.yml b/appveyor.yml index 75a63c87211..3dd659b87b4 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -10,9 +10,13 @@ environment: # Workaround for https://github.com/conda/conda-build/issues/636 PYTHONIOENCODING: UTF-8 CONDA_INSTALL_LOCN: "C:\\Miniconda35-x64" + CMAKE_OPTIONS: "" + CPP_TEST_OPTIONS: "" matrix: - CMAKE_VS_VERSION: "10 2010" + CMAKE_OPTIONS: "-DFLATBUFFERS_BUILD_LEGACY=1" + CPP_TEST_OPTIONS: "--std-cpp legacy" MONSTER_EXTRA: "skip" - CMAKE_VS_VERSION: "12 2013" @@ -31,7 +35,7 @@ configuration: before_build: - set MONSTER_EXTRA=%MONSTER_EXTRA% - - cmake -G"Visual Studio %CMAKE_VS_VERSION%" -DFLATBUFFERS_CODE_SANITIZE=1 . + - cmake . -G"Visual Studio %CMAKE_VS_VERSION%" -DFLATBUFFERS_CODE_SANITIZE=1 %CMAKE_OPTIONS% # This cuts down on a lot of noise generated by xamarin warnings. - del "C:\Program Files (x86)\MSBuild\14.0\Microsoft.Common.targets\ImportAfter\Xamarin.Common.targets" @@ -55,7 +59,7 @@ test_script: - call .appveyor\check-generate-code.bat -b %CONFIGURATION% - "cd tests" - rem "Building all code" - - generate_code.bat -b %CONFIGURATION% + - generate_code.bat -b %CONFIGURATION% %CPP_TEST_OPTIONS% - 7z a GeneratedMyGameCode.zip MyGame\ - rem "---------------- C++ -----------------" - "cd .." diff --git a/docs/source/Compiler.md b/docs/source/Compiler.md index df0378669cf..d8f6547cd27 100644 --- a/docs/source/Compiler.md +++ b/docs/source/Compiler.md @@ -132,6 +132,11 @@ Additional options: std::string from Flatbuffers, but (char* + length). This allows efficient construction of custom string types, including zero-copy construction. +- `--cpp-std CPP_STD` : Generate a C++ code using features of selected C++ standard. + Supported `CPP_STD` values: + * `c++` - use a standard code generation rules; + * `legacy` - generate code compatible with old compilers (VS2010). + - `--object-prefix` : Customise class prefix for C++ object-based API. - `--object-suffix` : Customise class suffix for C++ object-based API. diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 9a4f7ccd76f..6ebbde1b8d1 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -43,6 +43,20 @@ template<> inline bool IsTheSameAs(double e, double def) { } #endif +// Check 'v' is out of closed range [low; high]. +// Workaround for GCC warning [-Werror=type-limits]: +// comparison is always true due to limited range of data type. +template +inline bool IsOutRange(const T &v, const T &low, const T &high) { + return (v < low) || (high < v); +} + +// Check 'v' is in closed range [low; high]. +template +inline bool IsInRange(const T &v, const T &low, const T &high) { + return !IsOutRange(v, low, high); +} + // Wrapper for uoffset_t to allow safe template specialization. // Value is allowed to be 0 to indicate a null object (see e.g. AddOffset). template struct Offset { @@ -351,6 +365,7 @@ template class Vector { // This class is a pointer. Copying will therefore create an invalid object. // Private and unimplemented copy constructor. Vector(const Vector &); + Vector& operator=(const Vector&); template static int KeyCompare(const void *ap, const void *bp) { const K *key = reinterpret_cast(ap); @@ -381,6 +396,7 @@ class VectorOfAny { private: VectorOfAny(const VectorOfAny &); + VectorOfAny &operator=(const VectorOfAny &); }; #ifndef FLATBUFFERS_CPP98_STL @@ -2377,6 +2393,12 @@ class Struct FLATBUFFERS_FINAL_CLASS { uint8_t *GetAddressOf(uoffset_t o) { return &data_[o]; } private: + // private constructor & copy constructor: you obtain instances of this + // class by pointing to existing data only + Struct(); + Struct(const Struct &); + Struct &operator=(const Struct &); + uint8_t data_[1]; }; @@ -2489,6 +2511,7 @@ class Table { // class by pointing to existing data only Table(); Table(const Table &other); + Table &operator=(const Table &); uint8_t data_[1]; }; diff --git a/include/flatbuffers/flatc.h b/include/flatbuffers/flatc.h index 5afe3bc9ac9..c3b29c4e916 100644 --- a/include/flatbuffers/flatc.h +++ b/include/flatbuffers/flatc.h @@ -14,6 +14,9 @@ * limitations under the License. */ +#ifndef FLATBUFFERS_FLATC_H_ +#define FLATBUFFERS_FLATC_H_ + #include #include #include @@ -22,11 +25,18 @@ #include "flatbuffers/idl.h" #include "flatbuffers/util.h" -#ifndef FLATC_H_ -# define FLATC_H_ - namespace flatbuffers { +class FlatCompilerLogger { + FlatCompilerLogger() {} +public: + // There is no way to notify about an error from a code-generator + // except `assert` call. + // TODO: re-design interface to code generators. + static void Warn(const std::string &warn); + static void Error(const std::string &err); +}; + class FlatCompiler { public: // Output generator for the various programming languages and formats we @@ -94,4 +104,4 @@ class FlatCompiler { } // namespace flatbuffers -#endif // FLATC_H_ +#endif // FLATBUFFERS_FLATC_H_ diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index 21616210e69..792103c6435 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -556,6 +556,7 @@ struct IDLOptions { bool force_defaults; bool java_primitive_has_method; std::vector cpp_includes; + std::string cpp_std; // Possible options for the more general generator below. enum Language { diff --git a/include/flatbuffers/reflection_generated.h b/include/flatbuffers/reflection_generated.h index 77a2c7f90f2..b59637d8a2c 100644 --- a/include/flatbuffers/reflection_generated.h +++ b/include/flatbuffers/reflection_generated.h @@ -97,7 +97,7 @@ inline const char * const *EnumNamesBaseType() { } inline const char *EnumNameBaseType(BaseType e) { - if (e < None || e > Array) return ""; + if (flatbuffers::IsOutRange(e, None, Array)) return ""; const size_t index = static_cast(e); return EnumNamesBaseType()[index]; } diff --git a/reflection/generate_code.bat b/reflection/generate_code.bat index e2993253b0d..c43c334db36 100644 --- a/reflection/generate_code.bat +++ b/reflection/generate_code.bat @@ -15,4 +15,4 @@ set buildtype=Release if "%1"=="-b" set buildtype=%2 -..\%buildtype%\flatc.exe --cpp --no-prefix -o ../include/flatbuffers reflection.fbs || exit /b 1 +..\%buildtype%\flatc.exe --cpp --cpp-std legacy --no-prefix -o ../include/flatbuffers reflection.fbs || exit /b 1 diff --git a/reflection/generate_code.sh b/reflection/generate_code.sh index f186b732772..efa2ed00998 100755 --- a/reflection/generate_code.sh +++ b/reflection/generate_code.sh @@ -15,4 +15,4 @@ # limitations under the License. set -e -../flatc -c --no-prefix -o ../include/flatbuffers reflection.fbs +../flatc -c --cpp-std legacy --no-prefix -o ../include/flatbuffers reflection.fbs diff --git a/samples/monster_generated.h b/samples/monster_generated.h index ca231a4d708..6e607c94bf2 100644 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -30,7 +30,7 @@ inline const flatbuffers::TypeTable *MonsterTypeTable(); inline const flatbuffers::TypeTable *WeaponTypeTable(); -enum Color { +enum Color : int8_t { Color_Red = 0, Color_Green = 1, Color_Blue = 2, @@ -58,12 +58,12 @@ inline const char * const *EnumNamesColor() { } inline const char *EnumNameColor(Color e) { - if (e < Color_Red || e > Color_Blue) return ""; + if (flatbuffers::IsOutRange(e, Color_Red, Color_Blue)) return ""; const size_t index = static_cast(e); return EnumNamesColor()[index]; } -enum Equipment { +enum Equipment : uint8_t { Equipment_NONE = 0, Equipment_Weapon = 1, Equipment_MIN = Equipment_NONE, @@ -88,7 +88,7 @@ inline const char * const *EnumNamesEquipment() { } inline const char *EnumNameEquipment(Equipment e) { - if (e < Equipment_NONE || e > Equipment_Weapon) return ""; + if (flatbuffers::IsOutRange(e, Equipment_NONE, Equipment_Weapon)) return ""; const size_t index = static_cast(e); return EnumNamesEquipment()[index]; } diff --git a/src/flatc.cpp b/src/flatc.cpp index 9c59232f619..c9b21159e69 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -116,6 +116,11 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const { " (see the --cpp-str-flex-ctor option to change this behavior).\n" " --cpp-str-flex-ctor Don't construct custom string types by passing std::string\n" " from Flatbuffers, but (char* + length).\n" + " --cpp-std CPP_STD Generate a C++ code using features of selected C++ standard.\n" + " Supported CPP_STD values:\n" + " * 'legacy' - generate code compatible with old compilers.\n" + " * 'c++' - use default code generation rules (default);\n" + " * 'c++17' - use C++17 features in generated code (experemental);\n" " --object-prefix Customise class prefix for C++ object-based API.\n" " --object-suffix Customise class suffix for C++ object-based API.\n" " Default value is \"T\".\n" @@ -281,7 +286,7 @@ int FlatCompiler::Compile(int argc, const char **argv) { opts.include_dependence_headers = false; } else if (arg == "--gen-includes") { // Deprecated, remove this option some time in the future. - printf("warning: --gen-includes is deprecated (it is now default)\n"); + Warn("warning: --gen-includes is deprecated (it is now default)\n"); } else if (arg == "--no-includes") { opts.include_dependence_headers = false; } else if (arg == "--gen-onefile") { @@ -333,6 +338,9 @@ int FlatCompiler::Compile(int argc, const char **argv) { opts.java_primitive_has_method = true; } else if (arg == "--flexbuffers") { opts.use_flexbuffers = true; + } else if(arg == "--cpp-std") { + if (++argi >= argc) Error("missing C++ standard specification" + arg, true); + opts.cpp_std = argv[argi]; } else { for (size_t i = 0; i < params_.num_generators; ++i) { if (arg == params_.generators[i].generator_opt_long || diff --git a/src/flatc_main.cpp b/src/flatc_main.cpp index f54b94e8e94..d0f92dade4b 100644 --- a/src/flatc_main.cpp +++ b/src/flatc_main.cpp @@ -30,10 +30,20 @@ static void Error(const flatbuffers::FlatCompiler *flatc, const std::string &err, bool usage, bool show_exe_name) { if (show_exe_name) { printf("%s: ", g_program_name); } printf("error: %s\n", err.c_str()); - if (usage) { printf("%s", flatc->GetUsageString(g_program_name).c_str()); } + if (usage && flatc) { printf("%s", flatc->GetUsageString(g_program_name).c_str()); } exit(1); } +namespace flatbuffers { +void FlatCompilerLogger::Warn(const std::string &warn) { + ::Warn(static_cast(nullptr), warn, true); +} +void FlatCompilerLogger::Error(const std::string &err) { + ::Error(static_cast(nullptr), err, false, + true); +} +} // namespace flatbuffers + int main(int argc, const char *argv[]) { // Prevent Appveyor-CI hangs. flatbuffers::SetupDefaultCRTReportMode(); diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 81d6b91412f..af789e639aa 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -20,13 +20,16 @@ #include "flatbuffers/code_generators.h" #include "flatbuffers/flatbuffers.h" +#include "flatbuffers/flatc.h" #include "flatbuffers/idl.h" #include "flatbuffers/util.h" namespace flatbuffers { // Pedantic warning free version of toupper(). -inline char ToUpper(char c) { return static_cast(::toupper(c)); } +inline char ToUpper(char c) { + return static_cast(::toupper(static_cast(c))); +} // Make numerical literal with type-suffix. // This function is only needed for C++! Other languages do not need it. @@ -51,12 +54,28 @@ static std::string GeneratedFileName(const std::string &path, } namespace cpp { + +// Extension of IDLOptions for cpp-generator. +struct IDLOptionsCpp : public IDLOptions { + // All fields start with 'g_' prefix to distinguish from the base IDLOptions. + int g_cpp_std; // base version of C++ standard (0, 17) + bool g_only_fixed_enums; + // clang-format off + IDLOptionsCpp(const IDLOptions &opts) + : IDLOptions(opts), + g_cpp_std(0), + g_only_fixed_enums(true) + {} + // clang-format on +}; + class CppGenerator : public BaseGenerator { public: CppGenerator(const Parser &parser, const std::string &path, - const std::string &file_name) + const std::string &file_name, IDLOptionsCpp opts) : BaseGenerator(parser, path, file_name, "", "::"), cur_name_space_(nullptr), + opts_(opts), float_const_gen_("std::numeric_limits::", "std::numeric_limits::", "quiet_NaN()", "infinity()") { @@ -196,19 +215,18 @@ class CppGenerator : public BaseGenerator { auto noext = flatbuffers::StripExtension(it->second); auto basename = flatbuffers::StripPath(noext); - code_ += "#include \"" + parser_.opts.include_prefix + - (parser_.opts.keep_include_path ? noext : basename) + - "_generated.h\""; + code_ += "#include \"" + opts_.include_prefix + + (opts_.keep_include_path ? noext : basename) + "_generated.h\""; num_includes++; } 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] + "\""; + for (std::size_t i = 0; i < opts_.cpp_includes.size(); ++i) { + code_ += "#include \"" + opts_.cpp_includes[i] + "\""; } - if (!parser_.opts.cpp_includes.empty()) { code_ += ""; } + if (!opts_.cpp_includes.empty()) { code_ += ""; } } std::string EscapeKeyword(const std::string &name) const { @@ -232,9 +250,7 @@ class CppGenerator : public BaseGenerator { code_ += "#define " + include_guard; code_ += ""; - if (parser_.opts.gen_nullable) { - code_ += "#pragma clang system_header\n\n"; - } + if (opts_.gen_nullable) { code_ += "#pragma clang system_header\n\n"; } code_ += "#include \"flatbuffers/flatbuffers.h\""; if (parser_.uses_flexbuffers_) { @@ -242,7 +258,7 @@ class CppGenerator : public BaseGenerator { } code_ += ""; - if (parser_.opts.include_dependence_headers) { GenIncludeDependencies(); } + if (opts_.include_dependence_headers) { GenIncludeDependencies(); } GenExtraIncludes(); FLATBUFFERS_ASSERT(!cur_name_space_); @@ -255,9 +271,8 @@ class CppGenerator : public BaseGenerator { if (!struct_def.generated) { SetNameSpace(struct_def.defined_namespace); code_ += "struct " + Name(struct_def) + ";"; - if (parser_.opts.generate_object_based_api) { - auto nativeName = - NativeName(Name(struct_def), &struct_def, parser_.opts); + if (opts_.generate_object_based_api) { + auto nativeName = NativeName(Name(struct_def), &struct_def, opts_); if (!struct_def.fixed) { code_ += "struct " + nativeName + ";"; } } code_ += ""; @@ -265,14 +280,13 @@ class CppGenerator : public BaseGenerator { } // Generate forward declarations for all equal operators - if (parser_.opts.generate_object_based_api && parser_.opts.gen_compare) { + if (opts_.generate_object_based_api && opts_.gen_compare) { 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); - auto nativeName = - NativeName(Name(struct_def), &struct_def, parser_.opts); + auto nativeName = NativeName(Name(struct_def), &struct_def, opts_); code_ += "bool operator==(const " + nativeName + " &lhs, const " + nativeName + " &rhs);"; code_ += "bool operator!=(const " + nativeName + " &lhs, const " + @@ -283,7 +297,7 @@ class CppGenerator : public BaseGenerator { } // Generate preablmle code for mini reflection. - if (parser_.opts.mini_reflect != IDLOptions::kNone) { + if (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) { @@ -342,7 +356,7 @@ class CppGenerator : public BaseGenerator { } // Generate code for mini reflection. - if (parser_.opts.mini_reflect != IDLOptions::kNone) { + if (opts_.mini_reflect != IDLOptions::kNone) { // Then the unions/enums that may refer to them. for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); ++it) { @@ -393,7 +407,7 @@ class CppGenerator : public BaseGenerator { code_ += "}"; code_ += ""; - if (parser_.opts.mutable_buffer) { + if (opts_.mutable_buffer) { code_ += "inline \\"; code_ += "{{STRUCT_NAME}} *GetMutable{{STRUCT_NAME}}(void *buf) {"; code_ += " return flatbuffers::GetMutableRoot<{{STRUCT_NAME}}>(buf);"; @@ -466,10 +480,10 @@ class CppGenerator : public BaseGenerator { code_ += "}"; code_ += ""; - if (parser_.opts.generate_object_based_api) { + if (opts_.generate_object_based_api) { // A convenient root unpack function. auto native_name = - NativeName(WrapInNameSpace(struct_def), &struct_def, parser_.opts); + NativeName(WrapInNameSpace(struct_def), &struct_def, opts_); code_.SetValue("UNPACK_RETURN", GenTypeNativePtr(native_name, nullptr, false)); code_.SetValue("UNPACK_TYPE", @@ -511,6 +525,9 @@ class CppGenerator : public BaseGenerator { // This tracks the current namespace so we can insert namespace declarations. const Namespace *cur_name_space_; + const IDLOptionsCpp opts_; + const TypedFloatConstantGenerator float_const_gen_; + const Namespace *CurrentNameSpace() const { return cur_name_space_; } // Translates a qualified name in flatbuffer text format to the same name in @@ -606,7 +623,7 @@ class CppGenerator : public BaseGenerator { } std::string NullableExtension() { - return parser_.opts.gen_nullable ? " _Nullable " : ""; + return opts_.gen_nullable ? " _Nullable " : ""; } static std::string NativeName(const std::string &name, const StructDef *sd, @@ -617,12 +634,12 @@ class CppGenerator : public BaseGenerator { const std::string &PtrType(const FieldDef *field) { auto attr = field ? field->attributes.Lookup("cpp_ptr_type") : nullptr; - return attr ? attr->constant : parser_.opts.cpp_object_api_pointer_type; + return attr ? attr->constant : opts_.cpp_object_api_pointer_type; } const std::string NativeString(const FieldDef *field) { auto attr = field ? field->attributes.Lookup("cpp_str_type") : nullptr; - auto &ret = attr ? attr->constant : parser_.opts.cpp_object_api_string_type; + auto &ret = attr ? attr->constant : opts_.cpp_object_api_string_type; if (ret.empty()) { return "std::string"; } return ret; } @@ -631,8 +648,7 @@ class CppGenerator : public BaseGenerator { auto attr = field ? (field->attributes.Lookup("cpp_str_flex_ctor") != nullptr) : false; - auto ret = - attr ? attr : parser_.opts.cpp_object_api_string_flexible_constructor; + auto ret = attr ? attr : opts_.cpp_object_api_string_flexible_constructor; return ret && NativeString(field) != "std::string"; // Only for custom string types. } @@ -643,7 +659,7 @@ class CppGenerator : public BaseGenerator { if (ptr_type != "naked") { return (ptr_type != "default_ptr_type" ? ptr_type - : parser_.opts.cpp_object_api_pointer_type) + + : opts_.cpp_object_api_pointer_type) + "<" + type + ">"; } else if (is_constructor) { return ""; @@ -687,9 +703,8 @@ class CppGenerator : public BaseGenerator { return GenTypeNativePtr(type_name, &field, false); } } else { - return GenTypeNativePtr( - NativeName(type_name, type.struct_def, parser_.opts), &field, - false); + return GenTypeNativePtr(NativeName(type_name, type.struct_def, opts_), + &field, false); } } case BASE_TYPE_UNION: { @@ -711,6 +726,12 @@ class CppGenerator : public BaseGenerator { return GenTypeBasic(type, user_facing_type) + afterbasic; } else if (IsArray(type)) { auto element_type = type.VectorType(); + // Check if enum arrays are used in C++ without specifying --scoped-enums + if (IsEnum(element_type) && !opts_.g_only_fixed_enums) { + FlatCompilerLogger::Error( + "--scoped-enums must be enabled to use enum arrays in C++"); + FLATBUFFERS_ASSERT(true); + } return beforeptr + (IsScalar(element_type.base_type) ? GenTypeBasic(element_type, user_facing_type) @@ -721,23 +742,16 @@ class CppGenerator : public BaseGenerator { } } - std::string GenEnumDecl(const EnumDef &enum_def) const { - const IDLOptions &opts = parser_.opts; - return (opts.scoped_enums ? "enum class " : "enum ") + Name(enum_def); - } - std::string GenEnumValDecl(const EnumDef &enum_def, const std::string &enum_val) const { - const IDLOptions &opts = parser_.opts; - return opts.prefixed_enums ? Name(enum_def) + "_" + enum_val : enum_val; + return opts_.prefixed_enums ? Name(enum_def) + "_" + enum_val : enum_val; } std::string GetEnumValUse(const EnumDef &enum_def, const EnumVal &enum_val) const { - const IDLOptions &opts = parser_.opts; - if (opts.scoped_enums) { + if (opts_.scoped_enums) { return Name(enum_def) + "::" + Name(enum_val); - } else if (opts.prefixed_enums) { + } else if (opts_.prefixed_enums) { return Name(enum_def) + "_" + Name(enum_val); } else { return Name(enum_val); @@ -942,7 +956,7 @@ class CppGenerator : public BaseGenerator { code_ += " static const int64_t values[] = { {{VALUES}} };"; } auto has_names = - num_fields && parser_.opts.mini_reflect == IDLOptions::kTypesAndNames; + num_fields && opts_.mini_reflect == IDLOptions::kTypesAndNames; if (has_names) { code_ += " static const char * const names[] = {"; code_ += " {{NAMES}}"; @@ -969,14 +983,11 @@ class CppGenerator : public BaseGenerator { code_.SetValue("BASE_TYPE", GenTypeBasic(enum_def.underlying_type, false)); GenComment(enum_def.doc_comment); - code_ += GenEnumDecl(enum_def) + "\\"; - // MSVC doesn't support int64/uint64 enum without explicitly declared enum - // type. The value 4611686018427387904ULL is truncated to zero with warning: - // "warning C4309: 'initializing': truncation of constant value". - auto add_type = parser_.opts.scoped_enums; - add_type |= (enum_def.underlying_type.base_type == BASE_TYPE_LONG); - add_type |= (enum_def.underlying_type.base_type == BASE_TYPE_ULONG); - if (add_type) code_ += " : {{BASE_TYPE}}\\"; + code_ += + (opts_.scoped_enums ? "enum class " : "enum ") + Name(enum_def) + "\\"; + if (opts_.g_only_fixed_enums) { + code_ += " : {{BASE_TYPE}}\\"; + } code_ += " {"; code_.SetValue("SEP", ","); @@ -995,7 +1006,7 @@ class CppGenerator : public BaseGenerator { const EnumVal *minv = enum_def.MinValue(); const EnumVal *maxv = enum_def.MaxValue(); - if (parser_.opts.scoped_enums || parser_.opts.prefixed_enums) { + if (opts_.scoped_enums || opts_.prefixed_enums) { FLATBUFFERS_ASSERT(minv && maxv); code_.SetValue("SEP", ",\n"); @@ -1022,7 +1033,7 @@ class CppGenerator : public BaseGenerator { code_ += ""; code_ += "};"; - if (parser_.opts.scoped_enums && enum_def.attributes.Lookup("bit_flags")) { + if (opts_.scoped_enums && enum_def.attributes.Lookup("bit_flags")) { code_ += "FLATBUFFERS_DEFINE_BITMASK_OPERATORS({{ENUM_NAME}}, {{BASE_TYPE}})"; } @@ -1076,9 +1087,10 @@ class CppGenerator : public BaseGenerator { code_ += "inline const char *EnumName{{ENUM_NAME}}({{ENUM_NAME}} e) {"; - code_ += " if (e < " + GetEnumValUse(enum_def, *enum_def.MinValue()) + - " || e > " + GetEnumValUse(enum_def, *enum_def.MaxValue()) + - ") return \"\";"; + code_ += " if (flatbuffers::IsOutRange(e, " + + GetEnumValUse(enum_def, *enum_def.MinValue()) + ", " + + GetEnumValUse(enum_def, *enum_def.MaxValue()) + + ")) return \"\";"; code_ += " const size_t index = static_cast(e)\\"; if (enum_def.MinValue()->IsNonZero()) { @@ -1129,7 +1141,7 @@ class CppGenerator : public BaseGenerator { } } - if (parser_.opts.generate_object_based_api && enum_def.is_union) { + if (opts_.generate_object_based_api && enum_def.is_union) { // Generate a union type code_.SetValue("NAME", Name(enum_def)); FLATBUFFERS_ASSERT(enum_def.Lookup("NONE")); @@ -1185,7 +1197,7 @@ class CppGenerator : public BaseGenerator { const auto native_type = NativeName(GetUnionElement(ev, true, true, true), - ev.union_type.struct_def, parser_.opts); + ev.union_type.struct_def, opts_); code_.SetValue("NATIVE_TYPE", native_type); code_.SetValue("NATIVE_NAME", Name(ev)); code_.SetValue("NATIVE_ID", GetEnumValUse(enum_def, ev)); @@ -1204,7 +1216,7 @@ class CppGenerator : public BaseGenerator { code_ += "};"; code_ += ""; - if (parser_.opts.gen_compare) { + if (opts_.gen_compare) { code_ += ""; code_ += "inline bool operator==(const {{NAME}}Union &lhs, const " @@ -1219,7 +1231,7 @@ class CppGenerator : public BaseGenerator { if (ev.IsNonZero()) { const auto native_type = NativeName(GetUnionElement(ev, true, true, true), - ev.union_type.struct_def, parser_.opts); + ev.union_type.struct_def, opts_); code_.SetValue("NATIVE_TYPE", native_type); code_ += " case {{NATIVE_ID}}: {"; code_ += @@ -1318,7 +1330,7 @@ class CppGenerator : public BaseGenerator { code_ += "}"; code_ += ""; - if (parser_.opts.generate_object_based_api) { + if (opts_.generate_object_based_api) { // Generate union Unpack() and Pack() functions. code_ += "inline " + UnionUnPackSignature(enum_def, false) + " {"; code_ += " switch (type) {"; @@ -1358,9 +1370,8 @@ class CppGenerator : public BaseGenerator { if (ev.IsZero()) { continue; } code_.SetValue("LABEL", GetEnumValUse(enum_def, ev)); - code_.SetValue("TYPE", - NativeName(GetUnionElement(ev, true, true, true), - ev.union_type.struct_def, parser_.opts)); + code_.SetValue("TYPE", NativeName(GetUnionElement(ev, true, true, true), + ev.union_type.struct_def, opts_)); code_.SetValue("NAME", GetUnionElement(ev, false, true)); code_ += " case {{LABEL}}: {"; code_ += " auto ptr = reinterpret_cast(value);"; @@ -1394,9 +1405,8 @@ class CppGenerator : public BaseGenerator { const auto &ev = **it; if (ev.IsZero()) { continue; } code_.SetValue("LABEL", GetEnumValUse(enum_def, ev)); - code_.SetValue("TYPE", - NativeName(GetUnionElement(ev, true, true, true), - ev.union_type.struct_def, parser_.opts)); + code_.SetValue("TYPE", NativeName(GetUnionElement(ev, true, true, true), + ev.union_type.struct_def, opts_)); code_ += " case {{LABEL}}: {"; bool copyable = true; if (ev.union_type.base_type == BASE_TYPE_STRUCT) { @@ -1440,9 +1450,8 @@ class CppGenerator : public BaseGenerator { const auto &ev = **it; if (ev.IsZero()) { continue; } code_.SetValue("LABEL", GetEnumValUse(enum_def, ev)); - code_.SetValue("TYPE", - NativeName(GetUnionElement(ev, true, true, true), - ev.union_type.struct_def, parser_.opts)); + code_.SetValue("TYPE", NativeName(GetUnionElement(ev, true, true, true), + ev.union_type.struct_def, opts_)); code_ += " case {{LABEL}}: {"; code_ += " auto ptr = reinterpret_cast<{{TYPE}} *>(value);"; code_ += " delete ptr;"; @@ -1484,7 +1493,7 @@ class CppGenerator : public BaseGenerator { void GenFullyQualifiedNameGetter(const StructDef &struct_def, const std::string &name) { - if (!parser_.opts.generate_name_strings) { return; } + if (!opts_.generate_name_strings) { return; } auto fullname = struct_def.defined_namespace->GetFullyQualifiedName(name); code_.SetValue("NAME", fullname); code_.SetValue("CONSTEXPR", "FLATBUFFERS_CONSTEXPR"); @@ -1619,7 +1628,7 @@ class CppGenerator : public BaseGenerator { } code_.SetValue("NATIVE_NAME", - NativeName(Name(struct_def), &struct_def, parser_.opts)); + NativeName(Name(struct_def), &struct_def, opts_)); code_.SetValue("INIT_LIST", initializer_list); code_ += " {{NATIVE_NAME}}(){{INIT_LIST}} {"; @@ -1689,8 +1698,7 @@ class CppGenerator : public BaseGenerator { } void GenNativeTable(const StructDef &struct_def) { - const auto native_name = - NativeName(Name(struct_def), &struct_def, parser_.opts); + const auto native_name = NativeName(Name(struct_def), &struct_def, opts_); code_.SetValue("STRUCT_NAME", Name(struct_def)); code_.SetValue("NATIVE_NAME", native_name); @@ -1705,7 +1713,7 @@ class CppGenerator : public BaseGenerator { GenOperatorNewDelete(struct_def); GenDefaultConstructor(struct_def); code_ += "};"; - if (parser_.opts.gen_compare) GenCompareOperator(struct_def); + if (opts_.gen_compare) GenCompareOperator(struct_def); code_ += ""; } @@ -1794,7 +1802,7 @@ class CppGenerator : public BaseGenerator { } else { FLATBUFFERS_ASSERT(IsScalar(field.value.type.base_type)); auto type = GenTypeBasic(field.value.type, false); - if (parser_.opts.scoped_enums && field.value.type.enum_def && + if (opts_.scoped_enums && field.value.type.enum_def && IsScalar(field.value.type.base_type)) { type = GenTypeGet(field.value.type, " ", "const ", " *", true); } @@ -1810,7 +1818,7 @@ class CppGenerator : public BaseGenerator { // Generate an accessor struct, builder structs & function for a table. void GenTable(const StructDef &struct_def) { - if (parser_.opts.generate_object_based_api) { GenNativeTable(struct_def); } + if (opts_.generate_object_based_api) { GenNativeTable(struct_def); } // Generate an accessor struct, with methods of the form: // type name() const { return GetField(offset, defaultval); } @@ -1820,10 +1828,10 @@ class CppGenerator : public BaseGenerator { code_ += "struct {{STRUCT_NAME}} FLATBUFFERS_FINAL_CLASS" " : private flatbuffers::Table {"; - if (parser_.opts.generate_object_based_api) { + if (opts_.generate_object_based_api) { code_ += " typedef {{NATIVE_NAME}} NativeTableType;"; } - if (parser_.opts.mini_reflect != IDLOptions::kNone) { + if (opts_.mini_reflect != IDLOptions::kNone) { code_ += " static const flatbuffers::TypeTable *MiniReflectTypeTable() {"; code_ += " return {{STRUCT_NAME}}TypeTable();"; @@ -1931,8 +1939,7 @@ class CppGenerator : public BaseGenerator { } } - if (parser_.opts.mutable_buffer && - !(is_scalar && IsUnion(field.value.type))) { + if (opts_.mutable_buffer && !(is_scalar && IsUnion(field.value.type))) { if (is_scalar) { const auto type = GenTypeWire(field.value.type, "", false); code_.SetValue("SET_FN", "SetField<" + type + ">"); @@ -2014,13 +2021,11 @@ class CppGenerator : public BaseGenerator { code_ += " &&\n verifier.EndTable();"; code_ += " }"; - if (parser_.opts.generate_object_based_api) { + if (opts_.generate_object_based_api) { // Generate the UnPack() pre declaration. - code_ += - " " + TableUnPackSignature(struct_def, true, parser_.opts) + ";"; - code_ += - " " + TableUnPackToSignature(struct_def, true, parser_.opts) + ";"; - code_ += " " + TablePackSignature(struct_def, true, parser_.opts) + ";"; + code_ += " " + TableUnPackSignature(struct_def, true, opts_) + ";"; + code_ += " " + TableUnPackToSignature(struct_def, true, opts_) + ";"; + code_ += " " + TablePackSignature(struct_def, true, opts_) + ";"; } code_ += "};"; // End of table. @@ -2065,10 +2070,10 @@ class CppGenerator : public BaseGenerator { GenBuilders(struct_def); - if (parser_.opts.generate_object_based_api) { + if (opts_.generate_object_based_api) { // Generate a pre-declaration for a CreateX method that works with an // unpacked C++ object. - code_ += TableCreateSignature(struct_def, true, parser_.opts) + ";"; + code_ += TableCreateSignature(struct_def, true, opts_) + ";"; code_ += ""; } } @@ -2292,7 +2297,7 @@ class CppGenerator : public BaseGenerator { } } else { const auto ptype = GenTypeNativePtr( - NativeName(name, type.struct_def, parser_.opts), &afield, true); + NativeName(name, type.struct_def, opts_), &afield, true); return ptype + "(" + val + "->UnPack(_resolver))"; } } @@ -2419,8 +2424,6 @@ class CppGenerator : public BaseGenerator { } std::string GenCreateParam(const FieldDef &field) { - const IDLOptions &opts = parser_.opts; - std::string value = "_o->"; if (field.value.type.base_type == BASE_TYPE_UTYPE) { value += StripUnionType(Name(field)); @@ -2457,7 +2460,7 @@ class CppGenerator : public BaseGenerator { // depending on set_empty_strings_to_null either set it to 0 or an empty string. if (!field.required) { auto empty_value = - opts.set_empty_strings_to_null ? "0" : "_fbb.CreateSharedString(\"\")"; + opts_.set_empty_strings_to_null ? "0" : "_fbb.CreateSharedString(\"\")"; code = value + ".empty() ? " + empty_value + " : " + code; } break; @@ -2562,7 +2565,7 @@ class CppGenerator : public BaseGenerator { // If set_empty_vectors_to_null option is enabled, for optional fields, check to // see if there actually is any data in _o->field before attempting to // access it. - if (opts.set_empty_vectors_to_null && !field.required) { + if (opts_.set_empty_vectors_to_null && !field.required) { code = value + ".size() ? " + code + " : 0"; } break; @@ -2604,20 +2607,20 @@ class CppGenerator : public BaseGenerator { void GenTablePost(const StructDef &struct_def) { code_.SetValue("STRUCT_NAME", Name(struct_def)); code_.SetValue("NATIVE_NAME", - NativeName(Name(struct_def), &struct_def, parser_.opts)); + NativeName(Name(struct_def), &struct_def, opts_)); - if (parser_.opts.generate_object_based_api) { + if (opts_.generate_object_based_api) { // Generate the X::UnPack() method. - code_ += "inline " + - TableUnPackSignature(struct_def, false, parser_.opts) + " {"; + code_ += + "inline " + TableUnPackSignature(struct_def, false, opts_) + " {"; code_ += " auto _o = new {{NATIVE_NAME}}();"; code_ += " UnPackTo(_o, _resolver);"; code_ += " return _o;"; code_ += "}"; code_ += ""; - code_ += "inline " + - TableUnPackToSignature(struct_def, false, parser_.opts) + " {"; + code_ += + "inline " + TableUnPackToSignature(struct_def, false, opts_) + " {"; code_ += " (void)_o;"; code_ += " (void)_resolver;"; @@ -2644,15 +2647,14 @@ class CppGenerator : public BaseGenerator { // Generate the X::Pack member function that simply calls the global // CreateX function. - code_ += "inline " + TablePackSignature(struct_def, false, parser_.opts) + - " {"; + code_ += "inline " + TablePackSignature(struct_def, false, opts_) + " {"; code_ += " return Create{{STRUCT_NAME}}(_fbb, _o, _rehasher);"; code_ += "}"; code_ += ""; // Generate a CreateX method that works with an unpacked C++ object. - code_ += "inline " + - TableCreateSignature(struct_def, false, parser_.opts) + " {"; + code_ += + "inline " + TableCreateSignature(struct_def, false, opts_) + " {"; code_ += " (void)_rehasher;"; code_ += " (void)_o;"; @@ -2660,7 +2662,7 @@ class CppGenerator : public BaseGenerator { " struct _VectorArgs " "{ flatbuffers::FlatBufferBuilder *__fbb; " "const " + - NativeName(Name(struct_def), &struct_def, parser_.opts) + + NativeName(Name(struct_def), &struct_def, opts_) + "* __o; " "const flatbuffers::rehasher_function_t *__rehasher; } _va = { " "&_fbb, _o, _rehasher}; (void)_va;"; @@ -2775,7 +2777,7 @@ class CppGenerator : public BaseGenerator { code_ += " public:"; // Make TypeTable accessible via the generated struct. - if (parser_.opts.mini_reflect != IDLOptions::kNone) { + if (opts_.mini_reflect != IDLOptions::kNone) { code_ += " static const flatbuffers::TypeTable *MiniReflectTypeTable() {"; code_ += " return {{STRUCT_NAME}}TypeTable();"; @@ -2891,7 +2893,7 @@ class CppGenerator : public BaseGenerator { } // Generate a mutable accessor function. - if (parser_.opts.mutable_buffer) { + if (opts_.mutable_buffer) { auto mut_field_type = GenTypeGet(field.value.type, " ", "", IsArray(field.value.type) ? "" : " &", true); @@ -2932,7 +2934,7 @@ class CppGenerator : public BaseGenerator { code_.SetValue("STRUCT_BYTE_SIZE", NumToString(struct_def.bytesize)); code_ += "FLATBUFFERS_STRUCT_END({{STRUCT_NAME}}, {{STRUCT_BYTE_SIZE}});"; - if (parser_.opts.gen_compare) GenCompareOperator(struct_def, "()"); + if (opts_.gen_compare) GenCompareOperator(struct_def, "()"); code_ += ""; } @@ -2974,15 +2976,34 @@ class CppGenerator : public BaseGenerator { cur_name_space_ = ns; } - - const TypedFloatConstantGenerator float_const_gen_; }; } // namespace cpp bool GenerateCPP(const Parser &parser, const std::string &path, const std::string &file_name) { - cpp::CppGenerator generator(parser, path, file_name); + cpp::IDLOptionsCpp opts(parser.opts); + // The '--cpp_std' argument could be extended (like ASAN): + // Example: "flatc --cpp_std c++17:option1:option2". + if (opts.cpp_std.empty() || opts.cpp_std == "c++") { + // Use a `standard` code generator. + } else if (opts.cpp_std == "c++17") { + opts.g_cpp_std = 17; + // With c++17 generate strong enums only. + opts.scoped_enums = true; + opts.prefixed_enums = false; + } else if (opts.cpp_std == "legacy") { + opts.g_cpp_std = -1; + opts.g_only_fixed_enums = false; + } else { + FlatCompilerLogger::Error("Unknown value of the '--cpp-std' switch: " + + opts.cpp_std); + return false; + } + // The opts.scoped_enums has priority. + opts.g_only_fixed_enums |= opts.scoped_enums; + + cpp::CppGenerator generator(parser, path, file_name, opts); return generator.generate(); } diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 163d111b358..e082352b90f 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -627,12 +627,6 @@ CheckedError Parser::ParseType(Type &type) { "length of fixed-length array must be positive and fit to " "uint16_t type"); } - // Check if enum arrays are used in C++ without specifying --scoped-enums - if ((opts.lang_to_generate & IDLOptions::kCpp) && !opts.scoped_enums && - IsEnum(subtype)) { - return Error( - "--scoped-enums must be enabled to use enum arrays in C++\n"); - } type = Type(BASE_TYPE_ARRAY, subtype.struct_def, subtype.enum_def, fixed_length); NEXT(); diff --git a/tests/arrays_test_generated.h b/tests/arrays_test_generated.h index 9fc0d2f2ff4..746ff9c72c3 100644 --- a/tests/arrays_test_generated.h +++ b/tests/arrays_test_generated.h @@ -57,7 +57,7 @@ inline const char * const *EnumNamesTestEnum() { } inline const char *EnumNameTestEnum(TestEnum e) { - if (e < TestEnum::A || e > TestEnum::C) return ""; + if (flatbuffers::IsOutRange(e, TestEnum::A, TestEnum::C)) return ""; const size_t index = static_cast(e); return EnumNamesTestEnum()[index]; } diff --git a/tests/cpp17/generated_cpp17/monster_test_generated.h b/tests/cpp17/generated_cpp17/monster_test_generated.h new file mode 100644 index 00000000000..a77ddca1bc1 --- /dev/null +++ b/tests/cpp17/generated_cpp17/monster_test_generated.h @@ -0,0 +1,3296 @@ +// automatically generated by the FlatBuffers compiler, do not modify + + +#ifndef FLATBUFFERS_GENERATED_MONSTERTEST_MYGAME_EXAMPLE_H_ +#define FLATBUFFERS_GENERATED_MONSTERTEST_MYGAME_EXAMPLE_H_ + +#include "flatbuffers/flatbuffers.h" +#include "flatbuffers/flexbuffers.h" + +namespace MyGame { + +struct InParentNamespace; +struct InParentNamespaceT; + +namespace Example2 { + +struct Monster; +struct MonsterT; + +} // namespace Example2 + +namespace Example { + +struct Test; + +struct TestSimpleTableWithEnum; +struct TestSimpleTableWithEnumT; + +struct Vec3; + +struct Ability; + +struct Stat; +struct StatT; + +struct Referrable; +struct ReferrableT; + +struct Monster; +struct MonsterT; + +struct TypeAliases; +struct TypeAliasesT; + +} // namespace Example + +inline const flatbuffers::TypeTable *InParentNamespaceTypeTable(); + +namespace Example2 { + +inline const flatbuffers::TypeTable *MonsterTypeTable(); + +} // namespace Example2 + +namespace Example { + +inline const flatbuffers::TypeTable *TestTypeTable(); + +inline const flatbuffers::TypeTable *TestSimpleTableWithEnumTypeTable(); + +inline const flatbuffers::TypeTable *Vec3TypeTable(); + +inline const flatbuffers::TypeTable *AbilityTypeTable(); + +inline const flatbuffers::TypeTable *StatTypeTable(); + +inline const flatbuffers::TypeTable *ReferrableTypeTable(); + +inline const flatbuffers::TypeTable *MonsterTypeTable(); + +inline const flatbuffers::TypeTable *TypeAliasesTypeTable(); + +/// Composite components of Monster color. +enum class Color : uint8_t { + Red = 1, + /// \brief color Green + /// Green is bit_flag with value (1u << 1) + Green = 2, + /// \brief color Blue (1u << 3) + Blue = 8, + NONE = 0, + ANY = 11 +}; +FLATBUFFERS_DEFINE_BITMASK_OPERATORS(Color, uint8_t) + +inline const Color (&EnumValuesColor())[3] { + static const Color values[] = { + Color::Red, + Color::Green, + Color::Blue + }; + return values; +} + +inline const char * const *EnumNamesColor() { + static const char * const names[9] = { + "Red", + "Green", + "", + "", + "", + "", + "", + "Blue", + nullptr + }; + return names; +} + +inline const char *EnumNameColor(Color e) { + if (flatbuffers::IsOutRange(e, Color::Red, Color::Blue)) return ""; + const size_t index = static_cast(e) - static_cast(Color::Red); + return EnumNamesColor()[index]; +} + +enum class Race : int8_t { + None = -1, + Human = 0, + Dwarf = 1, + Elf = 2, + MIN = None, + MAX = Elf +}; + +inline const Race (&EnumValuesRace())[4] { + static const Race values[] = { + Race::None, + Race::Human, + Race::Dwarf, + Race::Elf + }; + return values; +} + +inline const char * const *EnumNamesRace() { + static const char * const names[5] = { + "None", + "Human", + "Dwarf", + "Elf", + nullptr + }; + return names; +} + +inline const char *EnumNameRace(Race e) { + if (flatbuffers::IsOutRange(e, Race::None, Race::Elf)) return ""; + const size_t index = static_cast(e) - static_cast(Race::None); + return EnumNamesRace()[index]; +} + +enum class Any : uint8_t { + NONE = 0, + Monster = 1, + TestSimpleTableWithEnum = 2, + MyGame_Example2_Monster = 3, + MIN = NONE, + MAX = MyGame_Example2_Monster +}; + +inline const Any (&EnumValuesAny())[4] { + static const Any values[] = { + Any::NONE, + Any::Monster, + Any::TestSimpleTableWithEnum, + Any::MyGame_Example2_Monster + }; + return values; +} + +inline const char * const *EnumNamesAny() { + static const char * const names[5] = { + "NONE", + "Monster", + "TestSimpleTableWithEnum", + "MyGame_Example2_Monster", + nullptr + }; + return names; +} + +inline const char *EnumNameAny(Any e) { + if (flatbuffers::IsOutRange(e, Any::NONE, Any::MyGame_Example2_Monster)) return ""; + const size_t index = static_cast(e); + return EnumNamesAny()[index]; +} + +template struct AnyTraits { + static const Any enum_value = Any::NONE; +}; + +template<> struct AnyTraits { + static const Any enum_value = Any::Monster; +}; + +template<> struct AnyTraits { + static const Any enum_value = Any::TestSimpleTableWithEnum; +}; + +template<> struct AnyTraits { + static const Any enum_value = Any::MyGame_Example2_Monster; +}; + +struct AnyUnion { + Any type; + void *value; + + AnyUnion() : type(Any::NONE), value(nullptr) {} + AnyUnion(AnyUnion&& u) FLATBUFFERS_NOEXCEPT : + type(Any::NONE), value(nullptr) + { std::swap(type, u.type); std::swap(value, u.value); } + AnyUnion(const AnyUnion &) FLATBUFFERS_NOEXCEPT; + AnyUnion &operator=(const AnyUnion &u) FLATBUFFERS_NOEXCEPT + { AnyUnion t(u); std::swap(type, t.type); std::swap(value, t.value); return *this; } + AnyUnion &operator=(AnyUnion &&u) FLATBUFFERS_NOEXCEPT + { std::swap(type, u.type); std::swap(value, u.value); return *this; } + ~AnyUnion() { Reset(); } + + void Reset(); + +#ifndef FLATBUFFERS_CPP98_STL + template + void Set(T&& val) { + using RT = typename std::remove_reference::type; + Reset(); + type = AnyTraits::enum_value; + if (type != Any::NONE) { + value = new RT(std::forward(val)); + } + } +#endif // FLATBUFFERS_CPP98_STL + + static void *UnPack(const void *obj, Any type, const flatbuffers::resolver_function_t *resolver); + flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *_rehasher = nullptr) const; + + MyGame::Example::MonsterT *AsMonster() { + return type == Any::Monster ? + reinterpret_cast(value) : nullptr; + } + const MyGame::Example::MonsterT *AsMonster() const { + return type == Any::Monster ? + reinterpret_cast(value) : nullptr; + } + MyGame::Example::TestSimpleTableWithEnumT *AsTestSimpleTableWithEnum() { + return type == Any::TestSimpleTableWithEnum ? + reinterpret_cast(value) : nullptr; + } + const MyGame::Example::TestSimpleTableWithEnumT *AsTestSimpleTableWithEnum() const { + return type == Any::TestSimpleTableWithEnum ? + reinterpret_cast(value) : nullptr; + } + MyGame::Example2::MonsterT *AsMyGame_Example2_Monster() { + return type == Any::MyGame_Example2_Monster ? + reinterpret_cast(value) : nullptr; + } + const MyGame::Example2::MonsterT *AsMyGame_Example2_Monster() const { + return type == Any::MyGame_Example2_Monster ? + reinterpret_cast(value) : nullptr; + } +}; + +bool VerifyAny(flatbuffers::Verifier &verifier, const void *obj, Any type); +bool VerifyAnyVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types); + +enum class AnyUniqueAliases : uint8_t { + NONE = 0, + M = 1, + TS = 2, + M2 = 3, + MIN = NONE, + MAX = M2 +}; + +inline const AnyUniqueAliases (&EnumValuesAnyUniqueAliases())[4] { + static const AnyUniqueAliases values[] = { + AnyUniqueAliases::NONE, + AnyUniqueAliases::M, + AnyUniqueAliases::TS, + AnyUniqueAliases::M2 + }; + return values; +} + +inline const char * const *EnumNamesAnyUniqueAliases() { + static const char * const names[5] = { + "NONE", + "M", + "TS", + "M2", + nullptr + }; + return names; +} + +inline const char *EnumNameAnyUniqueAliases(AnyUniqueAliases e) { + if (flatbuffers::IsOutRange(e, AnyUniqueAliases::NONE, AnyUniqueAliases::M2)) return ""; + const size_t index = static_cast(e); + return EnumNamesAnyUniqueAliases()[index]; +} + +template struct AnyUniqueAliasesTraits { + static const AnyUniqueAliases enum_value = AnyUniqueAliases::NONE; +}; + +template<> struct AnyUniqueAliasesTraits { + static const AnyUniqueAliases enum_value = AnyUniqueAliases::M; +}; + +template<> struct AnyUniqueAliasesTraits { + static const AnyUniqueAliases enum_value = AnyUniqueAliases::TS; +}; + +template<> struct AnyUniqueAliasesTraits { + static const AnyUniqueAliases enum_value = AnyUniqueAliases::M2; +}; + +struct AnyUniqueAliasesUnion { + AnyUniqueAliases type; + void *value; + + AnyUniqueAliasesUnion() : type(AnyUniqueAliases::NONE), value(nullptr) {} + AnyUniqueAliasesUnion(AnyUniqueAliasesUnion&& u) FLATBUFFERS_NOEXCEPT : + type(AnyUniqueAliases::NONE), value(nullptr) + { std::swap(type, u.type); std::swap(value, u.value); } + AnyUniqueAliasesUnion(const AnyUniqueAliasesUnion &) FLATBUFFERS_NOEXCEPT; + AnyUniqueAliasesUnion &operator=(const AnyUniqueAliasesUnion &u) FLATBUFFERS_NOEXCEPT + { AnyUniqueAliasesUnion t(u); std::swap(type, t.type); std::swap(value, t.value); return *this; } + AnyUniqueAliasesUnion &operator=(AnyUniqueAliasesUnion &&u) FLATBUFFERS_NOEXCEPT + { std::swap(type, u.type); std::swap(value, u.value); return *this; } + ~AnyUniqueAliasesUnion() { Reset(); } + + void Reset(); + +#ifndef FLATBUFFERS_CPP98_STL + template + void Set(T&& val) { + using RT = typename std::remove_reference::type; + Reset(); + type = AnyUniqueAliasesTraits::enum_value; + if (type != AnyUniqueAliases::NONE) { + value = new RT(std::forward(val)); + } + } +#endif // FLATBUFFERS_CPP98_STL + + static void *UnPack(const void *obj, AnyUniqueAliases type, const flatbuffers::resolver_function_t *resolver); + flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *_rehasher = nullptr) const; + + MyGame::Example::MonsterT *AsM() { + return type == AnyUniqueAliases::M ? + reinterpret_cast(value) : nullptr; + } + const MyGame::Example::MonsterT *AsM() const { + return type == AnyUniqueAliases::M ? + reinterpret_cast(value) : nullptr; + } + MyGame::Example::TestSimpleTableWithEnumT *AsTS() { + return type == AnyUniqueAliases::TS ? + reinterpret_cast(value) : nullptr; + } + const MyGame::Example::TestSimpleTableWithEnumT *AsTS() const { + return type == AnyUniqueAliases::TS ? + reinterpret_cast(value) : nullptr; + } + MyGame::Example2::MonsterT *AsM2() { + return type == AnyUniqueAliases::M2 ? + reinterpret_cast(value) : nullptr; + } + const MyGame::Example2::MonsterT *AsM2() const { + return type == AnyUniqueAliases::M2 ? + reinterpret_cast(value) : nullptr; + } +}; + +bool VerifyAnyUniqueAliases(flatbuffers::Verifier &verifier, const void *obj, AnyUniqueAliases type); +bool VerifyAnyUniqueAliasesVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types); + +enum class AnyAmbiguousAliases : uint8_t { + NONE = 0, + M1 = 1, + M2 = 2, + M3 = 3, + MIN = NONE, + MAX = M3 +}; + +inline const AnyAmbiguousAliases (&EnumValuesAnyAmbiguousAliases())[4] { + static const AnyAmbiguousAliases values[] = { + AnyAmbiguousAliases::NONE, + AnyAmbiguousAliases::M1, + AnyAmbiguousAliases::M2, + AnyAmbiguousAliases::M3 + }; + return values; +} + +inline const char * const *EnumNamesAnyAmbiguousAliases() { + static const char * const names[5] = { + "NONE", + "M1", + "M2", + "M3", + nullptr + }; + return names; +} + +inline const char *EnumNameAnyAmbiguousAliases(AnyAmbiguousAliases e) { + if (flatbuffers::IsOutRange(e, AnyAmbiguousAliases::NONE, AnyAmbiguousAliases::M3)) return ""; + const size_t index = static_cast(e); + return EnumNamesAnyAmbiguousAliases()[index]; +} + +struct AnyAmbiguousAliasesUnion { + AnyAmbiguousAliases type; + void *value; + + AnyAmbiguousAliasesUnion() : type(AnyAmbiguousAliases::NONE), value(nullptr) {} + AnyAmbiguousAliasesUnion(AnyAmbiguousAliasesUnion&& u) FLATBUFFERS_NOEXCEPT : + type(AnyAmbiguousAliases::NONE), value(nullptr) + { std::swap(type, u.type); std::swap(value, u.value); } + AnyAmbiguousAliasesUnion(const AnyAmbiguousAliasesUnion &) FLATBUFFERS_NOEXCEPT; + AnyAmbiguousAliasesUnion &operator=(const AnyAmbiguousAliasesUnion &u) FLATBUFFERS_NOEXCEPT + { AnyAmbiguousAliasesUnion t(u); std::swap(type, t.type); std::swap(value, t.value); return *this; } + AnyAmbiguousAliasesUnion &operator=(AnyAmbiguousAliasesUnion &&u) FLATBUFFERS_NOEXCEPT + { std::swap(type, u.type); std::swap(value, u.value); return *this; } + ~AnyAmbiguousAliasesUnion() { Reset(); } + + void Reset(); + + static void *UnPack(const void *obj, AnyAmbiguousAliases type, const flatbuffers::resolver_function_t *resolver); + flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *_rehasher = nullptr) const; + + MyGame::Example::MonsterT *AsM1() { + return type == AnyAmbiguousAliases::M1 ? + reinterpret_cast(value) : nullptr; + } + const MyGame::Example::MonsterT *AsM1() const { + return type == AnyAmbiguousAliases::M1 ? + reinterpret_cast(value) : nullptr; + } + MyGame::Example::MonsterT *AsM2() { + return type == AnyAmbiguousAliases::M2 ? + reinterpret_cast(value) : nullptr; + } + const MyGame::Example::MonsterT *AsM2() const { + return type == AnyAmbiguousAliases::M2 ? + reinterpret_cast(value) : nullptr; + } + MyGame::Example::MonsterT *AsM3() { + return type == AnyAmbiguousAliases::M3 ? + reinterpret_cast(value) : nullptr; + } + const MyGame::Example::MonsterT *AsM3() const { + return type == AnyAmbiguousAliases::M3 ? + reinterpret_cast(value) : nullptr; + } +}; + +bool VerifyAnyAmbiguousAliases(flatbuffers::Verifier &verifier, const void *obj, AnyAmbiguousAliases type); +bool VerifyAnyAmbiguousAliasesVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types); + +FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(2) Test FLATBUFFERS_FINAL_CLASS { + private: + int16_t a_; + int8_t b_; + int8_t padding0__; + + public: + static const flatbuffers::TypeTable *MiniReflectTypeTable() { + return TestTypeTable(); + } + Test() { + memset(static_cast(this), 0, sizeof(Test)); + } + Test(int16_t _a, int8_t _b) + : a_(flatbuffers::EndianScalar(_a)), + b_(flatbuffers::EndianScalar(_b)), + padding0__(0) { + (void)padding0__; + } + int16_t a() const { + return flatbuffers::EndianScalar(a_); + } + void mutate_a(int16_t _a) { + flatbuffers::WriteScalar(&a_, _a); + } + int8_t b() const { + return flatbuffers::EndianScalar(b_); + } + void mutate_b(int8_t _b) { + flatbuffers::WriteScalar(&b_, _b); + } +}; +FLATBUFFERS_STRUCT_END(Test, 4); + +FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Vec3 FLATBUFFERS_FINAL_CLASS { + private: + float x_; + float y_; + float z_; + int32_t padding0__; + double test1_; + uint8_t test2_; + int8_t padding1__; + MyGame::Example::Test test3_; + int16_t padding2__; + + public: + static const flatbuffers::TypeTable *MiniReflectTypeTable() { + return Vec3TypeTable(); + } + Vec3() { + memset(static_cast(this), 0, sizeof(Vec3)); + } + Vec3(float _x, float _y, float _z, double _test1, MyGame::Example::Color _test2, const MyGame::Example::Test &_test3) + : x_(flatbuffers::EndianScalar(_x)), + y_(flatbuffers::EndianScalar(_y)), + z_(flatbuffers::EndianScalar(_z)), + padding0__(0), + test1_(flatbuffers::EndianScalar(_test1)), + test2_(flatbuffers::EndianScalar(static_cast(_test2))), + padding1__(0), + test3_(_test3), + padding2__(0) { + (void)padding0__; + (void)padding1__; + (void)padding2__; + } + float x() const { + return flatbuffers::EndianScalar(x_); + } + void mutate_x(float _x) { + flatbuffers::WriteScalar(&x_, _x); + } + float y() const { + return flatbuffers::EndianScalar(y_); + } + void mutate_y(float _y) { + flatbuffers::WriteScalar(&y_, _y); + } + float z() const { + return flatbuffers::EndianScalar(z_); + } + void mutate_z(float _z) { + flatbuffers::WriteScalar(&z_, _z); + } + double test1() const { + return flatbuffers::EndianScalar(test1_); + } + void mutate_test1(double _test1) { + flatbuffers::WriteScalar(&test1_, _test1); + } + MyGame::Example::Color test2() const { + return static_cast(flatbuffers::EndianScalar(test2_)); + } + void mutate_test2(MyGame::Example::Color _test2) { + flatbuffers::WriteScalar(&test2_, static_cast(_test2)); + } + const MyGame::Example::Test &test3() const { + return test3_; + } + MyGame::Example::Test &mutable_test3() { + return test3_; + } +}; +FLATBUFFERS_STRUCT_END(Vec3, 32); + +FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Ability FLATBUFFERS_FINAL_CLASS { + private: + uint32_t id_; + uint32_t distance_; + + public: + static const flatbuffers::TypeTable *MiniReflectTypeTable() { + return AbilityTypeTable(); + } + Ability() { + memset(static_cast(this), 0, sizeof(Ability)); + } + Ability(uint32_t _id, uint32_t _distance) + : id_(flatbuffers::EndianScalar(_id)), + distance_(flatbuffers::EndianScalar(_distance)) { + } + uint32_t id() const { + return flatbuffers::EndianScalar(id_); + } + void mutate_id(uint32_t _id) { + flatbuffers::WriteScalar(&id_, _id); + } + bool KeyCompareLessThan(const Ability *o) const { + return id() < o->id(); + } + int KeyCompareWithValue(uint32_t val) const { + return static_cast(id() > val) - static_cast(id() < val); + } + uint32_t distance() const { + return flatbuffers::EndianScalar(distance_); + } + void mutate_distance(uint32_t _distance) { + flatbuffers::WriteScalar(&distance_, _distance); + } +}; +FLATBUFFERS_STRUCT_END(Ability, 8); + +} // namespace Example + +struct InParentNamespaceT : public flatbuffers::NativeTable { + typedef InParentNamespace TableType; + InParentNamespaceT() { + } +}; + +struct InParentNamespace FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { + typedef InParentNamespaceT NativeTableType; + static const flatbuffers::TypeTable *MiniReflectTypeTable() { + return InParentNamespaceTypeTable(); + } + bool Verify(flatbuffers::Verifier &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + InParentNamespaceT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(InParentNamespaceT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; + static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const InParentNamespaceT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct InParentNamespaceBuilder { + flatbuffers::FlatBufferBuilder &fbb_; + flatbuffers::uoffset_t start_; + explicit InParentNamespaceBuilder(flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + InParentNamespaceBuilder &operator=(const InParentNamespaceBuilder &); + flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = flatbuffers::Offset(end); + return o; + } +}; + +inline flatbuffers::Offset CreateInParentNamespace( + flatbuffers::FlatBufferBuilder &_fbb) { + InParentNamespaceBuilder builder_(_fbb); + return builder_.Finish(); +} + +flatbuffers::Offset CreateInParentNamespace(flatbuffers::FlatBufferBuilder &_fbb, const InParentNamespaceT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); + +namespace Example2 { + +struct MonsterT : public flatbuffers::NativeTable { + typedef Monster TableType; + MonsterT() { + } +}; + +struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { + typedef MonsterT NativeTableType; + static const flatbuffers::TypeTable *MiniReflectTypeTable() { + return MonsterTypeTable(); + } + bool Verify(flatbuffers::Verifier &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } + MonsterT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(MonsterT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; + static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct MonsterBuilder { + flatbuffers::FlatBufferBuilder &fbb_; + flatbuffers::uoffset_t start_; + explicit MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + MonsterBuilder &operator=(const MonsterBuilder &); + flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = flatbuffers::Offset(end); + return o; + } +}; + +inline flatbuffers::Offset CreateMonster( + flatbuffers::FlatBufferBuilder &_fbb) { + MonsterBuilder builder_(_fbb); + return builder_.Finish(); +} + +flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); + +} // namespace Example2 + +namespace Example { + +struct TestSimpleTableWithEnumT : public flatbuffers::NativeTable { + typedef TestSimpleTableWithEnum TableType; + MyGame::Example::Color color; + TestSimpleTableWithEnumT() + : color(MyGame::Example::Color::Green) { + } +}; + +struct TestSimpleTableWithEnum FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { + typedef TestSimpleTableWithEnumT NativeTableType; + static const flatbuffers::TypeTable *MiniReflectTypeTable() { + return TestSimpleTableWithEnumTypeTable(); + } + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_COLOR = 4 + }; + MyGame::Example::Color color() const { + return static_cast(GetField(VT_COLOR, 2)); + } + bool mutate_color(MyGame::Example::Color _color) { + return SetField(VT_COLOR, static_cast(_color), 2); + } + bool Verify(flatbuffers::Verifier &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_COLOR) && + verifier.EndTable(); + } + TestSimpleTableWithEnumT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(TestSimpleTableWithEnumT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; + static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const TestSimpleTableWithEnumT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct TestSimpleTableWithEnumBuilder { + flatbuffers::FlatBufferBuilder &fbb_; + flatbuffers::uoffset_t start_; + void add_color(MyGame::Example::Color color) { + fbb_.AddElement(TestSimpleTableWithEnum::VT_COLOR, static_cast(color), 2); + } + explicit TestSimpleTableWithEnumBuilder(flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + TestSimpleTableWithEnumBuilder &operator=(const TestSimpleTableWithEnumBuilder &); + flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = flatbuffers::Offset(end); + return o; + } +}; + +inline flatbuffers::Offset CreateTestSimpleTableWithEnum( + flatbuffers::FlatBufferBuilder &_fbb, + MyGame::Example::Color color = MyGame::Example::Color::Green) { + TestSimpleTableWithEnumBuilder builder_(_fbb); + builder_.add_color(color); + return builder_.Finish(); +} + +flatbuffers::Offset CreateTestSimpleTableWithEnum(flatbuffers::FlatBufferBuilder &_fbb, const TestSimpleTableWithEnumT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct StatT : public flatbuffers::NativeTable { + typedef Stat TableType; + std::string id; + int64_t val; + uint16_t count; + StatT() + : val(0), + count(0) { + } +}; + +struct Stat FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { + typedef StatT NativeTableType; + static const flatbuffers::TypeTable *MiniReflectTypeTable() { + return StatTypeTable(); + } + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_ID = 4, + VT_VAL = 6, + VT_COUNT = 8 + }; + const flatbuffers::String *id() const { + return GetPointer(VT_ID); + } + flatbuffers::String *mutable_id() { + return GetPointer(VT_ID); + } + int64_t val() const { + return GetField(VT_VAL, 0); + } + bool mutate_val(int64_t _val) { + return SetField(VT_VAL, _val, 0); + } + uint16_t count() const { + return GetField(VT_COUNT, 0); + } + bool mutate_count(uint16_t _count) { + return SetField(VT_COUNT, _count, 0); + } + bool Verify(flatbuffers::Verifier &verifier) const { + return VerifyTableStart(verifier) && + VerifyOffset(verifier, VT_ID) && + verifier.VerifyString(id()) && + VerifyField(verifier, VT_VAL) && + VerifyField(verifier, VT_COUNT) && + verifier.EndTable(); + } + StatT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(StatT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; + static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const StatT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct StatBuilder { + flatbuffers::FlatBufferBuilder &fbb_; + flatbuffers::uoffset_t start_; + void add_id(flatbuffers::Offset id) { + fbb_.AddOffset(Stat::VT_ID, id); + } + void add_val(int64_t val) { + fbb_.AddElement(Stat::VT_VAL, val, 0); + } + void add_count(uint16_t count) { + fbb_.AddElement(Stat::VT_COUNT, count, 0); + } + explicit StatBuilder(flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + StatBuilder &operator=(const StatBuilder &); + flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = flatbuffers::Offset(end); + return o; + } +}; + +inline flatbuffers::Offset CreateStat( + flatbuffers::FlatBufferBuilder &_fbb, + flatbuffers::Offset id = 0, + int64_t val = 0, + uint16_t count = 0) { + StatBuilder builder_(_fbb); + builder_.add_val(val); + builder_.add_id(id); + builder_.add_count(count); + return builder_.Finish(); +} + +inline flatbuffers::Offset CreateStatDirect( + flatbuffers::FlatBufferBuilder &_fbb, + const char *id = nullptr, + int64_t val = 0, + uint16_t count = 0) { + auto id__ = id ? _fbb.CreateString(id) : 0; + return MyGame::Example::CreateStat( + _fbb, + id__, + val, + count); +} + +flatbuffers::Offset CreateStat(flatbuffers::FlatBufferBuilder &_fbb, const StatT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct ReferrableT : public flatbuffers::NativeTable { + typedef Referrable TableType; + uint64_t id; + ReferrableT() + : id(0) { + } +}; + +struct Referrable FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { + typedef ReferrableT NativeTableType; + static const flatbuffers::TypeTable *MiniReflectTypeTable() { + return ReferrableTypeTable(); + } + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_ID = 4 + }; + uint64_t id() const { + return GetField(VT_ID, 0); + } + bool mutate_id(uint64_t _id) { + return SetField(VT_ID, _id, 0); + } + bool KeyCompareLessThan(const Referrable *o) const { + return id() < o->id(); + } + int KeyCompareWithValue(uint64_t val) const { + return static_cast(id() > val) - static_cast(id() < val); + } + bool Verify(flatbuffers::Verifier &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_ID) && + verifier.EndTable(); + } + ReferrableT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(ReferrableT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; + static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const ReferrableT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct ReferrableBuilder { + flatbuffers::FlatBufferBuilder &fbb_; + flatbuffers::uoffset_t start_; + void add_id(uint64_t id) { + fbb_.AddElement(Referrable::VT_ID, id, 0); + } + explicit ReferrableBuilder(flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + ReferrableBuilder &operator=(const ReferrableBuilder &); + flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = flatbuffers::Offset(end); + return o; + } +}; + +inline flatbuffers::Offset CreateReferrable( + flatbuffers::FlatBufferBuilder &_fbb, + uint64_t id = 0) { + ReferrableBuilder builder_(_fbb); + builder_.add_id(id); + return builder_.Finish(); +} + +flatbuffers::Offset CreateReferrable(flatbuffers::FlatBufferBuilder &_fbb, const ReferrableT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct MonsterT : public flatbuffers::NativeTable { + typedef Monster TableType; + std::unique_ptr pos; + int16_t mana; + int16_t hp; + std::string name; + std::vector inventory; + MyGame::Example::Color color; + AnyUnion test; + std::vector test4; + std::vector testarrayofstring; + std::vector> testarrayoftables; + std::unique_ptr enemy; + std::vector testnestedflatbuffer; + std::unique_ptr testempty; + bool testbool; + int32_t testhashs32_fnv1; + uint32_t testhashu32_fnv1; + int64_t testhashs64_fnv1; + uint64_t testhashu64_fnv1; + int32_t testhashs32_fnv1a; + Stat *testhashu32_fnv1a; + int64_t testhashs64_fnv1a; + uint64_t testhashu64_fnv1a; + std::vector testarrayofbools; + float testf; + float testf2; + float testf3; + std::vector testarrayofstring2; + std::vector testarrayofsortedstruct; + std::vector flex; + std::vector test5; + std::vector vector_of_longs; + std::vector vector_of_doubles; + std::unique_ptr parent_namespace_test; + std::vector> vector_of_referrables; + ReferrableT *single_weak_reference; + std::vector vector_of_weak_references; + std::vector> vector_of_strong_referrables; + ReferrableT *co_owning_reference; + std::vector> vector_of_co_owning_references; + ReferrableT *non_owning_reference; + std::vector vector_of_non_owning_references; + AnyUniqueAliasesUnion any_unique; + AnyAmbiguousAliasesUnion any_ambiguous; + std::vector vector_of_enums; + MyGame::Example::Race signed_enum; + MonsterT() + : mana(150), + hp(100), + color(MyGame::Example::Color::Blue), + testbool(false), + testhashs32_fnv1(0), + testhashu32_fnv1(0), + testhashs64_fnv1(0), + testhashu64_fnv1(0), + testhashs32_fnv1a(0), + testhashu32_fnv1a(nullptr), + testhashs64_fnv1a(0), + testhashu64_fnv1a(0), + testf(3.14159f), + testf2(3.0f), + testf3(0.0f), + single_weak_reference(nullptr), + co_owning_reference(nullptr), + non_owning_reference(nullptr), + signed_enum(MyGame::Example::Race::None) { + } +}; + +/// an example documentation comment: monster object +struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { + typedef MonsterT NativeTableType; + static const flatbuffers::TypeTable *MiniReflectTypeTable() { + return MonsterTypeTable(); + } + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_POS = 4, + VT_MANA = 6, + VT_HP = 8, + VT_NAME = 10, + VT_INVENTORY = 14, + VT_COLOR = 16, + VT_TEST_TYPE = 18, + VT_TEST = 20, + VT_TEST4 = 22, + VT_TESTARRAYOFSTRING = 24, + VT_TESTARRAYOFTABLES = 26, + VT_ENEMY = 28, + VT_TESTNESTEDFLATBUFFER = 30, + VT_TESTEMPTY = 32, + VT_TESTBOOL = 34, + VT_TESTHASHS32_FNV1 = 36, + VT_TESTHASHU32_FNV1 = 38, + VT_TESTHASHS64_FNV1 = 40, + VT_TESTHASHU64_FNV1 = 42, + VT_TESTHASHS32_FNV1A = 44, + VT_TESTHASHU32_FNV1A = 46, + VT_TESTHASHS64_FNV1A = 48, + VT_TESTHASHU64_FNV1A = 50, + VT_TESTARRAYOFBOOLS = 52, + VT_TESTF = 54, + VT_TESTF2 = 56, + VT_TESTF3 = 58, + VT_TESTARRAYOFSTRING2 = 60, + VT_TESTARRAYOFSORTEDSTRUCT = 62, + VT_FLEX = 64, + VT_TEST5 = 66, + VT_VECTOR_OF_LONGS = 68, + VT_VECTOR_OF_DOUBLES = 70, + VT_PARENT_NAMESPACE_TEST = 72, + VT_VECTOR_OF_REFERRABLES = 74, + VT_SINGLE_WEAK_REFERENCE = 76, + VT_VECTOR_OF_WEAK_REFERENCES = 78, + VT_VECTOR_OF_STRONG_REFERRABLES = 80, + VT_CO_OWNING_REFERENCE = 82, + VT_VECTOR_OF_CO_OWNING_REFERENCES = 84, + VT_NON_OWNING_REFERENCE = 86, + VT_VECTOR_OF_NON_OWNING_REFERENCES = 88, + VT_ANY_UNIQUE_TYPE = 90, + VT_ANY_UNIQUE = 92, + VT_ANY_AMBIGUOUS_TYPE = 94, + VT_ANY_AMBIGUOUS = 96, + VT_VECTOR_OF_ENUMS = 98, + VT_SIGNED_ENUM = 100 + }; + const MyGame::Example::Vec3 *pos() const { + return GetStruct(VT_POS); + } + MyGame::Example::Vec3 *mutable_pos() { + return GetStruct(VT_POS); + } + int16_t mana() const { + return GetField(VT_MANA, 150); + } + bool mutate_mana(int16_t _mana) { + return SetField(VT_MANA, _mana, 150); + } + int16_t hp() const { + return GetField(VT_HP, 100); + } + bool mutate_hp(int16_t _hp) { + return SetField(VT_HP, _hp, 100); + } + const flatbuffers::String *name() const { + return GetPointer(VT_NAME); + } + flatbuffers::String *mutable_name() { + return GetPointer(VT_NAME); + } + bool KeyCompareLessThan(const Monster *o) const { + return *name() < *o->name(); + } + int KeyCompareWithValue(const char *val) const { + return strcmp(name()->c_str(), val); + } + const flatbuffers::Vector *inventory() const { + return GetPointer *>(VT_INVENTORY); + } + flatbuffers::Vector *mutable_inventory() { + return GetPointer *>(VT_INVENTORY); + } + MyGame::Example::Color color() const { + return static_cast(GetField(VT_COLOR, 8)); + } + bool mutate_color(MyGame::Example::Color _color) { + return SetField(VT_COLOR, static_cast(_color), 8); + } + MyGame::Example::Any test_type() const { + return static_cast(GetField(VT_TEST_TYPE, 0)); + } + const void *test() const { + return GetPointer(VT_TEST); + } + template const T *test_as() const; + const MyGame::Example::Monster *test_as_Monster() const { + return test_type() == MyGame::Example::Any::Monster ? static_cast(test()) : nullptr; + } + const MyGame::Example::TestSimpleTableWithEnum *test_as_TestSimpleTableWithEnum() const { + return test_type() == MyGame::Example::Any::TestSimpleTableWithEnum ? static_cast(test()) : nullptr; + } + const MyGame::Example2::Monster *test_as_MyGame_Example2_Monster() const { + return test_type() == MyGame::Example::Any::MyGame_Example2_Monster ? static_cast(test()) : nullptr; + } + void *mutable_test() { + return GetPointer(VT_TEST); + } + const flatbuffers::Vector *test4() const { + return GetPointer *>(VT_TEST4); + } + flatbuffers::Vector *mutable_test4() { + return GetPointer *>(VT_TEST4); + } + const flatbuffers::Vector> *testarrayofstring() const { + return GetPointer> *>(VT_TESTARRAYOFSTRING); + } + flatbuffers::Vector> *mutable_testarrayofstring() { + return GetPointer> *>(VT_TESTARRAYOFSTRING); + } + /// an example documentation comment: this will end up in the generated code + /// multiline too + const flatbuffers::Vector> *testarrayoftables() const { + return GetPointer> *>(VT_TESTARRAYOFTABLES); + } + flatbuffers::Vector> *mutable_testarrayoftables() { + return GetPointer> *>(VT_TESTARRAYOFTABLES); + } + const MyGame::Example::Monster *enemy() const { + return GetPointer(VT_ENEMY); + } + MyGame::Example::Monster *mutable_enemy() { + return GetPointer(VT_ENEMY); + } + const flatbuffers::Vector *testnestedflatbuffer() const { + return GetPointer *>(VT_TESTNESTEDFLATBUFFER); + } + flatbuffers::Vector *mutable_testnestedflatbuffer() { + return GetPointer *>(VT_TESTNESTEDFLATBUFFER); + } + const MyGame::Example::Monster *testnestedflatbuffer_nested_root() const { + return flatbuffers::GetRoot(testnestedflatbuffer()->Data()); + } + const MyGame::Example::Stat *testempty() const { + return GetPointer(VT_TESTEMPTY); + } + MyGame::Example::Stat *mutable_testempty() { + return GetPointer(VT_TESTEMPTY); + } + bool testbool() const { + return GetField(VT_TESTBOOL, 0) != 0; + } + bool mutate_testbool(bool _testbool) { + return SetField(VT_TESTBOOL, static_cast(_testbool), 0); + } + int32_t testhashs32_fnv1() const { + return GetField(VT_TESTHASHS32_FNV1, 0); + } + bool mutate_testhashs32_fnv1(int32_t _testhashs32_fnv1) { + return SetField(VT_TESTHASHS32_FNV1, _testhashs32_fnv1, 0); + } + uint32_t testhashu32_fnv1() const { + return GetField(VT_TESTHASHU32_FNV1, 0); + } + bool mutate_testhashu32_fnv1(uint32_t _testhashu32_fnv1) { + return SetField(VT_TESTHASHU32_FNV1, _testhashu32_fnv1, 0); + } + int64_t testhashs64_fnv1() const { + return GetField(VT_TESTHASHS64_FNV1, 0); + } + bool mutate_testhashs64_fnv1(int64_t _testhashs64_fnv1) { + return SetField(VT_TESTHASHS64_FNV1, _testhashs64_fnv1, 0); + } + uint64_t testhashu64_fnv1() const { + return GetField(VT_TESTHASHU64_FNV1, 0); + } + bool mutate_testhashu64_fnv1(uint64_t _testhashu64_fnv1) { + return SetField(VT_TESTHASHU64_FNV1, _testhashu64_fnv1, 0); + } + int32_t testhashs32_fnv1a() const { + return GetField(VT_TESTHASHS32_FNV1A, 0); + } + bool mutate_testhashs32_fnv1a(int32_t _testhashs32_fnv1a) { + return SetField(VT_TESTHASHS32_FNV1A, _testhashs32_fnv1a, 0); + } + uint32_t testhashu32_fnv1a() const { + return GetField(VT_TESTHASHU32_FNV1A, 0); + } + bool mutate_testhashu32_fnv1a(uint32_t _testhashu32_fnv1a) { + return SetField(VT_TESTHASHU32_FNV1A, _testhashu32_fnv1a, 0); + } + int64_t testhashs64_fnv1a() const { + return GetField(VT_TESTHASHS64_FNV1A, 0); + } + bool mutate_testhashs64_fnv1a(int64_t _testhashs64_fnv1a) { + return SetField(VT_TESTHASHS64_FNV1A, _testhashs64_fnv1a, 0); + } + uint64_t testhashu64_fnv1a() const { + return GetField(VT_TESTHASHU64_FNV1A, 0); + } + bool mutate_testhashu64_fnv1a(uint64_t _testhashu64_fnv1a) { + return SetField(VT_TESTHASHU64_FNV1A, _testhashu64_fnv1a, 0); + } + const flatbuffers::Vector *testarrayofbools() const { + return GetPointer *>(VT_TESTARRAYOFBOOLS); + } + flatbuffers::Vector *mutable_testarrayofbools() { + return GetPointer *>(VT_TESTARRAYOFBOOLS); + } + float testf() const { + return GetField(VT_TESTF, 3.14159f); + } + bool mutate_testf(float _testf) { + return SetField(VT_TESTF, _testf, 3.14159f); + } + float testf2() const { + return GetField(VT_TESTF2, 3.0f); + } + bool mutate_testf2(float _testf2) { + return SetField(VT_TESTF2, _testf2, 3.0f); + } + float testf3() const { + return GetField(VT_TESTF3, 0.0f); + } + bool mutate_testf3(float _testf3) { + return SetField(VT_TESTF3, _testf3, 0.0f); + } + const flatbuffers::Vector> *testarrayofstring2() const { + return GetPointer> *>(VT_TESTARRAYOFSTRING2); + } + flatbuffers::Vector> *mutable_testarrayofstring2() { + return GetPointer> *>(VT_TESTARRAYOFSTRING2); + } + const flatbuffers::Vector *testarrayofsortedstruct() const { + return GetPointer *>(VT_TESTARRAYOFSORTEDSTRUCT); + } + flatbuffers::Vector *mutable_testarrayofsortedstruct() { + return GetPointer *>(VT_TESTARRAYOFSORTEDSTRUCT); + } + const flatbuffers::Vector *flex() const { + return GetPointer *>(VT_FLEX); + } + flatbuffers::Vector *mutable_flex() { + return GetPointer *>(VT_FLEX); + } + flexbuffers::Reference flex_flexbuffer_root() const { + return flexbuffers::GetRoot(flex()->Data(), flex()->size()); + } + const flatbuffers::Vector *test5() const { + return GetPointer *>(VT_TEST5); + } + flatbuffers::Vector *mutable_test5() { + return GetPointer *>(VT_TEST5); + } + const flatbuffers::Vector *vector_of_longs() const { + return GetPointer *>(VT_VECTOR_OF_LONGS); + } + flatbuffers::Vector *mutable_vector_of_longs() { + return GetPointer *>(VT_VECTOR_OF_LONGS); + } + const flatbuffers::Vector *vector_of_doubles() const { + return GetPointer *>(VT_VECTOR_OF_DOUBLES); + } + flatbuffers::Vector *mutable_vector_of_doubles() { + return GetPointer *>(VT_VECTOR_OF_DOUBLES); + } + const MyGame::InParentNamespace *parent_namespace_test() const { + return GetPointer(VT_PARENT_NAMESPACE_TEST); + } + MyGame::InParentNamespace *mutable_parent_namespace_test() { + return GetPointer(VT_PARENT_NAMESPACE_TEST); + } + const flatbuffers::Vector> *vector_of_referrables() const { + return GetPointer> *>(VT_VECTOR_OF_REFERRABLES); + } + flatbuffers::Vector> *mutable_vector_of_referrables() { + return GetPointer> *>(VT_VECTOR_OF_REFERRABLES); + } + uint64_t single_weak_reference() const { + return GetField(VT_SINGLE_WEAK_REFERENCE, 0); + } + bool mutate_single_weak_reference(uint64_t _single_weak_reference) { + return SetField(VT_SINGLE_WEAK_REFERENCE, _single_weak_reference, 0); + } + const flatbuffers::Vector *vector_of_weak_references() const { + return GetPointer *>(VT_VECTOR_OF_WEAK_REFERENCES); + } + flatbuffers::Vector *mutable_vector_of_weak_references() { + return GetPointer *>(VT_VECTOR_OF_WEAK_REFERENCES); + } + const flatbuffers::Vector> *vector_of_strong_referrables() const { + return GetPointer> *>(VT_VECTOR_OF_STRONG_REFERRABLES); + } + flatbuffers::Vector> *mutable_vector_of_strong_referrables() { + return GetPointer> *>(VT_VECTOR_OF_STRONG_REFERRABLES); + } + uint64_t co_owning_reference() const { + return GetField(VT_CO_OWNING_REFERENCE, 0); + } + bool mutate_co_owning_reference(uint64_t _co_owning_reference) { + return SetField(VT_CO_OWNING_REFERENCE, _co_owning_reference, 0); + } + const flatbuffers::Vector *vector_of_co_owning_references() const { + return GetPointer *>(VT_VECTOR_OF_CO_OWNING_REFERENCES); + } + flatbuffers::Vector *mutable_vector_of_co_owning_references() { + return GetPointer *>(VT_VECTOR_OF_CO_OWNING_REFERENCES); + } + uint64_t non_owning_reference() const { + return GetField(VT_NON_OWNING_REFERENCE, 0); + } + bool mutate_non_owning_reference(uint64_t _non_owning_reference) { + return SetField(VT_NON_OWNING_REFERENCE, _non_owning_reference, 0); + } + const flatbuffers::Vector *vector_of_non_owning_references() const { + return GetPointer *>(VT_VECTOR_OF_NON_OWNING_REFERENCES); + } + flatbuffers::Vector *mutable_vector_of_non_owning_references() { + return GetPointer *>(VT_VECTOR_OF_NON_OWNING_REFERENCES); + } + MyGame::Example::AnyUniqueAliases any_unique_type() const { + return static_cast(GetField(VT_ANY_UNIQUE_TYPE, 0)); + } + const void *any_unique() const { + return GetPointer(VT_ANY_UNIQUE); + } + template const T *any_unique_as() const; + const MyGame::Example::Monster *any_unique_as_M() const { + return any_unique_type() == MyGame::Example::AnyUniqueAliases::M ? static_cast(any_unique()) : nullptr; + } + const MyGame::Example::TestSimpleTableWithEnum *any_unique_as_TS() const { + return any_unique_type() == MyGame::Example::AnyUniqueAliases::TS ? static_cast(any_unique()) : nullptr; + } + const MyGame::Example2::Monster *any_unique_as_M2() const { + return any_unique_type() == MyGame::Example::AnyUniqueAliases::M2 ? static_cast(any_unique()) : nullptr; + } + void *mutable_any_unique() { + return GetPointer(VT_ANY_UNIQUE); + } + MyGame::Example::AnyAmbiguousAliases any_ambiguous_type() const { + return static_cast(GetField(VT_ANY_AMBIGUOUS_TYPE, 0)); + } + const void *any_ambiguous() const { + return GetPointer(VT_ANY_AMBIGUOUS); + } + const MyGame::Example::Monster *any_ambiguous_as_M1() const { + return any_ambiguous_type() == MyGame::Example::AnyAmbiguousAliases::M1 ? static_cast(any_ambiguous()) : nullptr; + } + const MyGame::Example::Monster *any_ambiguous_as_M2() const { + return any_ambiguous_type() == MyGame::Example::AnyAmbiguousAliases::M2 ? static_cast(any_ambiguous()) : nullptr; + } + const MyGame::Example::Monster *any_ambiguous_as_M3() const { + return any_ambiguous_type() == MyGame::Example::AnyAmbiguousAliases::M3 ? static_cast(any_ambiguous()) : nullptr; + } + void *mutable_any_ambiguous() { + return GetPointer(VT_ANY_AMBIGUOUS); + } + const flatbuffers::Vector *vector_of_enums() const { + return GetPointer *>(VT_VECTOR_OF_ENUMS); + } + flatbuffers::Vector *mutable_vector_of_enums() { + return GetPointer *>(VT_VECTOR_OF_ENUMS); + } + MyGame::Example::Race signed_enum() const { + return static_cast(GetField(VT_SIGNED_ENUM, -1)); + } + bool mutate_signed_enum(MyGame::Example::Race _signed_enum) { + return SetField(VT_SIGNED_ENUM, static_cast(_signed_enum), -1); + } + bool Verify(flatbuffers::Verifier &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_POS) && + VerifyField(verifier, VT_MANA) && + VerifyField(verifier, VT_HP) && + VerifyOffsetRequired(verifier, VT_NAME) && + verifier.VerifyString(name()) && + VerifyOffset(verifier, VT_INVENTORY) && + verifier.VerifyVector(inventory()) && + VerifyField(verifier, VT_COLOR) && + VerifyField(verifier, VT_TEST_TYPE) && + VerifyOffset(verifier, VT_TEST) && + VerifyAny(verifier, test(), test_type()) && + VerifyOffset(verifier, VT_TEST4) && + verifier.VerifyVector(test4()) && + VerifyOffset(verifier, VT_TESTARRAYOFSTRING) && + verifier.VerifyVector(testarrayofstring()) && + verifier.VerifyVectorOfStrings(testarrayofstring()) && + VerifyOffset(verifier, VT_TESTARRAYOFTABLES) && + verifier.VerifyVector(testarrayoftables()) && + verifier.VerifyVectorOfTables(testarrayoftables()) && + VerifyOffset(verifier, VT_ENEMY) && + verifier.VerifyTable(enemy()) && + VerifyOffset(verifier, VT_TESTNESTEDFLATBUFFER) && + verifier.VerifyVector(testnestedflatbuffer()) && + VerifyOffset(verifier, VT_TESTEMPTY) && + verifier.VerifyTable(testempty()) && + VerifyField(verifier, VT_TESTBOOL) && + VerifyField(verifier, VT_TESTHASHS32_FNV1) && + VerifyField(verifier, VT_TESTHASHU32_FNV1) && + VerifyField(verifier, VT_TESTHASHS64_FNV1) && + VerifyField(verifier, VT_TESTHASHU64_FNV1) && + VerifyField(verifier, VT_TESTHASHS32_FNV1A) && + VerifyField(verifier, VT_TESTHASHU32_FNV1A) && + VerifyField(verifier, VT_TESTHASHS64_FNV1A) && + VerifyField(verifier, VT_TESTHASHU64_FNV1A) && + VerifyOffset(verifier, VT_TESTARRAYOFBOOLS) && + verifier.VerifyVector(testarrayofbools()) && + VerifyField(verifier, VT_TESTF) && + VerifyField(verifier, VT_TESTF2) && + VerifyField(verifier, VT_TESTF3) && + VerifyOffset(verifier, VT_TESTARRAYOFSTRING2) && + verifier.VerifyVector(testarrayofstring2()) && + verifier.VerifyVectorOfStrings(testarrayofstring2()) && + VerifyOffset(verifier, VT_TESTARRAYOFSORTEDSTRUCT) && + verifier.VerifyVector(testarrayofsortedstruct()) && + VerifyOffset(verifier, VT_FLEX) && + verifier.VerifyVector(flex()) && + VerifyOffset(verifier, VT_TEST5) && + verifier.VerifyVector(test5()) && + VerifyOffset(verifier, VT_VECTOR_OF_LONGS) && + verifier.VerifyVector(vector_of_longs()) && + VerifyOffset(verifier, VT_VECTOR_OF_DOUBLES) && + verifier.VerifyVector(vector_of_doubles()) && + VerifyOffset(verifier, VT_PARENT_NAMESPACE_TEST) && + verifier.VerifyTable(parent_namespace_test()) && + VerifyOffset(verifier, VT_VECTOR_OF_REFERRABLES) && + verifier.VerifyVector(vector_of_referrables()) && + verifier.VerifyVectorOfTables(vector_of_referrables()) && + VerifyField(verifier, VT_SINGLE_WEAK_REFERENCE) && + VerifyOffset(verifier, VT_VECTOR_OF_WEAK_REFERENCES) && + verifier.VerifyVector(vector_of_weak_references()) && + VerifyOffset(verifier, VT_VECTOR_OF_STRONG_REFERRABLES) && + verifier.VerifyVector(vector_of_strong_referrables()) && + verifier.VerifyVectorOfTables(vector_of_strong_referrables()) && + VerifyField(verifier, VT_CO_OWNING_REFERENCE) && + VerifyOffset(verifier, VT_VECTOR_OF_CO_OWNING_REFERENCES) && + verifier.VerifyVector(vector_of_co_owning_references()) && + VerifyField(verifier, VT_NON_OWNING_REFERENCE) && + VerifyOffset(verifier, VT_VECTOR_OF_NON_OWNING_REFERENCES) && + verifier.VerifyVector(vector_of_non_owning_references()) && + VerifyField(verifier, VT_ANY_UNIQUE_TYPE) && + VerifyOffset(verifier, VT_ANY_UNIQUE) && + VerifyAnyUniqueAliases(verifier, any_unique(), any_unique_type()) && + VerifyField(verifier, VT_ANY_AMBIGUOUS_TYPE) && + VerifyOffset(verifier, VT_ANY_AMBIGUOUS) && + VerifyAnyAmbiguousAliases(verifier, any_ambiguous(), any_ambiguous_type()) && + VerifyOffset(verifier, VT_VECTOR_OF_ENUMS) && + verifier.VerifyVector(vector_of_enums()) && + VerifyField(verifier, VT_SIGNED_ENUM) && + verifier.EndTable(); + } + MonsterT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(MonsterT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; + static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +template<> inline const MyGame::Example::Monster *Monster::test_as() const { + return test_as_Monster(); +} + +template<> inline const MyGame::Example::TestSimpleTableWithEnum *Monster::test_as() const { + return test_as_TestSimpleTableWithEnum(); +} + +template<> inline const MyGame::Example2::Monster *Monster::test_as() const { + return test_as_MyGame_Example2_Monster(); +} + +template<> inline const MyGame::Example::Monster *Monster::any_unique_as() const { + return any_unique_as_M(); +} + +template<> inline const MyGame::Example::TestSimpleTableWithEnum *Monster::any_unique_as() const { + return any_unique_as_TS(); +} + +template<> inline const MyGame::Example2::Monster *Monster::any_unique_as() const { + return any_unique_as_M2(); +} + +struct MonsterBuilder { + flatbuffers::FlatBufferBuilder &fbb_; + flatbuffers::uoffset_t start_; + void add_pos(const MyGame::Example::Vec3 *pos) { + fbb_.AddStruct(Monster::VT_POS, pos); + } + void add_mana(int16_t mana) { + fbb_.AddElement(Monster::VT_MANA, mana, 150); + } + void add_hp(int16_t hp) { + fbb_.AddElement(Monster::VT_HP, hp, 100); + } + void add_name(flatbuffers::Offset name) { + fbb_.AddOffset(Monster::VT_NAME, name); + } + void add_inventory(flatbuffers::Offset> inventory) { + fbb_.AddOffset(Monster::VT_INVENTORY, inventory); + } + void add_color(MyGame::Example::Color color) { + fbb_.AddElement(Monster::VT_COLOR, static_cast(color), 8); + } + void add_test_type(MyGame::Example::Any test_type) { + fbb_.AddElement(Monster::VT_TEST_TYPE, static_cast(test_type), 0); + } + void add_test(flatbuffers::Offset test) { + fbb_.AddOffset(Monster::VT_TEST, test); + } + void add_test4(flatbuffers::Offset> test4) { + fbb_.AddOffset(Monster::VT_TEST4, test4); + } + void add_testarrayofstring(flatbuffers::Offset>> testarrayofstring) { + fbb_.AddOffset(Monster::VT_TESTARRAYOFSTRING, testarrayofstring); + } + void add_testarrayoftables(flatbuffers::Offset>> testarrayoftables) { + fbb_.AddOffset(Monster::VT_TESTARRAYOFTABLES, testarrayoftables); + } + void add_enemy(flatbuffers::Offset enemy) { + fbb_.AddOffset(Monster::VT_ENEMY, enemy); + } + void add_testnestedflatbuffer(flatbuffers::Offset> testnestedflatbuffer) { + fbb_.AddOffset(Monster::VT_TESTNESTEDFLATBUFFER, testnestedflatbuffer); + } + void add_testempty(flatbuffers::Offset testempty) { + fbb_.AddOffset(Monster::VT_TESTEMPTY, testempty); + } + void add_testbool(bool testbool) { + fbb_.AddElement(Monster::VT_TESTBOOL, static_cast(testbool), 0); + } + void add_testhashs32_fnv1(int32_t testhashs32_fnv1) { + fbb_.AddElement(Monster::VT_TESTHASHS32_FNV1, testhashs32_fnv1, 0); + } + void add_testhashu32_fnv1(uint32_t testhashu32_fnv1) { + fbb_.AddElement(Monster::VT_TESTHASHU32_FNV1, testhashu32_fnv1, 0); + } + void add_testhashs64_fnv1(int64_t testhashs64_fnv1) { + fbb_.AddElement(Monster::VT_TESTHASHS64_FNV1, testhashs64_fnv1, 0); + } + void add_testhashu64_fnv1(uint64_t testhashu64_fnv1) { + fbb_.AddElement(Monster::VT_TESTHASHU64_FNV1, testhashu64_fnv1, 0); + } + void add_testhashs32_fnv1a(int32_t testhashs32_fnv1a) { + fbb_.AddElement(Monster::VT_TESTHASHS32_FNV1A, testhashs32_fnv1a, 0); + } + void add_testhashu32_fnv1a(uint32_t testhashu32_fnv1a) { + fbb_.AddElement(Monster::VT_TESTHASHU32_FNV1A, testhashu32_fnv1a, 0); + } + void add_testhashs64_fnv1a(int64_t testhashs64_fnv1a) { + fbb_.AddElement(Monster::VT_TESTHASHS64_FNV1A, testhashs64_fnv1a, 0); + } + void add_testhashu64_fnv1a(uint64_t testhashu64_fnv1a) { + fbb_.AddElement(Monster::VT_TESTHASHU64_FNV1A, testhashu64_fnv1a, 0); + } + void add_testarrayofbools(flatbuffers::Offset> testarrayofbools) { + fbb_.AddOffset(Monster::VT_TESTARRAYOFBOOLS, testarrayofbools); + } + void add_testf(float testf) { + fbb_.AddElement(Monster::VT_TESTF, testf, 3.14159f); + } + void add_testf2(float testf2) { + fbb_.AddElement(Monster::VT_TESTF2, testf2, 3.0f); + } + void add_testf3(float testf3) { + fbb_.AddElement(Monster::VT_TESTF3, testf3, 0.0f); + } + void add_testarrayofstring2(flatbuffers::Offset>> testarrayofstring2) { + fbb_.AddOffset(Monster::VT_TESTARRAYOFSTRING2, testarrayofstring2); + } + void add_testarrayofsortedstruct(flatbuffers::Offset> testarrayofsortedstruct) { + fbb_.AddOffset(Monster::VT_TESTARRAYOFSORTEDSTRUCT, testarrayofsortedstruct); + } + void add_flex(flatbuffers::Offset> flex) { + fbb_.AddOffset(Monster::VT_FLEX, flex); + } + void add_test5(flatbuffers::Offset> test5) { + fbb_.AddOffset(Monster::VT_TEST5, test5); + } + void add_vector_of_longs(flatbuffers::Offset> vector_of_longs) { + fbb_.AddOffset(Monster::VT_VECTOR_OF_LONGS, vector_of_longs); + } + void add_vector_of_doubles(flatbuffers::Offset> vector_of_doubles) { + fbb_.AddOffset(Monster::VT_VECTOR_OF_DOUBLES, vector_of_doubles); + } + void add_parent_namespace_test(flatbuffers::Offset parent_namespace_test) { + fbb_.AddOffset(Monster::VT_PARENT_NAMESPACE_TEST, parent_namespace_test); + } + void add_vector_of_referrables(flatbuffers::Offset>> vector_of_referrables) { + fbb_.AddOffset(Monster::VT_VECTOR_OF_REFERRABLES, vector_of_referrables); + } + void add_single_weak_reference(uint64_t single_weak_reference) { + fbb_.AddElement(Monster::VT_SINGLE_WEAK_REFERENCE, single_weak_reference, 0); + } + void add_vector_of_weak_references(flatbuffers::Offset> vector_of_weak_references) { + fbb_.AddOffset(Monster::VT_VECTOR_OF_WEAK_REFERENCES, vector_of_weak_references); + } + void add_vector_of_strong_referrables(flatbuffers::Offset>> vector_of_strong_referrables) { + fbb_.AddOffset(Monster::VT_VECTOR_OF_STRONG_REFERRABLES, vector_of_strong_referrables); + } + void add_co_owning_reference(uint64_t co_owning_reference) { + fbb_.AddElement(Monster::VT_CO_OWNING_REFERENCE, co_owning_reference, 0); + } + void add_vector_of_co_owning_references(flatbuffers::Offset> vector_of_co_owning_references) { + fbb_.AddOffset(Monster::VT_VECTOR_OF_CO_OWNING_REFERENCES, vector_of_co_owning_references); + } + void add_non_owning_reference(uint64_t non_owning_reference) { + fbb_.AddElement(Monster::VT_NON_OWNING_REFERENCE, non_owning_reference, 0); + } + void add_vector_of_non_owning_references(flatbuffers::Offset> vector_of_non_owning_references) { + fbb_.AddOffset(Monster::VT_VECTOR_OF_NON_OWNING_REFERENCES, vector_of_non_owning_references); + } + void add_any_unique_type(MyGame::Example::AnyUniqueAliases any_unique_type) { + fbb_.AddElement(Monster::VT_ANY_UNIQUE_TYPE, static_cast(any_unique_type), 0); + } + void add_any_unique(flatbuffers::Offset any_unique) { + fbb_.AddOffset(Monster::VT_ANY_UNIQUE, any_unique); + } + void add_any_ambiguous_type(MyGame::Example::AnyAmbiguousAliases any_ambiguous_type) { + fbb_.AddElement(Monster::VT_ANY_AMBIGUOUS_TYPE, static_cast(any_ambiguous_type), 0); + } + void add_any_ambiguous(flatbuffers::Offset any_ambiguous) { + fbb_.AddOffset(Monster::VT_ANY_AMBIGUOUS, any_ambiguous); + } + void add_vector_of_enums(flatbuffers::Offset> vector_of_enums) { + fbb_.AddOffset(Monster::VT_VECTOR_OF_ENUMS, vector_of_enums); + } + void add_signed_enum(MyGame::Example::Race signed_enum) { + fbb_.AddElement(Monster::VT_SIGNED_ENUM, static_cast(signed_enum), -1); + } + explicit MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + MonsterBuilder &operator=(const MonsterBuilder &); + flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = flatbuffers::Offset(end); + fbb_.Required(o, Monster::VT_NAME); + return o; + } +}; + +inline flatbuffers::Offset CreateMonster( + flatbuffers::FlatBufferBuilder &_fbb, + const MyGame::Example::Vec3 *pos = 0, + int16_t mana = 150, + int16_t hp = 100, + flatbuffers::Offset name = 0, + flatbuffers::Offset> inventory = 0, + MyGame::Example::Color color = MyGame::Example::Color::Blue, + MyGame::Example::Any test_type = MyGame::Example::Any::NONE, + flatbuffers::Offset test = 0, + flatbuffers::Offset> test4 = 0, + flatbuffers::Offset>> testarrayofstring = 0, + flatbuffers::Offset>> testarrayoftables = 0, + flatbuffers::Offset enemy = 0, + flatbuffers::Offset> testnestedflatbuffer = 0, + flatbuffers::Offset testempty = 0, + bool testbool = false, + int32_t testhashs32_fnv1 = 0, + uint32_t testhashu32_fnv1 = 0, + int64_t testhashs64_fnv1 = 0, + uint64_t testhashu64_fnv1 = 0, + int32_t testhashs32_fnv1a = 0, + uint32_t testhashu32_fnv1a = 0, + int64_t testhashs64_fnv1a = 0, + uint64_t testhashu64_fnv1a = 0, + flatbuffers::Offset> testarrayofbools = 0, + float testf = 3.14159f, + float testf2 = 3.0f, + float testf3 = 0.0f, + flatbuffers::Offset>> testarrayofstring2 = 0, + flatbuffers::Offset> testarrayofsortedstruct = 0, + flatbuffers::Offset> flex = 0, + flatbuffers::Offset> test5 = 0, + flatbuffers::Offset> vector_of_longs = 0, + flatbuffers::Offset> vector_of_doubles = 0, + flatbuffers::Offset parent_namespace_test = 0, + flatbuffers::Offset>> vector_of_referrables = 0, + uint64_t single_weak_reference = 0, + flatbuffers::Offset> vector_of_weak_references = 0, + flatbuffers::Offset>> vector_of_strong_referrables = 0, + uint64_t co_owning_reference = 0, + flatbuffers::Offset> vector_of_co_owning_references = 0, + uint64_t non_owning_reference = 0, + flatbuffers::Offset> vector_of_non_owning_references = 0, + MyGame::Example::AnyUniqueAliases any_unique_type = MyGame::Example::AnyUniqueAliases::NONE, + flatbuffers::Offset any_unique = 0, + MyGame::Example::AnyAmbiguousAliases any_ambiguous_type = MyGame::Example::AnyAmbiguousAliases::NONE, + flatbuffers::Offset any_ambiguous = 0, + flatbuffers::Offset> vector_of_enums = 0, + MyGame::Example::Race signed_enum = MyGame::Example::Race::None) { + MonsterBuilder builder_(_fbb); + builder_.add_non_owning_reference(non_owning_reference); + builder_.add_co_owning_reference(co_owning_reference); + builder_.add_single_weak_reference(single_weak_reference); + builder_.add_testhashu64_fnv1a(testhashu64_fnv1a); + builder_.add_testhashs64_fnv1a(testhashs64_fnv1a); + builder_.add_testhashu64_fnv1(testhashu64_fnv1); + builder_.add_testhashs64_fnv1(testhashs64_fnv1); + builder_.add_vector_of_enums(vector_of_enums); + builder_.add_any_ambiguous(any_ambiguous); + builder_.add_any_unique(any_unique); + builder_.add_vector_of_non_owning_references(vector_of_non_owning_references); + builder_.add_vector_of_co_owning_references(vector_of_co_owning_references); + builder_.add_vector_of_strong_referrables(vector_of_strong_referrables); + builder_.add_vector_of_weak_references(vector_of_weak_references); + builder_.add_vector_of_referrables(vector_of_referrables); + builder_.add_parent_namespace_test(parent_namespace_test); + builder_.add_vector_of_doubles(vector_of_doubles); + builder_.add_vector_of_longs(vector_of_longs); + builder_.add_test5(test5); + builder_.add_flex(flex); + builder_.add_testarrayofsortedstruct(testarrayofsortedstruct); + builder_.add_testarrayofstring2(testarrayofstring2); + builder_.add_testf3(testf3); + builder_.add_testf2(testf2); + builder_.add_testf(testf); + builder_.add_testarrayofbools(testarrayofbools); + builder_.add_testhashu32_fnv1a(testhashu32_fnv1a); + builder_.add_testhashs32_fnv1a(testhashs32_fnv1a); + builder_.add_testhashu32_fnv1(testhashu32_fnv1); + builder_.add_testhashs32_fnv1(testhashs32_fnv1); + builder_.add_testempty(testempty); + builder_.add_testnestedflatbuffer(testnestedflatbuffer); + builder_.add_enemy(enemy); + builder_.add_testarrayoftables(testarrayoftables); + builder_.add_testarrayofstring(testarrayofstring); + builder_.add_test4(test4); + builder_.add_test(test); + builder_.add_inventory(inventory); + builder_.add_name(name); + builder_.add_pos(pos); + builder_.add_hp(hp); + builder_.add_mana(mana); + builder_.add_signed_enum(signed_enum); + builder_.add_any_ambiguous_type(any_ambiguous_type); + builder_.add_any_unique_type(any_unique_type); + builder_.add_testbool(testbool); + builder_.add_test_type(test_type); + builder_.add_color(color); + return builder_.Finish(); +} + +inline flatbuffers::Offset CreateMonsterDirect( + flatbuffers::FlatBufferBuilder &_fbb, + const MyGame::Example::Vec3 *pos = 0, + int16_t mana = 150, + int16_t hp = 100, + const char *name = nullptr, + const std::vector *inventory = nullptr, + MyGame::Example::Color color = MyGame::Example::Color::Blue, + MyGame::Example::Any test_type = MyGame::Example::Any::NONE, + flatbuffers::Offset test = 0, + const std::vector *test4 = nullptr, + const std::vector> *testarrayofstring = nullptr, + std::vector> *testarrayoftables = nullptr, + flatbuffers::Offset enemy = 0, + const std::vector *testnestedflatbuffer = nullptr, + flatbuffers::Offset testempty = 0, + bool testbool = false, + int32_t testhashs32_fnv1 = 0, + uint32_t testhashu32_fnv1 = 0, + int64_t testhashs64_fnv1 = 0, + uint64_t testhashu64_fnv1 = 0, + int32_t testhashs32_fnv1a = 0, + uint32_t testhashu32_fnv1a = 0, + int64_t testhashs64_fnv1a = 0, + uint64_t testhashu64_fnv1a = 0, + const std::vector *testarrayofbools = nullptr, + float testf = 3.14159f, + float testf2 = 3.0f, + float testf3 = 0.0f, + const std::vector> *testarrayofstring2 = nullptr, + std::vector *testarrayofsortedstruct = nullptr, + const std::vector *flex = nullptr, + const std::vector *test5 = nullptr, + const std::vector *vector_of_longs = nullptr, + const std::vector *vector_of_doubles = nullptr, + flatbuffers::Offset parent_namespace_test = 0, + std::vector> *vector_of_referrables = nullptr, + uint64_t single_weak_reference = 0, + const std::vector *vector_of_weak_references = nullptr, + std::vector> *vector_of_strong_referrables = nullptr, + uint64_t co_owning_reference = 0, + const std::vector *vector_of_co_owning_references = nullptr, + uint64_t non_owning_reference = 0, + const std::vector *vector_of_non_owning_references = nullptr, + MyGame::Example::AnyUniqueAliases any_unique_type = MyGame::Example::AnyUniqueAliases::NONE, + flatbuffers::Offset any_unique = 0, + MyGame::Example::AnyAmbiguousAliases any_ambiguous_type = MyGame::Example::AnyAmbiguousAliases::NONE, + flatbuffers::Offset any_ambiguous = 0, + const std::vector *vector_of_enums = nullptr, + MyGame::Example::Race signed_enum = MyGame::Example::Race::None) { + auto name__ = name ? _fbb.CreateString(name) : 0; + auto inventory__ = inventory ? _fbb.CreateVector(*inventory) : 0; + auto test4__ = test4 ? _fbb.CreateVectorOfStructs(*test4) : 0; + auto testarrayofstring__ = testarrayofstring ? _fbb.CreateVector>(*testarrayofstring) : 0; + auto testarrayoftables__ = testarrayoftables ? _fbb.CreateVectorOfSortedTables(testarrayoftables) : 0; + auto testnestedflatbuffer__ = testnestedflatbuffer ? _fbb.CreateVector(*testnestedflatbuffer) : 0; + auto testarrayofbools__ = testarrayofbools ? _fbb.CreateVector(*testarrayofbools) : 0; + auto testarrayofstring2__ = testarrayofstring2 ? _fbb.CreateVector>(*testarrayofstring2) : 0; + auto testarrayofsortedstruct__ = testarrayofsortedstruct ? _fbb.CreateVectorOfSortedStructs(testarrayofsortedstruct) : 0; + auto flex__ = flex ? _fbb.CreateVector(*flex) : 0; + auto test5__ = test5 ? _fbb.CreateVectorOfStructs(*test5) : 0; + auto vector_of_longs__ = vector_of_longs ? _fbb.CreateVector(*vector_of_longs) : 0; + auto vector_of_doubles__ = vector_of_doubles ? _fbb.CreateVector(*vector_of_doubles) : 0; + auto vector_of_referrables__ = vector_of_referrables ? _fbb.CreateVectorOfSortedTables(vector_of_referrables) : 0; + auto vector_of_weak_references__ = vector_of_weak_references ? _fbb.CreateVector(*vector_of_weak_references) : 0; + auto vector_of_strong_referrables__ = vector_of_strong_referrables ? _fbb.CreateVectorOfSortedTables(vector_of_strong_referrables) : 0; + auto vector_of_co_owning_references__ = vector_of_co_owning_references ? _fbb.CreateVector(*vector_of_co_owning_references) : 0; + auto vector_of_non_owning_references__ = vector_of_non_owning_references ? _fbb.CreateVector(*vector_of_non_owning_references) : 0; + auto vector_of_enums__ = vector_of_enums ? _fbb.CreateVector(*vector_of_enums) : 0; + return MyGame::Example::CreateMonster( + _fbb, + pos, + mana, + hp, + name__, + inventory__, + color, + test_type, + test, + test4__, + testarrayofstring__, + testarrayoftables__, + enemy, + testnestedflatbuffer__, + testempty, + testbool, + testhashs32_fnv1, + testhashu32_fnv1, + testhashs64_fnv1, + testhashu64_fnv1, + testhashs32_fnv1a, + testhashu32_fnv1a, + testhashs64_fnv1a, + testhashu64_fnv1a, + testarrayofbools__, + testf, + testf2, + testf3, + testarrayofstring2__, + testarrayofsortedstruct__, + flex__, + test5__, + vector_of_longs__, + vector_of_doubles__, + parent_namespace_test, + vector_of_referrables__, + single_weak_reference, + vector_of_weak_references__, + vector_of_strong_referrables__, + co_owning_reference, + vector_of_co_owning_references__, + non_owning_reference, + vector_of_non_owning_references__, + any_unique_type, + any_unique, + any_ambiguous_type, + any_ambiguous, + vector_of_enums__, + signed_enum); +} + +flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); + +struct TypeAliasesT : public flatbuffers::NativeTable { + typedef TypeAliases TableType; + int8_t i8; + uint8_t u8; + int16_t i16; + uint16_t u16; + int32_t i32; + uint32_t u32; + int64_t i64; + uint64_t u64; + float f32; + double f64; + std::vector v8; + std::vector vf64; + TypeAliasesT() + : i8(0), + u8(0), + i16(0), + u16(0), + i32(0), + u32(0), + i64(0), + u64(0), + f32(0.0f), + f64(0.0) { + } +}; + +struct TypeAliases FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { + typedef TypeAliasesT NativeTableType; + static const flatbuffers::TypeTable *MiniReflectTypeTable() { + return TypeAliasesTypeTable(); + } + enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE { + VT_I8 = 4, + VT_U8 = 6, + VT_I16 = 8, + VT_U16 = 10, + VT_I32 = 12, + VT_U32 = 14, + VT_I64 = 16, + VT_U64 = 18, + VT_F32 = 20, + VT_F64 = 22, + VT_V8 = 24, + VT_VF64 = 26 + }; + int8_t i8() const { + return GetField(VT_I8, 0); + } + bool mutate_i8(int8_t _i8) { + return SetField(VT_I8, _i8, 0); + } + uint8_t u8() const { + return GetField(VT_U8, 0); + } + bool mutate_u8(uint8_t _u8) { + return SetField(VT_U8, _u8, 0); + } + int16_t i16() const { + return GetField(VT_I16, 0); + } + bool mutate_i16(int16_t _i16) { + return SetField(VT_I16, _i16, 0); + } + uint16_t u16() const { + return GetField(VT_U16, 0); + } + bool mutate_u16(uint16_t _u16) { + return SetField(VT_U16, _u16, 0); + } + int32_t i32() const { + return GetField(VT_I32, 0); + } + bool mutate_i32(int32_t _i32) { + return SetField(VT_I32, _i32, 0); + } + uint32_t u32() const { + return GetField(VT_U32, 0); + } + bool mutate_u32(uint32_t _u32) { + return SetField(VT_U32, _u32, 0); + } + int64_t i64() const { + return GetField(VT_I64, 0); + } + bool mutate_i64(int64_t _i64) { + return SetField(VT_I64, _i64, 0); + } + uint64_t u64() const { + return GetField(VT_U64, 0); + } + bool mutate_u64(uint64_t _u64) { + return SetField(VT_U64, _u64, 0); + } + float f32() const { + return GetField(VT_F32, 0.0f); + } + bool mutate_f32(float _f32) { + return SetField(VT_F32, _f32, 0.0f); + } + double f64() const { + return GetField(VT_F64, 0.0); + } + bool mutate_f64(double _f64) { + return SetField(VT_F64, _f64, 0.0); + } + const flatbuffers::Vector *v8() const { + return GetPointer *>(VT_V8); + } + flatbuffers::Vector *mutable_v8() { + return GetPointer *>(VT_V8); + } + const flatbuffers::Vector *vf64() const { + return GetPointer *>(VT_VF64); + } + flatbuffers::Vector *mutable_vf64() { + return GetPointer *>(VT_VF64); + } + bool Verify(flatbuffers::Verifier &verifier) const { + return VerifyTableStart(verifier) && + VerifyField(verifier, VT_I8) && + VerifyField(verifier, VT_U8) && + VerifyField(verifier, VT_I16) && + VerifyField(verifier, VT_U16) && + VerifyField(verifier, VT_I32) && + VerifyField(verifier, VT_U32) && + VerifyField(verifier, VT_I64) && + VerifyField(verifier, VT_U64) && + VerifyField(verifier, VT_F32) && + VerifyField(verifier, VT_F64) && + VerifyOffset(verifier, VT_V8) && + verifier.VerifyVector(v8()) && + VerifyOffset(verifier, VT_VF64) && + verifier.VerifyVector(vf64()) && + verifier.EndTable(); + } + TypeAliasesT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(TypeAliasesT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; + static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const TypeAliasesT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); +}; + +struct TypeAliasesBuilder { + flatbuffers::FlatBufferBuilder &fbb_; + flatbuffers::uoffset_t start_; + void add_i8(int8_t i8) { + fbb_.AddElement(TypeAliases::VT_I8, i8, 0); + } + void add_u8(uint8_t u8) { + fbb_.AddElement(TypeAliases::VT_U8, u8, 0); + } + void add_i16(int16_t i16) { + fbb_.AddElement(TypeAliases::VT_I16, i16, 0); + } + void add_u16(uint16_t u16) { + fbb_.AddElement(TypeAliases::VT_U16, u16, 0); + } + void add_i32(int32_t i32) { + fbb_.AddElement(TypeAliases::VT_I32, i32, 0); + } + void add_u32(uint32_t u32) { + fbb_.AddElement(TypeAliases::VT_U32, u32, 0); + } + void add_i64(int64_t i64) { + fbb_.AddElement(TypeAliases::VT_I64, i64, 0); + } + void add_u64(uint64_t u64) { + fbb_.AddElement(TypeAliases::VT_U64, u64, 0); + } + void add_f32(float f32) { + fbb_.AddElement(TypeAliases::VT_F32, f32, 0.0f); + } + void add_f64(double f64) { + fbb_.AddElement(TypeAliases::VT_F64, f64, 0.0); + } + void add_v8(flatbuffers::Offset> v8) { + fbb_.AddOffset(TypeAliases::VT_V8, v8); + } + void add_vf64(flatbuffers::Offset> vf64) { + fbb_.AddOffset(TypeAliases::VT_VF64, vf64); + } + explicit TypeAliasesBuilder(flatbuffers::FlatBufferBuilder &_fbb) + : fbb_(_fbb) { + start_ = fbb_.StartTable(); + } + TypeAliasesBuilder &operator=(const TypeAliasesBuilder &); + flatbuffers::Offset Finish() { + const auto end = fbb_.EndTable(start_); + auto o = flatbuffers::Offset(end); + return o; + } +}; + +inline flatbuffers::Offset CreateTypeAliases( + flatbuffers::FlatBufferBuilder &_fbb, + int8_t i8 = 0, + uint8_t u8 = 0, + int16_t i16 = 0, + uint16_t u16 = 0, + int32_t i32 = 0, + uint32_t u32 = 0, + int64_t i64 = 0, + uint64_t u64 = 0, + float f32 = 0.0f, + double f64 = 0.0, + flatbuffers::Offset> v8 = 0, + flatbuffers::Offset> vf64 = 0) { + TypeAliasesBuilder builder_(_fbb); + builder_.add_f64(f64); + builder_.add_u64(u64); + builder_.add_i64(i64); + builder_.add_vf64(vf64); + builder_.add_v8(v8); + builder_.add_f32(f32); + builder_.add_u32(u32); + builder_.add_i32(i32); + builder_.add_u16(u16); + builder_.add_i16(i16); + builder_.add_u8(u8); + builder_.add_i8(i8); + return builder_.Finish(); +} + +inline flatbuffers::Offset CreateTypeAliasesDirect( + flatbuffers::FlatBufferBuilder &_fbb, + int8_t i8 = 0, + uint8_t u8 = 0, + int16_t i16 = 0, + uint16_t u16 = 0, + int32_t i32 = 0, + uint32_t u32 = 0, + int64_t i64 = 0, + uint64_t u64 = 0, + float f32 = 0.0f, + double f64 = 0.0, + const std::vector *v8 = nullptr, + const std::vector *vf64 = nullptr) { + auto v8__ = v8 ? _fbb.CreateVector(*v8) : 0; + auto vf64__ = vf64 ? _fbb.CreateVector(*vf64) : 0; + return MyGame::Example::CreateTypeAliases( + _fbb, + i8, + u8, + i16, + u16, + i32, + u32, + i64, + u64, + f32, + f64, + v8__, + vf64__); +} + +flatbuffers::Offset CreateTypeAliases(flatbuffers::FlatBufferBuilder &_fbb, const TypeAliasesT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); + +} // namespace Example + +inline InParentNamespaceT *InParentNamespace::UnPack(const flatbuffers::resolver_function_t *_resolver) const { + auto _o = new InParentNamespaceT(); + UnPackTo(_o, _resolver); + return _o; +} + +inline void InParentNamespace::UnPackTo(InParentNamespaceT *_o, const flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline flatbuffers::Offset InParentNamespace::Pack(flatbuffers::FlatBufferBuilder &_fbb, const InParentNamespaceT* _o, const flatbuffers::rehasher_function_t *_rehasher) { + return CreateInParentNamespace(_fbb, _o, _rehasher); +} + +inline flatbuffers::Offset CreateInParentNamespace(flatbuffers::FlatBufferBuilder &_fbb, const InParentNamespaceT *_o, const flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const InParentNamespaceT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return MyGame::CreateInParentNamespace( + _fbb); +} + +namespace Example2 { + +inline MonsterT *Monster::UnPack(const flatbuffers::resolver_function_t *_resolver) const { + auto _o = new MonsterT(); + UnPackTo(_o, _resolver); + return _o; +} + +inline void Monster::UnPackTo(MonsterT *_o, const flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; +} + +inline flatbuffers::Offset Monster::Pack(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT* _o, const flatbuffers::rehasher_function_t *_rehasher) { + return CreateMonster(_fbb, _o, _rehasher); +} + +inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT *_o, const flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const MonsterT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + return MyGame::Example2::CreateMonster( + _fbb); +} + +} // namespace Example2 + +namespace Example { + +inline TestSimpleTableWithEnumT *TestSimpleTableWithEnum::UnPack(const flatbuffers::resolver_function_t *_resolver) const { + auto _o = new TestSimpleTableWithEnumT(); + UnPackTo(_o, _resolver); + return _o; +} + +inline void TestSimpleTableWithEnum::UnPackTo(TestSimpleTableWithEnumT *_o, const flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = color(); _o->color = _e; } +} + +inline flatbuffers::Offset TestSimpleTableWithEnum::Pack(flatbuffers::FlatBufferBuilder &_fbb, const TestSimpleTableWithEnumT* _o, const flatbuffers::rehasher_function_t *_rehasher) { + return CreateTestSimpleTableWithEnum(_fbb, _o, _rehasher); +} + +inline flatbuffers::Offset CreateTestSimpleTableWithEnum(flatbuffers::FlatBufferBuilder &_fbb, const TestSimpleTableWithEnumT *_o, const flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const TestSimpleTableWithEnumT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _color = _o->color; + return MyGame::Example::CreateTestSimpleTableWithEnum( + _fbb, + _color); +} + +inline StatT *Stat::UnPack(const flatbuffers::resolver_function_t *_resolver) const { + auto _o = new StatT(); + UnPackTo(_o, _resolver); + return _o; +} + +inline void Stat::UnPackTo(StatT *_o, const flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = id(); if (_e) _o->id = _e->str(); } + { auto _e = val(); _o->val = _e; } + { auto _e = count(); _o->count = _e; } +} + +inline flatbuffers::Offset Stat::Pack(flatbuffers::FlatBufferBuilder &_fbb, const StatT* _o, const flatbuffers::rehasher_function_t *_rehasher) { + return CreateStat(_fbb, _o, _rehasher); +} + +inline flatbuffers::Offset CreateStat(flatbuffers::FlatBufferBuilder &_fbb, const StatT *_o, const flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const StatT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _id = _o->id.empty() ? 0 : _fbb.CreateString(_o->id); + auto _val = _o->val; + auto _count = _o->count; + return MyGame::Example::CreateStat( + _fbb, + _id, + _val, + _count); +} + +inline ReferrableT *Referrable::UnPack(const flatbuffers::resolver_function_t *_resolver) const { + auto _o = new ReferrableT(); + UnPackTo(_o, _resolver); + return _o; +} + +inline void Referrable::UnPackTo(ReferrableT *_o, const flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = id(); _o->id = _e; } +} + +inline flatbuffers::Offset Referrable::Pack(flatbuffers::FlatBufferBuilder &_fbb, const ReferrableT* _o, const flatbuffers::rehasher_function_t *_rehasher) { + return CreateReferrable(_fbb, _o, _rehasher); +} + +inline flatbuffers::Offset CreateReferrable(flatbuffers::FlatBufferBuilder &_fbb, const ReferrableT *_o, const flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const ReferrableT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _id = _o->id; + return MyGame::Example::CreateReferrable( + _fbb, + _id); +} + +inline MonsterT *Monster::UnPack(const flatbuffers::resolver_function_t *_resolver) const { + auto _o = new MonsterT(); + UnPackTo(_o, _resolver); + return _o; +} + +inline void Monster::UnPackTo(MonsterT *_o, const flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = pos(); if (_e) _o->pos = std::unique_ptr(new MyGame::Example::Vec3(*_e)); } + { auto _e = mana(); _o->mana = _e; } + { auto _e = hp(); _o->hp = _e; } + { auto _e = name(); if (_e) _o->name = _e->str(); } + { auto _e = inventory(); if (_e) { _o->inventory.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->inventory[_i] = _e->Get(_i); } } } + { auto _e = color(); _o->color = _e; } + { auto _e = test_type(); _o->test.type = _e; } + { auto _e = test(); if (_e) _o->test.value = AnyUnion::UnPack(_e, test_type(), _resolver); } + { auto _e = test4(); if (_e) { _o->test4.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->test4[_i] = *_e->Get(_i); } } } + { auto _e = testarrayofstring(); if (_e) { _o->testarrayofstring.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->testarrayofstring[_i] = _e->Get(_i)->str(); } } } + { auto _e = testarrayoftables(); if (_e) { _o->testarrayoftables.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->testarrayoftables[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } + { auto _e = enemy(); if (_e) _o->enemy = std::unique_ptr(_e->UnPack(_resolver)); } + { auto _e = testnestedflatbuffer(); if (_e) { _o->testnestedflatbuffer.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->testnestedflatbuffer[_i] = _e->Get(_i); } } } + { auto _e = testempty(); if (_e) _o->testempty = std::unique_ptr(_e->UnPack(_resolver)); } + { auto _e = testbool(); _o->testbool = _e; } + { auto _e = testhashs32_fnv1(); _o->testhashs32_fnv1 = _e; } + { auto _e = testhashu32_fnv1(); _o->testhashu32_fnv1 = _e; } + { auto _e = testhashs64_fnv1(); _o->testhashs64_fnv1 = _e; } + { auto _e = testhashu64_fnv1(); _o->testhashu64_fnv1 = _e; } + { auto _e = testhashs32_fnv1a(); _o->testhashs32_fnv1a = _e; } + { auto _e = testhashu32_fnv1a(); //scalar resolver, naked +if (_resolver) (*_resolver)(reinterpret_cast(&_o->testhashu32_fnv1a), static_cast(_e)); else _o->testhashu32_fnv1a = nullptr; } + { auto _e = testhashs64_fnv1a(); _o->testhashs64_fnv1a = _e; } + { auto _e = testhashu64_fnv1a(); _o->testhashu64_fnv1a = _e; } + { auto _e = testarrayofbools(); if (_e) { _o->testarrayofbools.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->testarrayofbools[_i] = _e->Get(_i) != 0; } } } + { auto _e = testf(); _o->testf = _e; } + { auto _e = testf2(); _o->testf2 = _e; } + { auto _e = testf3(); _o->testf3 = _e; } + { auto _e = testarrayofstring2(); if (_e) { _o->testarrayofstring2.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->testarrayofstring2[_i] = _e->Get(_i)->str(); } } } + { auto _e = testarrayofsortedstruct(); if (_e) { _o->testarrayofsortedstruct.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->testarrayofsortedstruct[_i] = *_e->Get(_i); } } } + { auto _e = flex(); if (_e) { _o->flex.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->flex[_i] = _e->Get(_i); } } } + { auto _e = test5(); if (_e) { _o->test5.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->test5[_i] = *_e->Get(_i); } } } + { auto _e = vector_of_longs(); if (_e) { _o->vector_of_longs.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->vector_of_longs[_i] = _e->Get(_i); } } } + { auto _e = vector_of_doubles(); if (_e) { _o->vector_of_doubles.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->vector_of_doubles[_i] = _e->Get(_i); } } } + { auto _e = parent_namespace_test(); if (_e) _o->parent_namespace_test = std::unique_ptr(_e->UnPack(_resolver)); } + { auto _e = vector_of_referrables(); if (_e) { _o->vector_of_referrables.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->vector_of_referrables[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } + { auto _e = single_weak_reference(); //scalar resolver, naked +if (_resolver) (*_resolver)(reinterpret_cast(&_o->single_weak_reference), static_cast(_e)); else _o->single_weak_reference = nullptr; } + { auto _e = vector_of_weak_references(); if (_e) { _o->vector_of_weak_references.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { //vector resolver, naked +if (_resolver) (*_resolver)(reinterpret_cast(&_o->vector_of_weak_references[_i]), static_cast(_e->Get(_i))); else _o->vector_of_weak_references[_i] = nullptr; } } } + { auto _e = vector_of_strong_referrables(); if (_e) { _o->vector_of_strong_referrables.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->vector_of_strong_referrables[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } + { auto _e = co_owning_reference(); //scalar resolver, naked +if (_resolver) (*_resolver)(reinterpret_cast(&_o->co_owning_reference), static_cast(_e)); else _o->co_owning_reference = nullptr; } + { auto _e = vector_of_co_owning_references(); if (_e) { _o->vector_of_co_owning_references.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { //vector resolver, default_ptr_type +if (_resolver) (*_resolver)(reinterpret_cast(&_o->vector_of_co_owning_references[_i]), static_cast(_e->Get(_i)));/* else do nothing */; } } } + { auto _e = non_owning_reference(); //scalar resolver, naked +if (_resolver) (*_resolver)(reinterpret_cast(&_o->non_owning_reference), static_cast(_e)); else _o->non_owning_reference = nullptr; } + { auto _e = vector_of_non_owning_references(); if (_e) { _o->vector_of_non_owning_references.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { //vector resolver, naked +if (_resolver) (*_resolver)(reinterpret_cast(&_o->vector_of_non_owning_references[_i]), static_cast(_e->Get(_i))); else _o->vector_of_non_owning_references[_i] = nullptr; } } } + { auto _e = any_unique_type(); _o->any_unique.type = _e; } + { auto _e = any_unique(); if (_e) _o->any_unique.value = AnyUniqueAliasesUnion::UnPack(_e, any_unique_type(), _resolver); } + { auto _e = any_ambiguous_type(); _o->any_ambiguous.type = _e; } + { auto _e = any_ambiguous(); if (_e) _o->any_ambiguous.value = AnyAmbiguousAliasesUnion::UnPack(_e, any_ambiguous_type(), _resolver); } + { auto _e = vector_of_enums(); if (_e) { _o->vector_of_enums.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->vector_of_enums[_i] = static_cast(_e->Get(_i)); } } } + { auto _e = signed_enum(); _o->signed_enum = _e; } +} + +inline flatbuffers::Offset Monster::Pack(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT* _o, const flatbuffers::rehasher_function_t *_rehasher) { + return CreateMonster(_fbb, _o, _rehasher); +} + +inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder &_fbb, const MonsterT *_o, const flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const MonsterT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _pos = _o->pos ? _o->pos.get() : 0; + auto _mana = _o->mana; + auto _hp = _o->hp; + auto _name = _fbb.CreateString(_o->name); + auto _inventory = _o->inventory.size() ? _fbb.CreateVector(_o->inventory) : 0; + auto _color = _o->color; + auto _test_type = _o->test.type; + auto _test = _o->test.Pack(_fbb); + auto _test4 = _o->test4.size() ? _fbb.CreateVectorOfStructs(_o->test4) : 0; + auto _testarrayofstring = _o->testarrayofstring.size() ? _fbb.CreateVectorOfStrings(_o->testarrayofstring) : 0; + auto _testarrayoftables = _o->testarrayoftables.size() ? _fbb.CreateVector> (_o->testarrayoftables.size(), [](size_t i, _VectorArgs *__va) { return CreateMonster(*__va->__fbb, __va->__o->testarrayoftables[i].get(), __va->__rehasher); }, &_va ) : 0; + auto _enemy = _o->enemy ? CreateMonster(_fbb, _o->enemy.get(), _rehasher) : 0; + auto _testnestedflatbuffer = _o->testnestedflatbuffer.size() ? _fbb.CreateVector(_o->testnestedflatbuffer) : 0; + auto _testempty = _o->testempty ? CreateStat(_fbb, _o->testempty.get(), _rehasher) : 0; + auto _testbool = _o->testbool; + auto _testhashs32_fnv1 = _o->testhashs32_fnv1; + auto _testhashu32_fnv1 = _o->testhashu32_fnv1; + auto _testhashs64_fnv1 = _o->testhashs64_fnv1; + auto _testhashu64_fnv1 = _o->testhashu64_fnv1; + auto _testhashs32_fnv1a = _o->testhashs32_fnv1a; + auto _testhashu32_fnv1a = _rehasher ? static_cast((*_rehasher)(_o->testhashu32_fnv1a)) : 0; + auto _testhashs64_fnv1a = _o->testhashs64_fnv1a; + auto _testhashu64_fnv1a = _o->testhashu64_fnv1a; + auto _testarrayofbools = _o->testarrayofbools.size() ? _fbb.CreateVector(_o->testarrayofbools) : 0; + auto _testf = _o->testf; + auto _testf2 = _o->testf2; + auto _testf3 = _o->testf3; + auto _testarrayofstring2 = _o->testarrayofstring2.size() ? _fbb.CreateVectorOfStrings(_o->testarrayofstring2) : 0; + auto _testarrayofsortedstruct = _o->testarrayofsortedstruct.size() ? _fbb.CreateVectorOfStructs(_o->testarrayofsortedstruct) : 0; + auto _flex = _o->flex.size() ? _fbb.CreateVector(_o->flex) : 0; + auto _test5 = _o->test5.size() ? _fbb.CreateVectorOfStructs(_o->test5) : 0; + auto _vector_of_longs = _o->vector_of_longs.size() ? _fbb.CreateVector(_o->vector_of_longs) : 0; + auto _vector_of_doubles = _o->vector_of_doubles.size() ? _fbb.CreateVector(_o->vector_of_doubles) : 0; + auto _parent_namespace_test = _o->parent_namespace_test ? CreateInParentNamespace(_fbb, _o->parent_namespace_test.get(), _rehasher) : 0; + auto _vector_of_referrables = _o->vector_of_referrables.size() ? _fbb.CreateVector> (_o->vector_of_referrables.size(), [](size_t i, _VectorArgs *__va) { return CreateReferrable(*__va->__fbb, __va->__o->vector_of_referrables[i].get(), __va->__rehasher); }, &_va ) : 0; + auto _single_weak_reference = _rehasher ? static_cast((*_rehasher)(_o->single_weak_reference)) : 0; + auto _vector_of_weak_references = _o->vector_of_weak_references.size() ? _fbb.CreateVector(_o->vector_of_weak_references.size(), [](size_t i, _VectorArgs *__va) { return __va->__rehasher ? static_cast((*__va->__rehasher)(__va->__o->vector_of_weak_references[i])) : 0; }, &_va ) : 0; + auto _vector_of_strong_referrables = _o->vector_of_strong_referrables.size() ? _fbb.CreateVector> (_o->vector_of_strong_referrables.size(), [](size_t i, _VectorArgs *__va) { return CreateReferrable(*__va->__fbb, __va->__o->vector_of_strong_referrables[i].get(), __va->__rehasher); }, &_va ) : 0; + auto _co_owning_reference = _rehasher ? static_cast((*_rehasher)(_o->co_owning_reference)) : 0; + auto _vector_of_co_owning_references = _o->vector_of_co_owning_references.size() ? _fbb.CreateVector(_o->vector_of_co_owning_references.size(), [](size_t i, _VectorArgs *__va) { return __va->__rehasher ? static_cast((*__va->__rehasher)(__va->__o->vector_of_co_owning_references[i].get())) : 0; }, &_va ) : 0; + auto _non_owning_reference = _rehasher ? static_cast((*_rehasher)(_o->non_owning_reference)) : 0; + auto _vector_of_non_owning_references = _o->vector_of_non_owning_references.size() ? _fbb.CreateVector(_o->vector_of_non_owning_references.size(), [](size_t i, _VectorArgs *__va) { return __va->__rehasher ? static_cast((*__va->__rehasher)(__va->__o->vector_of_non_owning_references[i])) : 0; }, &_va ) : 0; + auto _any_unique_type = _o->any_unique.type; + auto _any_unique = _o->any_unique.Pack(_fbb); + auto _any_ambiguous_type = _o->any_ambiguous.type; + auto _any_ambiguous = _o->any_ambiguous.Pack(_fbb); + auto _vector_of_enums = _o->vector_of_enums.size() ? _fbb.CreateVectorScalarCast(flatbuffers::data(_o->vector_of_enums), _o->vector_of_enums.size()) : 0; + auto _signed_enum = _o->signed_enum; + return MyGame::Example::CreateMonster( + _fbb, + _pos, + _mana, + _hp, + _name, + _inventory, + _color, + _test_type, + _test, + _test4, + _testarrayofstring, + _testarrayoftables, + _enemy, + _testnestedflatbuffer, + _testempty, + _testbool, + _testhashs32_fnv1, + _testhashu32_fnv1, + _testhashs64_fnv1, + _testhashu64_fnv1, + _testhashs32_fnv1a, + _testhashu32_fnv1a, + _testhashs64_fnv1a, + _testhashu64_fnv1a, + _testarrayofbools, + _testf, + _testf2, + _testf3, + _testarrayofstring2, + _testarrayofsortedstruct, + _flex, + _test5, + _vector_of_longs, + _vector_of_doubles, + _parent_namespace_test, + _vector_of_referrables, + _single_weak_reference, + _vector_of_weak_references, + _vector_of_strong_referrables, + _co_owning_reference, + _vector_of_co_owning_references, + _non_owning_reference, + _vector_of_non_owning_references, + _any_unique_type, + _any_unique, + _any_ambiguous_type, + _any_ambiguous, + _vector_of_enums, + _signed_enum); +} + +inline TypeAliasesT *TypeAliases::UnPack(const flatbuffers::resolver_function_t *_resolver) const { + auto _o = new TypeAliasesT(); + UnPackTo(_o, _resolver); + return _o; +} + +inline void TypeAliases::UnPackTo(TypeAliasesT *_o, const flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = i8(); _o->i8 = _e; } + { auto _e = u8(); _o->u8 = _e; } + { auto _e = i16(); _o->i16 = _e; } + { auto _e = u16(); _o->u16 = _e; } + { auto _e = i32(); _o->i32 = _e; } + { auto _e = u32(); _o->u32 = _e; } + { auto _e = i64(); _o->i64 = _e; } + { auto _e = u64(); _o->u64 = _e; } + { auto _e = f32(); _o->f32 = _e; } + { auto _e = f64(); _o->f64 = _e; } + { auto _e = v8(); if (_e) { _o->v8.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->v8[_i] = _e->Get(_i); } } } + { auto _e = vf64(); if (_e) { _o->vf64.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->vf64[_i] = _e->Get(_i); } } } +} + +inline flatbuffers::Offset TypeAliases::Pack(flatbuffers::FlatBufferBuilder &_fbb, const TypeAliasesT* _o, const flatbuffers::rehasher_function_t *_rehasher) { + return CreateTypeAliases(_fbb, _o, _rehasher); +} + +inline flatbuffers::Offset CreateTypeAliases(flatbuffers::FlatBufferBuilder &_fbb, const TypeAliasesT *_o, const flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const TypeAliasesT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _i8 = _o->i8; + auto _u8 = _o->u8; + auto _i16 = _o->i16; + auto _u16 = _o->u16; + auto _i32 = _o->i32; + auto _u32 = _o->u32; + auto _i64 = _o->i64; + auto _u64 = _o->u64; + auto _f32 = _o->f32; + auto _f64 = _o->f64; + auto _v8 = _o->v8.size() ? _fbb.CreateVector(_o->v8) : 0; + auto _vf64 = _o->vf64.size() ? _fbb.CreateVector(_o->vf64) : 0; + return MyGame::Example::CreateTypeAliases( + _fbb, + _i8, + _u8, + _i16, + _u16, + _i32, + _u32, + _i64, + _u64, + _f32, + _f64, + _v8, + _vf64); +} + +inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *obj, Any type) { + switch (type) { + case Any::NONE: { + return true; + } + case Any::Monster: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case Any::TestSimpleTableWithEnum: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case Any::MyGame_Example2_Monster: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + default: return true; + } +} + +inline bool VerifyAnyVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types) { + if (!values || !types) return !values && !types; + if (values->size() != types->size()) return false; + for (flatbuffers::uoffset_t i = 0; i < values->size(); ++i) { + if (!VerifyAny( + verifier, values->Get(i), types->GetEnum(i))) { + return false; + } + } + return true; +} + +inline void *AnyUnion::UnPack(const void *obj, Any type, const flatbuffers::resolver_function_t *resolver) { + switch (type) { + case Any::Monster: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case Any::TestSimpleTableWithEnum: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case Any::MyGame_Example2_Monster: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + default: return nullptr; + } +} + +inline flatbuffers::Offset AnyUnion::Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *_rehasher) const { + switch (type) { + case Any::Monster: { + auto ptr = reinterpret_cast(value); + return CreateMonster(_fbb, ptr, _rehasher).Union(); + } + case Any::TestSimpleTableWithEnum: { + auto ptr = reinterpret_cast(value); + return CreateTestSimpleTableWithEnum(_fbb, ptr, _rehasher).Union(); + } + case Any::MyGame_Example2_Monster: { + auto ptr = reinterpret_cast(value); + return CreateMonster(_fbb, ptr, _rehasher).Union(); + } + default: return 0; + } +} + +inline AnyUnion::AnyUnion(const AnyUnion &u) FLATBUFFERS_NOEXCEPT : type(u.type), value(nullptr) { + switch (type) { + case Any::Monster: { + FLATBUFFERS_ASSERT(false); // MyGame::Example::MonsterT not copyable. + break; + } + case Any::TestSimpleTableWithEnum: { + value = new MyGame::Example::TestSimpleTableWithEnumT(*reinterpret_cast(u.value)); + break; + } + case Any::MyGame_Example2_Monster: { + value = new MyGame::Example2::MonsterT(*reinterpret_cast(u.value)); + break; + } + default: + break; + } +} + +inline void AnyUnion::Reset() { + switch (type) { + case Any::Monster: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case Any::TestSimpleTableWithEnum: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case Any::MyGame_Example2_Monster: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + default: break; + } + value = nullptr; + type = Any::NONE; +} + +inline bool VerifyAnyUniqueAliases(flatbuffers::Verifier &verifier, const void *obj, AnyUniqueAliases type) { + switch (type) { + case AnyUniqueAliases::NONE: { + return true; + } + case AnyUniqueAliases::M: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case AnyUniqueAliases::TS: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case AnyUniqueAliases::M2: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + default: return true; + } +} + +inline bool VerifyAnyUniqueAliasesVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types) { + if (!values || !types) return !values && !types; + if (values->size() != types->size()) return false; + for (flatbuffers::uoffset_t i = 0; i < values->size(); ++i) { + if (!VerifyAnyUniqueAliases( + verifier, values->Get(i), types->GetEnum(i))) { + return false; + } + } + return true; +} + +inline void *AnyUniqueAliasesUnion::UnPack(const void *obj, AnyUniqueAliases type, const flatbuffers::resolver_function_t *resolver) { + switch (type) { + case AnyUniqueAliases::M: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case AnyUniqueAliases::TS: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case AnyUniqueAliases::M2: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + default: return nullptr; + } +} + +inline flatbuffers::Offset AnyUniqueAliasesUnion::Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *_rehasher) const { + switch (type) { + case AnyUniqueAliases::M: { + auto ptr = reinterpret_cast(value); + return CreateMonster(_fbb, ptr, _rehasher).Union(); + } + case AnyUniqueAliases::TS: { + auto ptr = reinterpret_cast(value); + return CreateTestSimpleTableWithEnum(_fbb, ptr, _rehasher).Union(); + } + case AnyUniqueAliases::M2: { + auto ptr = reinterpret_cast(value); + return CreateMonster(_fbb, ptr, _rehasher).Union(); + } + default: return 0; + } +} + +inline AnyUniqueAliasesUnion::AnyUniqueAliasesUnion(const AnyUniqueAliasesUnion &u) FLATBUFFERS_NOEXCEPT : type(u.type), value(nullptr) { + switch (type) { + case AnyUniqueAliases::M: { + FLATBUFFERS_ASSERT(false); // MyGame::Example::MonsterT not copyable. + break; + } + case AnyUniqueAliases::TS: { + value = new MyGame::Example::TestSimpleTableWithEnumT(*reinterpret_cast(u.value)); + break; + } + case AnyUniqueAliases::M2: { + value = new MyGame::Example2::MonsterT(*reinterpret_cast(u.value)); + break; + } + default: + break; + } +} + +inline void AnyUniqueAliasesUnion::Reset() { + switch (type) { + case AnyUniqueAliases::M: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case AnyUniqueAliases::TS: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case AnyUniqueAliases::M2: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + default: break; + } + value = nullptr; + type = AnyUniqueAliases::NONE; +} + +inline bool VerifyAnyAmbiguousAliases(flatbuffers::Verifier &verifier, const void *obj, AnyAmbiguousAliases type) { + switch (type) { + case AnyAmbiguousAliases::NONE: { + return true; + } + case AnyAmbiguousAliases::M1: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case AnyAmbiguousAliases::M2: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + case AnyAmbiguousAliases::M3: { + auto ptr = reinterpret_cast(obj); + return verifier.VerifyTable(ptr); + } + default: return true; + } +} + +inline bool VerifyAnyAmbiguousAliasesVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types) { + if (!values || !types) return !values && !types; + if (values->size() != types->size()) return false; + for (flatbuffers::uoffset_t i = 0; i < values->size(); ++i) { + if (!VerifyAnyAmbiguousAliases( + verifier, values->Get(i), types->GetEnum(i))) { + return false; + } + } + return true; +} + +inline void *AnyAmbiguousAliasesUnion::UnPack(const void *obj, AnyAmbiguousAliases type, const flatbuffers::resolver_function_t *resolver) { + switch (type) { + case AnyAmbiguousAliases::M1: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case AnyAmbiguousAliases::M2: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + case AnyAmbiguousAliases::M3: { + auto ptr = reinterpret_cast(obj); + return ptr->UnPack(resolver); + } + default: return nullptr; + } +} + +inline flatbuffers::Offset AnyAmbiguousAliasesUnion::Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *_rehasher) const { + switch (type) { + case AnyAmbiguousAliases::M1: { + auto ptr = reinterpret_cast(value); + return CreateMonster(_fbb, ptr, _rehasher).Union(); + } + case AnyAmbiguousAliases::M2: { + auto ptr = reinterpret_cast(value); + return CreateMonster(_fbb, ptr, _rehasher).Union(); + } + case AnyAmbiguousAliases::M3: { + auto ptr = reinterpret_cast(value); + return CreateMonster(_fbb, ptr, _rehasher).Union(); + } + default: return 0; + } +} + +inline AnyAmbiguousAliasesUnion::AnyAmbiguousAliasesUnion(const AnyAmbiguousAliasesUnion &u) FLATBUFFERS_NOEXCEPT : type(u.type), value(nullptr) { + switch (type) { + case AnyAmbiguousAliases::M1: { + FLATBUFFERS_ASSERT(false); // MyGame::Example::MonsterT not copyable. + break; + } + case AnyAmbiguousAliases::M2: { + FLATBUFFERS_ASSERT(false); // MyGame::Example::MonsterT not copyable. + break; + } + case AnyAmbiguousAliases::M3: { + FLATBUFFERS_ASSERT(false); // MyGame::Example::MonsterT not copyable. + break; + } + default: + break; + } +} + +inline void AnyAmbiguousAliasesUnion::Reset() { + switch (type) { + case AnyAmbiguousAliases::M1: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case AnyAmbiguousAliases::M2: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + case AnyAmbiguousAliases::M3: { + auto ptr = reinterpret_cast(value); + delete ptr; + break; + } + default: break; + } + value = nullptr; + type = AnyAmbiguousAliases::NONE; +} + +inline const flatbuffers::TypeTable *ColorTypeTable() { + static const flatbuffers::TypeCode type_codes[] = { + { flatbuffers::ET_UCHAR, 0, 0 }, + { flatbuffers::ET_UCHAR, 0, 0 }, + { flatbuffers::ET_UCHAR, 0, 0 } + }; + static const flatbuffers::TypeFunction type_refs[] = { + MyGame::Example::ColorTypeTable + }; + static const int64_t values[] = { 1, 2, 8 }; + static const char * const names[] = { + "Red", + "Green", + "Blue" + }; + static const flatbuffers::TypeTable tt = { + flatbuffers::ST_ENUM, 3, type_codes, type_refs, values, names + }; + return &tt; +} + +inline const flatbuffers::TypeTable *RaceTypeTable() { + static const flatbuffers::TypeCode type_codes[] = { + { flatbuffers::ET_CHAR, 0, 0 }, + { flatbuffers::ET_CHAR, 0, 0 }, + { flatbuffers::ET_CHAR, 0, 0 }, + { flatbuffers::ET_CHAR, 0, 0 } + }; + static const flatbuffers::TypeFunction type_refs[] = { + MyGame::Example::RaceTypeTable + }; + static const int64_t values[] = { -1, 0, 1, 2 }; + static const char * const names[] = { + "None", + "Human", + "Dwarf", + "Elf" + }; + static const flatbuffers::TypeTable tt = { + flatbuffers::ST_ENUM, 4, type_codes, type_refs, values, names + }; + return &tt; +} + +inline const flatbuffers::TypeTable *AnyTypeTable() { + static const flatbuffers::TypeCode type_codes[] = { + { flatbuffers::ET_SEQUENCE, 0, -1 }, + { flatbuffers::ET_SEQUENCE, 0, 0 }, + { flatbuffers::ET_SEQUENCE, 0, 1 }, + { flatbuffers::ET_SEQUENCE, 0, 2 } + }; + static const flatbuffers::TypeFunction type_refs[] = { + MyGame::Example::MonsterTypeTable, + MyGame::Example::TestSimpleTableWithEnumTypeTable, + MyGame::Example2::MonsterTypeTable + }; + static const char * const names[] = { + "NONE", + "Monster", + "TestSimpleTableWithEnum", + "MyGame_Example2_Monster" + }; + static const flatbuffers::TypeTable tt = { + flatbuffers::ST_UNION, 4, type_codes, type_refs, nullptr, names + }; + return &tt; +} + +inline const flatbuffers::TypeTable *AnyUniqueAliasesTypeTable() { + static const flatbuffers::TypeCode type_codes[] = { + { flatbuffers::ET_SEQUENCE, 0, -1 }, + { flatbuffers::ET_SEQUENCE, 0, 0 }, + { flatbuffers::ET_SEQUENCE, 0, 1 }, + { flatbuffers::ET_SEQUENCE, 0, 2 } + }; + static const flatbuffers::TypeFunction type_refs[] = { + MyGame::Example::MonsterTypeTable, + MyGame::Example::TestSimpleTableWithEnumTypeTable, + MyGame::Example2::MonsterTypeTable + }; + static const char * const names[] = { + "NONE", + "M", + "TS", + "M2" + }; + static const flatbuffers::TypeTable tt = { + flatbuffers::ST_UNION, 4, type_codes, type_refs, nullptr, names + }; + return &tt; +} + +inline const flatbuffers::TypeTable *AnyAmbiguousAliasesTypeTable() { + static const flatbuffers::TypeCode type_codes[] = { + { flatbuffers::ET_SEQUENCE, 0, -1 }, + { flatbuffers::ET_SEQUENCE, 0, 0 }, + { flatbuffers::ET_SEQUENCE, 0, 0 }, + { flatbuffers::ET_SEQUENCE, 0, 0 } + }; + static const flatbuffers::TypeFunction type_refs[] = { + MyGame::Example::MonsterTypeTable + }; + static const char * const names[] = { + "NONE", + "M1", + "M2", + "M3" + }; + static const flatbuffers::TypeTable tt = { + flatbuffers::ST_UNION, 4, type_codes, type_refs, nullptr, names + }; + return &tt; +} + +} // namespace Example + +inline const flatbuffers::TypeTable *InParentNamespaceTypeTable() { + static const flatbuffers::TypeTable tt = { + flatbuffers::ST_TABLE, 0, nullptr, nullptr, nullptr, nullptr + }; + return &tt; +} + +namespace Example2 { + +inline const flatbuffers::TypeTable *MonsterTypeTable() { + static const flatbuffers::TypeTable tt = { + flatbuffers::ST_TABLE, 0, nullptr, nullptr, nullptr, nullptr + }; + return &tt; +} + +} // namespace Example2 + +namespace Example { + +inline const flatbuffers::TypeTable *TestTypeTable() { + static const flatbuffers::TypeCode type_codes[] = { + { flatbuffers::ET_SHORT, 0, -1 }, + { flatbuffers::ET_CHAR, 0, -1 } + }; + static const int64_t values[] = { 0, 2, 4 }; + static const char * const names[] = { + "a", + "b" + }; + static const flatbuffers::TypeTable tt = { + flatbuffers::ST_STRUCT, 2, type_codes, nullptr, values, names + }; + return &tt; +} + +inline const flatbuffers::TypeTable *TestSimpleTableWithEnumTypeTable() { + static const flatbuffers::TypeCode type_codes[] = { + { flatbuffers::ET_UCHAR, 0, 0 } + }; + static const flatbuffers::TypeFunction type_refs[] = { + MyGame::Example::ColorTypeTable + }; + static const char * const names[] = { + "color" + }; + static const flatbuffers::TypeTable tt = { + flatbuffers::ST_TABLE, 1, type_codes, type_refs, nullptr, names + }; + return &tt; +} + +inline const flatbuffers::TypeTable *Vec3TypeTable() { + static const flatbuffers::TypeCode type_codes[] = { + { flatbuffers::ET_FLOAT, 0, -1 }, + { flatbuffers::ET_FLOAT, 0, -1 }, + { flatbuffers::ET_FLOAT, 0, -1 }, + { flatbuffers::ET_DOUBLE, 0, -1 }, + { flatbuffers::ET_UCHAR, 0, 0 }, + { flatbuffers::ET_SEQUENCE, 0, 1 } + }; + static const flatbuffers::TypeFunction type_refs[] = { + MyGame::Example::ColorTypeTable, + MyGame::Example::TestTypeTable + }; + static const int64_t values[] = { 0, 4, 8, 16, 24, 26, 32 }; + static const char * const names[] = { + "x", + "y", + "z", + "test1", + "test2", + "test3" + }; + static const flatbuffers::TypeTable tt = { + flatbuffers::ST_STRUCT, 6, type_codes, type_refs, values, names + }; + return &tt; +} + +inline const flatbuffers::TypeTable *AbilityTypeTable() { + static const flatbuffers::TypeCode type_codes[] = { + { flatbuffers::ET_UINT, 0, -1 }, + { flatbuffers::ET_UINT, 0, -1 } + }; + static const int64_t values[] = { 0, 4, 8 }; + static const char * const names[] = { + "id", + "distance" + }; + static const flatbuffers::TypeTable tt = { + flatbuffers::ST_STRUCT, 2, type_codes, nullptr, values, names + }; + return &tt; +} + +inline const flatbuffers::TypeTable *StatTypeTable() { + static const flatbuffers::TypeCode type_codes[] = { + { flatbuffers::ET_STRING, 0, -1 }, + { flatbuffers::ET_LONG, 0, -1 }, + { flatbuffers::ET_USHORT, 0, -1 } + }; + static const char * const names[] = { + "id", + "val", + "count" + }; + static const flatbuffers::TypeTable tt = { + flatbuffers::ST_TABLE, 3, type_codes, nullptr, nullptr, names + }; + return &tt; +} + +inline const flatbuffers::TypeTable *ReferrableTypeTable() { + static const flatbuffers::TypeCode type_codes[] = { + { flatbuffers::ET_ULONG, 0, -1 } + }; + static const char * const names[] = { + "id" + }; + static const flatbuffers::TypeTable tt = { + flatbuffers::ST_TABLE, 1, type_codes, nullptr, nullptr, names + }; + return &tt; +} + +inline const flatbuffers::TypeTable *MonsterTypeTable() { + static const flatbuffers::TypeCode type_codes[] = { + { flatbuffers::ET_SEQUENCE, 0, 0 }, + { flatbuffers::ET_SHORT, 0, -1 }, + { flatbuffers::ET_SHORT, 0, -1 }, + { flatbuffers::ET_STRING, 0, -1 }, + { flatbuffers::ET_BOOL, 0, -1 }, + { flatbuffers::ET_UCHAR, 1, -1 }, + { flatbuffers::ET_UCHAR, 0, 1 }, + { flatbuffers::ET_UTYPE, 0, 2 }, + { flatbuffers::ET_SEQUENCE, 0, 2 }, + { flatbuffers::ET_SEQUENCE, 1, 3 }, + { flatbuffers::ET_STRING, 1, -1 }, + { flatbuffers::ET_SEQUENCE, 1, 4 }, + { flatbuffers::ET_SEQUENCE, 0, 4 }, + { flatbuffers::ET_UCHAR, 1, -1 }, + { flatbuffers::ET_SEQUENCE, 0, 5 }, + { flatbuffers::ET_BOOL, 0, -1 }, + { flatbuffers::ET_INT, 0, -1 }, + { flatbuffers::ET_UINT, 0, -1 }, + { flatbuffers::ET_LONG, 0, -1 }, + { flatbuffers::ET_ULONG, 0, -1 }, + { flatbuffers::ET_INT, 0, -1 }, + { flatbuffers::ET_UINT, 0, -1 }, + { flatbuffers::ET_LONG, 0, -1 }, + { flatbuffers::ET_ULONG, 0, -1 }, + { flatbuffers::ET_BOOL, 1, -1 }, + { flatbuffers::ET_FLOAT, 0, -1 }, + { flatbuffers::ET_FLOAT, 0, -1 }, + { flatbuffers::ET_FLOAT, 0, -1 }, + { flatbuffers::ET_STRING, 1, -1 }, + { flatbuffers::ET_SEQUENCE, 1, 6 }, + { flatbuffers::ET_UCHAR, 1, -1 }, + { flatbuffers::ET_SEQUENCE, 1, 3 }, + { flatbuffers::ET_LONG, 1, -1 }, + { flatbuffers::ET_DOUBLE, 1, -1 }, + { flatbuffers::ET_SEQUENCE, 0, 7 }, + { flatbuffers::ET_SEQUENCE, 1, 8 }, + { flatbuffers::ET_ULONG, 0, -1 }, + { flatbuffers::ET_ULONG, 1, -1 }, + { flatbuffers::ET_SEQUENCE, 1, 8 }, + { flatbuffers::ET_ULONG, 0, -1 }, + { flatbuffers::ET_ULONG, 1, -1 }, + { flatbuffers::ET_ULONG, 0, -1 }, + { flatbuffers::ET_ULONG, 1, -1 }, + { flatbuffers::ET_UTYPE, 0, 9 }, + { flatbuffers::ET_SEQUENCE, 0, 9 }, + { flatbuffers::ET_UTYPE, 0, 10 }, + { flatbuffers::ET_SEQUENCE, 0, 10 }, + { flatbuffers::ET_UCHAR, 1, 1 }, + { flatbuffers::ET_CHAR, 0, 11 } + }; + static const flatbuffers::TypeFunction type_refs[] = { + MyGame::Example::Vec3TypeTable, + MyGame::Example::ColorTypeTable, + MyGame::Example::AnyTypeTable, + MyGame::Example::TestTypeTable, + MyGame::Example::MonsterTypeTable, + MyGame::Example::StatTypeTable, + MyGame::Example::AbilityTypeTable, + MyGame::InParentNamespaceTypeTable, + MyGame::Example::ReferrableTypeTable, + MyGame::Example::AnyUniqueAliasesTypeTable, + MyGame::Example::AnyAmbiguousAliasesTypeTable, + MyGame::Example::RaceTypeTable + }; + static const char * const names[] = { + "pos", + "mana", + "hp", + "name", + "friendly", + "inventory", + "color", + "test_type", + "test", + "test4", + "testarrayofstring", + "testarrayoftables", + "enemy", + "testnestedflatbuffer", + "testempty", + "testbool", + "testhashs32_fnv1", + "testhashu32_fnv1", + "testhashs64_fnv1", + "testhashu64_fnv1", + "testhashs32_fnv1a", + "testhashu32_fnv1a", + "testhashs64_fnv1a", + "testhashu64_fnv1a", + "testarrayofbools", + "testf", + "testf2", + "testf3", + "testarrayofstring2", + "testarrayofsortedstruct", + "flex", + "test5", + "vector_of_longs", + "vector_of_doubles", + "parent_namespace_test", + "vector_of_referrables", + "single_weak_reference", + "vector_of_weak_references", + "vector_of_strong_referrables", + "co_owning_reference", + "vector_of_co_owning_references", + "non_owning_reference", + "vector_of_non_owning_references", + "any_unique_type", + "any_unique", + "any_ambiguous_type", + "any_ambiguous", + "vector_of_enums", + "signed_enum" + }; + static const flatbuffers::TypeTable tt = { + flatbuffers::ST_TABLE, 49, type_codes, type_refs, nullptr, names + }; + return &tt; +} + +inline const flatbuffers::TypeTable *TypeAliasesTypeTable() { + static const flatbuffers::TypeCode type_codes[] = { + { flatbuffers::ET_CHAR, 0, -1 }, + { flatbuffers::ET_UCHAR, 0, -1 }, + { flatbuffers::ET_SHORT, 0, -1 }, + { flatbuffers::ET_USHORT, 0, -1 }, + { flatbuffers::ET_INT, 0, -1 }, + { flatbuffers::ET_UINT, 0, -1 }, + { flatbuffers::ET_LONG, 0, -1 }, + { flatbuffers::ET_ULONG, 0, -1 }, + { flatbuffers::ET_FLOAT, 0, -1 }, + { flatbuffers::ET_DOUBLE, 0, -1 }, + { flatbuffers::ET_CHAR, 1, -1 }, + { flatbuffers::ET_DOUBLE, 1, -1 } + }; + static const char * const names[] = { + "i8", + "u8", + "i16", + "u16", + "i32", + "u32", + "i64", + "u64", + "f32", + "f64", + "v8", + "vf64" + }; + static const flatbuffers::TypeTable tt = { + flatbuffers::ST_TABLE, 12, type_codes, nullptr, nullptr, names + }; + return &tt; +} + +inline const MyGame::Example::Monster *GetMonster(const void *buf) { + return flatbuffers::GetRoot(buf); +} + +inline const MyGame::Example::Monster *GetSizePrefixedMonster(const void *buf) { + return flatbuffers::GetSizePrefixedRoot(buf); +} + +inline Monster *GetMutableMonster(void *buf) { + return flatbuffers::GetMutableRoot(buf); +} + +inline const char *MonsterIdentifier() { + return "MONS"; +} + +inline bool MonsterBufferHasIdentifier(const void *buf) { + return flatbuffers::BufferHasIdentifier( + buf, MonsterIdentifier()); +} + +inline bool VerifyMonsterBuffer( + flatbuffers::Verifier &verifier) { + return verifier.VerifyBuffer(MonsterIdentifier()); +} + +inline bool VerifySizePrefixedMonsterBuffer( + flatbuffers::Verifier &verifier) { + return verifier.VerifySizePrefixedBuffer(MonsterIdentifier()); +} + +inline const char *MonsterExtension() { + return "mon"; +} + +inline void FinishMonsterBuffer( + flatbuffers::FlatBufferBuilder &fbb, + flatbuffers::Offset root) { + fbb.Finish(root, MonsterIdentifier()); +} + +inline void FinishSizePrefixedMonsterBuffer( + flatbuffers::FlatBufferBuilder &fbb, + flatbuffers::Offset root) { + fbb.FinishSizePrefixed(root, MonsterIdentifier()); +} + +inline std::unique_ptr UnPackMonster( + const void *buf, + const flatbuffers::resolver_function_t *res = nullptr) { + return std::unique_ptr(GetMonster(buf)->UnPack(res)); +} + +inline std::unique_ptr UnPackSizePrefixedMonster( + const void *buf, + const flatbuffers::resolver_function_t *res = nullptr) { + return std::unique_ptr(GetSizePrefixedMonster(buf)->UnPack(res)); +} + +} // namespace Example +} // namespace MyGame + +#endif // FLATBUFFERS_GENERATED_MONSTERTEST_MYGAME_EXAMPLE_H_ diff --git a/tests/cpp17/test_cpp17.cpp b/tests/cpp17/test_cpp17.cpp new file mode 100644 index 00000000000..6c03645fbf0 --- /dev/null +++ b/tests/cpp17/test_cpp17.cpp @@ -0,0 +1,38 @@ +/* + * Copyright 2014 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// This is a sandbox for modeling C++17 code generator. +// C++17 code generator: "flatc --cpp_std c++17". +// Warning: +// This is an experimental feature and could change at any time. + +#include "flatbuffers/flatbuffers.h" +#include "flatbuffers/flexbuffers.h" +#include "flatbuffers/idl.h" +#include "flatbuffers/minireflect.h" +#include "flatbuffers/registry.h" +#include "flatbuffers/util.h" + +// Embed generated code into an isolated namespace. +namespace cpp17 { +#include "generated_cpp17/monster_test_generated.h" +} // namespace cpp17 + +namespace cpp11 { +#include "../monster_test_generated.h" +} // namespace cpp11 + +int main(int /*argc*/, const char * /*argv*/ []) { return 0; } diff --git a/tests/generate_code.bat b/tests/generate_code.bat index 8f260140947..e2e2a44cb23 100644 --- a/tests/generate_code.bat +++ b/tests/generate_code.bat @@ -12,32 +12,55 @@ :: See the License for the specific language governing permissions and :: limitations under the License. +@SETLOCAL + set buildtype=Release if "%1"=="-b" set buildtype=%2 -..\%buildtype%\flatc.exe --cpp --java --csharp --dart --go --binary --lobster --lua --js --ts --php --rust --grpc --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --cpp-ptr-type flatbuffers::unique_ptr --no-fb-import -I include_test monster_test.fbs monsterdata_test.json || goto FAIL -..\%buildtype%\flatc.exe --python --gen-mutable --reflect-names --gen-object-api --gen-compare --cpp-ptr-type flatbuffers::unique_ptr --no-fb-import -I include_test monster_test.fbs monsterdata_test.json || goto FAIL -..\%buildtype%\flatc.exe --cpp --java --csharp --dart --go --binary --lobster --lua --python --js --ts --php --rust --gen-mutable --reflect-names --no-fb-import --cpp-ptr-type flatbuffers::unique_ptr -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs || goto FAIL -..\%buildtype%\flatc.exe --cpp --java --csharp --js --ts --php --gen-mutable --reflect-names --gen-object-api --gen-compare --cpp-ptr-type flatbuffers::unique_ptr -o union_vector ./union_vector/union_vector.fbs || goto FAIL -..\%buildtype%\flatc.exe --cpp --scoped-enums -o evolution_test ./evolution_test/evolution_v1.fbs ./evolution_test/evolution_v2.fbs|| goto FAIL +set commandline=%* +set TEST_CPP_FLAGS= +if NOT "%commandline%"=="%commandline:--cpp-std legacy=%" set TEST_CPP_FLAGS=--cpp-std legacy %TEST_CPP_FLAGS% + +set TEST_CPP_FLAGS=--gen-compare --cpp-ptr-type flatbuffers::unique_ptr %TEST_CPP_FLAGS% +set TEST_BASE_FLAGS=--reflect-names --gen-mutable --gen-object-api +set TEST_NOINCL_FLAGS=%TEST_BASE_FLAGS% --no-includes --no-fb-import + +..\%buildtype%\flatc.exe --binary --cpp --java --kotlin --csharp --dart --go --lobster --lua --js --ts --php --rust --grpc ^ +%TEST_NOINCL_FLAGS% %TEST_CPP_FLAGS% -I include_test monster_test.fbs monsterdata_test.json || goto FAIL + +..\%buildtype%\flatc.exe --python %TEST_BASE_FLAGS% --no-fb-import -I include_test monster_test.fbs monsterdata_test.json || goto FAIL + +..\%buildtype%\flatc.exe --binary --cpp --java --csharp --dart --go --lobster --lua --js --ts --php --python --rust ^ +%TEST_NOINCL_FLAGS% %TEST_CPP_FLAGS% -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs || goto FAIL + +..\%buildtype%\flatc.exe --cpp --java --csharp --js --ts --php %TEST_BASE_FLAGS% %TEST_CPP_FLAGS% -o union_vector ./union_vector/union_vector.fbs || goto FAIL ..\%buildtype%\flatc.exe -b --schema --bfbs-comments --bfbs-builtins -I include_test monster_test.fbs || goto FAIL ..\%buildtype%\flatc.exe -b --schema --bfbs-comments --bfbs-builtins -I include_test arrays_test.fbs || goto FAIL ..\%buildtype%\flatc.exe --jsonschema --schema -I include_test monster_test.fbs || goto FAIL -..\%buildtype%\flatc.exe --cpp --java --csharp --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --scoped-enums --jsonschema --cpp-ptr-type flatbuffers::unique_ptr arrays_test.fbs || goto FAIL -..\%buildtype%\flatc.exe --python --gen-mutable --reflect-names --gen-object-api --gen-compare --scoped-enums --jsonschema --cpp-ptr-type flatbuffers::unique_ptr arrays_test.fbs || goto FAIL -..\%buildtype%\flatc.exe --cpp --gen-mutable --gen-object-api --reflect-names --cpp-ptr-type flatbuffers::unique_ptr native_type_test.fbs || goto FAIL +..\%buildtype%\flatc.exe --cpp --java --csharp --jsonschema %TEST_NOINCL_FLAGS% %TEST_CPP_FLAGS% --scoped-enums arrays_test.fbs || goto FAIL +..\%buildtype%\flatc.exe --python %TEST_BASE_FLAGS% arrays_test.fbs || goto FAIL +..\%buildtype%\flatc.exe --cpp %TEST_BASE_FLAGS% --cpp-ptr-type flatbuffers::unique_ptr native_type_test.fbs || goto FAIL -IF NOT "%MONSTER_EXTRA%"=="skip" ( +if NOT "%MONSTER_EXTRA%"=="skip" ( @echo Generate MosterExtra - ..\%buildtype%\flatc.exe --cpp --java --csharp --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --cpp-ptr-type flatbuffers::unique_ptr monster_extra.fbs monsterdata_extra.json || goto FAIL - ..\%buildtype%\flatc.exe --python --gen-mutable --reflect-names --gen-object-api --gen-compare --cpp-ptr-type flatbuffers::unique_ptr monster_extra.fbs monsterdata_extra.json || goto FAIL - + ..\%buildtype%\flatc.exe --cpp --java --csharp %TEST_NOINCL_FLAGS% %TEST_CPP_FLAGS% monster_extra.fbs monsterdata_extra.json || goto FAIL + ..\%buildtype%\flatc.exe --python %TEST_BASE_FLAGS% monster_extra.fbs monsterdata_extra.json || goto FAIL ) else ( @echo monster_extra.fbs skipped (the strtod function from MSVC2013 or older doesn't support NaN/Inf arguments) ) +set TEST_CPP17_FLAGS=--cpp --cpp-std c++17 -o ./cpp17/generated_cpp17 %TEST_NOINCL_FLAGS% +if NOT "%MONSTER_EXTRA%"=="skip" ( + @rem Flag c++17 requires Clang6, GCC7, MSVC2017 (_MSC_VER >= 1914) or higher. + ..\%buildtype%\flatc.exe %TEST_CPP17_FLAGS% -I include_test monster_test.fbs || goto FAIL + @rem..\%buildtype%\flatc.exe %TEST_CPP17_FLAGS% arrays_test.fbs || goto FAIL + @rem..\%buildtype%\flatc.exe %TEST_CPP17_FLAGS% native_type_test.fbs || goto FAIL + @rem..\%buildtype%\flatc.exe %TEST_CPP17_FLAGS% monster_extra.fbs || goto FAIL + @rem..\%buildtype%\flatc.exe %TEST_CPP17_FLAGS% ./union_vector/union_vector.fbs || goto FAIL +) + cd ../samples -..\%buildtype%\flatc.exe --cpp --lobster --gen-mutable --reflect-names --gen-object-api --gen-compare --cpp-ptr-type flatbuffers::unique_ptr monster.fbs || goto FAIL +..\%buildtype%\flatc.exe --cpp --lobster %TEST_BASE_FLAGS% %TEST_CPP_FLAGS% monster.fbs || goto FAIL ..\%buildtype%\flatc.exe -b --schema --bfbs-comments --bfbs-builtins monster.fbs || goto FAIL cd ../reflection call generate_code.bat %1 %2 || goto FAIL diff --git a/tests/generate_code.sh b/tests/generate_code.sh index 32aff7360fb..633748d3160 100755 --- a/tests/generate_code.sh +++ b/tests/generate_code.sh @@ -15,19 +15,38 @@ # limitations under the License. set -e -../flatc --cpp --java --kotlin --csharp --dart --go --binary --lobster --lua --js --ts --php --rust --grpc --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --cpp-ptr-type flatbuffers::unique_ptr --no-fb-import -I include_test monster_test.fbs monsterdata_test.json -../flatc --python --gen-mutable --reflect-names --gen-object-api --gen-compare --cpp-ptr-type flatbuffers::unique_ptr --no-fb-import -I include_test monster_test.fbs monsterdata_test.json -../flatc --cpp --java --kotlin --csharp --dart --go --binary --lobster --lua --python --js --ts --php --rust --gen-mutable --reflect-names --no-fb-import --cpp-ptr-type flatbuffers::unique_ptr -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs -../flatc --cpp --java --kotlin --csharp --js --ts --php --gen-mutable --reflect-names --gen-object-api --gen-compare --cpp-ptr-type flatbuffers::unique_ptr -o union_vector ./union_vector/union_vector.fbs -../flatc --cpp --scoped-enums -o evolution_test ./evolution_test/evolution_v*.fbs +commandline="'$*'" +TEST_CPP_FLAGS= +if [[ $commandline == *"--cpp-std legacy"* ]]; then + TEST_CPP_FLAGS="--cpp-std legacy $TEST_CPP_FLAGS" +fi + +TEST_CPP_FLAGS="--gen-compare --cpp-ptr-type flatbuffers::unique_ptr $TEST_CPP_FLAGS" +TEST_BASE_FLAGS="--reflect-names --gen-mutable --gen-object-api" +TEST_NOINCL_FLAGS="$TEST_BASE_FLAGS --no-includes --no-fb-import" + +../flatc --binary --cpp --java --kotlin --csharp --dart --go --lobster --lua --js --ts --php --rust --grpc \ +$TEST_NOINCL_FLAGS $TEST_CPP_FLAGS -I include_test monster_test.fbs monsterdata_test.json + +../flatc --python $TEST_BASE_FLAGS -I include_test monster_test.fbs monsterdata_test.json + +../flatc --cpp --java --kotlin --csharp --dart --go --binary --lobster --lua --js --ts --php --python --rust \ +$TEST_NOINCL_FLAGS $TEST_CPP_FLAGS -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs + +../flatc --cpp --java --kotlin --csharp --js --ts --php $TEST_BASE_FLAGS $TEST_CPP_FLAGS -o union_vector ./union_vector/union_vector.fbs ../flatc -b --schema --bfbs-comments --bfbs-builtins -I include_test monster_test.fbs ../flatc -b --schema --bfbs-comments --bfbs-builtins -I include_test arrays_test.fbs ../flatc --jsonschema --schema -I include_test monster_test.fbs -../flatc --cpp --java --kotlin --csharp --python --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --cpp-ptr-type flatbuffers::unique_ptr monster_extra.fbs monsterdata_extra.json -../flatc --cpp --java --csharp --gen-mutable --reflect-names --gen-object-api --gen-compare --no-includes --scoped-enums --jsonschema --cpp-ptr-type flatbuffers::unique_ptr arrays_test.fbs -../flatc --python --gen-mutable --reflect-names --gen-object-api --gen-compare --scoped-enums --jsonschema --cpp-ptr-type flatbuffers::unique_ptr arrays_test.fbs +../flatc --cpp --java --kotlin --csharp --python $TEST_NOINCL_FLAGS $TEST_CPP_FLAGS monster_extra.fbs monsterdata_extra.json +../flatc --cpp --java --csharp --jsonschema $TEST_NOINCL_FLAGS $TEST_CPP_FLAGS --scoped-enums arrays_test.fbs +../flatc --python $TEST_BASE_FLAGS arrays_test.fbs + +# Flag c++17 requires Clang6, GCC7, MSVC2017 (_MSC_VER >= 1914) or higher. +TEST_CPP17_FLAGS="--cpp --cpp-std c++17 -o ./cpp17/generated_cpp17 $TEST_NOINCL_FLAGS" +../flatc $TEST_CPP17_FLAGS -I include_test monster_test.fbs + cd ../samples -../flatc --cpp --lobster --gen-mutable --reflect-names --gen-object-api --gen-compare --cpp-ptr-type flatbuffers::unique_ptr monster.fbs +../flatc --cpp --lobster $TEST_BASE_FLAGS $TEST_CPP_FLAGS monster.fbs ../flatc -b --schema --bfbs-comments --bfbs-builtins monster.fbs cd ../reflection -./generate_code.sh +./generate_code.sh --cpp-std legacy diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 0b38eb3a956..c2e58a8f79b 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -100,7 +100,7 @@ inline const flatbuffers::TypeTable *MonsterTypeTable(); inline const flatbuffers::TypeTable *TypeAliasesTypeTable(); /// Composite components of Monster color. -enum Color { +enum Color : uint8_t { Color_Red = 1, /// \brief color Green /// Green is bit_flag with value (1u << 1) @@ -136,12 +136,12 @@ inline const char * const *EnumNamesColor() { } inline const char *EnumNameColor(Color e) { - if (e < Color_Red || e > Color_Blue) return ""; + if (flatbuffers::IsOutRange(e, Color_Red, Color_Blue)) return ""; const size_t index = static_cast(e) - static_cast(Color_Red); return EnumNamesColor()[index]; } -enum Race { +enum Race : int8_t { Race_None = -1, Race_Human = 0, Race_Dwarf = 1, @@ -172,12 +172,12 @@ inline const char * const *EnumNamesRace() { } inline const char *EnumNameRace(Race e) { - if (e < Race_None || e > Race_Elf) return ""; + if (flatbuffers::IsOutRange(e, Race_None, Race_Elf)) return ""; const size_t index = static_cast(e) - static_cast(Race_None); return EnumNamesRace()[index]; } -enum Any { +enum Any : uint8_t { Any_NONE = 0, Any_Monster = 1, Any_TestSimpleTableWithEnum = 2, @@ -208,7 +208,7 @@ inline const char * const *EnumNamesAny() { } inline const char *EnumNameAny(Any e) { - if (e < Any_NONE || e > Any_MyGame_Example2_Monster) return ""; + if (flatbuffers::IsOutRange(e, Any_NONE, Any_MyGame_Example2_Monster)) return ""; const size_t index = static_cast(e); return EnumNamesAny()[index]; } @@ -319,7 +319,7 @@ inline bool operator!=(const AnyUnion &lhs, const AnyUnion &rhs) { bool VerifyAny(flatbuffers::Verifier &verifier, const void *obj, Any type); bool VerifyAnyVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types); -enum AnyUniqueAliases { +enum AnyUniqueAliases : uint8_t { AnyUniqueAliases_NONE = 0, AnyUniqueAliases_M = 1, AnyUniqueAliases_TS = 2, @@ -350,7 +350,7 @@ inline const char * const *EnumNamesAnyUniqueAliases() { } inline const char *EnumNameAnyUniqueAliases(AnyUniqueAliases e) { - if (e < AnyUniqueAliases_NONE || e > AnyUniqueAliases_M2) return ""; + if (flatbuffers::IsOutRange(e, AnyUniqueAliases_NONE, AnyUniqueAliases_M2)) return ""; const size_t index = static_cast(e); return EnumNamesAnyUniqueAliases()[index]; } @@ -461,7 +461,7 @@ inline bool operator!=(const AnyUniqueAliasesUnion &lhs, const AnyUniqueAliasesU bool VerifyAnyUniqueAliases(flatbuffers::Verifier &verifier, const void *obj, AnyUniqueAliases type); bool VerifyAnyUniqueAliasesVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types); -enum AnyAmbiguousAliases { +enum AnyAmbiguousAliases : uint8_t { AnyAmbiguousAliases_NONE = 0, AnyAmbiguousAliases_M1 = 1, AnyAmbiguousAliases_M2 = 2, @@ -492,7 +492,7 @@ inline const char * const *EnumNamesAnyAmbiguousAliases() { } inline const char *EnumNameAnyAmbiguousAliases(AnyAmbiguousAliases e) { - if (e < AnyAmbiguousAliases_NONE || e > AnyAmbiguousAliases_M3) return ""; + if (flatbuffers::IsOutRange(e, AnyAmbiguousAliases_NONE, AnyAmbiguousAliases_M3)) return ""; const size_t index = static_cast(e); return EnumNamesAnyAmbiguousAliases()[index]; } diff --git a/tests/namespace_test/NamespaceA/NamespaceB/StructInNestedNS.go b/tests/namespace_test/NamespaceA/NamespaceB/StructInNestedNS.go index e985fbf7703..fe9255e342b 100644 --- a/tests/namespace_test/NamespaceA/NamespaceB/StructInNestedNS.go +++ b/tests/namespace_test/NamespaceA/NamespaceB/StructInNestedNS.go @@ -6,6 +6,27 @@ import ( flatbuffers "github.com/google/flatbuffers/go" ) +type StructInNestedNST struct { + A int32 + B int32 +} + +func StructInNestedNSPack(builder *flatbuffers.Builder, t *StructInNestedNST) flatbuffers.UOffsetT { + if t == nil { return 0 } + return CreateStructInNestedNS(builder, t.A, t.B) +} +func (rcv *StructInNestedNS) UnPackTo(t *StructInNestedNST) { + t.A = rcv.A() + t.B = rcv.B() +} + +func (rcv *StructInNestedNS) UnPack() *StructInNestedNST { + if rcv == nil { return nil } + t := &StructInNestedNST{} + rcv.UnPackTo(t) + return t +} + type StructInNestedNS struct { _tab flatbuffers.Struct } diff --git a/tests/namespace_test/NamespaceA/NamespaceB/StructInNestedNS.py b/tests/namespace_test/NamespaceA/NamespaceB/StructInNestedNS.py index db9dcc41fc2..9df52bde483 100644 --- a/tests/namespace_test/NamespaceA/NamespaceB/StructInNestedNS.py +++ b/tests/namespace_test/NamespaceA/NamespaceB/StructInNestedNS.py @@ -23,3 +23,34 @@ def CreateStructInNestedNS(builder, a, b): builder.PrependInt32(b) builder.PrependInt32(a) return builder.Offset() + + +class StructInNestedNST(object): + + # StructInNestedNST + def __init__(self): + self.a = 0 # type: int + self.b = 0 # type: int + + @classmethod + def InitFromBuf(cls, buf, pos): + structInNestedNS = StructInNestedNS() + structInNestedNS.Init(buf, pos) + return cls.InitFromObj(structInNestedNS) + + @classmethod + def InitFromObj(cls, structInNestedNS): + x = StructInNestedNST() + x._UnPack(structInNestedNS) + return x + + # StructInNestedNST + def _UnPack(self, structInNestedNS): + if structInNestedNS is None: + return + self.a = structInNestedNS.A() + self.b = structInNestedNS.B() + + # StructInNestedNST + def Pack(self, builder): + return CreateStructInNestedNS(builder, self.a, self.b) diff --git a/tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.go b/tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.go index 75f7a5548a2..fb0d39b0b32 100644 --- a/tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.go +++ b/tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.go @@ -6,6 +6,28 @@ import ( flatbuffers "github.com/google/flatbuffers/go" ) +type TableInNestedNST struct { + Foo int32 +} + +func TableInNestedNSPack(builder *flatbuffers.Builder, t *TableInNestedNST) flatbuffers.UOffsetT { + if t == nil { return 0 } + TableInNestedNSStart(builder) + TableInNestedNSAddFoo(builder, t.Foo) + return TableInNestedNSEnd(builder) +} + +func (rcv *TableInNestedNS) UnPackTo(t *TableInNestedNST) { + t.Foo = rcv.Foo() +} + +func (rcv *TableInNestedNS) UnPack() *TableInNestedNST { + if rcv == nil { return nil } + t := &TableInNestedNST{} + rcv.UnPackTo(t) + return t +} + type TableInNestedNS struct { _tab flatbuffers.Table } diff --git a/tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.py b/tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.py index 020e189944e..e86ba633b29 100644 --- a/tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.py +++ b/tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.py @@ -30,3 +30,35 @@ def Foo(self): def TableInNestedNSStart(builder): builder.StartObject(1) def TableInNestedNSAddFoo(builder, foo): builder.PrependInt32Slot(0, foo, 0) def TableInNestedNSEnd(builder): return builder.EndObject() + + +class TableInNestedNST(object): + + # TableInNestedNST + def __init__(self): + self.foo = 0 # type: int + + @classmethod + def InitFromBuf(cls, buf, pos): + tableInNestedNS = TableInNestedNS() + tableInNestedNS.Init(buf, pos) + return cls.InitFromObj(tableInNestedNS) + + @classmethod + def InitFromObj(cls, tableInNestedNS): + x = TableInNestedNST() + x._UnPack(tableInNestedNS) + return x + + # TableInNestedNST + def _UnPack(self, tableInNestedNS): + if tableInNestedNS is None: + return + self.foo = tableInNestedNS.Foo() + + # TableInNestedNST + def Pack(self, builder): + TableInNestedNSStart(builder) + TableInNestedNSAddFoo(builder, self.foo) + tableInNestedNS = TableInNestedNSEnd(builder) + return tableInNestedNS diff --git a/tests/namespace_test/NamespaceA/SecondTableInA.go b/tests/namespace_test/NamespaceA/SecondTableInA.go index 6dd1eeff8f8..a6b35c7c834 100644 --- a/tests/namespace_test/NamespaceA/SecondTableInA.go +++ b/tests/namespace_test/NamespaceA/SecondTableInA.go @@ -8,6 +8,29 @@ import ( NamespaceC "NamespaceC" ) +type SecondTableInAT struct { + ReferToC *NamespaceC.TableInCT +} + +func SecondTableInAPack(builder *flatbuffers.Builder, t *SecondTableInAT) flatbuffers.UOffsetT { + if t == nil { return 0 } + referToCOffset := NamespaceC.TableInCPack(builder, t.ReferToC) + SecondTableInAStart(builder) + SecondTableInAAddReferToC(builder, referToCOffset) + return SecondTableInAEnd(builder) +} + +func (rcv *SecondTableInA) UnPackTo(t *SecondTableInAT) { + t.ReferToC = rcv.ReferToC(nil).UnPack() +} + +func (rcv *SecondTableInA) UnPack() *SecondTableInAT { + if rcv == nil { return nil } + t := &SecondTableInAT{} + rcv.UnPackTo(t) + return t +} + type SecondTableInA struct { _tab flatbuffers.Table } diff --git a/tests/namespace_test/NamespaceA/SecondTableInA.py b/tests/namespace_test/NamespaceA/SecondTableInA.py index bdcb06983de..5aaa8edc127 100644 --- a/tests/namespace_test/NamespaceA/SecondTableInA.py +++ b/tests/namespace_test/NamespaceA/SecondTableInA.py @@ -25,7 +25,6 @@ def ReferToC(self): o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4)) if o != 0: x = self._tab.Indirect(o + self._tab.Pos) - from NamespaceC.TableInC import TableInC obj = TableInC() obj.Init(self._tab.Bytes, x) return obj @@ -34,3 +33,43 @@ def ReferToC(self): def SecondTableInAStart(builder): builder.StartObject(1) def SecondTableInAAddReferToC(builder, referToC): builder.PrependUOffsetTRelativeSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(referToC), 0) def SecondTableInAEnd(builder): return builder.EndObject() + +try: + from typing import Optional +except: + pass + +class SecondTableInAT(object): + + # SecondTableInAT + def __init__(self): + self.referToC = None # type: Optional[TableInCT] + + @classmethod + def InitFromBuf(cls, buf, pos): + secondTableInA = SecondTableInA() + secondTableInA.Init(buf, pos) + return cls.InitFromObj(secondTableInA) + + @classmethod + def InitFromObj(cls, secondTableInA): + x = SecondTableInAT() + x._UnPack(secondTableInA) + return x + + # SecondTableInAT + def _UnPack(self, secondTableInA): + if secondTableInA is None: + return + if secondTableInA.ReferToC() is not None: + self.referToC = TableInCT.InitFromObj(secondTableInA.ReferToC()) + + # SecondTableInAT + def Pack(self, builder): + if self.referToC is not None: + referToC = self.referToC.Pack(builder) + SecondTableInAStart(builder) + if self.referToC is not None: + SecondTableInAAddReferToC(builder, referToC) + secondTableInA = SecondTableInAEnd(builder) + return secondTableInA diff --git a/tests/namespace_test/NamespaceA/TableInFirstNS.go b/tests/namespace_test/NamespaceA/TableInFirstNS.go index bbcbdc6057d..7e18416b14d 100644 --- a/tests/namespace_test/NamespaceA/TableInFirstNS.go +++ b/tests/namespace_test/NamespaceA/TableInFirstNS.go @@ -8,6 +8,36 @@ import ( NamespaceA__NamespaceB "NamespaceA/NamespaceB" ) +type TableInFirstNST struct { + FooTable *NamespaceA__NamespaceB.TableInNestedNST + FooEnum NamespaceA__NamespaceB.EnumInNestedNS + FooStruct *NamespaceA__NamespaceB.StructInNestedNST +} + +func TableInFirstNSPack(builder *flatbuffers.Builder, t *TableInFirstNST) flatbuffers.UOffsetT { + if t == nil { return 0 } + fooTableOffset := NamespaceA__NamespaceB.TableInNestedNSPack(builder, t.FooTable) + TableInFirstNSStart(builder) + TableInFirstNSAddFooTable(builder, fooTableOffset) + TableInFirstNSAddFooEnum(builder, t.FooEnum) + fooStructOffset := NamespaceA__NamespaceB.StructInNestedNSPack(builder, t.FooStruct) + TableInFirstNSAddFooStruct(builder, fooStructOffset) + return TableInFirstNSEnd(builder) +} + +func (rcv *TableInFirstNS) UnPackTo(t *TableInFirstNST) { + t.FooTable = rcv.FooTable(nil).UnPack() + t.FooEnum = rcv.FooEnum() + t.FooStruct = rcv.FooStruct(nil).UnPack() +} + +func (rcv *TableInFirstNS) UnPack() *TableInFirstNST { + if rcv == nil { return nil } + t := &TableInFirstNST{} + rcv.UnPackTo(t) + return t +} + type TableInFirstNS struct { _tab flatbuffers.Table } diff --git a/tests/namespace_test/NamespaceA/TableInFirstNS.py b/tests/namespace_test/NamespaceA/TableInFirstNS.py index 6c1804592a7..39598f5a4e4 100644 --- a/tests/namespace_test/NamespaceA/TableInFirstNS.py +++ b/tests/namespace_test/NamespaceA/TableInFirstNS.py @@ -25,7 +25,6 @@ def FooTable(self): o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4)) if o != 0: x = self._tab.Indirect(o + self._tab.Pos) - from NamespaceA.NamespaceB.TableInNestedNS import TableInNestedNS obj = TableInNestedNS() obj.Init(self._tab.Bytes, x) return obj @@ -43,7 +42,6 @@ def FooStruct(self): o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(8)) if o != 0: x = o + self._tab.Pos - from NamespaceA.NamespaceB.StructInNestedNS import StructInNestedNS obj = StructInNestedNS() obj.Init(self._tab.Bytes, x) return obj @@ -54,3 +52,52 @@ def TableInFirstNSAddFooTable(builder, fooTable): builder.PrependUOffsetTRelativ def TableInFirstNSAddFooEnum(builder, fooEnum): builder.PrependInt8Slot(1, fooEnum, 0) def TableInFirstNSAddFooStruct(builder, fooStruct): builder.PrependStructSlot(2, flatbuffers.number_types.UOffsetTFlags.py_type(fooStruct), 0) def TableInFirstNSEnd(builder): return builder.EndObject() + +try: + from typing import Optional +except: + pass + +class TableInFirstNST(object): + + # TableInFirstNST + def __init__(self): + self.fooTable = None # type: Optional[TableInNestedNST] + self.fooEnum = 0 # type: int + self.fooStruct = None # type: Optional[StructInNestedNST] + + @classmethod + def InitFromBuf(cls, buf, pos): + tableInFirstNS = TableInFirstNS() + tableInFirstNS.Init(buf, pos) + return cls.InitFromObj(tableInFirstNS) + + @classmethod + def InitFromObj(cls, tableInFirstNS): + x = TableInFirstNST() + x._UnPack(tableInFirstNS) + return x + + # TableInFirstNST + def _UnPack(self, tableInFirstNS): + if tableInFirstNS is None: + return + if tableInFirstNS.FooTable() is not None: + self.fooTable = TableInNestedNST.InitFromObj(tableInFirstNS.FooTable()) + self.fooEnum = tableInFirstNS.FooEnum() + if tableInFirstNS.FooStruct() is not None: + self.fooStruct = StructInNestedNST.InitFromObj(tableInFirstNS.FooStruct()) + + # TableInFirstNST + def Pack(self, builder): + if self.fooTable is not None: + fooTable = self.fooTable.Pack(builder) + TableInFirstNSStart(builder) + if self.fooTable is not None: + TableInFirstNSAddFooTable(builder, fooTable) + TableInFirstNSAddFooEnum(builder, self.fooEnum) + if self.fooStruct is not None: + fooStruct = self.fooStruct.Pack(builder) + TableInFirstNSAddFooStruct(builder, fooStruct) + tableInFirstNS = TableInFirstNSEnd(builder) + return tableInFirstNS diff --git a/tests/namespace_test/NamespaceC/TableInC.go b/tests/namespace_test/NamespaceC/TableInC.go index 59b3e484e13..8547295696f 100644 --- a/tests/namespace_test/NamespaceC/TableInC.go +++ b/tests/namespace_test/NamespaceC/TableInC.go @@ -8,6 +8,33 @@ import ( NamespaceA "NamespaceA" ) +type TableInCT struct { + ReferToA1 *NamespaceA.TableInFirstNST + ReferToA2 *NamespaceA.SecondTableInAT +} + +func TableInCPack(builder *flatbuffers.Builder, t *TableInCT) flatbuffers.UOffsetT { + if t == nil { return 0 } + referToA1Offset := NamespaceA.TableInFirstNSPack(builder, t.ReferToA1) + referToA2Offset := NamespaceA.SecondTableInAPack(builder, t.ReferToA2) + TableInCStart(builder) + TableInCAddReferToA1(builder, referToA1Offset) + TableInCAddReferToA2(builder, referToA2Offset) + return TableInCEnd(builder) +} + +func (rcv *TableInC) UnPackTo(t *TableInCT) { + t.ReferToA1 = rcv.ReferToA1(nil).UnPack() + t.ReferToA2 = rcv.ReferToA2(nil).UnPack() +} + +func (rcv *TableInC) UnPack() *TableInCT { + if rcv == nil { return nil } + t := &TableInCT{} + rcv.UnPackTo(t) + return t +} + type TableInC struct { _tab flatbuffers.Table } diff --git a/tests/namespace_test/NamespaceC/TableInC.py b/tests/namespace_test/NamespaceC/TableInC.py index 765032530c4..8f04b52b6ce 100644 --- a/tests/namespace_test/NamespaceC/TableInC.py +++ b/tests/namespace_test/NamespaceC/TableInC.py @@ -25,7 +25,6 @@ def ReferToA1(self): o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4)) if o != 0: x = self._tab.Indirect(o + self._tab.Pos) - from NamespaceA.TableInFirstNS import TableInFirstNS obj = TableInFirstNS() obj.Init(self._tab.Bytes, x) return obj @@ -36,7 +35,6 @@ def ReferToA2(self): o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(6)) if o != 0: x = self._tab.Indirect(o + self._tab.Pos) - from NamespaceA.SecondTableInA import SecondTableInA obj = SecondTableInA() obj.Init(self._tab.Bytes, x) return obj @@ -46,3 +44,50 @@ def TableInCStart(builder): builder.StartObject(2) def TableInCAddReferToA1(builder, referToA1): builder.PrependUOffsetTRelativeSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(referToA1), 0) def TableInCAddReferToA2(builder, referToA2): builder.PrependUOffsetTRelativeSlot(1, flatbuffers.number_types.UOffsetTFlags.py_type(referToA2), 0) def TableInCEnd(builder): return builder.EndObject() + +try: + from typing import Optional +except: + pass + +class TableInCT(object): + + # TableInCT + def __init__(self): + self.referToA1 = None # type: Optional[TableInFirstNST] + self.referToA2 = None # type: Optional[SecondTableInAT] + + @classmethod + def InitFromBuf(cls, buf, pos): + tableInC = TableInC() + tableInC.Init(buf, pos) + return cls.InitFromObj(tableInC) + + @classmethod + def InitFromObj(cls, tableInC): + x = TableInCT() + x._UnPack(tableInC) + return x + + # TableInCT + def _UnPack(self, tableInC): + if tableInC is None: + return + if tableInC.ReferToA1() is not None: + self.referToA1 = TableInFirstNST.InitFromObj(tableInC.ReferToA1()) + if tableInC.ReferToA2() is not None: + self.referToA2 = SecondTableInAT.InitFromObj(tableInC.ReferToA2()) + + # TableInCT + def Pack(self, builder): + if self.referToA1 is not None: + referToA1 = self.referToA1.Pack(builder) + if self.referToA2 is not None: + referToA2 = self.referToA2.Pack(builder) + TableInCStart(builder) + if self.referToA1 is not None: + TableInCAddReferToA1(builder, referToA1) + if self.referToA2 is not None: + TableInCAddReferToA2(builder, referToA2) + tableInC = TableInCEnd(builder) + return tableInC diff --git a/tests/namespace_test/namespace_test1_generated.h b/tests/namespace_test/namespace_test1_generated.h index daaf7ae4446..6d28e7eb826 100644 --- a/tests/namespace_test/namespace_test1_generated.h +++ b/tests/namespace_test/namespace_test1_generated.h @@ -10,14 +10,20 @@ namespace NamespaceA { namespace NamespaceB { struct TableInNestedNS; +struct TableInNestedNST; struct StructInNestedNS; +bool operator==(const TableInNestedNST &lhs, const TableInNestedNST &rhs); +bool operator!=(const TableInNestedNST &lhs, const TableInNestedNST &rhs); +bool operator==(const StructInNestedNS &lhs, const StructInNestedNS &rhs); +bool operator!=(const StructInNestedNS &lhs, const StructInNestedNS &rhs); + inline const flatbuffers::TypeTable *TableInNestedNSTypeTable(); inline const flatbuffers::TypeTable *StructInNestedNSTypeTable(); -enum EnumInNestedNS { +enum EnumInNestedNS : int8_t { EnumInNestedNS_A = 0, EnumInNestedNS_B = 1, EnumInNestedNS_C = 2, @@ -45,7 +51,7 @@ inline const char * const *EnumNamesEnumInNestedNS() { } inline const char *EnumNameEnumInNestedNS(EnumInNestedNS e) { - if (e < EnumInNestedNS_A || e > EnumInNestedNS_C) return ""; + if (flatbuffers::IsOutRange(e, EnumInNestedNS_A, EnumInNestedNS_C)) return ""; const size_t index = static_cast(e); return EnumNamesEnumInNestedNS()[index]; } @@ -81,7 +87,37 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) StructInNestedNS FLATBUFFERS_FINAL_CLASS }; FLATBUFFERS_STRUCT_END(StructInNestedNS, 8); +inline bool operator==(const StructInNestedNS &lhs, const StructInNestedNS &rhs) { + return + (lhs.a() == rhs.a()) && + (lhs.b() == rhs.b()); +} + +inline bool operator!=(const StructInNestedNS &lhs, const StructInNestedNS &rhs) { + return !(lhs == rhs); +} + + +struct TableInNestedNST : public flatbuffers::NativeTable { + typedef TableInNestedNS TableType; + int32_t foo; + TableInNestedNST() + : foo(0) { + } +}; + +inline bool operator==(const TableInNestedNST &lhs, const TableInNestedNST &rhs) { + return + (lhs.foo == rhs.foo); +} + +inline bool operator!=(const TableInNestedNST &lhs, const TableInNestedNST &rhs) { + return !(lhs == rhs); +} + + struct TableInNestedNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { + typedef TableInNestedNST NativeTableType; static const flatbuffers::TypeTable *MiniReflectTypeTable() { return TableInNestedNSTypeTable(); } @@ -99,6 +135,9 @@ struct TableInNestedNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { VerifyField(verifier, VT_FOO) && verifier.EndTable(); } + TableInNestedNST *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(TableInNestedNST *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; + static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const TableInNestedNST* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); }; struct TableInNestedNSBuilder { @@ -127,6 +166,34 @@ inline flatbuffers::Offset CreateTableInNestedNS( return builder_.Finish(); } +flatbuffers::Offset CreateTableInNestedNS(flatbuffers::FlatBufferBuilder &_fbb, const TableInNestedNST *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); + +inline TableInNestedNST *TableInNestedNS::UnPack(const flatbuffers::resolver_function_t *_resolver) const { + auto _o = new TableInNestedNST(); + UnPackTo(_o, _resolver); + return _o; +} + +inline void TableInNestedNS::UnPackTo(TableInNestedNST *_o, const flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = foo(); _o->foo = _e; } +} + +inline flatbuffers::Offset TableInNestedNS::Pack(flatbuffers::FlatBufferBuilder &_fbb, const TableInNestedNST* _o, const flatbuffers::rehasher_function_t *_rehasher) { + return CreateTableInNestedNS(_fbb, _o, _rehasher); +} + +inline flatbuffers::Offset CreateTableInNestedNS(flatbuffers::FlatBufferBuilder &_fbb, const TableInNestedNST *_o, const flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const TableInNestedNST* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _foo = _o->foo; + return NamespaceA::NamespaceB::CreateTableInNestedNS( + _fbb, + _foo); +} + inline const flatbuffers::TypeTable *EnumInNestedNSTypeTable() { static const flatbuffers::TypeCode type_codes[] = { { flatbuffers::ET_CHAR, 0, 0 }, diff --git a/tests/namespace_test/namespace_test2_generated.h b/tests/namespace_test/namespace_test2_generated.h index 135100a4007..dd9412e37a4 100644 --- a/tests/namespace_test/namespace_test2_generated.h +++ b/tests/namespace_test/namespace_test2_generated.h @@ -6,23 +6,39 @@ #include "flatbuffers/flatbuffers.h" -#include "namespace_test1_generated.h" - namespace NamespaceA { struct TableInFirstNS; +struct TableInFirstNST; } // namespace NamespaceA namespace NamespaceC { struct TableInC; +struct TableInCT; } // namespace NamespaceC namespace NamespaceA { struct SecondTableInA; +struct SecondTableInAT; + +bool operator==(const TableInFirstNST &lhs, const TableInFirstNST &rhs); +bool operator!=(const TableInFirstNST &lhs, const TableInFirstNST &rhs); +} // namespace NamespaceA + +namespace NamespaceC { + +bool operator==(const TableInCT &lhs, const TableInCT &rhs); +bool operator!=(const TableInCT &lhs, const TableInCT &rhs); +} // namespace NamespaceC + +namespace NamespaceA { + +bool operator==(const SecondTableInAT &lhs, const SecondTableInAT &rhs); +bool operator!=(const SecondTableInAT &lhs, const SecondTableInAT &rhs); inline const flatbuffers::TypeTable *TableInFirstNSTypeTable(); @@ -38,7 +54,30 @@ namespace NamespaceA { inline const flatbuffers::TypeTable *SecondTableInATypeTable(); +struct TableInFirstNST : public flatbuffers::NativeTable { + typedef TableInFirstNS TableType; + flatbuffers::unique_ptr foo_table; + NamespaceA::NamespaceB::EnumInNestedNS foo_enum; + flatbuffers::unique_ptr foo_struct; + TableInFirstNST() + : foo_enum(NamespaceA::NamespaceB::EnumInNestedNS_A) { + } +}; + +inline bool operator==(const TableInFirstNST &lhs, const TableInFirstNST &rhs) { + return + (lhs.foo_table == rhs.foo_table) && + (lhs.foo_enum == rhs.foo_enum) && + (lhs.foo_struct == rhs.foo_struct); +} + +inline bool operator!=(const TableInFirstNST &lhs, const TableInFirstNST &rhs) { + return !(lhs == rhs); +} + + struct TableInFirstNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { + typedef TableInFirstNST NativeTableType; static const flatbuffers::TypeTable *MiniReflectTypeTable() { return TableInFirstNSTypeTable(); } @@ -73,6 +112,9 @@ struct TableInFirstNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { VerifyField(verifier, VT_FOO_STRUCT) && verifier.EndTable(); } + TableInFirstNST *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(TableInFirstNST *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; + static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const TableInFirstNST* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); }; struct TableInFirstNSBuilder { @@ -111,11 +153,33 @@ inline flatbuffers::Offset CreateTableInFirstNS( return builder_.Finish(); } +flatbuffers::Offset CreateTableInFirstNS(flatbuffers::FlatBufferBuilder &_fbb, const TableInFirstNST *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); + } // namespace NamespaceA namespace NamespaceC { +struct TableInCT : public flatbuffers::NativeTable { + typedef TableInC TableType; + flatbuffers::unique_ptr refer_to_a1; + flatbuffers::unique_ptr refer_to_a2; + TableInCT() { + } +}; + +inline bool operator==(const TableInCT &lhs, const TableInCT &rhs) { + return + (lhs.refer_to_a1 == rhs.refer_to_a1) && + (lhs.refer_to_a2 == rhs.refer_to_a2); +} + +inline bool operator!=(const TableInCT &lhs, const TableInCT &rhs) { + return !(lhs == rhs); +} + + struct TableInC FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { + typedef TableInCT NativeTableType; static const flatbuffers::TypeTable *MiniReflectTypeTable() { return TableInCTypeTable(); } @@ -143,6 +207,9 @@ struct TableInC FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { verifier.VerifyTable(refer_to_a2()) && verifier.EndTable(); } + TableInCT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(TableInCT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; + static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const TableInCT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); }; struct TableInCBuilder { @@ -176,11 +243,31 @@ inline flatbuffers::Offset CreateTableInC( return builder_.Finish(); } +flatbuffers::Offset CreateTableInC(flatbuffers::FlatBufferBuilder &_fbb, const TableInCT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); + } // namespace NamespaceC namespace NamespaceA { +struct SecondTableInAT : public flatbuffers::NativeTable { + typedef SecondTableInA TableType; + flatbuffers::unique_ptr refer_to_c; + SecondTableInAT() { + } +}; + +inline bool operator==(const SecondTableInAT &lhs, const SecondTableInAT &rhs) { + return + (lhs.refer_to_c == rhs.refer_to_c); +} + +inline bool operator!=(const SecondTableInAT &lhs, const SecondTableInAT &rhs) { + return !(lhs == rhs); +} + + struct SecondTableInA FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { + typedef SecondTableInAT NativeTableType; static const flatbuffers::TypeTable *MiniReflectTypeTable() { return SecondTableInATypeTable(); } @@ -199,6 +286,9 @@ struct SecondTableInA FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { verifier.VerifyTable(refer_to_c()) && verifier.EndTable(); } + SecondTableInAT *UnPack(const flatbuffers::resolver_function_t *_resolver = nullptr) const; + void UnPackTo(SecondTableInAT *_o, const flatbuffers::resolver_function_t *_resolver = nullptr) const; + static flatbuffers::Offset Pack(flatbuffers::FlatBufferBuilder &_fbb, const SecondTableInAT* _o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); }; struct SecondTableInABuilder { @@ -227,14 +317,103 @@ inline flatbuffers::Offset CreateSecondTableInA( return builder_.Finish(); } +flatbuffers::Offset CreateSecondTableInA(flatbuffers::FlatBufferBuilder &_fbb, const SecondTableInAT *_o, const flatbuffers::rehasher_function_t *_rehasher = nullptr); + +inline TableInFirstNST *TableInFirstNS::UnPack(const flatbuffers::resolver_function_t *_resolver) const { + auto _o = new TableInFirstNST(); + UnPackTo(_o, _resolver); + return _o; +} + +inline void TableInFirstNS::UnPackTo(TableInFirstNST *_o, const flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = foo_table(); if (_e) _o->foo_table = flatbuffers::unique_ptr(_e->UnPack(_resolver)); } + { auto _e = foo_enum(); _o->foo_enum = _e; } + { auto _e = foo_struct(); if (_e) _o->foo_struct = flatbuffers::unique_ptr(new NamespaceA::NamespaceB::StructInNestedNS(*_e)); } +} + +inline flatbuffers::Offset TableInFirstNS::Pack(flatbuffers::FlatBufferBuilder &_fbb, const TableInFirstNST* _o, const flatbuffers::rehasher_function_t *_rehasher) { + return CreateTableInFirstNS(_fbb, _o, _rehasher); +} + +inline flatbuffers::Offset CreateTableInFirstNS(flatbuffers::FlatBufferBuilder &_fbb, const TableInFirstNST *_o, const flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const TableInFirstNST* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _foo_table = _o->foo_table ? CreateTableInNestedNS(_fbb, _o->foo_table.get(), _rehasher) : 0; + auto _foo_enum = _o->foo_enum; + auto _foo_struct = _o->foo_struct ? _o->foo_struct.get() : 0; + return NamespaceA::CreateTableInFirstNS( + _fbb, + _foo_table, + _foo_enum, + _foo_struct); +} + } // namespace NamespaceA namespace NamespaceC { +inline TableInCT *TableInC::UnPack(const flatbuffers::resolver_function_t *_resolver) const { + auto _o = new TableInCT(); + UnPackTo(_o, _resolver); + return _o; +} + +inline void TableInC::UnPackTo(TableInCT *_o, const flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = refer_to_a1(); if (_e) _o->refer_to_a1 = flatbuffers::unique_ptr(_e->UnPack(_resolver)); } + { auto _e = refer_to_a2(); if (_e) _o->refer_to_a2 = flatbuffers::unique_ptr(_e->UnPack(_resolver)); } +} + +inline flatbuffers::Offset TableInC::Pack(flatbuffers::FlatBufferBuilder &_fbb, const TableInCT* _o, const flatbuffers::rehasher_function_t *_rehasher) { + return CreateTableInC(_fbb, _o, _rehasher); +} + +inline flatbuffers::Offset CreateTableInC(flatbuffers::FlatBufferBuilder &_fbb, const TableInCT *_o, const flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const TableInCT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _refer_to_a1 = _o->refer_to_a1 ? CreateTableInFirstNS(_fbb, _o->refer_to_a1.get(), _rehasher) : 0; + auto _refer_to_a2 = _o->refer_to_a2 ? CreateSecondTableInA(_fbb, _o->refer_to_a2.get(), _rehasher) : 0; + return NamespaceC::CreateTableInC( + _fbb, + _refer_to_a1, + _refer_to_a2); +} + } // namespace NamespaceC namespace NamespaceA { +inline SecondTableInAT *SecondTableInA::UnPack(const flatbuffers::resolver_function_t *_resolver) const { + auto _o = new SecondTableInAT(); + UnPackTo(_o, _resolver); + return _o; +} + +inline void SecondTableInA::UnPackTo(SecondTableInAT *_o, const flatbuffers::resolver_function_t *_resolver) const { + (void)_o; + (void)_resolver; + { auto _e = refer_to_c(); if (_e) _o->refer_to_c = flatbuffers::unique_ptr(_e->UnPack(_resolver)); } +} + +inline flatbuffers::Offset SecondTableInA::Pack(flatbuffers::FlatBufferBuilder &_fbb, const SecondTableInAT* _o, const flatbuffers::rehasher_function_t *_rehasher) { + return CreateSecondTableInA(_fbb, _o, _rehasher); +} + +inline flatbuffers::Offset CreateSecondTableInA(flatbuffers::FlatBufferBuilder &_fbb, const SecondTableInAT *_o, const flatbuffers::rehasher_function_t *_rehasher) { + (void)_rehasher; + (void)_o; + struct _VectorArgs { flatbuffers::FlatBufferBuilder *__fbb; const SecondTableInAT* __o; const flatbuffers::rehasher_function_t *__rehasher; } _va = { &_fbb, _o, _rehasher}; (void)_va; + auto _refer_to_c = _o->refer_to_c ? CreateTableInC(_fbb, _o->refer_to_c.get(), _rehasher) : 0; + return NamespaceA::CreateSecondTableInA( + _fbb, + _refer_to_c); +} + inline const flatbuffers::TypeTable *TableInFirstNSTypeTable() { static const flatbuffers::TypeCode type_codes[] = { { flatbuffers::ET_SEQUENCE, 0, 0 }, diff --git a/tests/namespace_test/namespace_test2_namespace_a_generated.dart b/tests/namespace_test/namespace_test2_namespace_a_generated.dart index d5108eba6c9..e10ad4b78f8 100644 --- a/tests/namespace_test/namespace_test2_namespace_a_generated.dart +++ b/tests/namespace_test/namespace_test2_namespace_a_generated.dart @@ -6,7 +6,6 @@ library namespace_a; import 'dart:typed_data' show Uint8List; import 'package:flat_buffers/flat_buffers.dart' as fb; -import 'namespace_test1_namespace_a_generated.dart'; import './namespace_test2_namespace_c_generated.dart' as namespace_c; class TableInFirstNS { diff --git a/tests/namespace_test/namespace_test2_namespace_c_generated.dart b/tests/namespace_test/namespace_test2_namespace_c_generated.dart index 7214febc234..edb6ffcbc8a 100644 --- a/tests/namespace_test/namespace_test2_namespace_c_generated.dart +++ b/tests/namespace_test/namespace_test2_namespace_c_generated.dart @@ -6,7 +6,6 @@ library namespace_c; import 'dart:typed_data' show Uint8List; import 'package:flat_buffers/flat_buffers.dart' as fb; -import 'namespace_test1_namespace_c_generated.dart'; import './namespace_test2_namespace_a_generated.dart' as namespace_a; class TableInC { diff --git a/tests/test.cpp b/tests/test.cpp index 149831da51b..1dd8083c6d5 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -1394,7 +1394,7 @@ void FuzzTest2() { break; } } - TEST_NOTNULL(nullptr); + TEST_NOTNULL(nullptr); //-V501 (this comment supresses CWE-570 warning) } // clang-format off diff --git a/tests/union_vector/union_vector_generated.h b/tests/union_vector/union_vector_generated.h index 40fa6186d0b..d1dc312de8f 100644 --- a/tests/union_vector/union_vector_generated.h +++ b/tests/union_vector/union_vector_generated.h @@ -33,7 +33,7 @@ inline const flatbuffers::TypeTable *BookReaderTypeTable(); inline const flatbuffers::TypeTable *MovieTypeTable(); -enum Character { +enum Character : uint8_t { Character_NONE = 0, Character_MuLan = 1, Character_Rapunzel = 2, @@ -73,7 +73,7 @@ inline const char * const *EnumNamesCharacter() { } inline const char *EnumNameCharacter(Character e) { - if (e < Character_NONE || e > Character_Unused) return ""; + if (flatbuffers::IsOutRange(e, Character_NONE, Character_Unused)) return ""; const size_t index = static_cast(e); return EnumNamesCharacter()[index]; } From c17e1ec15b8cd74344ad94607b4b053848a92b05 Mon Sep 17 00:00:00 2001 From: vglavnyy Date: Tue, 17 Dec 2019 03:31:55 +0700 Subject: [PATCH 2/8] Fix --cpp_std values: c++0x, c++11, c++17 --- CMakeLists.txt | 4 ++-- appveyor.yml | 2 +- docs/source/Compiler.md | 4 ++-- include/flatbuffers/flatc.h | 11 ++--------- reflection/generate_code.bat | 2 +- reflection/generate_code.sh | 2 +- src/flatc.cpp | 6 +++--- src/flatc_main.cpp | 10 +++++----- src/idl_gen_cpp.cpp | 28 ++++++++++++++++------------ tests/generate_code.bat | 2 +- tests/generate_code.sh | 6 +++--- 11 files changed, 37 insertions(+), 40 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ef6df363c9d..e26f18ae835 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,7 +36,7 @@ option(FLATBUFFERS_BUILD_CPP17 Requirements: Clang6, GCC7, MSVC2017 (_MSC_VER >= 1914) or higher." OFF) option(FLATBUFFERS_BUILD_LEGACY - "Run C++ code generator with '--cpp-std legacy' switch." + "Run C++ code generator with '--cpp-std c++0x' switch." OFF) if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS) @@ -342,7 +342,7 @@ endif() function(compile_flatbuffers_schema_to_cpp_opt SRC_FBS OPT) if(FLATBUFFERS_BUILD_LEGACY) - set(OPT ${OPT};--cpp-std legacy) + set(OPT ${OPT};--cpp-std c++0x) endif() message(STATUS "Add the code-generation command for the `${SRC_FBS}` schema with '${OPT}'") get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH) diff --git a/appveyor.yml b/appveyor.yml index 3dd659b87b4..f60e6e39781 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -16,7 +16,7 @@ environment: matrix: - CMAKE_VS_VERSION: "10 2010" CMAKE_OPTIONS: "-DFLATBUFFERS_BUILD_LEGACY=1" - CPP_TEST_OPTIONS: "--std-cpp legacy" + CPP_TEST_OPTIONS: "--std-cpp c++0x" MONSTER_EXTRA: "skip" - CMAKE_VS_VERSION: "12 2013" diff --git a/docs/source/Compiler.md b/docs/source/Compiler.md index d8f6547cd27..c5768bd2d1e 100644 --- a/docs/source/Compiler.md +++ b/docs/source/Compiler.md @@ -134,8 +134,8 @@ Additional options: - `--cpp-std CPP_STD` : Generate a C++ code using features of selected C++ standard. Supported `CPP_STD` values: - * `c++` - use a standard code generation rules; - * `legacy` - generate code compatible with old compilers (VS2010). + * `c++0x` - generate code compatible with old compilers (VS2010). + * `c++11` - use C++11 code generator (default); - `--object-prefix` : Customise class prefix for C++ object-based API. diff --git a/include/flatbuffers/flatc.h b/include/flatbuffers/flatc.h index c3b29c4e916..5e2709e73ed 100644 --- a/include/flatbuffers/flatc.h +++ b/include/flatbuffers/flatc.h @@ -27,15 +27,8 @@ namespace flatbuffers { -class FlatCompilerLogger { - FlatCompilerLogger() {} -public: - // There is no way to notify about an error from a code-generator - // except `assert` call. - // TODO: re-design interface to code generators. - static void Warn(const std::string &warn); - static void Error(const std::string &err); -}; +extern void LogCompilerWarn(const std::string &warn); +extern void LogCompilerError(const std::string &err); class FlatCompiler { public: diff --git a/reflection/generate_code.bat b/reflection/generate_code.bat index c43c334db36..7b0f4d8dd96 100644 --- a/reflection/generate_code.bat +++ b/reflection/generate_code.bat @@ -15,4 +15,4 @@ set buildtype=Release if "%1"=="-b" set buildtype=%2 -..\%buildtype%\flatc.exe --cpp --cpp-std legacy --no-prefix -o ../include/flatbuffers reflection.fbs || exit /b 1 +..\%buildtype%\flatc.exe --cpp --cpp-std c++0x --no-prefix -o ../include/flatbuffers reflection.fbs || exit /b 1 diff --git a/reflection/generate_code.sh b/reflection/generate_code.sh index efa2ed00998..0c1157d3f17 100755 --- a/reflection/generate_code.sh +++ b/reflection/generate_code.sh @@ -15,4 +15,4 @@ # limitations under the License. set -e -../flatc -c --cpp-std legacy --no-prefix -o ../include/flatbuffers reflection.fbs +../flatc -c --cpp-std c++0x --no-prefix -o ../include/flatbuffers reflection.fbs diff --git a/src/flatc.cpp b/src/flatc.cpp index c9b21159e69..8df2014e96e 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -118,9 +118,9 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const { " from Flatbuffers, but (char* + length).\n" " --cpp-std CPP_STD Generate a C++ code using features of selected C++ standard.\n" " Supported CPP_STD values:\n" - " * 'legacy' - generate code compatible with old compilers.\n" - " * 'c++' - use default code generation rules (default);\n" - " * 'c++17' - use C++17 features in generated code (experemental);\n" + " * 'c++0x' - generate code compatible with old compilers;\n" + " * 'c++11' - use C++11 code generator (default);\n" + " * 'c++17' - use C++17 features in generated code (experimental).\n" " --object-prefix Customise class prefix for C++ object-based API.\n" " --object-suffix Customise class suffix for C++ object-based API.\n" " Default value is \"T\".\n" diff --git a/src/flatc_main.cpp b/src/flatc_main.cpp index d0f92dade4b..34739ced41b 100644 --- a/src/flatc_main.cpp +++ b/src/flatc_main.cpp @@ -35,12 +35,12 @@ static void Error(const flatbuffers::FlatCompiler *flatc, } namespace flatbuffers { -void FlatCompilerLogger::Warn(const std::string &warn) { - ::Warn(static_cast(nullptr), warn, true); +void LogCompilerWarn(const std::string &warn) { + Warn(static_cast(nullptr), warn, true); } -void FlatCompilerLogger::Error(const std::string &err) { - ::Error(static_cast(nullptr), err, false, - true); +void LogCompilerError(const std::string &err) { + Error(static_cast(nullptr), err, false, + true); } } // namespace flatbuffers diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index af789e639aa..43b17360c74 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -58,12 +58,12 @@ namespace cpp { // Extension of IDLOptions for cpp-generator. struct IDLOptionsCpp : public IDLOptions { // All fields start with 'g_' prefix to distinguish from the base IDLOptions. - int g_cpp_std; // base version of C++ standard (0, 17) - bool g_only_fixed_enums; + int g_cpp_std; // Base version of C++ standard (0, 11, 17). + bool g_only_fixed_enums; // Generate underlaying type for all enums. // clang-format off IDLOptionsCpp(const IDLOptions &opts) : IDLOptions(opts), - g_cpp_std(0), + g_cpp_std(11), g_only_fixed_enums(true) {} // clang-format on @@ -728,7 +728,7 @@ class CppGenerator : public BaseGenerator { auto element_type = type.VectorType(); // Check if enum arrays are used in C++ without specifying --scoped-enums if (IsEnum(element_type) && !opts_.g_only_fixed_enums) { - FlatCompilerLogger::Error( + LogCompilerError( "--scoped-enums must be enabled to use enum arrays in C++"); FLATBUFFERS_ASSERT(true); } @@ -2985,19 +2985,23 @@ bool GenerateCPP(const Parser &parser, const std::string &path, cpp::IDLOptionsCpp opts(parser.opts); // The '--cpp_std' argument could be extended (like ASAN): // Example: "flatc --cpp_std c++17:option1:option2". - if (opts.cpp_std.empty() || opts.cpp_std == "c++") { - // Use a `standard` code generator. - } else if (opts.cpp_std == "c++17") { + auto cpp_std = opts.cpp_std; + std::transform(cpp_std.begin(), cpp_std.end(), cpp_std.begin(), ToUpper); + if (cpp_std.empty() || cpp_std == "C++11") { + // Use the standard C++11 code generator. + opts.g_cpp_std = 11; + } else if (cpp_std == "C++0X") { + opts.g_cpp_std = 0; + opts.g_only_fixed_enums = false; + } else if (cpp_std == "C++17") { opts.g_cpp_std = 17; // With c++17 generate strong enums only. opts.scoped_enums = true; + // By default, prefixed_enums==true, reset it. opts.prefixed_enums = false; - } else if (opts.cpp_std == "legacy") { - opts.g_cpp_std = -1; - opts.g_only_fixed_enums = false; } else { - FlatCompilerLogger::Error("Unknown value of the '--cpp-std' switch: " + - opts.cpp_std); + LogCompilerError("Unknown value of the '--cpp-std' switch: " + + opts.cpp_std); return false; } // The opts.scoped_enums has priority. diff --git a/tests/generate_code.bat b/tests/generate_code.bat index e2e2a44cb23..a8bad26a331 100644 --- a/tests/generate_code.bat +++ b/tests/generate_code.bat @@ -19,7 +19,7 @@ if "%1"=="-b" set buildtype=%2 set commandline=%* set TEST_CPP_FLAGS= -if NOT "%commandline%"=="%commandline:--cpp-std legacy=%" set TEST_CPP_FLAGS=--cpp-std legacy %TEST_CPP_FLAGS% +if NOT "%commandline%"=="%commandline:--cpp-std c++0x=%" set TEST_CPP_FLAGS=--cpp-std c++0x %TEST_CPP_FLAGS% set TEST_CPP_FLAGS=--gen-compare --cpp-ptr-type flatbuffers::unique_ptr %TEST_CPP_FLAGS% set TEST_BASE_FLAGS=--reflect-names --gen-mutable --gen-object-api diff --git a/tests/generate_code.sh b/tests/generate_code.sh index 633748d3160..fc8241f5a90 100755 --- a/tests/generate_code.sh +++ b/tests/generate_code.sh @@ -17,8 +17,8 @@ set -e commandline="'$*'" TEST_CPP_FLAGS= -if [[ $commandline == *"--cpp-std legacy"* ]]; then - TEST_CPP_FLAGS="--cpp-std legacy $TEST_CPP_FLAGS" +if [[ $commandline == *"--cpp-std c++0x"* ]]; then + TEST_CPP_FLAGS="--cpp-std c++0x $TEST_CPP_FLAGS" fi TEST_CPP_FLAGS="--gen-compare --cpp-ptr-type flatbuffers::unique_ptr $TEST_CPP_FLAGS" @@ -49,4 +49,4 @@ cd ../samples ../flatc --cpp --lobster $TEST_BASE_FLAGS $TEST_CPP_FLAGS monster.fbs ../flatc -b --schema --bfbs-comments --bfbs-builtins monster.fbs cd ../reflection -./generate_code.sh --cpp-std legacy +./generate_code.sh --cpp-std c++0x From 74454e34940f04ea37ea0e2e0091ba8066342acc Mon Sep 17 00:00:00 2001 From: vglavnyy Date: Tue, 17 Dec 2019 20:15:04 +0700 Subject: [PATCH 3/8] Add 'cpp::CppStandard' enum --- src/idl_gen_cpp.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 43b17360c74..4f2612266cd 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -55,15 +55,17 @@ static std::string GeneratedFileName(const std::string &path, namespace cpp { +enum CppStandard { CPP_STD_X0 = 0, CPP_STD_11, CPP_STD_17 }; + // Extension of IDLOptions for cpp-generator. struct IDLOptionsCpp : public IDLOptions { // All fields start with 'g_' prefix to distinguish from the base IDLOptions. - int g_cpp_std; // Base version of C++ standard (0, 11, 17). - bool g_only_fixed_enums; // Generate underlaying type for all enums. + CppStandard g_cpp_std; // Base version of C++ standard. + bool g_only_fixed_enums; // Generate underlaying type for all enums. // clang-format off IDLOptionsCpp(const IDLOptions &opts) : IDLOptions(opts), - g_cpp_std(11), + g_cpp_std(CPP_STD_11), g_only_fixed_enums(true) {} // clang-format on @@ -2989,12 +2991,13 @@ bool GenerateCPP(const Parser &parser, const std::string &path, std::transform(cpp_std.begin(), cpp_std.end(), cpp_std.begin(), ToUpper); if (cpp_std.empty() || cpp_std == "C++11") { // Use the standard C++11 code generator. - opts.g_cpp_std = 11; + opts.g_cpp_std = cpp::CppStandard::CPP_STD_11; + opts.g_only_fixed_enums = true; } else if (cpp_std == "C++0X") { - opts.g_cpp_std = 0; + opts.g_cpp_std = cpp::CppStandard::CPP_STD_X0; opts.g_only_fixed_enums = false; } else if (cpp_std == "C++17") { - opts.g_cpp_std = 17; + opts.g_cpp_std = cpp::CppStandard::CPP_STD_17; // With c++17 generate strong enums only. opts.scoped_enums = true; // By default, prefixed_enums==true, reset it. From 2d0b12304f99afe1e3bc00fefb8542e11d6d12db Mon Sep 17 00:00:00 2001 From: vglavnyy Date: Thu, 19 Dec 2019 07:22:51 +0700 Subject: [PATCH 4/8] Add testing engine into test_cpp17 --- CMakeLists.txt | 46 ++++++++++++++++++++------------------ src/idl_gen_cpp.cpp | 6 ++--- tests/cpp17/test_cpp17.cpp | 19 +++++++++++++++- 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e26f18ae835..52372e28ae5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -150,6 +150,8 @@ set(FlatBuffers_Tests_SRCS set(FlatBuffers_Tests_CPP17_SRCS ${FlatBuffers_Library_SRCS} + tests/test_assert.h + tests/test_assert.cpp tests/cpp17/test_cpp17.cpp # file generate by running compiler on tests/monster_test.fbs ${CMAKE_CURRENT_BINARY_DIR}/tests/cpp17/generated_cpp17/monster_test_generated.h @@ -268,23 +270,28 @@ if(FLATBUFFERS_CODE_COVERAGE) endif() function(add_fsanitize_to_target _target _sanitizer) - # FLATBUFFERS_CODE_SANITIZE: boolean {ON,OFF,YES,NO} or string with list of sanitizer. - # List of sanitizer is string starts with '=': "=address,undefined,thread,memory". - if((${CMAKE_CXX_COMPILER_ID} MATCHES "Clang") OR - ((${CMAKE_CXX_COMPILER_ID} MATCHES "GNU") AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9")) - ) - set(_sanitizer_flags "=address,undefined") - if(_sanitizer MATCHES "=.*") - # override default by user-defined sanitizer list - set(_sanitizer_flags ${_sanitizer}) + if(WIN32) + target_compile_definitions(${_target} PRIVATE FLATBUFFERS_MEMORY_LEAK_TRACKING) + message(STATUS "Sanitizer MSVC::_CrtDumpMemoryLeaks added to ${_target}") + else() + # FLATBUFFERS_CODE_SANITIZE: boolean {ON,OFF,YES,NO} or string with list of sanitizer. + # List of sanitizer is string starts with '=': "=address,undefined,thread,memory". + if((${CMAKE_CXX_COMPILER_ID} MATCHES "Clang") OR + ((${CMAKE_CXX_COMPILER_ID} MATCHES "GNU") AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9")) + ) + set(_sanitizer_flags "=address,undefined") + if(_sanitizer MATCHES "=.*") + # override default by user-defined sanitizer list + set(_sanitizer_flags ${_sanitizer}) + endif() + target_compile_options(${_target} PRIVATE + -g -fsigned-char -fno-omit-frame-pointer + "-fsanitize${_sanitizer_flags}") + target_link_libraries(${_target} PRIVATE + "-fsanitize${_sanitizer_flags}") + set_property(TARGET ${_target} PROPERTY POSITION_INDEPENDENT_CODE ON) + message(STATUS "Sanitizer ${_sanitizer_flags} added to ${_target}") endif() - target_compile_options(${_target} PRIVATE - -g -fsigned-char -fno-omit-frame-pointer - "-fsanitize${_sanitizer_flags}") - target_link_libraries(${_target} PRIVATE - "-fsanitize${_sanitizer_flags}") - set_property(TARGET ${_target} PROPERTY POSITION_INDEPENDENT_CODE ON) - message(STATUS "Sanitizer ${_sanitizer_flags} added to ${_target}") endif() endfunction() @@ -388,12 +395,7 @@ if(FLATBUFFERS_BUILD_TESTS) PROPERTY COMPILE_DEFINITIONS FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE FLATBUFFERS_DEBUG_VERIFICATION_FAILURE=1) if(FLATBUFFERS_CODE_SANITIZE) - if(WIN32) - target_compile_definitions(flattests PRIVATE FLATBUFFERS_MEMORY_LEAK_TRACKING) - message(STATUS "Sanitizer MSVC::_CrtDumpMemoryLeaks added to flattests") - else() add_fsanitize_to_target(flattests ${FLATBUFFERS_CODE_SANITIZE}) - endif() endif() compile_flatbuffers_schema_to_cpp(samples/monster.fbs) @@ -410,7 +412,7 @@ if(FLATBUFFERS_BUILD_TESTS) FLATBUFFERS_DEBUG_VERIFICATION_FAILURE=1 ) - if(NOT WIN32 AND FLATBUFFERS_CODE_SANITIZE) + if(FLATBUFFERS_CODE_SANITIZE) add_fsanitize_to_target(flattests_cpp17 ${FLATBUFFERS_CODE_SANITIZE}) endif() endif(FLATBUFFERS_BUILD_CPP17) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 4f2612266cd..3c7d9a14d42 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -2991,13 +2991,13 @@ bool GenerateCPP(const Parser &parser, const std::string &path, std::transform(cpp_std.begin(), cpp_std.end(), cpp_std.begin(), ToUpper); if (cpp_std.empty() || cpp_std == "C++11") { // Use the standard C++11 code generator. - opts.g_cpp_std = cpp::CppStandard::CPP_STD_11; + opts.g_cpp_std = cpp::CPP_STD_11; opts.g_only_fixed_enums = true; } else if (cpp_std == "C++0X") { - opts.g_cpp_std = cpp::CppStandard::CPP_STD_X0; + opts.g_cpp_std = cpp::CPP_STD_X0; opts.g_only_fixed_enums = false; } else if (cpp_std == "C++17") { - opts.g_cpp_std = cpp::CppStandard::CPP_STD_17; + opts.g_cpp_std = cpp::CPP_STD_17; // With c++17 generate strong enums only. opts.scoped_enums = true; // By default, prefixed_enums==true, reset it. diff --git a/tests/cpp17/test_cpp17.cpp b/tests/cpp17/test_cpp17.cpp index 6c03645fbf0..291aa8389a3 100644 --- a/tests/cpp17/test_cpp17.cpp +++ b/tests/cpp17/test_cpp17.cpp @@ -25,6 +25,7 @@ #include "flatbuffers/minireflect.h" #include "flatbuffers/registry.h" #include "flatbuffers/util.h" +#include "test_assert.h" // Embed generated code into an isolated namespace. namespace cpp17 { @@ -35,4 +36,20 @@ namespace cpp11 { #include "../monster_test_generated.h" } // namespace cpp11 -int main(int /*argc*/, const char * /*argv*/ []) { return 0; } +int FlatBufferCpp17Tests() { + TEST_ASSERT(true); + return 0; +} + +int main(int /*argc*/, const char * /*argv*/[]) { + InitTestEngine(); + + FlatBufferCpp17Tests(); + + if (!testing_fails) { + TEST_OUTPUT_LINE("C++17: ALL TESTS PASSED"); + } else { + TEST_OUTPUT_LINE("C++17: %d FAILED TESTS", testing_fails); + } + return CloseTestEngine(); +} From dfc4c9bdc430aee7f791b96995bc06cc14fee8c4 Mon Sep 17 00:00:00 2001 From: vglavnyy Date: Thu, 19 Dec 2019 07:47:31 +0700 Subject: [PATCH 5/8] Rebase to upstream/master --- tests/cpp17/generated_cpp17/monster_test_generated.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/cpp17/generated_cpp17/monster_test_generated.h b/tests/cpp17/generated_cpp17/monster_test_generated.h index a77ddca1bc1..d539394f791 100644 --- a/tests/cpp17/generated_cpp17/monster_test_generated.h +++ b/tests/cpp17/generated_cpp17/monster_test_generated.h @@ -934,7 +934,7 @@ struct MonsterT : public flatbuffers::NativeTable { std::string name; std::vector inventory; MyGame::Example::Color color; - AnyUnion test; + MyGame::Example::AnyUnion test; std::vector test4; std::vector testarrayofstring; std::vector> testarrayoftables; @@ -969,8 +969,8 @@ struct MonsterT : public flatbuffers::NativeTable { std::vector> vector_of_co_owning_references; ReferrableT *non_owning_reference; std::vector vector_of_non_owning_references; - AnyUniqueAliasesUnion any_unique; - AnyAmbiguousAliasesUnion any_ambiguous; + MyGame::Example::AnyUniqueAliasesUnion any_unique; + MyGame::Example::AnyAmbiguousAliasesUnion any_ambiguous; std::vector vector_of_enums; MyGame::Example::Race signed_enum; MonsterT() @@ -2281,7 +2281,7 @@ inline void Monster::UnPackTo(MonsterT *_o, const flatbuffers::resolver_function { auto _e = inventory(); if (_e) { _o->inventory.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->inventory[_i] = _e->Get(_i); } } } { auto _e = color(); _o->color = _e; } { auto _e = test_type(); _o->test.type = _e; } - { auto _e = test(); if (_e) _o->test.value = AnyUnion::UnPack(_e, test_type(), _resolver); } + { auto _e = test(); if (_e) _o->test.value = MyGame::Example::AnyUnion::UnPack(_e, test_type(), _resolver); } { auto _e = test4(); if (_e) { _o->test4.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->test4[_i] = *_e->Get(_i); } } } { auto _e = testarrayofstring(); if (_e) { _o->testarrayofstring.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->testarrayofstring[_i] = _e->Get(_i)->str(); } } } { auto _e = testarrayoftables(); if (_e) { _o->testarrayoftables.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->testarrayoftables[_i] = std::unique_ptr(_e->Get(_i)->UnPack(_resolver)); } } } @@ -2324,9 +2324,9 @@ if (_resolver) (*_resolver)(reinterpret_cast(&_o->non_owning_reference) { auto _e = vector_of_non_owning_references(); if (_e) { _o->vector_of_non_owning_references.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { //vector resolver, naked if (_resolver) (*_resolver)(reinterpret_cast(&_o->vector_of_non_owning_references[_i]), static_cast(_e->Get(_i))); else _o->vector_of_non_owning_references[_i] = nullptr; } } } { auto _e = any_unique_type(); _o->any_unique.type = _e; } - { auto _e = any_unique(); if (_e) _o->any_unique.value = AnyUniqueAliasesUnion::UnPack(_e, any_unique_type(), _resolver); } + { auto _e = any_unique(); if (_e) _o->any_unique.value = MyGame::Example::AnyUniqueAliasesUnion::UnPack(_e, any_unique_type(), _resolver); } { auto _e = any_ambiguous_type(); _o->any_ambiguous.type = _e; } - { auto _e = any_ambiguous(); if (_e) _o->any_ambiguous.value = AnyAmbiguousAliasesUnion::UnPack(_e, any_ambiguous_type(), _resolver); } + { auto _e = any_ambiguous(); if (_e) _o->any_ambiguous.value = MyGame::Example::AnyAmbiguousAliasesUnion::UnPack(_e, any_ambiguous_type(), _resolver); } { auto _e = vector_of_enums(); if (_e) { _o->vector_of_enums.resize(_e->size()); for (flatbuffers::uoffset_t _i = 0; _i < _e->size(); _i++) { _o->vector_of_enums[_i] = static_cast(_e->Get(_i)); } } } { auto _e = signed_enum(); _o->signed_enum = _e; } } From 0214897a2b7a63733f0436a75a40f07249e5d841 Mon Sep 17 00:00:00 2001 From: vglavnyy Date: Fri, 20 Dec 2019 00:42:22 +0700 Subject: [PATCH 6/8] Set default '--cpp-std C++0x' --- src/idl_gen_cpp.cpp | 10 +++++----- tests/generate_code.bat | 8 ++++++-- tests/generate_code.sh | 6 ++++-- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 3c7d9a14d42..b59d567ee1c 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -2987,15 +2987,15 @@ bool GenerateCPP(const Parser &parser, const std::string &path, cpp::IDLOptionsCpp opts(parser.opts); // The '--cpp_std' argument could be extended (like ASAN): // Example: "flatc --cpp_std c++17:option1:option2". - auto cpp_std = opts.cpp_std; + auto cpp_std = !opts.cpp_std.empty() ? opts.cpp_std : "C++0X"; std::transform(cpp_std.begin(), cpp_std.end(), cpp_std.begin(), ToUpper); - if (cpp_std.empty() || cpp_std == "C++11") { + if (cpp_std == "C++0X") { + opts.g_cpp_std = cpp::CPP_STD_X0; + opts.g_only_fixed_enums = false; + } else if (cpp_std == "C++11") { // Use the standard C++11 code generator. opts.g_cpp_std = cpp::CPP_STD_11; opts.g_only_fixed_enums = true; - } else if (cpp_std == "C++0X") { - opts.g_cpp_std = cpp::CPP_STD_X0; - opts.g_only_fixed_enums = false; } else if (cpp_std == "C++17") { opts.g_cpp_std = cpp::CPP_STD_17; // With c++17 generate strong enums only. diff --git a/tests/generate_code.bat b/tests/generate_code.bat index a8bad26a331..06ad544f126 100644 --- a/tests/generate_code.bat +++ b/tests/generate_code.bat @@ -18,8 +18,12 @@ set buildtype=Release if "%1"=="-b" set buildtype=%2 set commandline=%* -set TEST_CPP_FLAGS= -if NOT "%commandline%"=="%commandline:--cpp-std c++0x=%" set TEST_CPP_FLAGS=--cpp-std c++0x %TEST_CPP_FLAGS% + +if NOT "%commandline%"=="%commandline:--cpp-std c++0x=%" ( + set TEST_CPP_FLAGS=--cpp-std c++0x +) else ( + set TEST_CPP_FLAGS=--cpp-std c++11 +) set TEST_CPP_FLAGS=--gen-compare --cpp-ptr-type flatbuffers::unique_ptr %TEST_CPP_FLAGS% set TEST_BASE_FLAGS=--reflect-names --gen-mutable --gen-object-api diff --git a/tests/generate_code.sh b/tests/generate_code.sh index fc8241f5a90..f68f7d0439f 100755 --- a/tests/generate_code.sh +++ b/tests/generate_code.sh @@ -16,9 +16,11 @@ set -e commandline="'$*'" -TEST_CPP_FLAGS= + if [[ $commandline == *"--cpp-std c++0x"* ]]; then - TEST_CPP_FLAGS="--cpp-std c++0x $TEST_CPP_FLAGS" + TEST_CPP_FLAGS="--cpp-std c++0x" +else + TEST_CPP_FLAGS="--cpp-std c++11" fi TEST_CPP_FLAGS="--gen-compare --cpp-ptr-type flatbuffers::unique_ptr $TEST_CPP_FLAGS" From d8a3f10aa843889dfa6a3339daa3972b8c6aec5d Mon Sep 17 00:00:00 2001 From: vglavnyy Date: Sun, 22 Dec 2019 19:51:31 +0700 Subject: [PATCH 7/8] Fix code generation (--cpp_std C++11) in CMakeLists.txt - Fix dependency declaration of grpctest target --- .gitignore | 1 + CMakeLists.txt | 16 ++++++-- grpc/build_grpc.sh | 2 +- grpc/tests/message_builder_test.cpp | 62 +++++++++++++++++------------ tests/test_builder.cpp | 17 ++++---- tests/test_builder.h | 45 ++++++++++++--------- 6 files changed, 86 insertions(+), 57 deletions(-) diff --git a/.gitignore b/.gitignore index b7b41acb330..d6ec9fd87ec 100644 --- a/.gitignore +++ b/.gitignore @@ -116,3 +116,4 @@ dart/doc/api/ Cargo.lock .corpus** .seed** +grpc/google/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 52372e28ae5..75744d5b272 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -192,8 +192,8 @@ set(FlatBuffers_GRPCTest_SRCS tests/test_builder.cpp grpc/tests/grpctest.cpp grpc/tests/message_builder_test.cpp - # file generated by running compiler on samples/monster.fbs - ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h + # file generate by running compiler on tests/monster_test.fbs + ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h ) # source_group(Compiler FILES ${FlatBuffers_Compiler_SRCS}) @@ -350,6 +350,8 @@ endif() function(compile_flatbuffers_schema_to_cpp_opt SRC_FBS OPT) if(FLATBUFFERS_BUILD_LEGACY) set(OPT ${OPT};--cpp-std c++0x) + else() + set(OPT ${OPT};--cpp-std c++11) endif() message(STATUS "Add the code-generation command for the `${SRC_FBS}` schema with '${OPT}'") get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH) @@ -380,6 +382,7 @@ function(compile_flatbuffers_schema_to_binary SRC_FBS) endfunction() if(FLATBUFFERS_BUILD_TESTS) + # TODO: Replace this sequence by custom_target which calls the generate_code.bat(sh). compile_flatbuffers_schema_to_cpp(tests/monster_test.fbs) compile_flatbuffers_schema_to_cpp(tests/namespace_test/namespace_test1.fbs) compile_flatbuffers_schema_to_cpp(tests/namespace_test/namespace_test2.fbs) @@ -405,6 +408,9 @@ if(FLATBUFFERS_BUILD_TESTS) add_executable(flatsamplebfbs ${FlatBuffers_Sample_BFBS_SRCS}) if(FLATBUFFERS_BUILD_CPP17) + # Don't generate header for flattests_cpp17 target. + # This target uses "generated_cpp17/monster_test_generated.h" + # produced by direct call of generate_code.bat(sh) script. add_executable(flattests_cpp17 ${FlatBuffers_Tests_CPP17_SRCS}) target_compile_features(flattests_cpp17 PRIVATE cxx_std_17) target_compile_definitions(flattests_cpp17 PRIVATE @@ -432,7 +438,11 @@ if(FLATBUFFERS_BUILD_GRPCTEST) INCLUDE_DIRECTORIES(${PROTOBUF_DOWNLOAD_PATH}/src) LINK_DIRECTORIES(${GRPC_INSTALL_PATH}/lib) add_executable(grpctest ${FlatBuffers_GRPCTest_SRCS}) - target_link_libraries(grpctest grpc++_unsecure grpc_unsecure gpr pthread dl) + target_link_libraries(grpctest PRIVATE grpc++_unsecure grpc_unsecure gpr pthread dl) + if(NOT WIN32 AND FLATBUFFERS_CODE_SANITIZE) + # GRPC test has problems with alignment and will fail under ASAN/UBSAN. + # add_fsanitize_to_target(grpctest ${FLATBUFFERS_CODE_SANITIZE}) + endif() endif() include(CMake/Version.cmake) diff --git a/grpc/build_grpc.sh b/grpc/build_grpc.sh index 8fb9e1c4c39..3c0c27e1a17 100755 --- a/grpc/build_grpc.sh +++ b/grpc/build_grpc.sh @@ -5,7 +5,7 @@ grpc_1_15_1_githash=1a60e6971f428323245a930031ad267bb3142ba4 function build_grpc () { git clone https://github.com/grpc/grpc.git google/grpc cd google/grpc - git checkout ${grpc_1_15_1_githash} + git checkout ${grpc_1_15_1_githash} git submodule update --init make make install prefix=`pwd`/install diff --git a/grpc/tests/message_builder_test.cpp b/grpc/tests/message_builder_test.cpp index 04153f1b9b2..906fe68206b 100644 --- a/grpc/tests/message_builder_test.cpp +++ b/grpc/tests/message_builder_test.cpp @@ -8,18 +8,19 @@ using MyGame::Example::CreateStat; using MyGame::Example::Vec3; bool verify(flatbuffers::grpc::Message &msg, - const std::string &expected_name, Color color) { + const std::string &expected_name, Color expected_color) { const Monster *monster = msg.GetRoot(); - return (monster->name()->str() == expected_name) && - (monster->color() == color); + const auto name = monster->name()->str(); + const auto color = monster->color(); + TEST_EQ(name, expected_name); + TEST_EQ(color, expected_color); + return (name == expected_name) && (color == expected_color); } bool release_n_verify(flatbuffers::grpc::MessageBuilder &mbb, - const std::string &expected_name, Color color) { + const std::string &expected_name, Color expected_color) { flatbuffers::grpc::Message msg = mbb.ReleaseMessage(); - const Monster *monster = msg.GetRoot(); - return (monster->name()->str() == expected_name) && - (monster->color() == color); + return verify(msg, expected_name, expected_color); } void builder_move_assign_after_releaseraw_test( @@ -36,7 +37,7 @@ void builder_move_assign_after_releaseraw_test( // Move into a released builder. dst = std::move(src); TEST_EQ(dst.GetSize(), src_size); - TEST_ASSERT(release_n_verify(dst, m2_name, m2_color)); + TEST_ASSERT(release_n_verify(dst, m2_name(), m2_color())); TEST_EQ(src.GetSize(), 0); grpc_slice_unref(slice); } @@ -53,7 +54,7 @@ struct BuilderReuseTests { auto root_offset1 = populate1(mb); mb.Finish(root_offset1); buffers.push_back(mb.ReleaseMessage()); - TEST_ASSERT_FUNC(verify(buffers[i], m1_name, m1_color)); + TEST_ASSERT_FUNC(verify(buffers[i], m1_name(), m1_color())); } } @@ -69,7 +70,7 @@ struct BuilderReuseTests { auto root_offset1 = populate1(mb); mb.Finish(root_offset1); buffers.push_back(mb.Release()); - TEST_ASSERT_FUNC(verify(buffers[i], m1_name, m1_color)); + TEST_ASSERT_FUNC(verify(buffers[i], m1_name(), m1_color())); } } @@ -83,7 +84,7 @@ struct BuilderReuseTests { size_t size, offset; grpc_slice slice; const uint8_t *buf = mb.ReleaseRaw(size, offset, slice); - TEST_ASSERT_FUNC(verify(buf, offset, m1_name, m1_color)); + TEST_ASSERT_FUNC(verify(buf, offset, m1_name(), m1_color())); grpc_slice_unref(slice); } } @@ -103,7 +104,7 @@ struct BuilderReuseTests { auto root_offset1 = populate1(dst); dst.Finish(root_offset1); buffers.push_back(dst.Release()); - TEST_ASSERT_FUNC(verify(buffers[i], m1_name, m1_color)); + TEST_ASSERT_FUNC(verify(buffers[i], m1_name(), m1_color())); // bring dst back to life. SrcBuilder src; @@ -126,7 +127,7 @@ struct BuilderReuseTests { auto root_offset1 = populate1(dst); dst.Finish(root_offset1); buffers.push_back(dst.ReleaseMessage()); - TEST_ASSERT_FUNC(verify(buffers[i], m1_name, m1_color)); + TEST_ASSERT_FUNC(verify(buffers[i], m1_name(), m1_color())); // bring dst back to life. SrcBuilder src; @@ -147,7 +148,7 @@ struct BuilderReuseTests { size_t size, offset; grpc_slice slice = grpc_empty_slice(); const uint8_t *buf = dst.ReleaseRaw(size, offset, slice); - TEST_ASSERT_FUNC(verify(buf, offset, m1_name, m1_color)); + TEST_ASSERT_FUNC(verify(buf, offset, m1_name(), m1_color())); grpc_slice_unref(slice); SrcBuilder src; @@ -199,7 +200,7 @@ void slice_allocator_tests() { void populate_first_half(MyGame::Example::MonsterBuilder &wrapper, flatbuffers::Offset name_offset) { wrapper.add_name(name_offset); - wrapper.add_color(m1_color); + wrapper.add_color(m1_color()); } /// This function does not populate exactly the second half of the table. But it @@ -230,7 +231,7 @@ void test_only_hack_update_fbb_reference( void builder_move_ctor_conversion_before_finish_half_n_half_table_test() { for (size_t initial_size = 4; initial_size <= 2048; initial_size *= 2) { flatbuffers::FlatBufferBuilder fbb(initial_size); - auto name_offset = fbb.CreateString(m1_name); + auto name_offset = fbb.CreateString(m1_name()); MyGame::Example::MonsterBuilder monsterBuilder( fbb); // starts a table in FlatBufferBuilder populate_first_half(monsterBuilder, name_offset); @@ -238,7 +239,7 @@ void builder_move_ctor_conversion_before_finish_half_n_half_table_test() { test_only_hack_update_fbb_reference(monsterBuilder, mb); // hack populate_second_half(monsterBuilder); mb.Finish(monsterBuilder.Finish()); // ends the table in MessageBuilder - TEST_ASSERT_FUNC(release_n_verify(mb, m1_name, m1_color)); + TEST_ASSERT_FUNC(release_n_verify(mb, m1_name(), m1_color())); TEST_EQ_FUNC(fbb.GetSize(), 0); } } @@ -246,15 +247,24 @@ void builder_move_ctor_conversion_before_finish_half_n_half_table_test() { /// This test populates a COMPLETE inner table before move conversion and later /// populates more members in the outer table. void builder_move_ctor_conversion_before_finish_test() { - for (size_t initial_size = 4; initial_size <= 2048; initial_size *= 2) { + for (size_t initial_size = 1; initial_size <= 2048; initial_size += 1) { flatbuffers::FlatBufferBuilder fbb(initial_size); auto stat_offset = CreateStat(fbb, fbb.CreateString("SomeId"), 0, 0); flatbuffers::grpc::MessageBuilder mb(std::move(fbb)); auto monster_offset = - CreateMonster(mb, 0, 150, 100, mb.CreateString(m1_name), 0, m1_color, + CreateMonster(mb, 0, 150, 100, mb.CreateString(m1_name()), 0, m1_color(), Any_NONE, 0, 0, 0, 0, 0, 0, stat_offset); mb.Finish(monster_offset); - TEST_ASSERT_FUNC(release_n_verify(mb, m1_name, m1_color)); + { + auto mon = flatbuffers::GetRoot(mb.GetBufferPointer()); + TEST_NOTNULL(mon); + TEST_NOTNULL(mon->name()); + TEST_EQ_STR(mon->name()->c_str(), m1_name().c_str()); + TEST_EQ(mon->color(), m1_color()); + } + TEST_EQ(1, MyGame::Example::Color_Red); + TEST_EQ(1, m1_color()); + TEST_ASSERT_FUNC(release_n_verify(mb, m1_name(), m1_color())); TEST_EQ_FUNC(fbb.GetSize(), 0); } } @@ -269,7 +279,7 @@ void builder_move_assign_conversion_before_finish_half_n_half_table_test() { for (int i = 0; i < 5; ++i) { flatbuffers::FlatBufferBuilder fbb; - auto name_offset = fbb.CreateString(m1_name); + auto name_offset = fbb.CreateString(m1_name()); MyGame::Example::MonsterBuilder monsterBuilder( fbb); // starts a table in FlatBufferBuilder populate_first_half(monsterBuilder, name_offset); @@ -277,7 +287,7 @@ void builder_move_assign_conversion_before_finish_half_n_half_table_test() { test_only_hack_update_fbb_reference(monsterBuilder, mb); // hack populate_second_half(monsterBuilder); mb.Finish(monsterBuilder.Finish()); // ends the table in MessageBuilder - TEST_ASSERT_FUNC(release_n_verify(mb, m1_name, m1_color)); + TEST_ASSERT_FUNC(release_n_verify(mb, m1_name(), m1_color())); TEST_EQ_FUNC(fbb.GetSize(), 0); } } @@ -292,10 +302,10 @@ void builder_move_assign_conversion_before_finish_test() { auto stat_offset = CreateStat(fbb, fbb.CreateString("SomeId"), 0, 0); mb = std::move(fbb); auto monster_offset = - CreateMonster(mb, 0, 150, 100, mb.CreateString(m1_name), 0, m1_color, + CreateMonster(mb, 0, 150, 100, mb.CreateString(m1_name()), 0, m1_color(), Any_NONE, 0, 0, 0, 0, 0, 0, stat_offset); mb.Finish(monster_offset); - TEST_ASSERT_FUNC(release_n_verify(mb, m1_name, m1_color)); + TEST_ASSERT_FUNC(release_n_verify(mb, m1_name(), m1_color())); TEST_EQ_FUNC(fbb.GetSize(), 0); } } @@ -306,7 +316,7 @@ void builder_move_ctor_conversion_after_finish_test() { flatbuffers::FlatBufferBuilder fbb; fbb.Finish(populate1(fbb)); flatbuffers::grpc::MessageBuilder mb(std::move(fbb)); - TEST_ASSERT_FUNC(release_n_verify(mb, m1_name, m1_color)); + TEST_ASSERT_FUNC(release_n_verify(mb, m1_name(), m1_color())); TEST_EQ_FUNC(fbb.GetSize(), 0); } @@ -319,7 +329,7 @@ void builder_move_assign_conversion_after_finish_test() { for (int i = 0; i < 5; ++i) { fbb.Finish(populate1(fbb)); mb = std::move(fbb); - TEST_ASSERT_FUNC(release_n_verify(mb, m1_name, m1_color)); + TEST_ASSERT_FUNC(release_n_verify(mb, m1_name(), m1_color())); TEST_EQ_FUNC(fbb.GetSize(), 0); } } diff --git a/tests/test_builder.cpp b/tests/test_builder.cpp index 685c643ccea..d46c9129985 100644 --- a/tests/test_builder.cpp +++ b/tests/test_builder.cpp @@ -5,11 +5,6 @@ using namespace MyGame::Example; -const std::string m1_name = "Cyberdemon"; -const Color m1_color = Color_Red; -const std::string m2_name = "Imp"; -const Color m2_color = Color_Green; - struct OwnedAllocator : public flatbuffers::DefaultAllocator {}; class TestHeapBuilder : public flatbuffers::FlatBufferBuilder { @@ -88,14 +83,14 @@ struct GrpcLikeMessageBuilder : private AllocatorMember, flatbuffers::Offset populate1( flatbuffers::FlatBufferBuilder &builder) { - auto name_offset = builder.CreateString(m1_name); - return CreateMonster(builder, nullptr, 0, 0, name_offset, 0, m1_color); + auto name_offset = builder.CreateString(m1_name()); + return CreateMonster(builder, nullptr, 0, 0, name_offset, 0, m1_color()); } flatbuffers::Offset populate2( flatbuffers::FlatBufferBuilder &builder) { - auto name_offset = builder.CreateString(m2_name); - return CreateMonster(builder, nullptr, 0, 0, name_offset, 0, m2_color); + auto name_offset = builder.CreateString(m2_name()); + return CreateMonster(builder, nullptr, 0, 0, name_offset, 0, m2_color()); } uint8_t *release_raw_base(flatbuffers::FlatBufferBuilder &fbb, size_t &size, @@ -158,3 +153,7 @@ void FlatBufferBuilderTest() { BuilderReuseTests::run_tests( TestSelector(tests, tests + 4)); } + +// Link-time check using pointer type. +void CheckTestGeneratedIsValid(const MyGame::Example::Color&) +{} \ No newline at end of file diff --git a/tests/test_builder.h b/tests/test_builder.h index eb719a80c4a..5555e907175 100644 --- a/tests/test_builder.h +++ b/tests/test_builder.h @@ -21,10 +21,19 @@ template struct is_same { static const bool value = false; }; template struct is_same { static const bool value = true; }; -extern const std::string m1_name; -extern const Color m1_color; -extern const std::string m2_name; -extern const Color m2_color; +inline std::string m1_name() { return "Cyberdemon"; } +inline std::string m2_name() { return "Imp"; } +inline MyGame::Example::Color m1_color() { + return MyGame::Example::Color_Red; +} +inline MyGame::Example::Color m2_color() { + return MyGame::Example::Color_Green; +} +inline void m1_color_check() { + // Ensure that all compilation units see the same monster_test_generated.h. + extern void CheckTestGeneratedIsValid(const MyGame::Example::Color&); + CheckTestGeneratedIsValid(m1_color()); +} flatbuffers::Offset populate1(flatbuffers::FlatBufferBuilder &builder); flatbuffers::Offset populate2(flatbuffers::FlatBufferBuilder &builder); @@ -66,7 +75,7 @@ void builder_move_assign_after_releaseraw_test(Builder b1) { // Move into a released builder. b1 = std::move(src); TEST_EQ_FUNC(b1.GetSize(), src_size); - TEST_ASSERT_FUNC(release_n_verify(b1, m2_name, m2_color)); + TEST_ASSERT_FUNC(release_n_verify(b1, m2_name(), m2_color())); TEST_EQ_FUNC(src.GetSize(), 0); } // clang-format off @@ -104,7 +113,7 @@ struct BuilderTests { auto root_offset1 = populate1(src); DestBuilder dst(std::move(src)); dst.Finish(root_offset1); - TEST_ASSERT_FUNC(release_n_verify(dst, m1_name, m1_color)); + TEST_ASSERT_FUNC(release_n_verify(dst, m1_name(), m1_color())); TEST_EQ_FUNC(src.GetSize(), 0); } @@ -115,7 +124,7 @@ struct BuilderTests { auto src_size = src.GetSize(); DestBuilder dst(std::move(src)); TEST_EQ_FUNC(dst.GetSize(), src_size); - TEST_ASSERT_FUNC(release_n_verify(dst, m1_name, m1_color)); + TEST_ASSERT_FUNC(release_n_verify(dst, m1_name(), m1_color())); TEST_EQ_FUNC(src.GetSize(), 0); } @@ -126,7 +135,7 @@ struct BuilderTests { populate2(dst); dst = std::move(src); dst.Finish(root_offset1); - TEST_ASSERT_FUNC(release_n_verify(dst, m1_name, m1_color)); + TEST_ASSERT_FUNC(release_n_verify(dst, m1_name(), m1_color())); TEST_EQ_FUNC(src.GetSize(), 0); } @@ -140,7 +149,7 @@ struct BuilderTests { dst.Finish(root_offset2); dst = std::move(src); TEST_EQ_FUNC(dst.GetSize(), src_size); - TEST_ASSERT_FUNC(release_n_verify(dst, m1_name, m1_color)); + TEST_ASSERT_FUNC(release_n_verify(dst, m1_name(), m1_color())); TEST_EQ_FUNC(src.GetSize(), 0); } @@ -159,7 +168,7 @@ struct BuilderTests { // Move into a released builder. dst = std::move(src); TEST_EQ_FUNC(dst.GetSize(), src_size); - TEST_ASSERT_FUNC(release_n_verify(dst, m2_name, m2_color)); + TEST_ASSERT_FUNC(release_n_verify(dst, m2_name(), m2_color())); TEST_EQ_FUNC(src.GetSize(), 0); } // clang-format off @@ -181,8 +190,8 @@ struct BuilderTests { dst.Finish(root_offset1); TEST_EQ_FUNC(src.GetSize() > size2, true); TEST_EQ_FUNC(dst.GetSize() > size1, true); - TEST_ASSERT_FUNC(release_n_verify(src, m2_name, m2_color)); - TEST_ASSERT_FUNC(release_n_verify(dst, m1_name, m1_color)); + TEST_ASSERT_FUNC(release_n_verify(src, m2_name(), m2_color())); + TEST_ASSERT_FUNC(release_n_verify(dst, m1_name(), m1_color())); } } @@ -201,8 +210,8 @@ struct BuilderTests { src.Swap(dst); TEST_EQ_FUNC(src.GetSize(), size2); TEST_EQ_FUNC(dst.GetSize(), size1); - TEST_ASSERT_FUNC(release_n_verify(src, m2_name, m2_color)); - TEST_ASSERT_FUNC(release_n_verify(dst, m1_name, m1_color)); + TEST_ASSERT_FUNC(release_n_verify(src, m2_name(), m2_color())); + TEST_ASSERT_FUNC(release_n_verify(dst, m1_name(), m1_color())); } } @@ -247,7 +256,7 @@ template struct BuilderReuseTests { auto root_offset1 = populate1(fbb); fbb.Finish(root_offset1); buffers.push_back(fbb.Release()); - TEST_ASSERT_FUNC(verify(buffers[i], m1_name, m1_color)); + TEST_ASSERT_FUNC(verify(buffers[i], m1_name(), m1_color())); } } @@ -260,7 +269,7 @@ template struct BuilderReuseTests { fbb.Finish(root_offset1); size_t size, offset; uint8_t *buf = release_raw_base(fbb, size, offset); - TEST_ASSERT_FUNC(verify(buf, offset, m1_name, m1_color)); + TEST_ASSERT_FUNC(verify(buf, offset, m1_name(), m1_color())); free_raw(fbb, buf); } } @@ -278,7 +287,7 @@ template struct BuilderReuseTests { auto root_offset1 = populate1(dst); dst.Finish(root_offset1); buffers.push_back(dst.Release()); - TEST_ASSERT_FUNC(verify(buffers[i], m1_name, m1_color)); + TEST_ASSERT_FUNC(verify(buffers[i], m1_name(), m1_color())); SrcBuilder src; dst = std::move(src); TEST_EQ_FUNC(src.GetSize(), 0); @@ -295,7 +304,7 @@ template struct BuilderReuseTests { dst.Finish(root_offset1); size_t size, offset; uint8_t *buf = release_raw_base(dst, size, offset); - TEST_ASSERT_FUNC(verify(buf, offset, m1_name, m1_color)); + TEST_ASSERT_FUNC(verify(buf, offset, m1_name(), m1_color())); free_raw(dst, buf); SrcBuilder src; dst = std::move(src); From 75fbd4da0aeea33558ace5de013c847813956cc8 Mon Sep 17 00:00:00 2001 From: vglavnyy Date: Tue, 24 Dec 2019 01:56:24 +0700 Subject: [PATCH 8/8] Revert --cpp-std for the tests from explicit C++11 to flatc default value (C++0x) --- CMakeLists.txt | 2 +- samples/monster_generated.h | 4 ++-- tests/generate_code.bat | 4 +++- tests/generate_code.sh | 3 ++- tests/monster_test_generated.h | 10 +++++----- tests/namespace_test/namespace_test1_generated.h | 2 +- tests/union_vector/union_vector_generated.h | 2 +- 7 files changed, 15 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 75744d5b272..2bb07a03d7e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -351,7 +351,7 @@ function(compile_flatbuffers_schema_to_cpp_opt SRC_FBS OPT) if(FLATBUFFERS_BUILD_LEGACY) set(OPT ${OPT};--cpp-std c++0x) else() - set(OPT ${OPT};--cpp-std c++11) + # --cpp-std is defined by flatc default settings. endif() message(STATUS "Add the code-generation command for the `${SRC_FBS}` schema with '${OPT}'") get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH) diff --git a/samples/monster_generated.h b/samples/monster_generated.h index 6e607c94bf2..8caef9e9872 100644 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -30,7 +30,7 @@ inline const flatbuffers::TypeTable *MonsterTypeTable(); inline const flatbuffers::TypeTable *WeaponTypeTable(); -enum Color : int8_t { +enum Color { Color_Red = 0, Color_Green = 1, Color_Blue = 2, @@ -63,7 +63,7 @@ inline const char *EnumNameColor(Color e) { return EnumNamesColor()[index]; } -enum Equipment : uint8_t { +enum Equipment { Equipment_NONE = 0, Equipment_Weapon = 1, Equipment_MIN = Equipment_NONE, diff --git a/tests/generate_code.bat b/tests/generate_code.bat index 06ad544f126..11846975e5c 100644 --- a/tests/generate_code.bat +++ b/tests/generate_code.bat @@ -19,10 +19,12 @@ if "%1"=="-b" set buildtype=%2 set commandline=%* + if NOT "%commandline%"=="%commandline:--cpp-std c++0x=%" ( set TEST_CPP_FLAGS=--cpp-std c++0x ) else ( - set TEST_CPP_FLAGS=--cpp-std c++11 + @rem --cpp-std is defined by flatc default settings. + set TEST_CPP_FLAGS= ) set TEST_CPP_FLAGS=--gen-compare --cpp-ptr-type flatbuffers::unique_ptr %TEST_CPP_FLAGS% diff --git a/tests/generate_code.sh b/tests/generate_code.sh index f68f7d0439f..57ed0145661 100755 --- a/tests/generate_code.sh +++ b/tests/generate_code.sh @@ -20,7 +20,8 @@ commandline="'$*'" if [[ $commandline == *"--cpp-std c++0x"* ]]; then TEST_CPP_FLAGS="--cpp-std c++0x" else - TEST_CPP_FLAGS="--cpp-std c++11" + # --cpp-std is defined by flatc default settings. + TEST_CPP_FLAGS= fi TEST_CPP_FLAGS="--gen-compare --cpp-ptr-type flatbuffers::unique_ptr $TEST_CPP_FLAGS" diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index c2e58a8f79b..fb3ca511def 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -100,7 +100,7 @@ inline const flatbuffers::TypeTable *MonsterTypeTable(); inline const flatbuffers::TypeTable *TypeAliasesTypeTable(); /// Composite components of Monster color. -enum Color : uint8_t { +enum Color { Color_Red = 1, /// \brief color Green /// Green is bit_flag with value (1u << 1) @@ -141,7 +141,7 @@ inline const char *EnumNameColor(Color e) { return EnumNamesColor()[index]; } -enum Race : int8_t { +enum Race { Race_None = -1, Race_Human = 0, Race_Dwarf = 1, @@ -177,7 +177,7 @@ inline const char *EnumNameRace(Race e) { return EnumNamesRace()[index]; } -enum Any : uint8_t { +enum Any { Any_NONE = 0, Any_Monster = 1, Any_TestSimpleTableWithEnum = 2, @@ -319,7 +319,7 @@ inline bool operator!=(const AnyUnion &lhs, const AnyUnion &rhs) { bool VerifyAny(flatbuffers::Verifier &verifier, const void *obj, Any type); bool VerifyAnyVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types); -enum AnyUniqueAliases : uint8_t { +enum AnyUniqueAliases { AnyUniqueAliases_NONE = 0, AnyUniqueAliases_M = 1, AnyUniqueAliases_TS = 2, @@ -461,7 +461,7 @@ inline bool operator!=(const AnyUniqueAliasesUnion &lhs, const AnyUniqueAliasesU bool VerifyAnyUniqueAliases(flatbuffers::Verifier &verifier, const void *obj, AnyUniqueAliases type); bool VerifyAnyUniqueAliasesVector(flatbuffers::Verifier &verifier, const flatbuffers::Vector> *values, const flatbuffers::Vector *types); -enum AnyAmbiguousAliases : uint8_t { +enum AnyAmbiguousAliases { AnyAmbiguousAliases_NONE = 0, AnyAmbiguousAliases_M1 = 1, AnyAmbiguousAliases_M2 = 2, diff --git a/tests/namespace_test/namespace_test1_generated.h b/tests/namespace_test/namespace_test1_generated.h index 6d28e7eb826..704c6c7d516 100644 --- a/tests/namespace_test/namespace_test1_generated.h +++ b/tests/namespace_test/namespace_test1_generated.h @@ -23,7 +23,7 @@ inline const flatbuffers::TypeTable *TableInNestedNSTypeTable(); inline const flatbuffers::TypeTable *StructInNestedNSTypeTable(); -enum EnumInNestedNS : int8_t { +enum EnumInNestedNS { EnumInNestedNS_A = 0, EnumInNestedNS_B = 1, EnumInNestedNS_C = 2, diff --git a/tests/union_vector/union_vector_generated.h b/tests/union_vector/union_vector_generated.h index d1dc312de8f..9e21f704fa5 100644 --- a/tests/union_vector/union_vector_generated.h +++ b/tests/union_vector/union_vector_generated.h @@ -33,7 +33,7 @@ inline const flatbuffers::TypeTable *BookReaderTypeTable(); inline const flatbuffers::TypeTable *MovieTypeTable(); -enum Character : uint8_t { +enum Character { Character_NONE = 0, Character_MuLan = 1, Character_Rapunzel = 2,