From 6267f8c6c002cb62d168fa7b7c6137e65f47b8a1 Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Wed, 25 Nov 2015 15:50:30 -0800 Subject: [PATCH 01/13] Fixed crash related to flatc parsing duplicate input files. Thanks @Chaosvex for reporting. Change-Id: I73f60ab0bf875a3e0849eaec5f42f6d036881094 Tested: on Linux. --- include/flatbuffers/idl.h | 2 +- src/idl_parser.cpp | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index ad755f8c9..f1276f01b 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -419,7 +419,7 @@ class Parser { const char *source_, *cursor_; int line_; // the current line being parsed int token_; - std::stack files_being_parsed_; + std::string files_being_parsed_; bool proto_mode_; bool strict_json_; std::string attribute_; diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 13290e74b..233eeceef 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -892,7 +892,7 @@ EnumDef &Parser::ParseEnum(bool is_union) { Expect(kTokenIdentifier); auto &enum_def = *new EnumDef(); enum_def.name = enum_name; - if (!files_being_parsed_.empty()) enum_def.file = files_being_parsed_.top(); + enum_def.file = files_being_parsed_; enum_def.doc_comment = enum_comment; enum_def.is_union = is_union; enum_def.defined_namespace = namespaces_.back(); @@ -973,7 +973,7 @@ StructDef &Parser::StartStruct(const std::string &name) { if (!struct_def.predecl) Error("datatype already exists: " + name); struct_def.predecl = false; struct_def.name = name; - if (!files_being_parsed_.empty()) struct_def.file = files_being_parsed_.top(); + struct_def.file = files_being_parsed_; // Move this struct to the back of the vector just in case it was predeclared, // to preserve declaration order. *remove(structs_.vec.begin(), structs_.vec.end(), &struct_def) = &struct_def; @@ -1352,11 +1352,11 @@ Type Parser::ParseTypeFromProtoType() { bool Parser::Parse(const char *source, const char **include_paths, const char *source_filename) { + files_being_parsed_ = source_filename ? source_filename : ""; if (source_filename && included_files_.find(source_filename) == included_files_.end()) { included_files_[source_filename] = true; files_included_per_file_[source_filename] = std::set(); - files_being_parsed_.push(source_filename); } if (!include_paths) { static const char *current_directory[] = { "", nullptr }; @@ -1499,10 +1499,8 @@ bool Parser::Parse(const char *source, const char **include_paths, error_ += NumToString(line_) + ":0"; // gcc alike #endif error_ += ": error: " + msg; - if (source_filename) files_being_parsed_.pop(); return false; } - if (source_filename) files_being_parsed_.pop(); return true; } From ec0c0b14cad95f8e0e5ee21763d0b064254e974d Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Wed, 25 Nov 2015 16:48:28 -0800 Subject: [PATCH 02/13] Made generated C++ code respect gcc -Werror=shadow. Change-Id: I17de0bcc462770124227a9967e2aab620467f6eb Tested: on Linux. --- CMakeLists.txt | 2 +- include/flatbuffers/flatbuffers.h | 4 +-- samples/monster_generated.h | 16 ++++----- src/idl_gen_cpp.cpp | 14 ++++---- tests/monster_test_generated.h | 54 +++++++++++++++---------------- 5 files changed, 45 insertions(+), 45 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1eef851b0..47a428230 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,7 +81,7 @@ if(APPLE) "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++ -Wall -pedantic -Werror -Wextra") elseif(CMAKE_COMPILER_IS_GNUCXX) set(CMAKE_CXX_FLAGS - "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -pedantic -Werror -Wextra") + "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -pedantic -Werror -Wextra -Werror=shadow") elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -stdlib=libc++ -Wall -pedantic -Werror -Wextra") diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 41b3b266c..a728a79cf 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -329,9 +329,9 @@ public: return nullptr; // Key not found. } - const uint8_t *data = reinterpret_cast(search_result); + const uint8_t *element = reinterpret_cast(search_result); - return IndirectHelper::Read(data, 0); + return IndirectHelper::Read(element, 0); } protected: diff --git a/samples/monster_generated.h b/samples/monster_generated.h index 878a7688b..854e712ee 100644 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -46,15 +46,15 @@ MANUALLY_ALIGNED_STRUCT(4) Vec3 FLATBUFFERS_FINAL_CLASS { float z_; public: - Vec3(float x, float y, float z) - : x_(flatbuffers::EndianScalar(x)), y_(flatbuffers::EndianScalar(y)), z_(flatbuffers::EndianScalar(z)) { } + Vec3(float _x, float _y, float _z) + : x_(flatbuffers::EndianScalar(_x)), y_(flatbuffers::EndianScalar(_y)), z_(flatbuffers::EndianScalar(_z)) { } float x() const { return flatbuffers::EndianScalar(x_); } - void mutate_x(float x) { flatbuffers::WriteScalar(&x_, 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); } + 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); } + void mutate_z(float _z) { flatbuffers::WriteScalar(&z_, _z); } }; STRUCT_END(Vec3, 12); @@ -62,15 +62,15 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { const Vec3 *pos() const { return GetStruct(4); } Vec3 *mutable_pos() { return GetStruct(4); } int16_t mana() const { return GetField(6, 150); } - bool mutate_mana(int16_t mana) { return SetField(6, mana); } + bool mutate_mana(int16_t _mana) { return SetField(6, _mana); } int16_t hp() const { return GetField(8, 100); } - bool mutate_hp(int16_t hp) { return SetField(8, hp); } + bool mutate_hp(int16_t _hp) { return SetField(8, _hp); } const flatbuffers::String *name() const { return GetPointer(10); } flatbuffers::String *mutable_name() { return GetPointer(10); } const flatbuffers::Vector *inventory() const { return GetPointer *>(14); } flatbuffers::Vector *mutable_inventory() { return GetPointer *>(14); } Color color() const { return static_cast(GetField(16, 2)); } - bool mutate_color(Color color) { return SetField(16, static_cast(color)); } + bool mutate_color(Color _color) { return SetField(16, static_cast(_color)); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField(verifier, 4 /* pos */) && diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 904f4706b..641103810 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -277,8 +277,8 @@ static void GenTable(const Parser &parser, StructDef &struct_def, if (is_scalar) { code += " bool mutate_" + field.name + "("; code += GenTypeBasic(parser, field.value.type, true); - code += " " + field.name + ") { return SetField(" + offsetstr + ", "; - code += GenUnderlyingCast(parser, field, false, field.name); + code += " _" + field.name + ") { return SetField(" + offsetstr + ", "; + code += GenUnderlyingCast(parser, field, false, "_" + field.name); code += "); }\n"; } else { auto type = GenTypeGet(parser, field.value.type, " ", "", " *", true); @@ -524,7 +524,7 @@ static void GenStruct(const Parser &parser, StructDef &struct_def, auto &field = **it; if (it != struct_def.fields.vec.begin()) code += ", "; code += GenTypeGet(parser, field.value.type, " ", "const ", " &", true); - code += field.name; + code += "_" + field.name; } code += ")\n : "; padding_id = 0; @@ -536,10 +536,10 @@ static void GenStruct(const Parser &parser, StructDef &struct_def, code += field.name + "_("; if (IsScalar(field.value.type.base_type)) { code += "flatbuffers::EndianScalar("; - code += GenUnderlyingCast(parser, field, false, field.name); + code += GenUnderlyingCast(parser, field, false, "_" + field.name); code += "))"; } else { - code += field.name + ")"; + code += "_" + field.name + ")"; } GenPadding(field, [&code, &padding_id](int bits) { (void)bits; @@ -579,9 +579,9 @@ static void GenStruct(const Parser &parser, StructDef &struct_def, if (is_scalar) { code += " void mutate_" + field.name + "("; code += GenTypeBasic(parser, field.value.type, true); - code += " " + field.name + ") { flatbuffers::WriteScalar(&"; + code += " _" + field.name + ") { flatbuffers::WriteScalar(&"; code += field.name + "_, "; - code += GenUnderlyingCast(parser, field, false, field.name); + code += GenUnderlyingCast(parser, field, false, "_" + field.name); code += "); }\n"; } else { code += " "; diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index ece639808..9ef76597b 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -55,13 +55,13 @@ MANUALLY_ALIGNED_STRUCT(2) Test FLATBUFFERS_FINAL_CLASS { int8_t __padding0; public: - Test(int16_t a, int8_t b) - : a_(flatbuffers::EndianScalar(a)), b_(flatbuffers::EndianScalar(b)), __padding0(0) { (void)__padding0; } + 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); } + 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); } + void mutate_b(int8_t _b) { flatbuffers::WriteScalar(&b_, _b); } }; STRUCT_END(Test, 4); @@ -78,19 +78,19 @@ MANUALLY_ALIGNED_STRUCT(16) Vec3 FLATBUFFERS_FINAL_CLASS { int16_t __padding2; public: - Vec3(float x, float y, float z, double test1, Color test2, const 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; } + Vec3(float _x, float _y, float _z, double _test1, Color _test2, const 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); } + 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); } + 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); } + 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); } + void mutate_test1(double _test1) { flatbuffers::WriteScalar(&test1_, _test1); } Color test2() const { return static_cast(flatbuffers::EndianScalar(test2_)); } - void mutate_test2(Color test2) { flatbuffers::WriteScalar(&test2_, static_cast(test2)); } + void mutate_test2(Color _test2) { flatbuffers::WriteScalar(&test2_, static_cast(_test2)); } const Test &test3() const { return test3_; } Test &mutable_test3() { return test3_; } }; @@ -98,7 +98,7 @@ STRUCT_END(Vec3, 32); struct TestSimpleTableWithEnum FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { Color color() const { return static_cast(GetField(4, 2)); } - bool mutate_color(Color color) { return SetField(4, static_cast(color)); } + bool mutate_color(Color _color) { return SetField(4, static_cast(_color)); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField(verifier, 4 /* color */) && @@ -129,9 +129,9 @@ struct Stat FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { const flatbuffers::String *id() const { return GetPointer(4); } flatbuffers::String *mutable_id() { return GetPointer(4); } int64_t val() const { return GetField(6, 0); } - bool mutate_val(int64_t val) { return SetField(6, val); } + bool mutate_val(int64_t _val) { return SetField(6, _val); } uint16_t count() const { return GetField(8, 0); } - bool mutate_count(uint16_t count) { return SetField(8, count); } + bool mutate_count(uint16_t _count) { return SetField(8, _count); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField(verifier, 4 /* id */) && @@ -172,9 +172,9 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { const Vec3 *pos() const { return GetStruct(4); } Vec3 *mutable_pos() { return GetStruct(4); } int16_t mana() const { return GetField(6, 150); } - bool mutate_mana(int16_t mana) { return SetField(6, mana); } + bool mutate_mana(int16_t _mana) { return SetField(6, _mana); } int16_t hp() const { return GetField(8, 100); } - bool mutate_hp(int16_t hp) { return SetField(8, hp); } + bool mutate_hp(int16_t _hp) { return SetField(8, _hp); } const flatbuffers::String *name() const { return GetPointer(10); } flatbuffers::String *mutable_name() { return GetPointer(10); } bool KeyCompareLessThan(const Monster *o) const { return *name() < *o->name(); } @@ -182,9 +182,9 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { const flatbuffers::Vector *inventory() const { return GetPointer *>(14); } flatbuffers::Vector *mutable_inventory() { return GetPointer *>(14); } Color color() const { return static_cast(GetField(16, 8)); } - bool mutate_color(Color color) { return SetField(16, static_cast(color)); } + bool mutate_color(Color _color) { return SetField(16, static_cast(_color)); } Any test_type() const { return static_cast(GetField(18, 0)); } - bool mutate_test_type(Any test_type) { return SetField(18, static_cast(test_type)); } + bool mutate_test_type(Any _test_type) { return SetField(18, static_cast(_test_type)); } const void *test() const { return GetPointer(20); } void *mutable_test() { return GetPointer(20); } const flatbuffers::Vector *test4() const { return GetPointer *>(22); } @@ -203,23 +203,23 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { const Stat *testempty() const { return GetPointer(32); } Stat *mutable_testempty() { return GetPointer(32); } uint8_t testbool() const { return GetField(34, 0); } - bool mutate_testbool(uint8_t testbool) { return SetField(34, testbool); } + bool mutate_testbool(uint8_t _testbool) { return SetField(34, _testbool); } int32_t testhashs32_fnv1() const { return GetField(36, 0); } - bool mutate_testhashs32_fnv1(int32_t testhashs32_fnv1) { return SetField(36, testhashs32_fnv1); } + bool mutate_testhashs32_fnv1(int32_t _testhashs32_fnv1) { return SetField(36, _testhashs32_fnv1); } uint32_t testhashu32_fnv1() const { return GetField(38, 0); } - bool mutate_testhashu32_fnv1(uint32_t testhashu32_fnv1) { return SetField(38, testhashu32_fnv1); } + bool mutate_testhashu32_fnv1(uint32_t _testhashu32_fnv1) { return SetField(38, _testhashu32_fnv1); } int64_t testhashs64_fnv1() const { return GetField(40, 0); } - bool mutate_testhashs64_fnv1(int64_t testhashs64_fnv1) { return SetField(40, testhashs64_fnv1); } + bool mutate_testhashs64_fnv1(int64_t _testhashs64_fnv1) { return SetField(40, _testhashs64_fnv1); } uint64_t testhashu64_fnv1() const { return GetField(42, 0); } - bool mutate_testhashu64_fnv1(uint64_t testhashu64_fnv1) { return SetField(42, testhashu64_fnv1); } + bool mutate_testhashu64_fnv1(uint64_t _testhashu64_fnv1) { return SetField(42, _testhashu64_fnv1); } int32_t testhashs32_fnv1a() const { return GetField(44, 0); } - bool mutate_testhashs32_fnv1a(int32_t testhashs32_fnv1a) { return SetField(44, testhashs32_fnv1a); } + bool mutate_testhashs32_fnv1a(int32_t _testhashs32_fnv1a) { return SetField(44, _testhashs32_fnv1a); } uint32_t testhashu32_fnv1a() const { return GetField(46, 0); } - bool mutate_testhashu32_fnv1a(uint32_t testhashu32_fnv1a) { return SetField(46, testhashu32_fnv1a); } + bool mutate_testhashu32_fnv1a(uint32_t _testhashu32_fnv1a) { return SetField(46, _testhashu32_fnv1a); } int64_t testhashs64_fnv1a() const { return GetField(48, 0); } - bool mutate_testhashs64_fnv1a(int64_t testhashs64_fnv1a) { return SetField(48, testhashs64_fnv1a); } + bool mutate_testhashs64_fnv1a(int64_t _testhashs64_fnv1a) { return SetField(48, _testhashs64_fnv1a); } uint64_t testhashu64_fnv1a() const { return GetField(50, 0); } - bool mutate_testhashu64_fnv1a(uint64_t testhashu64_fnv1a) { return SetField(50, testhashu64_fnv1a); } + bool mutate_testhashu64_fnv1a(uint64_t _testhashu64_fnv1a) { return SetField(50, _testhashu64_fnv1a); } const flatbuffers::Vector *testarrayofbools() const { return GetPointer *>(52); } flatbuffers::Vector *mutable_testarrayofbools() { return GetPointer *>(52); } bool Verify(flatbuffers::Verifier &verifier) const { From d10f9a6abcf85d1ce13c4705b311292446b67ec1 Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Wed, 25 Nov 2015 17:26:34 -0800 Subject: [PATCH 03/13] Added long form options for all generators (--cpp etc.) Short-form versions are still supported, but are deprecated. Change-Id: I15f70ca8d69ba5790368205caa9603b65e1b7fff Tested: on Linux. --- docs/html/md__compiler.html | 27 +++++++++++++++++-------- docs/source/Compiler.md | 34 +++++++++++++++++++++++--------- src/flatc.cpp | 34 +++++++++++++++++++------------- tests/generate_code.sh | 4 ++-- tests/monster_test.bfbs | Bin 2784 -> 2744 bytes tests/monster_test_generated.js | 2 ++ 6 files changed, 68 insertions(+), 33 deletions(-) diff --git a/docs/html/md__compiler.html b/docs/html/md__compiler.html index 2fe560da6..ff3f7fe22 100644 --- a/docs/html/md__compiler.html +++ b/docs/html/md__compiler.html @@ -61,18 +61,28 @@ $(document).ready(function(){initNavTree('md__compiler.html','');});
Using the schema compiler
-

Usage:

flatc [ -c ] [ -j ] [ -b ] [ -t ] [ -o PATH ] [ -I PATH ] [ -S ] FILES...
+

Usage:

flatc [ GENERATOR OPTIONS ] [ -o PATH ] [ -I PATH ] [ -S ] FILES...
       [ -- FILES...]
 

The files are read and parsed in order, and can contain either schemas or data (see below). Later files can make use of definitions in earlier files.

-- indicates that the following files are binary files in FlatBuffer format conforming to the schema(s) indicated before it. Incompatible binary files currently will give unpredictable results (!)

Depending on the flags passed, additional files may be generated for each file processed:

+

For any schema input files, one or more generators can be specified:

+
    +
  • --cpp, -c : Generate a C++ header for all definitions in this file (as filename_generated.h).
  • +
  • --java, -j : Generate Java code.
  • +
  • --csharp, -n : Generate C# code.
  • +
  • --go, -g : Generate Go code.
  • +
  • --python, -p: Generate Python code.
  • +
  • --javascript, -s: Generate JavaScript code.
  • +
  • --php: Generate PHP code.
  • +
+

For any data input files:

+
    +
  • --binary, -b : If data is contained in this file, generate a filename.bin containing the binary flatbuffer (or a different extension if one is specified in the schema).
  • +
  • --json, -t : If data is contained in this file, generate a filename.json representing the data in the flatbuffer.
  • +
+

Additional options:

    -
  • -c : Generate a C++ header for all definitions in this file (as filename_generated.h). Skipped for data.
  • -
  • -j : Generate Java classes. Skipped for data.
  • -
  • -n : Generate C# classes. Skipped for data.
  • -
  • -g : Generate Go classes. Skipped for data.
  • -
  • -b : If data is contained in this file, generate a filename.bin containing the binary flatbuffer.
  • -
  • -t : If data is contained in this file, generate a filename.json representing the data in the flatbuffer.
  • -o PATH : Output all generated files to PATH (either absolute, or relative to the current directory). If omitted, PATH will be the current directory. PATH should end in your systems path separator, e.g. / or \.
  • -I PATH : when encountering include statements, attempt to load the files from this path. Paths will be tried in the order given, and if all fail (or none are specified) it will try to load relative to the path of the schema file being parsed.
  • -M : Print make rules for generated files.
  • @@ -86,8 +96,9 @@ $(document).ready(function(){initNavTree('md__compiler.html','');});
  • --gen-onefile : Generate single output file (useful for C#)
  • --raw-binary : Allow binaries without a file_indentifier to be read. This may crash flatc given a mismatched schema.
  • --proto: Expect input files to be .proto files (protocol buffers). Output the corresponding .fbs file. Currently supports: package, message, enum, nested declarations, import (use -I for paths), extend, oneof, group. Does not support, but will skip without error: option, service, extensions, and most everything else.
  • -
  • --schema: Serialize schemas instead of JSON (use with -b). This will output a binary version of the specified schema that itself corresponds to the reflection/reflection.fbs schema. Loading this binary file is the basis for reflection functionality.
  • +
  • --schema: Serialize schemas instead of JSON (use with -b). This will output a binary version of the specified schema that itself corresponds to the reflection/reflection.fbs schema. Loading this binary file is the basis for reflection functionality.
+

NOTE: short-form options for generators are deprecated, use the long form whenever possible.

diff --git a/docs/source/Compiler.md b/docs/source/Compiler.md index 4e6b7994b..27ddf52e4 100755 --- a/docs/source/Compiler.md +++ b/docs/source/Compiler.md @@ -2,7 +2,7 @@ Usage: - flatc [ -c ] [ -j ] [ -b ] [ -t ] [ -o PATH ] [ -I PATH ] [ -S ] FILES... + flatc [ GENERATOR OPTIONS ] [ -o PATH ] [ -I PATH ] [ -S ] FILES... [ -- FILES...] The files are read and parsed in order, and can contain either schemas @@ -16,21 +16,34 @@ Incompatible binary files currently will give unpredictable results (!) Depending on the flags passed, additional files may be generated for each file processed: -- `-c` : Generate a C++ header for all definitions in this file (as - `filename_generated.h`). Skipped for data. +For any schema input files, one or more generators can be specified: -- `-j` : Generate Java classes. Skipped for data. +- `--cpp`, `-c` : Generate a C++ header for all definitions in this file (as + `filename_generated.h`). -- `-n` : Generate C# classes. Skipped for data. +- `--java`, `-j` : Generate Java code. -- `-g` : Generate Go classes. Skipped for data. +- `--csharp`, `-n` : Generate C# code. -- `-b` : If data is contained in this file, generate a - `filename.bin` containing the binary flatbuffer. +- `--go`, `-g` : Generate Go code. -- `-t` : If data is contained in this file, generate a +- `--python`, `-p`: Generate Python code. + +- `--javascript`, `-s`: Generate JavaScript code. + +- `--php`: Generate PHP code. + +For any data input files: + +- `--binary`, `-b` : If data is contained in this file, generate a + `filename.bin` containing the binary flatbuffer (or a different extension + if one is specified in the schema). + +- `--json`, `-t` : If data is contained in this file, generate a `filename.json` representing the data in the flatbuffer. +Additional options: + - `-o PATH` : Output all generated files to PATH (either absolute, or relative to the current directory). If omitted, PATH will be the current directory. PATH should end in your systems path separator, @@ -82,3 +95,6 @@ be generated for each file processed: output a binary version of the specified schema that itself corresponds to the reflection/reflection.fbs schema. Loading this binary file is the basis for reflection functionality. + +NOTE: short-form options for generators are deprecated, use the long form +whenever possible. diff --git a/src/flatc.cpp b/src/flatc.cpp index 337ce5d8e..2479da12b 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -28,7 +28,8 @@ struct Generator { const std::string &path, const std::string &file_name, const flatbuffers::GeneratorOptions &opts); - const char *generator_opt; + const char *generator_opt_short; + const char *generator_opt_long; const char *lang_name; flatbuffers::GeneratorOptions::Language lang; const char *generator_help; @@ -40,41 +41,41 @@ struct Generator { }; const Generator generators[] = { - { flatbuffers::GenerateBinary, "-b", "binary", + { flatbuffers::GenerateBinary, "-b", "--binary", "binary", flatbuffers::GeneratorOptions::kMAX, "Generate wire format binaries for any data definitions", flatbuffers::BinaryMakeRule }, - { flatbuffers::GenerateTextFile, "-t", "text", + { flatbuffers::GenerateTextFile, "-t", "--json", "text", flatbuffers::GeneratorOptions::kMAX, "Generate text output for any data definitions", flatbuffers::TextMakeRule }, - { flatbuffers::GenerateCPP, "-c", "C++", + { flatbuffers::GenerateCPP, "-c", "--cpp", "C++", flatbuffers::GeneratorOptions::kMAX, "Generate C++ headers for tables/structs", flatbuffers::CPPMakeRule }, - { flatbuffers::GenerateGo, "-g", "Go", + { flatbuffers::GenerateGo, "-g", "--go", "Go", flatbuffers::GeneratorOptions::kGo, "Generate Go files for tables/structs", flatbuffers::GeneralMakeRule }, - { flatbuffers::GenerateGeneral, "-j", "Java", + { flatbuffers::GenerateGeneral, "-j", "--java", "Java", flatbuffers::GeneratorOptions::kJava, "Generate Java classes for tables/structs", flatbuffers::GeneralMakeRule }, - { flatbuffers::GenerateJS, "-s", "JavaScript", + { flatbuffers::GenerateJS, "-s", "--js", "JavaScript", flatbuffers::GeneratorOptions::kMAX, "Generate JavaScript code for tables/structs", flatbuffers::JSMakeRule }, - { flatbuffers::GenerateGeneral, "-n", "C#", + { flatbuffers::GenerateGeneral, "-n", "--csharp", "C#", flatbuffers::GeneratorOptions::kCSharp, "Generate C# classes for tables/structs", flatbuffers::GeneralMakeRule }, - { flatbuffers::GeneratePython, "-p", "Python", + { flatbuffers::GeneratePython, "-p", "--python", "Python", flatbuffers::GeneratorOptions::kMAX, "Generate Python files for tables/structs", flatbuffers::GeneralMakeRule }, - { flatbuffers::GeneratePhp, "--php", "PHP", + { flatbuffers::GeneratePhp, nullptr, "--php", "PHP", flatbuffers::GeneratorOptions::kMAX, - "Generate Php files for tables/structs", + "Generate PHP files for tables/structs", flatbuffers::GeneralMakeRule }, }; @@ -86,8 +87,11 @@ static void Error(const std::string &err, bool usage, bool show_exe_name) { if (usage) { printf("usage: %s [OPTION]... FILE... [-- FILE...]\n", program_name); for (size_t i = 0; i < sizeof(generators) / sizeof(generators[0]); ++i) - printf(" %s %s.\n", - generators[i].generator_opt, + printf(" %-12s %s %s.\n", + generators[i].generator_opt_long, + generators[i].generator_opt_short + ? generators[i].generator_opt_short + : " ", generators[i].generator_help); printf( " -o PATH Prefix PATH to all generated files.\n" @@ -179,7 +183,9 @@ int main(int argc, const char *argv[]) { print_make_rules = true; } else { for (size_t i = 0; i < num_generators; ++i) { - if (arg == generators[i].generator_opt) { + if (arg == generators[i].generator_opt_long || + (generators[i].generator_opt_short && + arg == generators[i].generator_opt_short)) { generator_enabled[i] = true; any_generator = true; goto found; diff --git a/tests/generate_code.sh b/tests/generate_code.sh index 91158bff4..91661f30d 100644 --- a/tests/generate_code.sh +++ b/tests/generate_code.sh @@ -1,2 +1,2 @@ -../flatc -c -j -n -g -b -p -s --php --gen-mutable --no-includes monster_test.fbs monsterdata_test.json -../flatc -b --schema monster_test.fbs +../flatc --cpp --java --csharp --go --binary --python --js --php --gen-mutable --no-includes monster_test.fbs monsterdata_test.json +../flatc --binary --schema monster_test.fbs diff --git a/tests/monster_test.bfbs b/tests/monster_test.bfbs index 8d54ead7e9a3a5047063843fdf0f38af5266385b..e5f51face696a1ee9b3aa64b70bba83df1c4645a 100644 GIT binary patch literal 2744 zcmaJ@O^91n6h4{!|4fqBF&(9pAVVodhITp(B1P&*>kl>sCr-hIoi~#g6G`3+$xCQl zhzl39Dy{}sBBc~@DXzK@aUs%05f??Il;TRda5-qLjo)|fyP1auFPxm0d%tt;ch0%z zzIRka)-SAYKw%VRQ4*4of|O+e{{+7x&qH2-oP!kRL{iWR=-1LBUt~qfkT-K8_wYZC zHR2yYc4NRnU%XUP|6J?(9=tE;9%<}Sz#K-5x(qoAA?{^wtoma-Z$UW!aDOs^f>(~5 zaxK6UwpO0I84=K$jw5)gyMth(-R*VUy0hJJFSmo%d2iUoN)9#&>~HM3_bGS|oeM&d zi@rAq+`i-{lL_OX)GpP|L&+BA$eH=6LXwbI{Epv;CjS=XEQEe<{h_?$^!+RBI0vfJ zLBH#rp=&vDjWH1yjfZ=@+IL+Kt7WZ|!I6jM&As1p8(KkD5*5=N6SD|m9`le3eZTv< zx6>ImT(#x=8-{1r?3sf#nVCKKL{6d2rqdDG2j1p@`$u#34E}!&GM~cP5{!2tKjZ(m z&~?afkaPH7fNVi{zJ37q6UaR}gT4X%0FP&(uR=GVKZkw{AwC1lO>kJ0RgIJ}FlPa3 z?)3Z4*l%w8erJFw@&0BFigR~?KO!qr{Gkxvat5v8+Uk|2H#+TL?#Z88vs?nHTX;u_yu$fD@FcMK7$DUn!%^{fPYF(PVqM0D1W$- zZwx;7J@AL+#7w;9PyFv8|9#*O$?=(Z%b)mTe`&m#VJflpqZRa~?_;rR-9sqlbZH-1aB zc@Z|eo#=ZBVrv@O41#{!dk@`|!A;7ZVPApMd>?jMsi+-$m7EyIDi8(5n!5_v5{|xH z2R?&nZ478B#Y4%mo_7sRZUdK=!-`{WY%Z?^?*lN8K|W4!cj2l?!T zcic5&qj%a#6WAgEqrWO^D zeUcSD9R+Y_KI|aR7-npK_Ur;Q^WSwmULW!j@(7jm^jz4R*t{Eh{y^@S^%C_L0?wo;Ae@Nf-z4s%YZ$LhR&^{UV5pBtvXPbt< zm_77NkAO>ry%X($73~3@6?<+(&)!Yo;~LRHAJgw?Aipzy3%7x>Ib+;B=pR7%E{4iK z#Ci;oFkh@I*LiL;+MTvPC4V0{n^R)0Lg-uaMKCzX9lx8n&k}^V|N9Q%I(vp4z#GHV zg&8u#)`c@w2dxUDHnh literal 2784 zcmaKuOK6-`6vxjblTI?3Owt&qQG$e0N{~=&T8gWpv8^%CRQiaULY*Yvn8?gG!#slN zLPUxyS6y^bBr6dYr4$!I#8nn9y68$=Ns%n13m0A0cgNrV-nm~t>*K?678Wi=J^)uJ%F-Wts=a!tTc*mg!(8n3G zeazo8pURot8bTg=GY^k_0d2C8!%6`hBQwK~Y)I!9kGfs)AZP0X=F8AzZ~tn%#VYT7 zEuaatPWBEC;$AWiu(}wv+7~uzjcRnUyi$$c+Gwwzjyp9@^2lVd@8!qf%wo|QWsRG5 zwjQ_IQPYauZdZPZmd-7mX0kPSX`d@UJD>X{uUNbdDIU^00KHbP)|MiMzkAbv;50gY$alnMdf9P(- zKJDA&z1(b;x9XLZdcE3$Ww*f}u(7tRtjJIwcCBxjwJYRJbo~Z?;M*;^q&MjVY3PTk;muK_*M!ZgMY?eOz?-<<7PhG zH|;-s+va@SW)l3e#}B3P!GGcJalYRUC-{#%elU#>{tN$@^D(-E&K3V1kI$v?!GGb8 z{MPM`+Q9_>qsRBDhLkTQO1NckMVn4)c9%WuQOdw*BMuwY8f|rf4>=pL7ZRP#$F2sl zs(0y(p=hmAEw@)Xl?soUv4c#(rV{-JfA{)zrneTwQBAt8Q0eo zn^o7xN@!IF&$>SEfc%u80sb|l!#U&ni0>w z>dqmKY5YO~uKggf=@*}QKlxtkMM3%xGytl!2AAJ4S zC=P34GOm010p()7Ax?>V{=0kzFG37%2-+Lp^>oOGS~*sWv3Fg4WRo1-{?pf@VojU0 zM!jVxsA=g7y;gkrCpT0N(s!X@E3SJr~uXI^eZG1(wci|*}NW|qBVg`0+?Lv|M+-Qx%7bLbi*-6Rah_=W$7ms{ZHTZ^Q4$i{S6F#}yO{VU9BI!b`{Ns>ckg z*Ib39JCnNe&D6Q-S8pJ^72s|tPQm8g6#hNl#*=?$UIRGYA>>sy^}Y4~-z_B~6mlag zeuUi2uvKj+Cw{1(=v{?=Vo99*Th=$9^yzENfAay(C{N0v^eD!fFGGiV`-`r}ggW_W zAIKmPVsOvZxVO7P%!>aJGuiYv()Wk&1-=K`fMQcd^2wK@k00!eWI_yCx_Y}5cUpW3 F*)yDt; diff --git a/tests/monster_test_generated.js b/tests/monster_test_generated.js index d46a9c684..9d0f48b09 100644 --- a/tests/monster_test_generated.js +++ b/tests/monster_test_generated.js @@ -352,6 +352,8 @@ MyGame.Example.Stat.endStat = function(builder) { }; /** + * an example documentation comment: monster object + * * @constructor */ MyGame.Example.Monster = function() { From 9202f0cf09b80e356a6b468e280b2fb0f10d2871 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 27 Nov 2015 20:05:48 +0000 Subject: [PATCH 04/13] Return the full string when requested from a flatbuffers::String, even if it contains a NULL byte. --- include/flatbuffers/flatbuffers.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index a728a79cf..c2b938d36 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -379,7 +379,7 @@ template static inline size_t VectorLength(const Vector *v) { struct String : public Vector { const char *c_str() const { return reinterpret_cast(Data()); } - std::string str() const { return c_str(); } + std::string str() const { return std::string(c_str(), Length()); } bool operator <(const String &o) const { return strcmp(c_str(), o.c_str()) < 0; From 345e8ca804265640f12e4edd93fb9d752aed9cf8 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 27 Nov 2015 20:06:41 +0000 Subject: [PATCH 05/13] Fix use of std::max when Windows.h is imported. --- include/flatbuffers/flatbuffers.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index c2b938d36..2af4486c7 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -445,7 +445,7 @@ class vector_downward { if (len > static_cast(cur_ - buf_)) { auto old_size = size(); auto largest_align = AlignOf(); - reserved_ += std::max(len, growth_policy(reserved_)); + reserved_ += (std::max)(len, growth_policy(reserved_)); // Round up to avoid undefined behavior from unaligned loads and stores. reserved_ = (reserved_ + (largest_align - 1)) & ~(largest_align - 1); auto new_buf = allocator_.allocate(reserved_); From c5835b896cfc6202b327a88d2bd8517c5ae94f51 Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Mon, 30 Nov 2015 11:25:21 -0800 Subject: [PATCH 06/13] Java builder now checks if buffer was finished upon access. Also checks for nesting of objects in vector construction. This avoids common errors in FlatBuffer construction. Change-Id: I5507c5d767684e20e94883a92448f05acefba4d6 Tested: on Linux. --- .../google/flatbuffers/FlatBufferBuilder.java | 46 ++++++++++++++----- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/java/com/google/flatbuffers/FlatBufferBuilder.java b/java/com/google/flatbuffers/FlatBufferBuilder.java index 368be2f30..a6234ca63 100644 --- a/java/com/google/flatbuffers/FlatBufferBuilder.java +++ b/java/com/google/flatbuffers/FlatBufferBuilder.java @@ -28,18 +28,19 @@ import java.nio.charset.Charset; * main FlatBuffers documentation. */ public class FlatBufferBuilder { - ByteBuffer bb; // Where we construct the FlatBuffer. - int space; // Remaining space in the ByteBuffer. + ByteBuffer bb; // Where we construct the FlatBuffer. + int space; // Remaining space in the ByteBuffer. static final Charset utf8charset = Charset.forName("UTF-8"); - int minalign = 1; // Minimum alignment encountered so far. - int[] vtable = null; // The vtable for the current table. - int vtable_in_use = 0; // The amount of fields we're actually using. - boolean nested = false; // Whether we are currently serializing a table. - int object_start; // Starting offset of the current struct/table. - int[] vtables = new int[16]; // List of offsets of all vtables. - int num_vtables = 0; // Number of entries in `vtables` in use. - int vector_num_elems = 0; // For the current vector being built. - boolean force_defaults = false; // False omits default values from the serialized data + int minalign = 1; // Minimum alignment encountered so far. + int[] vtable = null; // The vtable for the current table. + int vtable_in_use = 0; // The amount of fields we're actually using. + boolean nested = false; // Whether we are currently serializing a table. + boolean finished = false; // Whether the buffer is finished. + int object_start; // Starting offset of the current struct/table. + int[] vtables = new int[16]; // List of offsets of all vtables. + int num_vtables = 0; // Number of entries in `vtables` in use. + int vector_num_elems = 0; // For the current vector being built. + boolean force_defaults = false; // False omits default values from the serialized data /** * Start with a buffer of size {@code initial_size}, then grow as required. @@ -86,6 +87,7 @@ public class FlatBufferBuilder { space = bb.capacity(); vtable_in_use = 0; nested = false; + finished = false; object_start = 0; num_vtables = 0; vector_num_elems = 0; @@ -240,6 +242,7 @@ public class FlatBufferBuilder { vector_num_elems = num_elems; prep(SIZEOF_INT, elem_size * num_elems); prep(alignment, elem_size * num_elems); // Just in case alignment > int. + nested = true; } /** @@ -250,6 +253,9 @@ public class FlatBufferBuilder { * @see #startVector(int, int, int) */ public int endVector() { + if (!nested) + throw new AssertionError("FlatBuffers: endVector called without startVector"); + nested = false; putInt(vector_num_elems); return offset(); } @@ -284,6 +290,16 @@ public class FlatBufferBuilder { return endVector(); } + /** + * Should not be accessing the final buffer before it is finished. + */ + public void finished() { + if (!finished) + throw new AssertionError( + "FlatBuffers: you can only access the serialized buffer after it has been" + + " finished by FlatBufferBuilder.finish()."); + } + /** * Should not be creating any other object, string or vector * while an object is being constructed @@ -452,6 +468,7 @@ public class FlatBufferBuilder { prep(minalign, SIZEOF_INT); addOffset(root_table); bb.position(space); + finished = true; } public void finish(int root_table, String file_identifier) { @@ -481,7 +498,10 @@ public class FlatBufferBuilder { // Get the ByteBuffer representing the FlatBuffer. Only call this after you've // called finish(). The actual data starts at the ByteBuffer's current position, // not necessarily at 0. - public ByteBuffer dataBuffer() { return bb; } + public ByteBuffer dataBuffer() { + finished(); + return bb; + } /** * The FlatBuffer data doesn't start at offset 0 in the {@link ByteBuffer}, but @@ -493,6 +513,7 @@ public class FlatBufferBuilder { */ @Deprecated private int dataStart() { + finished(); return space; } @@ -506,6 +527,7 @@ public class FlatBufferBuilder { * @throws IndexOutOfBoundsException If the range of bytes is ouf of bound */ public byte[] sizedByteArray(int start, int length){ + finished(); byte[] array = new byte[length]; bb.position(start); bb.get(array); From 07da3fc216c62b18eb13a8bcb9afa95d7c325418 Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Mon, 30 Nov 2015 11:50:09 -0800 Subject: [PATCH 07/13] Allow vectors to be aligned beyond their natural type. Change-Id: I09ade6b688a6b97d65fd832558917225d86c9118 --- include/flatbuffers/flatbuffers.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index a728a79cf..62c3d1f8f 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -799,6 +799,15 @@ class FlatBufferBuilder FLATBUFFERS_FINAL_CLASS { PreAlign(len * elemsize, elemsize); // Just in case elemsize > uoffset_t. } + // Call this right before StartVector/CreateVector if you want to force the + // alignment to be something different than what the element size would + // normally dictate. + // This is useful when storing a nested_flatbuffer in a vector of bytes, + // or when storing SIMD floats, etc. + void ForceVectorAlignment(size_t len, size_t elemsize, size_t alignment) { + PreAlign(len * elemsize, alignment); + } + uint8_t *ReserveElements(size_t len, size_t elemsize) { return buf_.make_space(len * elemsize); } From 59dc29a19a4ade02440c5cd336aa92aaa0824c5b Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Mon, 30 Nov 2015 13:54:24 -0800 Subject: [PATCH 08/13] Made C++ generated code use "bool" instead of uint8_t wire type. Change-Id: I5756d15a758429ca67456264842017063d1f755e Tested: on Linux. --- src/idl_gen_cpp.cpp | 34 +++++++++++++++++++++------------- tests/monster_test_generated.h | 8 ++++---- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 641103810..7c39c325a 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -59,20 +59,22 @@ static std::string TranslateNameSpace(const std::string &qualified_name) { // Return a C++ type from the table in idl.h static std::string GenTypeBasic(const Parser &parser, const Type &type, - bool real_enum) { + bool user_facing_type) { static const char *ctypename[] = { #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \ #CTYPE, FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD) #undef FLATBUFFERS_TD }; - return real_enum && type.enum_def - ? WrapInNameSpace(parser, *type.enum_def) - : ctypename[type.base_type]; + if (user_facing_type) { + if (type.enum_def) return WrapInNameSpace(parser, *type.enum_def); + if (type.base_type == BASE_TYPE_BOOL) return "bool"; + } + return ctypename[type.base_type]; } static std::string GenTypeWire(const Parser &parser, const Type &type, - const char *postfix, bool real_enum); + const char *postfix, bool user_facing_type); // Return a C++ pointer type, specialized to the actual struct/table types, // and vector element types. @@ -96,9 +98,9 @@ static std::string GenTypePointer(const Parser &parser, const Type &type) { // Return a C++ type for any type (scalar/pointer) specifically for // building a flatbuffer. static std::string GenTypeWire(const Parser &parser, const Type &type, - const char *postfix, bool real_enum) { + const char *postfix, bool user_facing_type) { return IsScalar(type.base_type) - ? GenTypeBasic(parser, type, real_enum) + postfix + ? GenTypeBasic(parser, type, user_facing_type) + postfix : IsStruct(type) ? "const " + GenTypePointer(parser, type) + " *" : "flatbuffers::Offset<" + GenTypePointer(parser, type) + ">" + postfix; @@ -118,9 +120,9 @@ static std::string GenTypeSize(const Parser &parser, const Type &type) { // using a flatbuffer. static std::string GenTypeGet(const Parser &parser, const Type &type, const char *afterbasic, const char *beforeptr, - const char *afterptr, bool real_enum) { + const char *afterptr, bool user_facing_type) { return IsScalar(type.base_type) - ? GenTypeBasic(parser, type, real_enum) + afterbasic + ? GenTypeBasic(parser, type, user_facing_type) + afterbasic : beforeptr + GenTypePointer(parser, type) + afterptr; } @@ -190,9 +192,12 @@ static void GenEnum(const Parser &parser, EnumDef &enum_def, } code += "nullptr };\n return names;\n}\n\n"; code += "inline const char *EnumName" + enum_def.name; - code += "(" + enum_def.name + " e) { return EnumNames" + enum_def.name + "()[static_cast(e)"; - if (enum_def.vals.vec.front()->value) - code += " - static_cast(" + GetEnumVal(enum_def, *enum_def.vals.vec.front(), opts) +")"; + code += "(" + enum_def.name + " e) { return EnumNames" + enum_def.name; + code += "()[static_cast(e)"; + if (enum_def.vals.vec.front()->value) { + code += " - static_cast("; + code += GetEnumVal(enum_def, *enum_def.vals.vec.front(), opts) +")"; + } code += "]; }\n\n"; } @@ -230,7 +235,8 @@ static void GenEnum(const Parser &parser, EnumDef &enum_def, // underlying type to the interface type. std::string GenUnderlyingCast(const Parser &parser, const FieldDef &field, bool from, const std::string &val) { - return field.value.type.enum_def && IsScalar(field.value.type.base_type) + return (field.value.type.enum_def && IsScalar(field.value.type.base_type)) || + field.value.type.base_type == BASE_TYPE_BOOL ? "static_cast<" + GenTypeBasic(parser, field.value.type, from) + ">(" + val + ")" : val; @@ -456,6 +462,8 @@ static void GenTable(const Parser &parser, StructDef &struct_def, } else { code += GenUnderlyingCast(parser, field, true, field.value.constant); } + } else if (field.value.type.base_type == BASE_TYPE_BOOL) { + code += field.value.constant == "0" ? "false" : "true"; } else { code += field.value.constant; } diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 9ef76597b..8a78de530 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -202,8 +202,8 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { const MyGame::Example::Monster *testnestedflatbuffer_nested_root() const { return flatbuffers::GetRoot(testnestedflatbuffer()->Data()); } const Stat *testempty() const { return GetPointer(32); } Stat *mutable_testempty() { return GetPointer(32); } - uint8_t testbool() const { return GetField(34, 0); } - bool mutate_testbool(uint8_t _testbool) { return SetField(34, _testbool); } + bool testbool() const { return static_cast(GetField(34, 0)); } + bool mutate_testbool(bool _testbool) { return SetField(34, static_cast(_testbool)); } int32_t testhashs32_fnv1() const { return GetField(36, 0); } bool mutate_testhashs32_fnv1(int32_t _testhashs32_fnv1) { return SetField(36, _testhashs32_fnv1); } uint32_t testhashu32_fnv1() const { return GetField(38, 0); } @@ -281,7 +281,7 @@ struct MonsterBuilder { void add_enemy(flatbuffers::Offset enemy) { fbb_.AddOffset(28, enemy); } void add_testnestedflatbuffer(flatbuffers::Offset> testnestedflatbuffer) { fbb_.AddOffset(30, testnestedflatbuffer); } void add_testempty(flatbuffers::Offset testempty) { fbb_.AddOffset(32, testempty); } - void add_testbool(uint8_t testbool) { fbb_.AddElement(34, testbool, 0); } + void add_testbool(bool testbool) { fbb_.AddElement(34, static_cast(testbool), 0); } void add_testhashs32_fnv1(int32_t testhashs32_fnv1) { fbb_.AddElement(36, testhashs32_fnv1, 0); } void add_testhashu32_fnv1(uint32_t testhashu32_fnv1) { fbb_.AddElement(38, testhashu32_fnv1, 0); } void add_testhashs64_fnv1(int64_t testhashs64_fnv1) { fbb_.AddElement(40, testhashs64_fnv1, 0); } @@ -315,7 +315,7 @@ inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder flatbuffers::Offset enemy = 0, flatbuffers::Offset> testnestedflatbuffer = 0, flatbuffers::Offset testempty = 0, - uint8_t testbool = 0, + bool testbool = false, int32_t testhashs32_fnv1 = 0, uint32_t testhashu32_fnv1 = 0, int64_t testhashs64_fnv1 = 0, From 1fa803d187c244e8de0bb40f0d3c91f22408bde0 Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Mon, 30 Nov 2015 14:27:47 -0800 Subject: [PATCH 09/13] C++ generated code now has constants for vtable offsets. Besides making the generated code looking a lot more readable, it also allows you to use these offsets in calls to Table::CheckField, to see if a field is present in a table. Change-Id: I1b4cc350c4f27c4e474c31add40c701ef4ae63b2 Tested: On Linux. --- samples/monster_generated.h | 56 ++++--- src/idl_gen_cpp.cpp | 29 +++- tests/monster_test_generated.h | 260 +++++++++++++++++++-------------- 3 files changed, 203 insertions(+), 142 deletions(-) diff --git a/samples/monster_generated.h b/samples/monster_generated.h index 854e712ee..53935ee93 100644 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -59,28 +59,36 @@ MANUALLY_ALIGNED_STRUCT(4) Vec3 FLATBUFFERS_FINAL_CLASS { STRUCT_END(Vec3, 12); struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - const Vec3 *pos() const { return GetStruct(4); } - Vec3 *mutable_pos() { return GetStruct(4); } - int16_t mana() const { return GetField(6, 150); } - bool mutate_mana(int16_t _mana) { return SetField(6, _mana); } - int16_t hp() const { return GetField(8, 100); } - bool mutate_hp(int16_t _hp) { return SetField(8, _hp); } - const flatbuffers::String *name() const { return GetPointer(10); } - flatbuffers::String *mutable_name() { return GetPointer(10); } - const flatbuffers::Vector *inventory() const { return GetPointer *>(14); } - flatbuffers::Vector *mutable_inventory() { return GetPointer *>(14); } - Color color() const { return static_cast(GetField(16, 2)); } - bool mutate_color(Color _color) { return SetField(16, static_cast(_color)); } + enum { + VT_POS = 4, + VT_MANA = 6, + VT_HP = 8, + VT_NAME = 10, + VT_INVENTORY = 14, + VT_COLOR = 16, + }; + const Vec3 *pos() const { return GetStruct(VT_POS); } + 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); } + int16_t hp() const { return GetField(VT_HP, 100); } + bool mutate_hp(int16_t _hp) { return SetField(VT_HP, _hp); } + const flatbuffers::String *name() const { return GetPointer(VT_NAME); } + flatbuffers::String *mutable_name() { return GetPointer(VT_NAME); } + const flatbuffers::Vector *inventory() const { return GetPointer *>(VT_INVENTORY); } + flatbuffers::Vector *mutable_inventory() { return GetPointer *>(VT_INVENTORY); } + Color color() const { return static_cast(GetField(VT_COLOR, 2)); } + bool mutate_color(Color _color) { return SetField(VT_COLOR, static_cast(_color)); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && - VerifyField(verifier, 4 /* pos */) && - VerifyField(verifier, 6 /* mana */) && - VerifyField(verifier, 8 /* hp */) && - VerifyField(verifier, 10 /* name */) && + VerifyField(verifier, VT_POS) && + VerifyField(verifier, VT_MANA) && + VerifyField(verifier, VT_HP) && + VerifyField(verifier, VT_NAME) && verifier.Verify(name()) && - VerifyField(verifier, 14 /* inventory */) && + VerifyField(verifier, VT_INVENTORY) && verifier.Verify(inventory()) && - VerifyField(verifier, 16 /* color */) && + VerifyField(verifier, VT_COLOR) && verifier.EndTable(); } }; @@ -88,12 +96,12 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { struct MonsterBuilder { flatbuffers::FlatBufferBuilder &fbb_; flatbuffers::uoffset_t start_; - void add_pos(const Vec3 *pos) { fbb_.AddStruct(4, pos); } - void add_mana(int16_t mana) { fbb_.AddElement(6, mana, 150); } - void add_hp(int16_t hp) { fbb_.AddElement(8, hp, 100); } - void add_name(flatbuffers::Offset name) { fbb_.AddOffset(10, name); } - void add_inventory(flatbuffers::Offset> inventory) { fbb_.AddOffset(14, inventory); } - void add_color(Color color) { fbb_.AddElement(16, static_cast(color), 2); } + void add_pos(const 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(Color color) { fbb_.AddElement(Monster::VT_COLOR, static_cast(color), 2); } MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } MonsterBuilder &operator=(const MonsterBuilder &); flatbuffers::Offset Finish() { diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 7c39c325a..ca523dcda 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -242,6 +242,12 @@ std::string GenUnderlyingCast(const Parser &parser, const FieldDef &field, : val; } +std::string GenFieldOffsetName(const FieldDef &field) { + std::string uname = field.name; + std::transform(uname.begin(), uname.end(), uname.begin(), ::toupper); + return "VT_" + uname; +} + // Generate an accessor struct, builder structs & function for a table. static void GenTable(const Parser &parser, StructDef &struct_def, const GeneratorOptions &opts, std::string *code_ptr) { @@ -254,6 +260,19 @@ static void GenTable(const Parser &parser, StructDef &struct_def, code += "struct " + struct_def.name; code += " FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table"; code += " {\n"; + // Generate field id constants. + code += " enum {\n"; + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); + ++it) { + auto &field = **it; + if (!field.deprecated) { // Deprecated fields won't be accessible. + code += " " + GenFieldOffsetName(field) + " = "; + code += NumToString(field.value.offset) + ",\n"; + } + } + code += " };\n"; + // Generate the accessors. for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { @@ -268,7 +287,7 @@ static void GenTable(const Parser &parser, StructDef &struct_def, auto accessor = is_scalar ? "GetField<" : (IsStruct(field.value.type) ? "GetStruct<" : "GetPointer<"); - auto offsetstr = NumToString(field.value.offset); + auto offsetstr = GenFieldOffsetName(field); auto call = accessor + GenTypeGet(parser, field.value.type, "", "const ", " *", false) + @@ -347,8 +366,7 @@ static void GenTable(const Parser &parser, StructDef &struct_def, code += prefix + "VerifyField"; if (field.required) code += "Required"; code += "<" + GenTypeSize(parser, field.value.type); - code += ">(verifier, " + NumToString(field.value.offset); - code += " /* " + field.name + " */)"; + code += ">(verifier, " + GenFieldOffsetName(field) + ")"; switch (field.value.type.base_type) { case BASE_TYPE_UNION: code += prefix + "Verify" + field.value.type.enum_def->name; @@ -412,7 +430,7 @@ static void GenTable(const Parser &parser, StructDef &struct_def, } else { code += "Offset"; } - code += "(" + NumToString(field.value.offset) + ", "; + code += "(" + struct_def.name + "::" + GenFieldOffsetName(field) + ", "; code += GenUnderlyingCast(parser, field, false, field.name); if (IsScalar(field.value.type.base_type)) code += ", " + field.value.constant; @@ -433,7 +451,8 @@ static void GenTable(const Parser &parser, StructDef &struct_def, ++it) { auto &field = **it; if (!field.deprecated && field.required) { - code += " fbb_.Required(o, " + NumToString(field.value.offset); + code += " fbb_.Required(o, "; + code += struct_def.name + "::" + GenFieldOffsetName(field); code += "); // " + field.name + "\n"; } } diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 8a78de530..90814a94f 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -97,11 +97,14 @@ MANUALLY_ALIGNED_STRUCT(16) Vec3 FLATBUFFERS_FINAL_CLASS { STRUCT_END(Vec3, 32); struct TestSimpleTableWithEnum FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - Color color() const { return static_cast(GetField(4, 2)); } - bool mutate_color(Color _color) { return SetField(4, static_cast(_color)); } + enum { + VT_COLOR = 4, + }; + Color color() const { return static_cast(GetField(VT_COLOR, 2)); } + bool mutate_color(Color _color) { return SetField(VT_COLOR, static_cast(_color)); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && - VerifyField(verifier, 4 /* color */) && + VerifyField(verifier, VT_COLOR) && verifier.EndTable(); } }; @@ -109,7 +112,7 @@ struct TestSimpleTableWithEnum FLATBUFFERS_FINAL_CLASS : private flatbuffers::Ta struct TestSimpleTableWithEnumBuilder { flatbuffers::FlatBufferBuilder &fbb_; flatbuffers::uoffset_t start_; - void add_color(Color color) { fbb_.AddElement(4, static_cast(color), 2); } + void add_color(Color color) { fbb_.AddElement(TestSimpleTableWithEnum::VT_COLOR, static_cast(color), 2); } TestSimpleTableWithEnumBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } TestSimpleTableWithEnumBuilder &operator=(const TestSimpleTableWithEnumBuilder &); flatbuffers::Offset Finish() { @@ -126,18 +129,23 @@ inline flatbuffers::Offset CreateTestSimpleTableWithEnu } struct Stat FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - const flatbuffers::String *id() const { return GetPointer(4); } - flatbuffers::String *mutable_id() { return GetPointer(4); } - int64_t val() const { return GetField(6, 0); } - bool mutate_val(int64_t _val) { return SetField(6, _val); } - uint16_t count() const { return GetField(8, 0); } - bool mutate_count(uint16_t _count) { return SetField(8, _count); } + enum { + 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); } + uint16_t count() const { return GetField(VT_COUNT, 0); } + bool mutate_count(uint16_t _count) { return SetField(VT_COUNT, _count); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && - VerifyField(verifier, 4 /* id */) && + VerifyField(verifier, VT_ID) && verifier.Verify(id()) && - VerifyField(verifier, 6 /* val */) && - VerifyField(verifier, 8 /* count */) && + VerifyField(verifier, VT_VAL) && + VerifyField(verifier, VT_COUNT) && verifier.EndTable(); } }; @@ -145,9 +153,9 @@ struct Stat FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { struct StatBuilder { flatbuffers::FlatBufferBuilder &fbb_; flatbuffers::uoffset_t start_; - void add_id(flatbuffers::Offset id) { fbb_.AddOffset(4, id); } - void add_val(int64_t val) { fbb_.AddElement(6, val, 0); } - void add_count(uint16_t count) { fbb_.AddElement(8, count, 0); } + 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); } StatBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } StatBuilder &operator=(const StatBuilder &); flatbuffers::Offset Finish() { @@ -169,96 +177,122 @@ inline flatbuffers::Offset CreateStat(flatbuffers::FlatBufferBuilder &_fbb /// an example documentation comment: monster object struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { - const Vec3 *pos() const { return GetStruct(4); } - Vec3 *mutable_pos() { return GetStruct(4); } - int16_t mana() const { return GetField(6, 150); } - bool mutate_mana(int16_t _mana) { return SetField(6, _mana); } - int16_t hp() const { return GetField(8, 100); } - bool mutate_hp(int16_t _hp) { return SetField(8, _hp); } - const flatbuffers::String *name() const { return GetPointer(10); } - flatbuffers::String *mutable_name() { return GetPointer(10); } + enum { + 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, + }; + const Vec3 *pos() const { return GetStruct(VT_POS); } + 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); } + int16_t hp() const { return GetField(VT_HP, 100); } + bool mutate_hp(int16_t _hp) { return SetField(VT_HP, _hp); } + 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 *>(14); } - flatbuffers::Vector *mutable_inventory() { return GetPointer *>(14); } - Color color() const { return static_cast(GetField(16, 8)); } - bool mutate_color(Color _color) { return SetField(16, static_cast(_color)); } - Any test_type() const { return static_cast(GetField(18, 0)); } - bool mutate_test_type(Any _test_type) { return SetField(18, static_cast(_test_type)); } - const void *test() const { return GetPointer(20); } - void *mutable_test() { return GetPointer(20); } - const flatbuffers::Vector *test4() const { return GetPointer *>(22); } - flatbuffers::Vector *mutable_test4() { return GetPointer *>(22); } - const flatbuffers::Vector> *testarrayofstring() const { return GetPointer> *>(24); } - flatbuffers::Vector> *mutable_testarrayofstring() { return GetPointer> *>(24); } + const flatbuffers::Vector *inventory() const { return GetPointer *>(VT_INVENTORY); } + flatbuffers::Vector *mutable_inventory() { return GetPointer *>(VT_INVENTORY); } + Color color() const { return static_cast(GetField(VT_COLOR, 8)); } + bool mutate_color(Color _color) { return SetField(VT_COLOR, static_cast(_color)); } + Any test_type() const { return static_cast(GetField(VT_TEST_TYPE, 0)); } + bool mutate_test_type(Any _test_type) { return SetField(VT_TEST_TYPE, static_cast(_test_type)); } + const void *test() const { return GetPointer(VT_TEST); } + 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> *>(26); } - flatbuffers::Vector> *mutable_testarrayoftables() { return GetPointer> *>(26); } - const Monster *enemy() const { return GetPointer(28); } - Monster *mutable_enemy() { return GetPointer(28); } - const flatbuffers::Vector *testnestedflatbuffer() const { return GetPointer *>(30); } - flatbuffers::Vector *mutable_testnestedflatbuffer() { return GetPointer *>(30); } + const flatbuffers::Vector> *testarrayoftables() const { return GetPointer> *>(VT_TESTARRAYOFTABLES); } + flatbuffers::Vector> *mutable_testarrayoftables() { return GetPointer> *>(VT_TESTARRAYOFTABLES); } + const Monster *enemy() const { return GetPointer(VT_ENEMY); } + 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 Stat *testempty() const { return GetPointer(32); } - Stat *mutable_testempty() { return GetPointer(32); } - bool testbool() const { return static_cast(GetField(34, 0)); } - bool mutate_testbool(bool _testbool) { return SetField(34, static_cast(_testbool)); } - int32_t testhashs32_fnv1() const { return GetField(36, 0); } - bool mutate_testhashs32_fnv1(int32_t _testhashs32_fnv1) { return SetField(36, _testhashs32_fnv1); } - uint32_t testhashu32_fnv1() const { return GetField(38, 0); } - bool mutate_testhashu32_fnv1(uint32_t _testhashu32_fnv1) { return SetField(38, _testhashu32_fnv1); } - int64_t testhashs64_fnv1() const { return GetField(40, 0); } - bool mutate_testhashs64_fnv1(int64_t _testhashs64_fnv1) { return SetField(40, _testhashs64_fnv1); } - uint64_t testhashu64_fnv1() const { return GetField(42, 0); } - bool mutate_testhashu64_fnv1(uint64_t _testhashu64_fnv1) { return SetField(42, _testhashu64_fnv1); } - int32_t testhashs32_fnv1a() const { return GetField(44, 0); } - bool mutate_testhashs32_fnv1a(int32_t _testhashs32_fnv1a) { return SetField(44, _testhashs32_fnv1a); } - uint32_t testhashu32_fnv1a() const { return GetField(46, 0); } - bool mutate_testhashu32_fnv1a(uint32_t _testhashu32_fnv1a) { return SetField(46, _testhashu32_fnv1a); } - int64_t testhashs64_fnv1a() const { return GetField(48, 0); } - bool mutate_testhashs64_fnv1a(int64_t _testhashs64_fnv1a) { return SetField(48, _testhashs64_fnv1a); } - uint64_t testhashu64_fnv1a() const { return GetField(50, 0); } - bool mutate_testhashu64_fnv1a(uint64_t _testhashu64_fnv1a) { return SetField(50, _testhashu64_fnv1a); } - const flatbuffers::Vector *testarrayofbools() const { return GetPointer *>(52); } - flatbuffers::Vector *mutable_testarrayofbools() { return GetPointer *>(52); } + const Stat *testempty() const { return GetPointer(VT_TESTEMPTY); } + Stat *mutable_testempty() { return GetPointer(VT_TESTEMPTY); } + bool testbool() const { return static_cast(GetField(VT_TESTBOOL, 0)); } + bool mutate_testbool(bool _testbool) { return SetField(VT_TESTBOOL, static_cast(_testbool)); } + 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); } + 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); } + 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); } + 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); } + 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); } + 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); } + 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); } + 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); } + const flatbuffers::Vector *testarrayofbools() const { return GetPointer *>(VT_TESTARRAYOFBOOLS); } + flatbuffers::Vector *mutable_testarrayofbools() { return GetPointer *>(VT_TESTARRAYOFBOOLS); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && - VerifyField(verifier, 4 /* pos */) && - VerifyField(verifier, 6 /* mana */) && - VerifyField(verifier, 8 /* hp */) && - VerifyFieldRequired(verifier, 10 /* name */) && + VerifyField(verifier, VT_POS) && + VerifyField(verifier, VT_MANA) && + VerifyField(verifier, VT_HP) && + VerifyFieldRequired(verifier, VT_NAME) && verifier.Verify(name()) && - VerifyField(verifier, 14 /* inventory */) && + VerifyField(verifier, VT_INVENTORY) && verifier.Verify(inventory()) && - VerifyField(verifier, 16 /* color */) && - VerifyField(verifier, 18 /* test_type */) && - VerifyField(verifier, 20 /* test */) && + VerifyField(verifier, VT_COLOR) && + VerifyField(verifier, VT_TEST_TYPE) && + VerifyField(verifier, VT_TEST) && VerifyAny(verifier, test(), test_type()) && - VerifyField(verifier, 22 /* test4 */) && + VerifyField(verifier, VT_TEST4) && verifier.Verify(test4()) && - VerifyField(verifier, 24 /* testarrayofstring */) && + VerifyField(verifier, VT_TESTARRAYOFSTRING) && verifier.Verify(testarrayofstring()) && verifier.VerifyVectorOfStrings(testarrayofstring()) && - VerifyField(verifier, 26 /* testarrayoftables */) && + VerifyField(verifier, VT_TESTARRAYOFTABLES) && verifier.Verify(testarrayoftables()) && verifier.VerifyVectorOfTables(testarrayoftables()) && - VerifyField(verifier, 28 /* enemy */) && + VerifyField(verifier, VT_ENEMY) && verifier.VerifyTable(enemy()) && - VerifyField(verifier, 30 /* testnestedflatbuffer */) && + VerifyField(verifier, VT_TESTNESTEDFLATBUFFER) && verifier.Verify(testnestedflatbuffer()) && - VerifyField(verifier, 32 /* testempty */) && + VerifyField(verifier, VT_TESTEMPTY) && verifier.VerifyTable(testempty()) && - VerifyField(verifier, 34 /* testbool */) && - VerifyField(verifier, 36 /* testhashs32_fnv1 */) && - VerifyField(verifier, 38 /* testhashu32_fnv1 */) && - VerifyField(verifier, 40 /* testhashs64_fnv1 */) && - VerifyField(verifier, 42 /* testhashu64_fnv1 */) && - VerifyField(verifier, 44 /* testhashs32_fnv1a */) && - VerifyField(verifier, 46 /* testhashu32_fnv1a */) && - VerifyField(verifier, 48 /* testhashs64_fnv1a */) && - VerifyField(verifier, 50 /* testhashu64_fnv1a */) && - VerifyField(verifier, 52 /* testarrayofbools */) && + 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) && + VerifyField(verifier, VT_TESTARRAYOFBOOLS) && verifier.Verify(testarrayofbools()) && verifier.EndTable(); } @@ -267,35 +301,35 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { struct MonsterBuilder { flatbuffers::FlatBufferBuilder &fbb_; flatbuffers::uoffset_t start_; - void add_pos(const Vec3 *pos) { fbb_.AddStruct(4, pos); } - void add_mana(int16_t mana) { fbb_.AddElement(6, mana, 150); } - void add_hp(int16_t hp) { fbb_.AddElement(8, hp, 100); } - void add_name(flatbuffers::Offset name) { fbb_.AddOffset(10, name); } - void add_inventory(flatbuffers::Offset> inventory) { fbb_.AddOffset(14, inventory); } - void add_color(Color color) { fbb_.AddElement(16, static_cast(color), 8); } - void add_test_type(Any test_type) { fbb_.AddElement(18, static_cast(test_type), 0); } - void add_test(flatbuffers::Offset test) { fbb_.AddOffset(20, test); } - void add_test4(flatbuffers::Offset> test4) { fbb_.AddOffset(22, test4); } - void add_testarrayofstring(flatbuffers::Offset>> testarrayofstring) { fbb_.AddOffset(24, testarrayofstring); } - void add_testarrayoftables(flatbuffers::Offset>> testarrayoftables) { fbb_.AddOffset(26, testarrayoftables); } - void add_enemy(flatbuffers::Offset enemy) { fbb_.AddOffset(28, enemy); } - void add_testnestedflatbuffer(flatbuffers::Offset> testnestedflatbuffer) { fbb_.AddOffset(30, testnestedflatbuffer); } - void add_testempty(flatbuffers::Offset testempty) { fbb_.AddOffset(32, testempty); } - void add_testbool(bool testbool) { fbb_.AddElement(34, static_cast(testbool), 0); } - void add_testhashs32_fnv1(int32_t testhashs32_fnv1) { fbb_.AddElement(36, testhashs32_fnv1, 0); } - void add_testhashu32_fnv1(uint32_t testhashu32_fnv1) { fbb_.AddElement(38, testhashu32_fnv1, 0); } - void add_testhashs64_fnv1(int64_t testhashs64_fnv1) { fbb_.AddElement(40, testhashs64_fnv1, 0); } - void add_testhashu64_fnv1(uint64_t testhashu64_fnv1) { fbb_.AddElement(42, testhashu64_fnv1, 0); } - void add_testhashs32_fnv1a(int32_t testhashs32_fnv1a) { fbb_.AddElement(44, testhashs32_fnv1a, 0); } - void add_testhashu32_fnv1a(uint32_t testhashu32_fnv1a) { fbb_.AddElement(46, testhashu32_fnv1a, 0); } - void add_testhashs64_fnv1a(int64_t testhashs64_fnv1a) { fbb_.AddElement(48, testhashs64_fnv1a, 0); } - void add_testhashu64_fnv1a(uint64_t testhashu64_fnv1a) { fbb_.AddElement(50, testhashu64_fnv1a, 0); } - void add_testarrayofbools(flatbuffers::Offset> testarrayofbools) { fbb_.AddOffset(52, testarrayofbools); } + void add_pos(const 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(Color color) { fbb_.AddElement(Monster::VT_COLOR, static_cast(color), 8); } + void add_test_type(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); } MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } MonsterBuilder &operator=(const MonsterBuilder &); flatbuffers::Offset Finish() { auto o = flatbuffers::Offset(fbb_.EndTable(start_, 25)); - fbb_.Required(o, 10); // name + fbb_.Required(o, Monster::VT_NAME); // name return o; } }; From ac49eda0531b8679bc1eeb55e486dc4390c3f08c Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Mon, 30 Nov 2015 16:42:48 -0800 Subject: [PATCH 10/13] Multiple schemas parsed by flatc are now parsed independently. It used to be such that later schemas could depend on earlier schemas. This was a convenience from days before include files were implemented. Nowadays they cause subtle bugs rather than being useful, so this functionality has been removed. You now need to explicitly include files you depend upon. Change-Id: Id8292c3c621fc38fbd796da2d2cbdd63efc230d1 Tested: on Linux. --- docs/html/md__compiler.html | 4 ++-- docs/source/Compiler.md | 7 +++---- include/flatbuffers/util.h | 6 ++++++ src/flatc.cpp | 36 +++++++++++++++++++++--------------- src/idl_parser.cpp | 7 +++---- 5 files changed, 35 insertions(+), 25 deletions(-) diff --git a/docs/html/md__compiler.html b/docs/html/md__compiler.html index ff3f7fe22..1b3cd1db4 100644 --- a/docs/html/md__compiler.html +++ b/docs/html/md__compiler.html @@ -63,8 +63,8 @@ $(document).ready(function(){initNavTree('md__compiler.html','');});

Usage:

flatc [ GENERATOR OPTIONS ] [ -o PATH ] [ -I PATH ] [ -S ] FILES...
       [ -- FILES...]
-

The files are read and parsed in order, and can contain either schemas or data (see below). Later files can make use of definitions in earlier files.

-

-- indicates that the following files are binary files in FlatBuffer format conforming to the schema(s) indicated before it. Incompatible binary files currently will give unpredictable results (!)

+

The files are read and parsed in order, and can contain either schemas or data (see below). Data files are processed according to the definitions of the most recent schema specified.

+

-- indicates that the following files are binary files in FlatBuffer format conforming to the schema indicated before it.

Depending on the flags passed, additional files may be generated for each file processed:

For any schema input files, one or more generators can be specified:

    diff --git a/docs/source/Compiler.md b/docs/source/Compiler.md index 27ddf52e4..9d89cb104 100755 --- a/docs/source/Compiler.md +++ b/docs/source/Compiler.md @@ -6,12 +6,11 @@ Usage: [ -- FILES...] The files are read and parsed in order, and can contain either schemas -or data (see below). Later files can make use of definitions in earlier -files. +or data (see below). Data files are processed according to the definitions of +the most recent schema specified. `--` indicates that the following files are binary files in -FlatBuffer format conforming to the schema(s) indicated before it. -Incompatible binary files currently will give unpredictable results (!) +FlatBuffer format conforming to the schema indicated before it. Depending on the flags passed, additional files may be generated for each file processed: diff --git a/include/flatbuffers/util.h b/include/flatbuffers/util.h index 8e5d09908..ba73d67bd 100644 --- a/include/flatbuffers/util.h +++ b/include/flatbuffers/util.h @@ -174,6 +174,12 @@ inline std::string StripExtension(const std::string &filepath) { return i != std::string::npos ? filepath.substr(0, i) : filepath; } +// Returns the extension, if any. +inline std::string GetExtension(const std::string &filepath) { + size_t i = filepath.find_last_of("."); + return i != std::string::npos ? filepath.substr(i + 1) : ""; +} + // Return the last component of the path, after the last separator. inline std::string StripPath(const std::string &filepath) { size_t i = filepath.find_last_of(PathSeparatorSet); diff --git a/src/flatc.cpp b/src/flatc.cpp index 2479da12b..86de77416 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -79,7 +79,8 @@ const Generator generators[] = { flatbuffers::GeneralMakeRule }, }; -const char *program_name = NULL; +const char *program_name = nullptr; +flatbuffers::Parser *parser = nullptr; static void Error(const std::string &err, bool usage, bool show_exe_name) { if (show_exe_name) printf("%s: ", program_name); @@ -122,6 +123,7 @@ static void Error(const std::string &err, bool usage, bool show_exe_name) { "example: %s -c -b schema1.fbs schema2.fbs data.json\n", program_name); } + if (parser) delete parser; exit(1); } @@ -205,7 +207,7 @@ int main(int argc, const char *argv[]) { Error("no options: specify one of -c -g -j -t -b etc.", true); // Now process the files: - flatbuffers::Parser parser(opts.strict_json, proto_mode); + parser = new flatbuffers::Parser(opts.strict_json, proto_mode); for (auto file_it = filenames.begin(); file_it != filenames.end(); ++file_it) { @@ -216,8 +218,8 @@ int main(int argc, const char *argv[]) { bool is_binary = static_cast(file_it - filenames.begin()) >= binary_files_from; if (is_binary) { - parser.builder_.Clear(); - parser.builder_.PushFlatBuffer( + parser->builder_.Clear(); + parser->builder_.PushFlatBuffer( reinterpret_cast(contents.c_str()), contents.length()); if (!raw_binary) { @@ -226,30 +228,37 @@ int main(int argc, const char *argv[]) { // does not contain a file identifier. // We'd expect that typically any binary used as a file would have // such an identifier, so by default we require them to match. - if (!parser.file_identifier_.length()) { + if (!parser->file_identifier_.length()) { Error("current schema has no file_identifier: cannot test if \"" + *file_it + "\" matches the schema, use --raw-binary to read this file" " anyway."); } else if (!flatbuffers::BufferHasIdentifier(contents.c_str(), - parser.file_identifier_.c_str())) { + parser->file_identifier_.c_str())) { Error("binary \"" + *file_it + "\" does not have expected file_identifier \"" + - parser.file_identifier_ + + parser->file_identifier_ + "\", use --raw-binary to read this file anyway."); } } } else { + if (flatbuffers::GetExtension(*file_it) == "fbs") { + // If we're processing multiple schemas, make sure to start each + // one from scratch. If it depends on previous schemas it must do + // so explicitly using an include. + delete parser; + parser = new flatbuffers::Parser(opts.strict_json, proto_mode); + } auto local_include_directory = flatbuffers::StripFileName(*file_it); include_directories.push_back(local_include_directory.c_str()); include_directories.push_back(nullptr); - if (!parser.Parse(contents.c_str(), &include_directories[0], + if (!parser->Parse(contents.c_str(), &include_directories[0], file_it->c_str())) - Error(parser.error_, false, false); + Error(parser->error_, false, false); if (schema_binary) { - parser.Serialize(); - parser.file_extension_ = reflection::SchemaExtension(); + parser->Serialize(); + parser->file_extension_ = reflection::SchemaExtension(); } include_directories.pop_back(); include_directories.pop_back(); @@ -280,11 +289,8 @@ int main(int argc, const char *argv[]) { } if (proto_mode) GenerateFBS(parser, output_path, filebase, opts); - - // We do not want to generate code for the definitions in this file - // in any files coming up next. - parser.MarkGenerated(); } + delete parser; return 0; } diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 233eeceef..f2b7fc9c4 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -1072,10 +1072,9 @@ bool Parser::SetRootType(const char *name) { } void Parser::MarkGenerated() { - // Since the Parser object retains definitions across files, we must - // ensure we only output code for definitions once, in the file they are first - // declared. This function marks all existing definitions as having already - // been generated. + // This function marks all existing definitions as having already + // been generated, which signals no code for included files should be + // generated. for (auto it = enums_.vec.begin(); it != enums_.vec.end(); ++it) { (*it)->generated = true; From e4abeef65c81f079909b66af82be450601d54d52 Mon Sep 17 00:00:00 2001 From: Perry Hung Date: Mon, 30 Nov 2015 19:41:54 -0500 Subject: [PATCH 11/13] Link clang builds against libc++abi on Linux Simply running clang with -stdlib=libc++ requires a manual link against libc++abi on Linux. This is documented in the libc++ documentation: http://libcxx.llvm.org/ Tested on Arch Linux, using clang 3.7.0 RELEASE. Signed-off-by: Perry Hung --- CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 47a428230..6dc73e7a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -84,7 +84,9 @@ elseif(CMAKE_COMPILER_IS_GNUCXX) "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -pedantic -Werror -Wextra -Werror=shadow") elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") set(CMAKE_CXX_FLAGS - "${CMAKE_CXX_FLAGS} -std=c++0x -stdlib=libc++ -Wall -pedantic -Werror -Wextra") + "${CMAKE_CXX_FLAGS} -std=c++0x -stdlib=libc++ -Wall -pedantic -Werror -Wextra") + set(CMAKE_EXE_LINKER_FLAGS + "${CMAKE_EXE_LINKER_FLAGS} -lc++abi") endif() if(FLATBUFFERS_CODE_COVERAGE) From b611dc4b993a48af61f84c2807d7d65879aaf769 Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Mon, 30 Nov 2015 17:49:51 -0800 Subject: [PATCH 12/13] Fixed implicit dereference in flatc. Change-Id: If2e62a325b47399561b4c20d2def55ede4831d19 Tested: on Linux. --- src/flatc.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/flatc.cpp b/src/flatc.cpp index 86de77416..a27af7950 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -272,7 +272,7 @@ int main(int argc, const char *argv[]) { if (generator_enabled[i]) { if (!print_make_rules) { flatbuffers::EnsureDirExists(output_path); - if (!generators[i].generate(parser, output_path, filebase, opts)) { + if (!generators[i].generate(*parser, output_path, filebase, opts)) { Error(std::string("Unable to generate ") + generators[i].lang_name + " for " + @@ -280,7 +280,7 @@ int main(int argc, const char *argv[]) { } } else { std::string make_rule = generators[i].make_rule( - parser, output_path, *file_it, opts); + *parser, output_path, *file_it, opts); if (!make_rule.empty()) printf("%s\n", flatbuffers::WordWrap( make_rule, 80, " ", " \\").c_str()); @@ -288,7 +288,7 @@ int main(int argc, const char *argv[]) { } } - if (proto_mode) GenerateFBS(parser, output_path, filebase, opts); + if (proto_mode) GenerateFBS(*parser, output_path, filebase, opts); } delete parser; From 5ea2b49f58462728a895bc61ba290f16bed220d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E5=87=A1?= Date: Fri, 1 Jul 2016 11:17:03 +0800 Subject: [PATCH 13/13] Fix some error --- CMakeLists.txt | 9 - docs/html/md__compiler.html | 114 ------ docs/source/Compiler.md | 3 - include/flatbuffers/idl.h | 11 - .../google/flatbuffers/FlatBufferBuilder.java | 32 -- samples/monster_generated.h | 12 - src/flatc.cpp | 64 ---- src/idl_gen_cpp.cpp | 344 ------------------ src/idl_gen_grpc.cpp | 2 +- src/idl_parser.cpp | 27 -- tests/generate_code.sh | 5 - tests/monster_test.bfbs | Bin 2744 -> 3152 bytes tests/monster_test_generated.h | 25 -- 13 files changed, 1 insertion(+), 647 deletions(-) delete mode 100644 docs/html/md__compiler.html diff --git a/CMakeLists.txt b/CMakeLists.txt index 32a5e2141..c2dd95085 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -105,14 +105,6 @@ elseif(CMAKE_COMPILER_IS_GNUCXX) "${CMAKE_CXX_FLAGS} -std=c++0x") endif(CYGWIN) set(CMAKE_CXX_FLAGS -<<<<<<< HEAD - "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -pedantic -Werror -Wextra -Werror=shadow") -elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") - set(CMAKE_CXX_FLAGS - "${CMAKE_CXX_FLAGS} -std=c++0x -stdlib=libc++ -Wall -pedantic -Werror -Wextra") - set(CMAKE_EXE_LINKER_FLAGS - "${CMAKE_EXE_LINKER_FLAGS} -lc++abi") -======= "${CMAKE_CXX_FLAGS} -Wall -pedantic -Werror -Wextra -Werror=shadow") if (GCC_VERSION VERSION_GREATER 4.4) set(CMAKE_CXX_FLAGS @@ -137,7 +129,6 @@ elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsigned-char") ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 endif() if(FLATBUFFERS_CODE_COVERAGE) diff --git a/docs/html/md__compiler.html b/docs/html/md__compiler.html deleted file mode 100644 index 1b3cd1db4..000000000 --- a/docs/html/md__compiler.html +++ /dev/null @@ -1,114 +0,0 @@ - - - - - - - -FlatBuffers: Using the schema compiler - - - - - - - - - - - -
    -
    - - - - - - - -
    -
    FlatBuffers -
    -
    - An open source project by FPL. -
    -
    -
    - - -
    -
    - -
    -
    -
    - -
    -
    -
    -
    Using the schema compiler
    -
    -
    -

    Usage:

    flatc [ GENERATOR OPTIONS ] [ -o PATH ] [ -I PATH ] [ -S ] FILES...
    -      [ -- FILES...]
    -

    The files are read and parsed in order, and can contain either schemas or data (see below). Data files are processed according to the definitions of the most recent schema specified.

    -

    -- indicates that the following files are binary files in FlatBuffer format conforming to the schema indicated before it.

    -

    Depending on the flags passed, additional files may be generated for each file processed:

    -

    For any schema input files, one or more generators can be specified:

    -
      -
    • --cpp, -c : Generate a C++ header for all definitions in this file (as filename_generated.h).
    • -
    • --java, -j : Generate Java code.
    • -
    • --csharp, -n : Generate C# code.
    • -
    • --go, -g : Generate Go code.
    • -
    • --python, -p: Generate Python code.
    • -
    • --javascript, -s: Generate JavaScript code.
    • -
    • --php: Generate PHP code.
    • -
    -

    For any data input files:

    -
      -
    • --binary, -b : If data is contained in this file, generate a filename.bin containing the binary flatbuffer (or a different extension if one is specified in the schema).
    • -
    • --json, -t : If data is contained in this file, generate a filename.json representing the data in the flatbuffer.
    • -
    -

    Additional options:

    -
      -
    • -o PATH : Output all generated files to PATH (either absolute, or relative to the current directory). If omitted, PATH will be the current directory. PATH should end in your systems path separator, e.g. / or \.
    • -
    • -I PATH : when encountering include statements, attempt to load the files from this path. Paths will be tried in the order given, and if all fail (or none are specified) it will try to load relative to the path of the schema file being parsed.
    • -
    • -M : Print make rules for generated files.
    • -
    • --strict-json : Require & generate strict JSON (field names are enclosed in quotes, no trailing commas in tables/vectors). By default, no quotes are required/generated, and trailing commas are allowed.
    • -
    • --defaults-json : Output fields whose value is equal to the default value when writing JSON text.
    • -
    • --no-prefix : Don't prefix enum values in generated C++ by their enum type.
    • -
    • --scoped-enums : Use C++11 style scoped and strongly typed enums in generated C++. This also implies --no-prefix.
    • -
    • --gen-includes : (deprecated), this is the default behavior. If the original behavior is required (no include statements) use --no-includes.
    • -
    • --no-includes : Don't generate include statements for included schemas the generated file depends on (C++).
    • -
    • --gen-mutable : Generate additional non-const accessors for mutating FlatBuffers in-place.
    • -
    • --gen-onefile : Generate single output file (useful for C#)
    • -
    • --raw-binary : Allow binaries without a file_indentifier to be read. This may crash flatc given a mismatched schema.
    • -
    • --proto: Expect input files to be .proto files (protocol buffers). Output the corresponding .fbs file. Currently supports: package, message, enum, nested declarations, import (use -I for paths), extend, oneof, group. Does not support, but will skip without error: option, service, extensions, and most everything else.
    • -
    • --schema: Serialize schemas instead of JSON (use with -b). This will output a binary version of the specified schema that itself corresponds to the reflection/reflection.fbs schema. Loading this binary file is the basis for reflection functionality.
    • -
    -

    NOTE: short-form options for generators are deprecated, use the long form whenever possible.

    -
    -
    - - - - diff --git a/docs/source/Compiler.md b/docs/source/Compiler.md index 5f1427d0c..ad584c739 100755 --- a/docs/source/Compiler.md +++ b/docs/source/Compiler.md @@ -33,11 +33,8 @@ For any schema input files, one or more generators can be specified: - `--php`: Generate PHP code. -<<<<<<< HEAD -======= - `--grpc`: Generate RPC stub code for GRPC. ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 For any data input files: - `--binary`, `-b` : If data is contained in this file, generate a diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index 1b45425b5..b22bc08a1 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -550,18 +550,7 @@ private: IDLOptions opts; private: -<<<<<<< HEAD - const char *source_, *cursor_; - int line_; // the current line being parsed - int token_; - std::string files_being_parsed_; - bool proto_mode_; - bool strict_json_; - std::string attribute_; - std::vector doc_comment_; -======= const char *source_; ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 std::string file_being_parsed_; diff --git a/java/com/google/flatbuffers/FlatBufferBuilder.java b/java/com/google/flatbuffers/FlatBufferBuilder.java index 3bbd5d32d..c2186fa7b 100644 --- a/java/com/google/flatbuffers/FlatBufferBuilder.java +++ b/java/com/google/flatbuffers/FlatBufferBuilder.java @@ -36,21 +36,6 @@ import java.nio.charset.Charset; * "Use in Java/C#" in the main FlatBuffers documentation. */ public class FlatBufferBuilder { -<<<<<<< HEAD - ByteBuffer bb; // Where we construct the FlatBuffer. - int space; // Remaining space in the ByteBuffer. - static final Charset utf8charset = Charset.forName("UTF-8"); - int minalign = 1; // Minimum alignment encountered so far. - int[] vtable = null; // The vtable for the current table. - int vtable_in_use = 0; // The amount of fields we're actually using. - boolean nested = false; // Whether we are currently serializing a table. - boolean finished = false; // Whether the buffer is finished. - int object_start; // Starting offset of the current struct/table. - int[] vtables = new int[16]; // List of offsets of all vtables. - int num_vtables = 0; // Number of entries in `vtables` in use. - int vector_num_elems = 0; // For the current vector being built. - boolean force_defaults = false; // False omits default values from the serialized data -======= /// @cond FLATBUFFERS_INTERNAL ByteBuffer bb; // Where we construct the FlatBuffer. int space; // Remaining space in the ByteBuffer. @@ -68,7 +53,6 @@ public class FlatBufferBuilder { CharsetEncoder encoder = utf8charset.newEncoder(); ByteBuffer dst; /// @endcond ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 /** * Start with a buffer of size `initial_size`, then grow as required. @@ -440,16 +424,6 @@ public class FlatBufferBuilder { " finished by FlatBufferBuilder.finish()."); } - /** - * Should not be accessing the final buffer before it is finished. - */ - public void finished() { - if (!finished) - throw new AssertionError( - "FlatBuffers: you can only access the serialized buffer after it has been" + - " finished by FlatBufferBuilder.finish()."); - } - /** * Should not be creating any other object, string or vector * while an object is being constructed. @@ -751,11 +725,6 @@ public class FlatBufferBuilder { return this; } -<<<<<<< HEAD - // Get the ByteBuffer representing the FlatBuffer. Only call this after you've - // called finish(). The actual data starts at the ByteBuffer's current position, - // not necessarily at 0. -======= /** * Get the ByteBuffer representing the FlatBuffer. Only call this after you've * called `finish()`. The actual data starts at the ByteBuffer's current position, @@ -763,7 +732,6 @@ public class FlatBufferBuilder { * * @return The {@link ByteBuffer} representing the FlatBuffer */ ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 public ByteBuffer dataBuffer() { finished(); return bb; diff --git a/samples/monster_generated.h b/samples/monster_generated.h index 93bf73fdf..1a16126a5 100644 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -72,12 +72,9 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { VT_NAME = 10, VT_INVENTORY = 14, VT_COLOR = 16, -<<<<<<< HEAD -======= VT_WEAPONS = 18, VT_EQUIPPED_TYPE = 20, VT_EQUIPPED = 22 ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 }; const Vec3 *pos() const { return GetStruct(VT_POS); } Vec3 *mutable_pos() { return GetStruct(VT_POS); } @@ -91,15 +88,12 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { flatbuffers::Vector *mutable_inventory() { return GetPointer *>(VT_INVENTORY); } Color color() const { return static_cast(GetField(VT_COLOR, 2)); } bool mutate_color(Color _color) { return SetField(VT_COLOR, static_cast(_color)); } -<<<<<<< HEAD -======= const flatbuffers::Vector> *weapons() const { return GetPointer> *>(VT_WEAPONS); } flatbuffers::Vector> *mutable_weapons() { return GetPointer> *>(VT_WEAPONS); } Equipment equipped_type() const { return static_cast(GetField(VT_EQUIPPED_TYPE, 0)); } bool mutate_equipped_type(Equipment _equipped_type) { return SetField(VT_EQUIPPED_TYPE, static_cast(_equipped_type)); } const void *equipped() const { return GetPointer(VT_EQUIPPED); } void *mutable_equipped() { return GetPointer(VT_EQUIPPED); } ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField(verifier, VT_POS) && @@ -110,15 +104,12 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { VerifyField(verifier, VT_INVENTORY) && verifier.Verify(inventory()) && VerifyField(verifier, VT_COLOR) && -<<<<<<< HEAD -======= VerifyField(verifier, VT_WEAPONS) && verifier.Verify(weapons()) && verifier.VerifyVectorOfTables(weapons()) && VerifyField(verifier, VT_EQUIPPED_TYPE) && VerifyField(verifier, VT_EQUIPPED) && VerifyEquipment(verifier, equipped(), equipped_type()) && ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 verifier.EndTable(); } }; @@ -132,12 +123,9 @@ struct MonsterBuilder { 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(Color color) { fbb_.AddElement(Monster::VT_COLOR, static_cast(color), 2); } -<<<<<<< HEAD -======= void add_weapons(flatbuffers::Offset>> weapons) { fbb_.AddOffset(Monster::VT_WEAPONS, weapons); } void add_equipped_type(Equipment equipped_type) { fbb_.AddElement(Monster::VT_EQUIPPED_TYPE, static_cast(equipped_type), 0); } void add_equipped(flatbuffers::Offset equipped) { fbb_.AddOffset(Monster::VT_EQUIPPED, equipped); } ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } MonsterBuilder &operator=(const MonsterBuilder &); flatbuffers::Offset Finish() { diff --git a/src/flatc.cpp b/src/flatc.cpp index 6a8d89125..c7ba29e86 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -29,12 +29,7 @@ static void Error(const std::string &err, bool usage = false, struct Generator { bool (*generate)(const flatbuffers::Parser &parser, const std::string &path, -<<<<<<< HEAD - const std::string &file_name, - const flatbuffers::GeneratorOptions &opts); -======= const std::string &file_name); ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 const char *generator_opt_short; const char *generator_opt_long; const char *lang_name; @@ -48,41 +43,6 @@ struct Generator { const Generator generators[] = { { flatbuffers::GenerateBinary, "-b", "--binary", "binary", -<<<<<<< HEAD - flatbuffers::GeneratorOptions::kMAX, - "Generate wire format binaries for any data definitions", - flatbuffers::BinaryMakeRule }, - { flatbuffers::GenerateTextFile, "-t", "--json", "text", - flatbuffers::GeneratorOptions::kMAX, - "Generate text output for any data definitions", - flatbuffers::TextMakeRule }, - { flatbuffers::GenerateCPP, "-c", "--cpp", "C++", - flatbuffers::GeneratorOptions::kMAX, - "Generate C++ headers for tables/structs", - flatbuffers::CPPMakeRule }, - { flatbuffers::GenerateGo, "-g", "--go", "Go", - flatbuffers::GeneratorOptions::kGo, - "Generate Go files for tables/structs", - flatbuffers::GeneralMakeRule }, - { flatbuffers::GenerateGeneral, "-j", "--java", "Java", - flatbuffers::GeneratorOptions::kJava, - "Generate Java classes for tables/structs", - flatbuffers::GeneralMakeRule }, - { flatbuffers::GenerateJS, "-s", "--js", "JavaScript", - flatbuffers::GeneratorOptions::kMAX, - "Generate JavaScript code for tables/structs", - flatbuffers::JSMakeRule }, - { flatbuffers::GenerateGeneral, "-n", "--csharp", "C#", - flatbuffers::GeneratorOptions::kCSharp, - "Generate C# classes for tables/structs", - flatbuffers::GeneralMakeRule }, - { flatbuffers::GeneratePython, "-p", "--python", "Python", - flatbuffers::GeneratorOptions::kMAX, - "Generate Python files for tables/structs", - flatbuffers::GeneralMakeRule }, - { flatbuffers::GeneratePhp, nullptr, "--php", "PHP", - flatbuffers::GeneratorOptions::kMAX, -======= flatbuffers::IDLOptions::kMAX, "Generate wire format binaries for any data definitions", flatbuffers::BinaryMakeRule }, @@ -116,7 +76,6 @@ const Generator generators[] = { flatbuffers::GeneralMakeRule }, { flatbuffers::GeneratePhp, nullptr, "--php", "PHP", flatbuffers::IDLOptions::kMAX, ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 "Generate PHP files for tables/structs", flatbuffers::GeneralMakeRule }, { flatbuffers::GenerateGRPC, nullptr, "--grpc", "GRPC", @@ -270,11 +229,7 @@ int main(int argc, const char *argv[]) { } // Now process the files: -<<<<<<< HEAD - parser = new flatbuffers::Parser(opts.strict_json, proto_mode); -======= parser = new flatbuffers::Parser(opts); ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 for (auto file_it = filenames.begin(); file_it != filenames.end(); ++file_it) { @@ -310,23 +265,16 @@ int main(int argc, const char *argv[]) { } } } else { -<<<<<<< HEAD -======= // Check if file contains 0 bytes. if (contents.length() != strlen(contents.c_str())) { Error("input file appears to be binary: " + *file_it, true); } ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 if (flatbuffers::GetExtension(*file_it) == "fbs") { // If we're processing multiple schemas, make sure to start each // one from scratch. If it depends on previous schemas it must do // so explicitly using an include. delete parser; -<<<<<<< HEAD - parser = new flatbuffers::Parser(opts.strict_json, proto_mode); -======= parser = new flatbuffers::Parser(opts); ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 } auto local_include_directory = flatbuffers::StripFileName(*file_it); include_directories.push_back(local_include_directory.c_str()); @@ -350,11 +298,7 @@ int main(int argc, const char *argv[]) { if (generator_enabled[i]) { if (!print_make_rules) { flatbuffers::EnsureDirExists(output_path); -<<<<<<< HEAD - if (!generators[i].generate(*parser, output_path, filebase, opts)) { -======= if (!generators[i].generate(*parser, output_path, filebase)) { ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 Error(std::string("Unable to generate ") + generators[i].lang_name + " for " + @@ -362,11 +306,7 @@ int main(int argc, const char *argv[]) { } } else { std::string make_rule = generators[i].make_rule( -<<<<<<< HEAD - *parser, output_path, *file_it, opts); -======= *parser, output_path, *file_it); ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 if (!make_rule.empty()) printf("%s\n", flatbuffers::WordWrap( make_rule, 80, " ", " \\").c_str()); @@ -374,15 +314,11 @@ int main(int argc, const char *argv[]) { } } -<<<<<<< HEAD - if (proto_mode) GenerateFBS(*parser, output_path, filebase, opts); -======= if (opts.proto_mode) GenerateFBS(*parser, output_path, filebase); // We do not want to generate code for the definitions in this file // in any files coming up next. parser->MarkGenerated(); ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 } delete parser; diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index c8337af72..c690240d6 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -208,40 +208,6 @@ class CppGenerator : public BaseGenerator { // This tracks the current namespace so we can insert namespace declarations. const Namespace *cur_name_space_ = nullptr; -<<<<<<< HEAD -// Return a C++ type from the table in idl.h -static std::string GenTypeBasic(const Parser &parser, const Type &type, - bool user_facing_type) { - static const char *ctypename[] = { - #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \ - #CTYPE, - FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD) - #undef FLATBUFFERS_TD - }; - if (user_facing_type) { - if (type.enum_def) return WrapInNameSpace(parser, *type.enum_def); - if (type.base_type == BASE_TYPE_BOOL) return "bool"; - } - return ctypename[type.base_type]; -} - -static std::string GenTypeWire(const Parser &parser, const Type &type, - const char *postfix, bool user_facing_type); - -// Return a C++ pointer type, specialized to the actual struct/table types, -// and vector element types. -static std::string GenTypePointer(const Parser &parser, const Type &type) { - switch (type.base_type) { - case BASE_TYPE_STRING: - return "flatbuffers::String"; - case BASE_TYPE_VECTOR: - return "flatbuffers::Vector<" + - GenTypeWire(parser, type.VectorType(), "", false) + ">"; - case BASE_TYPE_STRUCT: { - return WrapInNameSpace(parser, *type.struct_def); - } - case BASE_TYPE_UNION: -======= const Namespace *CurrentNameSpace() { return cur_name_space_; } // Translates a qualified name in flatbuffer text format to the same name in @@ -283,25 +249,12 @@ static std::string GenTypePointer(const Parser &parser, const Type &type) { return WrapInNameSpace(*type.struct_def); } case BASE_TYPE_UNION: ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 // fall through default: return "void"; } } -<<<<<<< HEAD -// Return a C++ type for any type (scalar/pointer) specifically for -// building a flatbuffer. -static std::string GenTypeWire(const Parser &parser, const Type &type, - const char *postfix, bool user_facing_type) { - return IsScalar(type.base_type) - ? GenTypeBasic(parser, type, user_facing_type) + postfix - : IsStruct(type) - ? "const " + GenTypePointer(parser, type) + " *" - : "flatbuffers::Offset<" + GenTypePointer(parser, type) + ">" + postfix; -} -======= // Return a C++ type for any type (scalar/pointer) specifically for // building a flatbuffer. std::string GenTypeWire(const Type &type, @@ -313,7 +266,6 @@ static std::string GenTypeWire(const Parser &parser, const Type &type, : "flatbuffers::Offset<" + GenTypePointer(type) + ">" + postfix; } ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 // Return a C++ type for any type (scalar/pointer) that reflects its // serialized size. @@ -324,17 +276,6 @@ static std::string GenTypeWire(const Parser &parser, const Type &type, : "flatbuffers::uoffset_t"; } -<<<<<<< HEAD -// Return a C++ type for any type (scalar/pointer) specifically for -// using a flatbuffer. -static std::string GenTypeGet(const Parser &parser, const Type &type, - const char *afterbasic, const char *beforeptr, - const char *afterptr, bool user_facing_type) { - return IsScalar(type.base_type) - ? GenTypeBasic(parser, type, user_facing_type) + afterbasic - : beforeptr + GenTypePointer(parser, type) + afterptr; -} -======= // Return a C++ type for any type (scalar/pointer) specifically for // using a flatbuffer. std::string GenTypeGet(const Type &type, @@ -344,7 +285,6 @@ static std::string GenTypeGet(const Parser &parser, const Type &type, ? GenTypeBasic(type, user_facing_type) + afterbasic : beforeptr + GenTypePointer(type) + afterptr; } ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 static std::string GenEnumDecl(const EnumDef &enum_def, const IDLOptions &opts) { @@ -387,20 +327,6 @@ static std::string GenTypeGet(const Parser &parser, const Type &type, EnumVal *minv = nullptr, *maxv = nullptr; for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); ++it) { -<<<<<<< HEAD - while (val++ != (*it)->value) code += "\"\", "; - code += "\"" + (*it)->name + "\", "; - } - code += "nullptr };\n return names;\n}\n\n"; - code += "inline const char *EnumName" + enum_def.name; - code += "(" + enum_def.name + " e) { return EnumNames" + enum_def.name; - code += "()[static_cast(e)"; - if (enum_def.vals.vec.front()->value) { - code += " - static_cast("; - code += GetEnumVal(enum_def, *enum_def.vals.vec.front(), opts) +")"; - } - code += "]; }\n\n"; -======= auto &ev = **it; GenComment(ev.doc_comment, code_ptr, nullptr, " "); code += " " + GenEnumVal(enum_def, ev.name, parser_.opts) + " = "; @@ -464,7 +390,6 @@ static std::string GenTypeGet(const Parser &parser, const Type &type, if (enum_def.is_union) { code += EnumSignature(enum_def) + ";\n\n"; } ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 } void GenEnumPost(EnumDef &enum_def, std::string *code_ptr_post) { @@ -490,90 +415,6 @@ static std::string GenTypeGet(const Parser &parser, const Type &type, code_post += " default: return false;\n }\n}\n\n"; } -<<<<<<< HEAD -// Generates a value with optionally a cast applied if the field has a -// different underlying type from its interface type (currently only the -// case for enums. "from" specify the direction, true meaning from the -// underlying type to the interface type. -std::string GenUnderlyingCast(const Parser &parser, const FieldDef &field, - bool from, const std::string &val) { - return (field.value.type.enum_def && IsScalar(field.value.type.base_type)) || - field.value.type.base_type == BASE_TYPE_BOOL - ? "static_cast<" + GenTypeBasic(parser, field.value.type, from) + ">(" + - val + ")" - : val; -} - -std::string GenFieldOffsetName(const FieldDef &field) { - std::string uname = field.name; - std::transform(uname.begin(), uname.end(), uname.begin(), ::toupper); - return "VT_" + uname; -} - -// Generate an accessor struct, builder structs & function for a table. -static void GenTable(const Parser &parser, StructDef &struct_def, - const GeneratorOptions &opts, std::string *code_ptr) { - if (struct_def.generated) return; - std::string &code = *code_ptr; - - // Generate an accessor struct, with methods of the form: - // type name() const { return GetField(offset, defaultval); } - GenComment(struct_def.doc_comment, code_ptr, nullptr); - code += "struct " + struct_def.name; - code += " FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table"; - code += " {\n"; - // Generate field id constants. - code += " enum {\n"; - for (auto it = struct_def.fields.vec.begin(); - it != struct_def.fields.vec.end(); - ++it) { - auto &field = **it; - if (!field.deprecated) { // Deprecated fields won't be accessible. - code += " " + GenFieldOffsetName(field) + " = "; - code += NumToString(field.value.offset) + ",\n"; - } - } - code += " };\n"; - // Generate the accessors. - for (auto it = struct_def.fields.vec.begin(); - it != struct_def.fields.vec.end(); - ++it) { - auto &field = **it; - if (!field.deprecated) { // Deprecated fields won't be accessible. - auto is_scalar = IsScalar(field.value.type.base_type); - GenComment(field.doc_comment, code_ptr, nullptr, " "); - code += " " + GenTypeGet(parser, field.value.type, " ", "const ", " *", - true); - code += field.name + "() const { return "; - // Call a different accessor for pointers, that indirects. - auto accessor = is_scalar - ? "GetField<" - : (IsStruct(field.value.type) ? "GetStruct<" : "GetPointer<"); - auto offsetstr = GenFieldOffsetName(field); - auto call = - accessor + - GenTypeGet(parser, field.value.type, "", "const ", " *", false) + - ">(" + offsetstr; - // Default value as second arg for non-pointer types. - if (IsScalar(field.value.type.base_type)) - call += ", " + field.value.constant; - call += ")"; - code += GenUnderlyingCast(parser, field, true, call); - code += "; }\n"; - if (opts.mutable_buffer) { - if (is_scalar) { - code += " bool mutate_" + field.name + "("; - code += GenTypeBasic(parser, field.value.type, true); - code += " _" + field.name + ") { return SetField(" + offsetstr + ", "; - code += GenUnderlyingCast(parser, field, false, "_" + field.name); - code += "); }\n"; - } else { - auto type = GenTypeGet(parser, field.value.type, " ", "", " *", true); - code += " " + type + "mutable_" + field.name + "() { return "; - code += GenUnderlyingCast(parser, field, true, - accessor + type + ">(" + offsetstr + ")"); - code += "; }\n"; -======= // Generates a value with optionally a cast applied if the field has a // different underlying type from its interface type (currently only the // case for enums. "from" specify the direction, true meaning from the @@ -646,7 +487,6 @@ static void GenTable(const Parser &parser, StructDef &struct_def, } code += " " + GenFieldOffsetName(field) + " = "; code += NumToString(field.value.offset); ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 } } code += "\n };\n"; @@ -732,42 +572,6 @@ static void GenTable(const Parser &parser, StructDef &struct_def, } } } -<<<<<<< HEAD - } - // Generate a verifier function that can check a buffer from an untrusted - // source will never cause reads outside the buffer. - code += " bool Verify(flatbuffers::Verifier &verifier) const {\n"; - code += " return VerifyTableStart(verifier)"; - std::string prefix = " &&\n "; - for (auto it = struct_def.fields.vec.begin(); - it != struct_def.fields.vec.end(); - ++it) { - auto &field = **it; - if (!field.deprecated) { - code += prefix + "VerifyField"; - if (field.required) code += "Required"; - code += "<" + GenTypeSize(parser, field.value.type); - code += ">(verifier, " + GenFieldOffsetName(field) + ")"; - switch (field.value.type.base_type) { - case BASE_TYPE_UNION: - code += prefix + "Verify" + field.value.type.enum_def->name; - code += "(verifier, " + field.name + "(), " + field.name + "_type())"; - break; - case BASE_TYPE_STRUCT: - if (!field.value.type.struct_def->fixed) { - code += prefix + "verifier.VerifyTable(" + field.name; - code += "())"; - } - break; - case BASE_TYPE_STRING: - code += prefix + "verifier.Verify(" + field.name + "())"; - break; - case BASE_TYPE_VECTOR: - code += prefix + "verifier.Verify(" + field.name + "())"; - switch (field.value.type.element) { - case BASE_TYPE_STRING: { - code += prefix + "verifier.VerifyVectorOfStrings(" + field.name; -======= // Generate a verifier function that can check a buffer from an untrusted // source will never cause reads outside the buffer. code += " bool Verify(flatbuffers::Verifier &verifier) const {\n"; @@ -790,7 +594,6 @@ static void GenTable(const Parser &parser, StructDef &struct_def, case BASE_TYPE_STRUCT: if (!field.value.type.struct_def->fixed) { code += prefix + "verifier.VerifyTable(" + field.name; ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 code += "())"; } break; @@ -853,32 +656,6 @@ static void GenTable(const Parser &parser, StructDef &struct_def, code += ", " + GenDefaultConstant(field); code += "); }\n"; } -<<<<<<< HEAD - code += "(" + struct_def.name + "::" + GenFieldOffsetName(field) + ", "; - code += GenUnderlyingCast(parser, field, false, field.name); - if (IsScalar(field.value.type.base_type)) - code += ", " + field.value.constant; - code += "); }\n"; - } - } - code += " " + struct_def.name; - code += "Builder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) "; - code += "{ start_ = fbb_.StartTable(); }\n"; - code += " " + struct_def.name + "Builder &operator=(const "; - code += struct_def.name + "Builder &);\n"; - code += " flatbuffers::Offset<" + struct_def.name; - code += "> Finish() {\n auto o = flatbuffers::Offset<" + struct_def.name; - code += ">(fbb_.EndTable(start_, "; - code += NumToString(struct_def.fields.vec.size()) + "));\n"; - for (auto it = struct_def.fields.vec.begin(); - it != struct_def.fields.vec.end(); - ++it) { - auto &field = **it; - if (!field.deprecated && field.required) { - code += " fbb_.Required(o, "; - code += struct_def.name + "::" + GenFieldOffsetName(field); - code += "); // " + field.name + "\n"; -======= } code += " " + struct_def.name; code += "Builder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) "; @@ -897,7 +674,6 @@ static void GenTable(const Parser &parser, StructDef &struct_def, code += struct_def.name + "::" + GenFieldOffsetName(field); code += "); // " + field.name + "\n"; } ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 } code += " return o;\n }\n};\n\n"; @@ -928,13 +704,6 @@ static void GenTable(const Parser &parser, StructDef &struct_def, } else { code += GenDefaultConstant(field); } -<<<<<<< HEAD - } else if (field.value.type.base_type == BASE_TYPE_BOOL) { - code += field.value.constant == "0" ? "false" : "true"; - } else { - code += field.value.constant; -======= ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 } } code += ") {\n " + struct_def.name + "Builder builder_(_fbb);\n"; @@ -952,118 +721,6 @@ static void GenTable(const Parser &parser, StructDef &struct_def, code += " return builder_.Finish();\n}\n\n"; } -<<<<<<< HEAD -static void GenPadding(const FieldDef &field, - const std::function &f) { - if (field.padding) { - for (int i = 0; i < 4; i++) - if (static_cast(field.padding) & (1 << i)) - f((1 << i) * 8); - assert(!(field.padding & ~0xF)); - } -} - -// Generate an accessor struct with constructor for a flatbuffers struct. -static void GenStruct(const Parser &parser, StructDef &struct_def, - const GeneratorOptions &opts, std::string *code_ptr) { - if (struct_def.generated) return; - std::string &code = *code_ptr; - - // Generate an accessor struct, with private variables of the form: - // type name_; - // Generates manual padding and alignment. - // Variables are private because they contain little endian data on all - // platforms. - GenComment(struct_def.doc_comment, code_ptr, nullptr); - code += "MANUALLY_ALIGNED_STRUCT(" + NumToString(struct_def.minalign) + ") "; - code += struct_def.name + " FLATBUFFERS_FINAL_CLASS {\n private:\n"; - int padding_id = 0; - for (auto it = struct_def.fields.vec.begin(); - it != struct_def.fields.vec.end(); - ++it) { - auto &field = **it; - code += " " + GenTypeGet(parser, field.value.type, " ", "", " ", false); - code += field.name + "_;\n"; - GenPadding(field, [&code, &padding_id](int bits) { - code += " int" + NumToString(bits) + - "_t __padding" + NumToString(padding_id++) + ";\n"; - }); - } - - // Generate a constructor that takes all fields as arguments. - code += "\n public:\n " + struct_def.name + "("; - for (auto it = struct_def.fields.vec.begin(); - it != struct_def.fields.vec.end(); - ++it) { - auto &field = **it; - if (it != struct_def.fields.vec.begin()) code += ", "; - code += GenTypeGet(parser, field.value.type, " ", "const ", " &", true); - code += "_" + field.name; - } - code += ")\n : "; - padding_id = 0; - for (auto it = struct_def.fields.vec.begin(); - it != struct_def.fields.vec.end(); - ++it) { - auto &field = **it; - if (it != struct_def.fields.vec.begin()) code += ", "; - code += field.name + "_("; - if (IsScalar(field.value.type.base_type)) { - code += "flatbuffers::EndianScalar("; - code += GenUnderlyingCast(parser, field, false, "_" + field.name); - code += "))"; - } else { - code += "_" + field.name + ")"; - } - GenPadding(field, [&code, &padding_id](int bits) { - (void)bits; - code += ", __padding" + NumToString(padding_id++) + "(0)"; - }); - } - code += " {"; - padding_id = 0; - for (auto it = struct_def.fields.vec.begin(); - it != struct_def.fields.vec.end(); - ++it) { - auto &field = **it; - GenPadding(field, [&code, &padding_id](int bits) { - (void)bits; - code += " (void)__padding" + NumToString(padding_id++) + ";"; - }); - } - code += " }\n\n"; - - // Generate accessor methods of the form: - // type name() const { return flatbuffers::EndianScalar(name_); } - for (auto it = struct_def.fields.vec.begin(); - it != struct_def.fields.vec.end(); - ++it) { - auto &field = **it; - GenComment(field.doc_comment, code_ptr, nullptr, " "); - auto is_scalar = IsScalar(field.value.type.base_type); - code += " " + GenTypeGet(parser, field.value.type, " ", "const ", " &", - true); - code += field.name + "() const { return "; - code += GenUnderlyingCast(parser, field, true, - is_scalar - ? "flatbuffers::EndianScalar(" + field.name + "_)" - : field.name + "_"); - code += "; }\n"; - if (opts.mutable_buffer) { - if (is_scalar) { - code += " void mutate_" + field.name + "("; - code += GenTypeBasic(parser, field.value.type, true); - code += " _" + field.name + ") { flatbuffers::WriteScalar(&"; - code += field.name + "_, "; - code += GenUnderlyingCast(parser, field, false, "_" + field.name); - code += "); }\n"; - } else { - code += " "; - code += GenTypeGet(parser, field.value.type, "", "", " &", true); - code += "mutable_" + field.name + "() { return " + field.name; - code += "_; }\n"; - } -======= static void GenPadding(const FieldDef &field, std::string &code, int &padding_id, const std::function(field.padding) & (1 << i)) f((1 << i) * 8, code, padding_id); assert(!(field.padding & ~0xF)); ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 } } diff --git a/src/idl_gen_grpc.cpp b/src/idl_gen_grpc.cpp index 0dcc316d6..2bf0e911d 100644 --- a/src/idl_gen_grpc.cpp +++ b/src/idl_gen_grpc.cpp @@ -112,7 +112,7 @@ class FlatBufPrinter : public grpc_cpp_generator::Printer { // Current indentation. str_->insert(str_->end(), indent_ * 2, ' '); // See if this contains more than one line. - auto lf = strchr(s, '\n'); + const char * lf = strchr(s, '\n'); if (lf) { (*str_) += std::string(s, lf + 1); s = lf + 1; diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index 36a63c6d0..b758e9592 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -1167,11 +1167,7 @@ CheckedError Parser::ParseEnum(bool is_union, EnumDef **dest) { EXPECT(kTokenIdentifier); auto &enum_def = *new EnumDef(); enum_def.name = enum_name; -<<<<<<< HEAD - enum_def.file = files_being_parsed_; -======= enum_def.file = file_being_parsed_; ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 enum_def.doc_comment = enum_comment; enum_def.is_union = is_union; enum_def.defined_namespace = namespaces_.back(); @@ -1268,11 +1264,7 @@ CheckedError Parser::StartStruct(const std::string &name, StructDef **dest) { if (!struct_def.predecl) return Error("datatype already exists: " + name); struct_def.predecl = false; struct_def.name = name; -<<<<<<< HEAD - struct_def.file = files_being_parsed_; -======= struct_def.file = file_being_parsed_; ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 // Move this struct to the back of the vector just in case it was predeclared, // to preserve declaration order. *remove(structs_.vec.begin(), structs_.vec.end(), &struct_def) = &struct_def; @@ -1787,16 +1779,12 @@ CheckedError Parser::SkipJsonString() { bool Parser::Parse(const char *source, const char **include_paths, const char *source_filename) { -<<<<<<< HEAD - files_being_parsed_ = source_filename ? source_filename : ""; -======= return !DoParse(source, include_paths, source_filename).Check(); } CheckedError Parser::DoParse(const char *source, const char **include_paths, const char *source_filename) { file_being_parsed_ = source_filename ? source_filename : ""; ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 if (source_filename && included_files_.find(source_filename) == included_files_.end()) { included_files_[source_filename] = true; @@ -1937,23 +1925,8 @@ CheckedError Parser::DoParse(const char *source, const char **include_paths, return Error("only tables can be union elements: " + val.name); } } -<<<<<<< HEAD - } catch (const std::string &msg) { - error_ = source_filename ? AbsolutePath(source_filename) : ""; - #ifdef _WIN32 - error_ += "(" + NumToString(line_) + ")"; // MSVC alike - #else - if (source_filename) error_ += ":"; - error_ += NumToString(line_) + ":0"; // gcc alike - #endif - error_ += ": error: " + msg; - return false; - } - return true; -======= } return NoError(); ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 } std::set Parser::GetIncludedFilesRecursive( diff --git a/tests/generate_code.sh b/tests/generate_code.sh index eca73aa42..3436d8586 100644 --- a/tests/generate_code.sh +++ b/tests/generate_code.sh @@ -1,7 +1,3 @@ -<<<<<<< HEAD -../flatc --cpp --java --csharp --go --binary --python --js --php --gen-mutable --no-includes monster_test.fbs monsterdata_test.json -../flatc --binary --schema monster_test.fbs -======= #!/bin/bash # # Copyright 2015 Google Inc. All rights reserved. @@ -22,4 +18,3 @@ ../flatc --cpp --java --csharp --go --binary --python --js --php --gen-mutable -o namespace_test namespace_test/namespace_test1.fbs namespace_test/namespace_test2.fbs ../flatc --binary --schema monster_test.fbs ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 diff --git a/tests/monster_test.bfbs b/tests/monster_test.bfbs index e5f51face696a1ee9b3aa64b70bba83df1c4645a..e131ac8c2ee97914bfc1c02ce683ac4f5ecb43d6 100644 GIT binary patch literal 3152 zcmaJ@PiS0a5T8vp$!`8^(i+<}jS@>KMMTmx1d$wKY+41Af{8J=bd$VwSKhu|cHf4^ zlXxg1QaniT&_fSB6e;4N^b|@dB1NPG3F4^)Pab;c#b|5o^Y@$CZ}(Y(AIzKYd-I$5 zX6Bo3-WwK?=`+*wU>F(6N>WnND+4l!mEb8d3Yr9+1}(LTbb){0Bl0fhTPcww=6<)x zF8D%Pq#bhV3@3mCB{82pH>Z10Rw@U0W?-8;bzv{AbR>GgxE=u!`)07A`dgTP4O#$E zzWGNKfx)`Sk5LqVu5(%iYZ z88BIa&y1CRkAONrZ3Z=O%4ZwL4z@`mD>Tezk8{3(%3kYU-gjq@3j*Y(_K z(?)8Fyora+TMc{`JM)$#RZ6Qicor|nMAJ@i@yNqRFQ=y{<$2T3Jb33{wr>SIi;8lw z&{8axm2%my;gwu^qOr7H0sgRzHSq@;_(HB$s85V9=Y#c=IlRl8{qAk>L%8+MejHzm z;WrJRmPb+ayu7wAzJ6+~Y5xbqr$&Jvlvnq~*JAiR!*`tr{yBN2iFfhF{b#(#fdlLQ z68s>l+xZ{IJO7DaG<^Ct_yIY-FW&i2{6)j39)b_Zv3>E*f8uW%zH8)h6!ptd#j{7y zFP;|lQtSxUv`d=-$Xm<%xp1YP z&uep(P4q0(bn+T>dbI|vU$C~kz$?-Akk-BQo&a;)+(FQ{y^?F^L%v${f;FGV zB9AS$Hl7P{)<06Zzl)K)QZ1INMJ*Ez?j+u2u}^$&b|vm_dnXd@>TfHb0luyfOJMQ# z(RbIcG`SRFP`?clzGLpzKpd>gLPg|T)9;A)yJz(i@WthQQ?>2Eb1mfpwjT=I@eB3@ac{;0SN(pyw4a;rISLURMjqjLVpRadO65N+q@(~dIUG!G*buo(t z@JSHmw?UUd7eLhSXxvTbJMTRe@0)GCFJEDv)bne_NE|*#(y93rt>-P^+BKH{;#fcH zX0LY_+~tcAGru2$tgqk~0`?M!z~zc^7Vk~d@A(}(E@K=&H)MCgY4aF}djI#IMmg_@ zt4n5zIdH?}-sP0J=V;w!_7>g%cN;AS1DYrMdu9DJY&Oq`c6dId1>_J+Ykdja{Nep} z-*rlD(4U0f6ssZnzXH0~$Ui%SWzF#JNj>I^zECHp_tZLv^+f3yfS0X|o6pRn(;?dZ ZvtkN#{13eqV_b)o{C-fc>2D+E{{r&+C1U^p literal 2744 zcmaJ@O^91n6h4{!|4fqBF&(9pAVVodhITp(B1P&*>kl>sCr-hIoi~#g6G`3+$xCQl zhzl39Dy{}sBBc~@DXzK@aUs%05f??Il;TRda5-qLjo)|fyP1auFPxm0d%tt;ch0%z zzIRka)-SAYKw%VRQ4*4of|O+e{{+7x&qH2-oP!kRL{iWR=-1LBUt~qfkT-K8_wYZC zHR2yYc4NRnU%XUP|6J?(9=tE;9%<}Sz#K-5x(qoAA?{^wtoma-Z$UW!aDOs^f>(~5 zaxK6UwpO0I84=K$jw5)gyMth(-R*VUy0hJJFSmo%d2iUoN)9#&>~HM3_bGS|oeM&d zi@rAq+`i-{lL_OX)GpP|L&+BA$eH=6LXwbI{Epv;CjS=XEQEe<{h_?$^!+RBI0vfJ zLBH#rp=&vDjWH1yjfZ=@+IL+Kt7WZ|!I6jM&As1p8(KkD5*5=N6SD|m9`le3eZTv< zx6>ImT(#x=8-{1r?3sf#nVCKKL{6d2rqdDG2j1p@`$u#34E}!&GM~cP5{!2tKjZ(m z&~?afkaPH7fNVi{zJ37q6UaR}gT4X%0FP&(uR=GVKZkw{AwC1lO>kJ0RgIJ}FlPa3 z?)3Z4*l%w8erJFw@&0BFigR~?KO!qr{Gkxvat5v8+Uk|2H#+TL?#Z88vs?nHTX;u_yu$fD@FcMK7$DUn!%^{fPYF(PVqM0D1W$- zZwx;7J@AL+#7w;9PyFv8|9#*O$?=(Z%b)mTe`&m#VJflpqZRa~?_;rR-9sqlbZH-1aB zc@Z|eo#=ZBVrv@O41#{!dk@`|!A;7ZVPApMd>?jMsi+-$m7EyIDi8(5n!5_v5{|xH z2R?&nZ478B#Y4%mo_7sRZUdK=!-`{WY%Z?^?*lN8K|W4!cj2l?!T zcic5&qj%a#6WAgEqrWO^D zeUcSD9R+Y_KI|aR7-npK_Ur;Q^WSwmULW!j@(7jm^jz4R*t{Eh{y^@S^%C_L0?wo;Ae@Nf-z4s%YZ$LhR&^{UV5pBtvXPbt< zm_77NkAO>ry%X($73~3@6?<+(&)!Yo;~LRHAJgw?Aipzy3%7x>Ib+;B=pR7%E{4iK z#Ci;oFkh@I*LiL;+MTvPC4V0{n^R)0Lg-uaMKCzX9lx8n&k}^V|N9Q%I(vp4z#GHV zg&8u#)`c@w2dxUDHnh diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 5038edd10..50fbc28ce 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -138,11 +138,7 @@ namespace Example { struct TestSimpleTableWithEnum FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { enum { -<<<<<<< HEAD - VT_COLOR = 4, -======= VT_COLOR = 4 ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 }; Color color() const { return static_cast(GetField(VT_COLOR, 2)); } bool mutate_color(Color _color) { return SetField(VT_COLOR, static_cast(_color)); } @@ -176,11 +172,7 @@ struct Stat FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { enum { VT_ID = 4, VT_VAL = 6, -<<<<<<< HEAD - VT_COUNT = 8, -======= VT_COUNT = 8 ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 }; const flatbuffers::String *id() const { return GetPointer(VT_ID); } flatbuffers::String *mutable_id() { return GetPointer(VT_ID); } @@ -250,13 +242,10 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { VT_TESTHASHS64_FNV1A = 48, VT_TESTHASHU64_FNV1A = 50, VT_TESTARRAYOFBOOLS = 52, -<<<<<<< HEAD -======= VT_TESTF = 54, VT_TESTF2 = 56, VT_TESTF3 = 58, VT_TESTARRAYOFSTRING2 = 60 ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 }; const Vec3 *pos() const { return GetStruct(VT_POS); } Vec3 *mutable_pos() { return GetStruct(VT_POS); } @@ -291,11 +280,7 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { const MyGame::Example::Monster *testnestedflatbuffer_nested_root() const { return flatbuffers::GetRoot(testnestedflatbuffer()->Data()); } const Stat *testempty() const { return GetPointer(VT_TESTEMPTY); } Stat *mutable_testempty() { return GetPointer(VT_TESTEMPTY); } -<<<<<<< HEAD - bool testbool() const { return static_cast(GetField(VT_TESTBOOL, 0)); } -======= bool testbool() const { return GetField(VT_TESTBOOL, 0) != 0; } ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 bool mutate_testbool(bool _testbool) { return SetField(VT_TESTBOOL, static_cast(_testbool)); } 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); } @@ -315,8 +300,6 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool mutate_testhashu64_fnv1a(uint64_t _testhashu64_fnv1a) { return SetField(VT_TESTHASHU64_FNV1A, _testhashu64_fnv1a); } const flatbuffers::Vector *testarrayofbools() const { return GetPointer *>(VT_TESTARRAYOFBOOLS); } flatbuffers::Vector *mutable_testarrayofbools() { return GetPointer *>(VT_TESTARRAYOFBOOLS); } -<<<<<<< HEAD -======= float testf() const { return GetField(VT_TESTF, 3.14159f); } bool mutate_testf(float _testf) { return SetField(VT_TESTF, _testf); } float testf2() const { return GetField(VT_TESTF2, 3.0f); } @@ -325,7 +308,6 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool mutate_testf3(float _testf3) { return SetField(VT_TESTF3, _testf3); } const flatbuffers::Vector> *testarrayofstring2() const { return GetPointer> *>(VT_TESTARRAYOFSTRING2); } flatbuffers::Vector> *mutable_testarrayofstring2() { return GetPointer> *>(VT_TESTARRAYOFSTRING2); } ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField(verifier, VT_POS) && @@ -401,12 +383,6 @@ struct MonsterBuilder { 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); } -<<<<<<< HEAD - MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } - MonsterBuilder &operator=(const MonsterBuilder &); - flatbuffers::Offset Finish() { - auto o = flatbuffers::Offset(fbb_.EndTable(start_, 25)); -======= 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); } @@ -415,7 +391,6 @@ struct MonsterBuilder { MonsterBuilder &operator=(const MonsterBuilder &); flatbuffers::Offset Finish() { auto o = flatbuffers::Offset(fbb_.EndTable(start_, 29)); ->>>>>>> 48f37f9e0a04f2b60046dda7fef20a8b0ebc1a70 fbb_.Required(o, Monster::VT_NAME); // name return o; }