From b49a7d146d1d30e44714c2d8b0f352bb6c8e0ce8 Mon Sep 17 00:00:00 2001 From: artiom Date: Tue, 8 Mar 2016 23:57:12 +0000 Subject: [PATCH 1/6] Update google user group link Original link returns 404. --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 4806f9d07..954604863 100755 --- a/readme.md +++ b/readme.md @@ -47,7 +47,7 @@ you would leave it in. [CONTRIBUTING]: http://github.com/google/flatbuffers/blob/master/CONTRIBUTING [`flatbuffers` tag]: https://stackoverflow.com/questions/tagged/flatbuffers - [FlatBuffers Google Group]: http://group.google.com/group/flatbuffers + [FlatBuffers Google Group]: https://groups.google.com/forum/#!forum/flatbuffers [FlatBuffers Issues Tracker]: http://github.com/google/flatbuffers/issues [stackoverflow.com]: http://www.stackoverflow.com [landing page]: http://google.github.io/flatbuffers From 68c69b3717976565333cc03e3f15ee919969656d Mon Sep 17 00:00:00 2001 From: Alex McGuire Date: Sun, 20 Mar 2016 12:55:05 +0000 Subject: [PATCH 2/6] Tutorial's Java example uses incorrect types `FlatBufferBuilder.createString` returns an int offset, not a string --- docs/source/Tutorial.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/source/Tutorial.md b/docs/source/Tutorial.md index 74941be8c..de1e9fd95 100644 --- a/docs/source/Tutorial.md +++ b/docs/source/Tutorial.md @@ -432,10 +432,10 @@ our `orc` Monster, lets create some `Weapon`s: a `Sword` and an `Axe`.
~~~{.java} - String weaponOneName = builder.createString("Sword") + int weaponOneName = builder.createString("Sword") short weaponOneDamage = 3; - String weaponTwoName = builder.createString("Axe"); + int weaponTwoName = builder.createString("Axe"); short weaponTwoDamage = 5; // Use the `createWeapon()` helper function to create the weapons, since we set every field. From 28a3c939e7f2083b79254ff90cfde77e51908bc8 Mon Sep 17 00:00:00 2001 From: Armen Baghumian Date: Mon, 14 Mar 2016 17:37:22 +1100 Subject: [PATCH 3/6] Implement __vector_as_bytes and methods to get [ubyte] efficiently Currently in order to get a value type of [ubyte] in PHP, iteration is necessary which is not efficient. Helper __vector_as_bytes has been implemented in order to return the byte arrays in PHP efficiently. Appropriate methods also been added to use aforementioned method to return the byte array. (e.g. get*Bytes()). The reason the methods are named get*Bytes() and not for instance get*ByteArray() is the fact that PHP doesn't support byte arrays and the binary safe string implementation in PHP is used to simulate byte arrays and since there is chance for PHP users to confuse this with PHP arrays the name get*Bytes() has been chosen. In the future __vector_as_bytebuffer() method can also be implemented to return PHP implementation of ByteBuffer. --- php/Table.php | 12 +++++++++--- src/idl_gen_php.cpp | 21 ++++++++++++++++++++- tests/MyGame/Example/Monster.php | 16 ++++++++++++++++ tests/phpTest.php | 1 + 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/php/Table.php b/php/Table.php index 7f6114560..6f917c1af 100644 --- a/php/Table.php +++ b/php/Table.php @@ -89,9 +89,15 @@ abstract class Table return $offset + $this->bb->getInt($offset) + Constants::SIZEOF_INT; } -// protected function __vector_as_bytebuffer($vector_offset, $elem_size) -// { -// } + protected function __vector_as_bytes($vector_offset, $elem_size=1) + { + $o = $this->__offset($vector_offset); + if ($o == 0) { + return null; + } + + return substr($this->bb->_buffer, $this->__vector($o), $this->__vector_len($o) * $elem_size); + } /** * @param Table $table diff --git a/src/idl_gen_php.cpp b/src/idl_gen_php.cpp index 2488b1d18..5b904a077 100644 --- a/src/idl_gen_php.cpp +++ b/src/idl_gen_php.cpp @@ -165,6 +165,22 @@ namespace php { code += Indent + "}\n\n"; } + // Get a [ubyte] vector as a byte array. + static void GetUByte(const FieldDef &field, + std::string *code_ptr) { + std::string &code = *code_ptr; + + code += Indent + "/**\n"; + code += Indent + " * @return string\n"; + code += Indent + " */\n"; + code += Indent + "public function get"; + code += MakeCamel(field.name) + "Bytes()\n"; + code += Indent + "{\n"; + code += Indent + Indent + "return $this->__vector_as_bytes("; + code += NumToString(field.value.offset) + ");\n"; + code += Indent + "}\n\n"; + } + // Get the value of a struct's scalar. static void GetScalarFieldOfStruct(const FieldDef &field, std::string *code_ptr) { @@ -250,7 +266,7 @@ namespace php { ");\n"; code += Indent + Indent; code += "return $o != 0 ? $obj->init("; - if (field.value.type.struct_def->fixed) + if (field.value.type.struct_def->fixed) { code += "$o + $this->bb_pos, $this->bb) : "; } else { @@ -690,6 +706,9 @@ namespace php { } if (field.value.type.base_type == BASE_TYPE_VECTOR) { GetVectorLen(field, code_ptr); + if (field.value.type.element == BASE_TYPE_UCHAR) { + GetUByte(field, code_ptr); + } } } diff --git a/tests/MyGame/Example/Monster.php b/tests/MyGame/Example/Monster.php index 6c1d34a0b..48b9c7df2 100644 --- a/tests/MyGame/Example/Monster.php +++ b/tests/MyGame/Example/Monster.php @@ -98,6 +98,14 @@ class Monster extends Table return $o != 0 ? $this->__vector_len($o) : 0; } + /** + * @return string + */ + public function getInventoryBytes() + { + return $this->__vector_as_bytes(14); + } + /** * @return sbyte */ @@ -210,6 +218,14 @@ class Monster extends Table return $o != 0 ? $this->__vector_len($o) : 0; } + /** + * @return string + */ + public function getTestnestedflatbufferBytes() + { + return $this->__vector_as_bytes(30); + } + public function getTestempty() { $obj = new Stat(); diff --git a/tests/phpTest.php b/tests/phpTest.php index 0afc0af7f..4dc83b28d 100644 --- a/tests/phpTest.php +++ b/tests/phpTest.php @@ -128,6 +128,7 @@ function test_buffer(Assert $assert, Google\FlatBuffers\ByteBuffer $bb) { } $assert->strictEqual($invsum, 10); + $assert->strictEqual(bin2hex($monster->GetInventoryBytes()), "0001020304"); $test_0 = $monster->GetTest4(0); $test_1 = $monster->GetTest4(1); From 35f6bb506018976a923b67fb14dd1eb5e30e08b9 Mon Sep 17 00:00:00 2001 From: Nnamdi Date: Tue, 29 Mar 2016 22:25:04 +0100 Subject: [PATCH 4/6] Added --gen-name-strings command line option. To support the use case described in issue google/flatbuffers#3826, a new command line option --gen-name-strings has been added, which will cause a static GetFullyQualifiedName function to be added to the C++ output for tables/structs. --- include/flatbuffers/flatbuffers.h | 8 +++++ include/flatbuffers/idl.h | 2 ++ src/flatc.cpp | 53 ++++++++++++++++--------------- src/idl_gen_cpp.cpp | 16 +++++++++- 4 files changed, 53 insertions(+), 26 deletions(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index ff9f7269d..e08e96118 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -91,6 +91,14 @@ #else #define FLATBUFFERS_FINAL_CLASS #endif + +#if (!defined(_MSC_VER) || _MSC_VER >= 1900) && \ + (!defined(__GNUC__) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 406)) + #define FLATBUFFERS_CONSTEXPR constexpr +#else + #define FLATBUFFERS_CONSTEXPR +#endif + /// @endcond /// @file diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index e16d3ca90..3229576c9 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -321,6 +321,7 @@ struct IDLOptions { bool proto_mode; bool generate_all; bool skip_unexpected_fields_in_json; + bool generate_name_strings; // Possible options for the more general generator below. enum Language { kJava, kCSharp, kGo, kMAX }; @@ -339,6 +340,7 @@ struct IDLOptions { proto_mode(false), generate_all(false), skip_unexpected_fields_in_json(false), + generate_name_strings(false), lang(IDLOptions::kJava) {} }; diff --git a/src/flatc.cpp b/src/flatc.cpp index c0696dd46..d4be151c8 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -96,31 +96,32 @@ static void Error(const std::string &err, bool usage, bool show_exe_name) { : " ", generators[i].generator_help); printf( - " -o PATH Prefix PATH to all generated files.\n" - " -I PATH Search for includes in the specified path.\n" - " -M Print make rules for generated files.\n" - " --version Print the version number of flatc and exit.\n" - " --strict-json Strict JSON: field names must be / will be quoted,\n" - " no trailing commas in tables/vectors.\n" - " --defaults-json Output fields whose value is the default when\n" - " writing JSON\n" - " --unknown-json Allow fields in JSON that are not defined in the\n" - " schema. These fields will be discared when generating\n" - " binaries.\n" - " --no-prefix Don\'t prefix enum values with the enum type in C++.\n" - " --scoped-enums Use C++11 style scoped and strongly typed enums.\n" - " also implies --no-prefix.\n" - " --gen-includes (deprecated), this is the default behavior.\n" - " If the original behavior is required (no include\n" - " statements) use --no-includes.\n" - " --no-includes Don\'t generate include statements for included\n" - " schemas the generated file depends on (C++).\n" - " --gen-mutable Generate accessors that can mutate buffers in-place.\n" - " --gen-onefile Generate single output file for C#\n" - " --raw-binary Allow binaries without file_indentifier to be read.\n" - " This may crash flatc given a mismatched schema.\n" - " --proto Input is a .proto, translate to .fbs.\n" - " --schema Serialize schemas instead of JSON (use with -b)\n" + " -o PATH Prefix PATH to all generated files.\n" + " -I PATH Search for includes in the specified path.\n" + " -M Print make rules for generated files.\n" + " --version Print the version number of flatc and exit.\n" + " --strict-json Strict JSON: field names must be / will be quoted,\n" + " no trailing commas in tables/vectors.\n" + " --defaults-json Output fields whose value is the default when\n" + " writing JSON\n" + " --unknown-json Allow fields in JSON that are not defined in the\n" + " schema. These fields will be discared when generating\n" + " binaries.\n" + " --no-prefix Don\'t prefix enum values with the enum type in C++.\n" + " --scoped-enums Use C++11 style scoped and strongly typed enums.\n" + " also implies --no-prefix.\n" + " --gen-includes (deprecated), this is the default behavior.\n" + " If the original behavior is required (no include\n" + " statements) use --no-includes.\n" + " --no-includes Don\'t generate include statements for included\n" + " schemas the generated file depends on (C++).\n" + " --gen-mutable Generate accessors that can mutate buffers in-place.\n" + " --gen-onefile Generate single output file for C#\n" + " --gen-name-strings Generate type name functions for C++.\n" + " --raw-binary Allow binaries without file_indentifier to be read.\n" + " This may crash flatc given a mismatched schema.\n" + " --proto Input is a .proto, translate to .fbs.\n" + " --schema Serialize schemas instead of JSON (use with -b)\n" "FILEs may be schemas, or JSON files (conforming to preceding schema)\n" "FILEs after the -- must be binary flatbuffer format files.\n" "Output files are named using the base file name of the input,\n" @@ -171,6 +172,8 @@ int main(int argc, const char *argv[]) { opts.scoped_enums = true; } else if(arg == "--gen-mutable") { opts.mutable_buffer = true; + } else if(arg == "--gen-name-strings") { + opts.generate_name_strings = true; } else if(arg == "--gen-all") { opts.generate_all = true; opts.include_dependence_headers = false; diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index eb5093461..cae3812db 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -267,6 +267,14 @@ std::string GenFieldOffsetName(const FieldDef &field) { return "VT_" + uname; } +static void GenFullyQualifiedNameGetter(const Parser &parser, const std::string& name, std::string &code) { + if (parser.opts.generate_name_strings) { + code += " static FLATBUFFERS_CONSTEXPR const char *GetFullyQualifiedName() {\n"; + code += " return \"" + parser.namespaces_.back()->GetFullyQualifiedName(name) + "\";\n"; + code += " }\n"; + } +} + // Generate an accessor struct, builder structs & function for a table. static void GenTable(const Parser &parser, StructDef &struct_def, std::string *code_ptr) { @@ -277,6 +285,8 @@ static void GenTable(const Parser &parser, StructDef &struct_def, code += "struct " + struct_def.name; code += " FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table"; code += " {\n"; + // Generate GetFullyQualifiedName + GenFullyQualifiedNameGetter(parser, struct_def.name, code); // Generate field id constants. if (struct_def.fields.vec.size() > 0) { code += " enum {\n"; @@ -583,8 +593,12 @@ static void GenStruct(const Parser &parser, StructDef &struct_def, GenPadding(field, code, padding_id, PaddingDefinition); } + // Generate GetFullyQualifiedName + code += "\n public:\n"; + GenFullyQualifiedNameGetter(parser, struct_def.name, code); + // Generate a constructor that takes all fields as arguments. - code += "\n public:\n " + struct_def.name + "("; + code += " " + struct_def.name + "("; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { From 4c16038e72b378e5864f12fad607abc071d94c6f Mon Sep 17 00:00:00 2001 From: Tony Kelman Date: Fri, 1 Apr 2016 21:42:23 -0700 Subject: [PATCH 5/6] Fix build on Cygwin use -std=gnu++11 instead of c++0x on cygwin to avoid error: realpath was not declared in this scope ref http://www.cygwin.com/ml/cygwin/2016-03/msg00005.html --- CMakeLists.txt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d2edf829b..542bce3b4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -80,8 +80,15 @@ if(APPLE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++ -Wall -pedantic -Werror -Wextra") elseif(CMAKE_COMPILER_IS_GNUCXX) + if(CYGWIN) + set(CMAKE_CXX_FLAGS + "${CMAKE_CXX_FLAGS} -std=gnu++11") + else(CYGWIN) + set(CMAKE_CXX_FLAGS + "${CMAKE_CXX_FLAGS} -std=c++0x") + endif(CYGWIN) set(CMAKE_CXX_FLAGS - "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -pedantic -Werror -Wextra -Werror=shadow") + "${CMAKE_CXX_FLAGS} -Wall -pedantic -Werror -Wextra -Werror=shadow") if (GCC_VERSION VERSION_GREATER 4.4) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wunused-result -Werror=unused-result") From f4a5c9de5094173a214e2f34d1db9d7e57fcb840 Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Mon, 4 Apr 2016 13:00:48 -0700 Subject: [PATCH 6/6] Fixed VS assignment constructor warning. --- include/flatbuffers/flatbuffers.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index e08e96118..2c3398bd2 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -1117,14 +1117,14 @@ FLATBUFFERS_FINAL_CLASS bool force_defaults_; // Serialize values equal to their defaults anyway. struct StringOffsetCompare { - StringOffsetCompare(const vector_downward &buf) : buf_(buf) {} + StringOffsetCompare(const vector_downward &buf) : buf_(&buf) {} bool operator() (const Offset &a, const Offset &b) const { - auto stra = reinterpret_cast(buf_.data_at(a.o)); - auto strb = reinterpret_cast(buf_.data_at(b.o)); + auto stra = reinterpret_cast(buf_->data_at(a.o)); + auto strb = reinterpret_cast(buf_->data_at(b.o)); return strncmp(stra->c_str(), strb->c_str(), std::min(stra->size(), strb->size()) + 1) < 0; } - const vector_downward &buf_; + const vector_downward *buf_; }; // For use with CreateSharedString. Instantiated on first use only.