diff --git a/CMake/BuildFlatBuffers.cmake b/CMake/BuildFlatBuffers.cmake index 2c28899a7..42f78de77 100644 --- a/CMake/BuildFlatBuffers.cmake +++ b/CMake/BuildFlatBuffers.cmake @@ -63,6 +63,13 @@ function(build_flatbuffers flatbuffers_schemas set(FLATC_TARGET flatc) set(FLATC flatc) endif() + set(FLATC_SCHEMA_ARGS --gen-mutable) + if(FLATBUFFERS_FLATC_SCHEMA_EXTRA_ARGS) + set(FLATC_SCHEMA_ARGS + ${FLATBUFFERS_FLATC_SCHEMA_EXTRA_ARGS} + ${FLATC_SCHEMA_ARGS} + ) + endif() set(schema_glob "*.fbs") # Generate the include files parameters. @@ -86,7 +93,7 @@ function(build_flatbuffers flatbuffers_schemas set(generated_include ${generated_includes_dir}/${filename}_generated.h) add_custom_command( OUTPUT ${generated_include} - COMMAND ${FLATC} --gen-mutable + COMMAND ${FLATC} ${FLATC_SCHEMA_ARGS} -o ${generated_includes_dir} ${include_params} -c ${schema} diff --git a/CMakeLists.txt b/CMakeLists.txt index b1e56d6b0..c2dd95085 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ option(FLATBUFFERS_INSTALL "Enable the installation of targets." ON) option(FLATBUFFERS_BUILD_FLATLIB "Enable the build of the flatbuffers library" ON) option(FLATBUFFERS_BUILD_FLATC "Enable the build of the flatbuffers compiler" ON) option(FLATBUFFERS_BUILD_FLATHASH "Enable the build of flathash" ON) +option(FLATBUFFERS_BUILD_GRPCTEST "Enable the build of grpctest" OFF) if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS) message(WARNING @@ -39,7 +40,10 @@ set(FlatBuffers_Compiler_SRCS src/idl_gen_php.cpp src/idl_gen_python.cpp src/idl_gen_fbs.cpp + src/idl_gen_grpc.cpp src/flatc.cpp + grpc/src/compiler/cpp_generator.h + grpc/src/compiler/cpp_generator.cc ) set(FlatHash_SRCS @@ -76,6 +80,16 @@ set(FlatBuffers_Sample_Text_SRCS ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h ) +set(FlatBuffers_GRPCTest_SRCS + include/flatbuffers/flatbuffers.h + include/flatbuffers/grpc.h + tests/monster_test.grpc.fb.h + tests/monster_test.grpc.fb.cc + grpc/tests/grpctest.cpp + # file generated by running compiler on samples/monster.fbs + ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h +) + # source_group(Compiler FILES ${FlatBuffers_Compiler_SRCS}) # source_group(Tests FILES ${FlatBuffers_Tests_SRCS}) @@ -129,6 +143,7 @@ if(BIICODE) endif() include_directories(include) +include_directories(grpc) if(FLATBUFFERS_BUILD_FLATLIB) add_library(flatbuffers STATIC ${FlatBuffers_Library_SRCS}) @@ -174,6 +189,14 @@ if(FLATBUFFERS_BUILD_TESTS) add_executable(flatsampletext ${FlatBuffers_Sample_Text_SRCS}) endif() +if(FLATBUFFERS_BUILD_GRPCTEST) + if(CMAKE_COMPILER_IS_GNUCXX) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter") + endif() + add_executable(grpctest ${FlatBuffers_GRPCTest_SRCS}) + target_link_libraries(grpctest grpc++_unsecure grpc pthread dl) +endif() + if(FLATBUFFERS_INSTALL) install(DIRECTORY include/flatbuffers DESTINATION include) if(FLATBUFFERS_BUILD_FLATLIB) diff --git a/android/jni/Application.mk b/android/jni/Application.mk index 56952fc96..2fc9c7379 100755 --- a/android/jni/Application.mk +++ b/android/jni/Application.mk @@ -18,5 +18,5 @@ APP_PROJECT_PATH := $(call my-dir)/.. APP_STL := gnustl_static APP_ABI := armeabi-v7a -NDK_TOOLCHAIN_VERSION := 4.8 + APP_CPPFLAGS += -std=c++11 diff --git a/docs/source/Compiler.md b/docs/source/Compiler.md index 886fbd9b8..ad584c739 100755 --- a/docs/source/Compiler.md +++ b/docs/source/Compiler.md @@ -33,6 +33,8 @@ For any schema input files, one or more generators can be specified: - `--php`: Generate PHP code. +- `--grpc`: Generate RPC stub code for GRPC. + For any data input files: - `--binary`, `-b` : If data is contained in this file, generate a diff --git a/docs/source/Schemas.md b/docs/source/Schemas.md index 4004803de..5bcaccb37 100755 --- a/docs/source/Schemas.md +++ b/docs/source/Schemas.md @@ -237,7 +237,8 @@ as the response (both of which must be table types): } What code this produces and how it is used depends on language and RPC system -used, FlatBuffers itself does not offer this functionality. +used, there is preliminary support for GRPC through the `--grpc` code generator, +see `grpc/tests` for an example. ### Comments & documentation @@ -271,7 +272,7 @@ Current understood attributes: the union field should have id 8, and the unions type field will implicitly be 7. IDs allow the fields to be placed in any order in the schema. - When a new field is added to the schema is must use the next available ID. + When a new field is added to the schema it must use the next available ID. - `deprecated` (on a field): do not generate accessors for this field anymore, code should stop using this data. - `required` (on a non-scalar table field): this field must always be set. @@ -333,6 +334,10 @@ JSON: - A field that has the value `null` (e.g. `field: null`) is intended to have the default value for that field (thus has the same effect as if that field wasn't specified at all). +- It has some built in conversion functions, so you can write for example + `rad(180)` where ever you'd normally write `3.14159`. + Currently supports the following functions: `rad`, `deg`, `cos`, `sin`, + `tan`, `acos`, `asin`, `atan`. When parsing JSON, it recognizes the following escape codes in strings: diff --git a/docs/source/Tutorial.md b/docs/source/Tutorial.md index 1e48796d9..6f6ac9a51 100644 --- a/docs/source/Tutorial.md +++ b/docs/source/Tutorial.md @@ -790,6 +790,14 @@ offsets. ~~~ +
+
+Note there's additional convenience overloads of `CreateVector`, allowing you +to work with data that's not in a `std::vector`, or allowing you to generate +elements by calling a lambda. For the common case of `std::vector` +there's also `CreateVectorOfStrings`. +
+ To create a `struct`, use the `Vec3` class/struct that was generated by the schema compiler: @@ -1075,7 +1083,7 @@ Here is a repetition these lines, to help highlight them more clearly:
~~~{.c} // Add union type and data simultanously. - ns(Monster_equipped_Weapon_add(B, axe)); + ns(Monster_equipped_Weapon_add(B, axe)); ~~~
diff --git a/grpc/README.md b/grpc/README.md new file mode 100644 index 000000000..13485198c --- /dev/null +++ b/grpc/README.md @@ -0,0 +1,11 @@ +GRPC implementation and test +============================ + +NOTE: files in `src/` are shared with the GRPC project, and maintained there +(any changes should be submitted to GRPC instead). These files are copied +from GRPC, and work with both the Protobuf and FlatBuffers code generator. + +`tests/` contains a GRPC specific test, you need to have built and installed +the GRPC libraries for this to compile. This test will build using the +`FLATBUFFERS_BUILD_GRPCTEST` option to the main FlatBuffers CMake project. + diff --git a/grpc/src/compiler/cpp_generator.cc b/grpc/src/compiler/cpp_generator.cc new file mode 100644 index 000000000..9319c4193 --- /dev/null +++ b/grpc/src/compiler/cpp_generator.cc @@ -0,0 +1,1202 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +#include "src/compiler/cpp_generator.h" + +#include + +namespace grpc_cpp_generator { +namespace { + +template +grpc::string as_string(T x) { + std::ostringstream out; + out << x; + return out.str(); +} + +grpc::string FilenameIdentifier(const grpc::string &filename) { + grpc::string result; + for (unsigned i = 0; i < filename.size(); i++) { + char c = filename[i]; + if (isalnum(c)) { + result.push_back(c); + } else { + static char hex[] = "0123456789abcdef"; + result.push_back('_'); + result.push_back(hex[(c >> 4) & 0xf]); + result.push_back(hex[c & 0xf]); + } + } + return result; +} +} // namespace + +template +T *array_end(T (&array)[N]) { return array + N; } + +void PrintIncludes(Printer *printer, const std::vector& headers, const Parameters ¶ms) { + std::map vars; + + vars["l"] = params.use_system_headers ? '<' : '"'; + vars["r"] = params.use_system_headers ? '>' : '"'; + + if (!params.grpc_search_path.empty()) { + vars["l"] += params.grpc_search_path; + if (params.grpc_search_path.back() != '/') { + vars["l"] += '/'; + } + } + + for (auto i = headers.begin(); i != headers.end(); i++) { + vars["h"] = *i; + printer->Print(vars, "#include $l$$h$$r$\n"); + } +} + +grpc::string GetHeaderPrologue(File *file, const Parameters & /*params*/) { + grpc::string output; + { + // Scope the output stream so it closes and finalizes output to the string. + auto printer = file->CreatePrinter(&output); + std::map vars; + + vars["filename"] = file->filename(); + vars["filename_identifier"] = FilenameIdentifier(file->filename()); + vars["filename_base"] = file->filename_without_ext(); + vars["message_header_ext"] = file->message_header_ext(); + + printer->Print(vars, "// Generated by the gRPC protobuf plugin.\n"); + printer->Print(vars, + "// If you make any local change, they will be lost.\n"); + printer->Print(vars, "// source: $filename$\n"); + printer->Print(vars, "#ifndef GRPC_$filename_identifier$__INCLUDED\n"); + printer->Print(vars, "#define GRPC_$filename_identifier$__INCLUDED\n"); + printer->Print(vars, "\n"); + printer->Print(vars, "#include \"$filename_base$$message_header_ext$\"\n"); + printer->Print(vars, "\n"); + } + return output; +} + +grpc::string GetHeaderIncludes(File *file, + const Parameters ¶ms) { + grpc::string output; + { + // Scope the output stream so it closes and finalizes output to the string. + auto printer = file->CreatePrinter(&output); + std::map vars; + + static const char *headers_strs[] = { + "grpc++/impl/codegen/async_stream.h", + "grpc++/impl/codegen/async_unary_call.h", + "grpc++/impl/codegen/proto_utils.h", + "grpc++/impl/codegen/rpc_method.h", + "grpc++/impl/codegen/service_type.h", + "grpc++/impl/codegen/status.h", + "grpc++/impl/codegen/stub_options.h", + "grpc++/impl/codegen/sync_stream.h" + }; + std::vector headers(headers_strs, array_end(headers_strs)); + PrintIncludes(printer.get(), headers, params); + printer->Print(vars, "\n"); + printer->Print(vars, "namespace grpc {\n"); + printer->Print(vars, "class CompletionQueue;\n"); + printer->Print(vars, "class Channel;\n"); + printer->Print(vars, "class RpcService;\n"); + printer->Print(vars, "class ServerCompletionQueue;\n"); + printer->Print(vars, "class ServerContext;\n"); + printer->Print(vars, "} // namespace grpc\n\n"); + + if (!file->package().empty()) { + std::vector parts = file->package_parts(); + + for (auto part = parts.begin(); part != parts.end(); part++) { + vars["part"] = *part; + printer->Print(vars, "namespace $part$ {\n"); + } + printer->Print(vars, "\n"); + } + } + return output; +} + +void PrintHeaderClientMethodInterfaces( + Printer *printer, const Method *method, + std::map *vars, bool is_public) { + (*vars)["Method"] = method->name(); + (*vars)["Request"] = method->input_type_name(); + (*vars)["Response"] = method->output_type_name(); + + if (is_public) { + if (method->NoStreaming()) { + printer->Print( + *vars, + "virtual ::grpc::Status $Method$(::grpc::ClientContext* context, " + "const $Request$& request, $Response$* response) = 0;\n"); + printer->Print(*vars, + "std::unique_ptr< " + "::grpc::ClientAsyncResponseReaderInterface< $Response$>> " + "Async$Method$(::grpc::ClientContext* context, " + "const $Request$& request, " + "::grpc::CompletionQueue* cq) {\n"); + printer->Indent(); + printer->Print(*vars, + "return std::unique_ptr< " + "::grpc::ClientAsyncResponseReaderInterface< $Response$>>(" + "Async$Method$Raw(context, request, cq));\n"); + printer->Outdent(); + printer->Print("}\n"); + } else if (method->ClientOnlyStreaming()) { + printer->Print( + *vars, + "std::unique_ptr< ::grpc::ClientWriterInterface< $Request$>>" + " $Method$(" + "::grpc::ClientContext* context, $Response$* response) {\n"); + printer->Indent(); + printer->Print( + *vars, + "return std::unique_ptr< ::grpc::ClientWriterInterface< $Request$>>" + "($Method$Raw(context, response));\n"); + printer->Outdent(); + printer->Print("}\n"); + printer->Print( + *vars, + "std::unique_ptr< ::grpc::ClientAsyncWriterInterface< $Request$>>" + " Async$Method$(::grpc::ClientContext* context, $Response$* " + "response, " + "::grpc::CompletionQueue* cq, void* tag) {\n"); + printer->Indent(); + printer->Print(*vars, + "return std::unique_ptr< " + "::grpc::ClientAsyncWriterInterface< $Request$>>(" + "Async$Method$Raw(context, response, cq, tag));\n"); + printer->Outdent(); + printer->Print("}\n"); + } else if (method->ServerOnlyStreaming()) { + printer->Print( + *vars, + "std::unique_ptr< ::grpc::ClientReaderInterface< $Response$>>" + " $Method$(::grpc::ClientContext* context, const $Request$& request)" + " {\n"); + printer->Indent(); + printer->Print( + *vars, + "return std::unique_ptr< ::grpc::ClientReaderInterface< $Response$>>" + "($Method$Raw(context, request));\n"); + printer->Outdent(); + printer->Print("}\n"); + printer->Print( + *vars, + "std::unique_ptr< ::grpc::ClientAsyncReaderInterface< $Response$>> " + "Async$Method$(" + "::grpc::ClientContext* context, const $Request$& request, " + "::grpc::CompletionQueue* cq, void* tag) {\n"); + printer->Indent(); + printer->Print(*vars, + "return std::unique_ptr< " + "::grpc::ClientAsyncReaderInterface< $Response$>>(" + "Async$Method$Raw(context, request, cq, tag));\n"); + printer->Outdent(); + printer->Print("}\n"); + } else if (method->BidiStreaming()) { + printer->Print(*vars, + "std::unique_ptr< ::grpc::ClientReaderWriterInterface< " + "$Request$, $Response$>> " + "$Method$(::grpc::ClientContext* context) {\n"); + printer->Indent(); + printer->Print( + *vars, + "return std::unique_ptr< " + "::grpc::ClientReaderWriterInterface< $Request$, $Response$>>(" + "$Method$Raw(context));\n"); + printer->Outdent(); + printer->Print("}\n"); + printer->Print( + *vars, + "std::unique_ptr< " + "::grpc::ClientAsyncReaderWriterInterface< $Request$, $Response$>> " + "Async$Method$(::grpc::ClientContext* context, " + "::grpc::CompletionQueue* cq, void* tag) {\n"); + printer->Indent(); + printer->Print( + *vars, + "return std::unique_ptr< " + "::grpc::ClientAsyncReaderWriterInterface< $Request$, $Response$>>(" + "Async$Method$Raw(context, cq, tag));\n"); + printer->Outdent(); + printer->Print("}\n"); + } + } else { + if (method->NoStreaming()) { + printer->Print( + *vars, + "virtual ::grpc::ClientAsyncResponseReaderInterface< $Response$>* " + "Async$Method$Raw(::grpc::ClientContext* context, " + "const $Request$& request, " + "::grpc::CompletionQueue* cq) = 0;\n"); + } else if (method->ClientOnlyStreaming()) { + printer->Print( + *vars, + "virtual ::grpc::ClientWriterInterface< $Request$>*" + " $Method$Raw(" + "::grpc::ClientContext* context, $Response$* response) = 0;\n"); + printer->Print(*vars, + "virtual ::grpc::ClientAsyncWriterInterface< $Request$>*" + " Async$Method$Raw(::grpc::ClientContext* context, " + "$Response$* response, " + "::grpc::CompletionQueue* cq, void* tag) = 0;\n"); + } else if (method->ServerOnlyStreaming()) { + printer->Print( + *vars, + "virtual ::grpc::ClientReaderInterface< $Response$>* $Method$Raw(" + "::grpc::ClientContext* context, const $Request$& request) = 0;\n"); + printer->Print( + *vars, + "virtual ::grpc::ClientAsyncReaderInterface< $Response$>* " + "Async$Method$Raw(" + "::grpc::ClientContext* context, const $Request$& request, " + "::grpc::CompletionQueue* cq, void* tag) = 0;\n"); + } else if (method->BidiStreaming()) { + printer->Print(*vars, + "virtual ::grpc::ClientReaderWriterInterface< $Request$, " + "$Response$>* " + "$Method$Raw(::grpc::ClientContext* context) = 0;\n"); + printer->Print(*vars, + "virtual ::grpc::ClientAsyncReaderWriterInterface< " + "$Request$, $Response$>* " + "Async$Method$Raw(::grpc::ClientContext* context, " + "::grpc::CompletionQueue* cq, void* tag) = 0;\n"); + } + } +} + +void PrintHeaderClientMethod(Printer *printer, + const Method *method, + std::map *vars, + bool is_public) { + (*vars)["Method"] = method->name(); + (*vars)["Request"] = method->input_type_name(); + (*vars)["Response"] = method->output_type_name(); + if (is_public) { + if (method->NoStreaming()) { + printer->Print( + *vars, + "::grpc::Status $Method$(::grpc::ClientContext* context, " + "const $Request$& request, $Response$* response) GRPC_OVERRIDE;\n"); + printer->Print( + *vars, + "std::unique_ptr< ::grpc::ClientAsyncResponseReader< $Response$>> " + "Async$Method$(::grpc::ClientContext* context, " + "const $Request$& request, " + "::grpc::CompletionQueue* cq) {\n"); + printer->Indent(); + printer->Print(*vars, + "return std::unique_ptr< " + "::grpc::ClientAsyncResponseReader< $Response$>>(" + "Async$Method$Raw(context, request, cq));\n"); + printer->Outdent(); + printer->Print("}\n"); + } else if (method->ClientOnlyStreaming()) { + printer->Print( + *vars, + "std::unique_ptr< ::grpc::ClientWriter< $Request$>>" + " $Method$(" + "::grpc::ClientContext* context, $Response$* response) {\n"); + printer->Indent(); + printer->Print(*vars, + "return std::unique_ptr< ::grpc::ClientWriter< $Request$>>" + "($Method$Raw(context, response));\n"); + printer->Outdent(); + printer->Print("}\n"); + printer->Print(*vars, + "std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>>" + " Async$Method$(::grpc::ClientContext* context, " + "$Response$* response, " + "::grpc::CompletionQueue* cq, void* tag) {\n"); + printer->Indent(); + printer->Print( + *vars, + "return std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>>(" + "Async$Method$Raw(context, response, cq, tag));\n"); + printer->Outdent(); + printer->Print("}\n"); + } else if (method->ServerOnlyStreaming()) { + printer->Print( + *vars, + "std::unique_ptr< ::grpc::ClientReader< $Response$>>" + " $Method$(::grpc::ClientContext* context, const $Request$& request)" + " {\n"); + printer->Indent(); + printer->Print( + *vars, + "return std::unique_ptr< ::grpc::ClientReader< $Response$>>" + "($Method$Raw(context, request));\n"); + printer->Outdent(); + printer->Print("}\n"); + printer->Print( + *vars, + "std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>> " + "Async$Method$(" + "::grpc::ClientContext* context, const $Request$& request, " + "::grpc::CompletionQueue* cq, void* tag) {\n"); + printer->Indent(); + printer->Print( + *vars, + "return std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>>(" + "Async$Method$Raw(context, request, cq, tag));\n"); + printer->Outdent(); + printer->Print("}\n"); + } else if (method->BidiStreaming()) { + printer->Print( + *vars, + "std::unique_ptr< ::grpc::ClientReaderWriter< $Request$, $Response$>>" + " $Method$(::grpc::ClientContext* context) {\n"); + printer->Indent(); + printer->Print(*vars, + "return std::unique_ptr< " + "::grpc::ClientReaderWriter< $Request$, $Response$>>(" + "$Method$Raw(context));\n"); + printer->Outdent(); + printer->Print("}\n"); + printer->Print(*vars, + "std::unique_ptr< ::grpc::ClientAsyncReaderWriter< " + "$Request$, $Response$>> " + "Async$Method$(::grpc::ClientContext* context, " + "::grpc::CompletionQueue* cq, void* tag) {\n"); + printer->Indent(); + printer->Print(*vars, + "return std::unique_ptr< " + "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>>(" + "Async$Method$Raw(context, cq, tag));\n"); + printer->Outdent(); + printer->Print("}\n"); + } + } else { + if (method->NoStreaming()) { + printer->Print(*vars, + "::grpc::ClientAsyncResponseReader< $Response$>* " + "Async$Method$Raw(::grpc::ClientContext* context, " + "const $Request$& request, " + "::grpc::CompletionQueue* cq) GRPC_OVERRIDE;\n"); + } else if (method->ClientOnlyStreaming()) { + printer->Print(*vars, + "::grpc::ClientWriter< $Request$>* $Method$Raw(" + "::grpc::ClientContext* context, $Response$* response) " + "GRPC_OVERRIDE;\n"); + printer->Print( + *vars, + "::grpc::ClientAsyncWriter< $Request$>* Async$Method$Raw(" + "::grpc::ClientContext* context, $Response$* response, " + "::grpc::CompletionQueue* cq, void* tag) GRPC_OVERRIDE;\n"); + } else if (method->ServerOnlyStreaming()) { + printer->Print(*vars, + "::grpc::ClientReader< $Response$>* $Method$Raw(" + "::grpc::ClientContext* context, const $Request$& request)" + " GRPC_OVERRIDE;\n"); + printer->Print( + *vars, + "::grpc::ClientAsyncReader< $Response$>* Async$Method$Raw(" + "::grpc::ClientContext* context, const $Request$& request, " + "::grpc::CompletionQueue* cq, void* tag) GRPC_OVERRIDE;\n"); + } else if (method->BidiStreaming()) { + printer->Print( + *vars, + "::grpc::ClientReaderWriter< $Request$, $Response$>* " + "$Method$Raw(::grpc::ClientContext* context) GRPC_OVERRIDE;\n"); + printer->Print( + *vars, + "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>* " + "Async$Method$Raw(::grpc::ClientContext* context, " + "::grpc::CompletionQueue* cq, void* tag) GRPC_OVERRIDE;\n"); + } + } +} + +void PrintHeaderClientMethodData(Printer *printer, const Method *method, + std::map *vars) { + (*vars)["Method"] = method->name(); + printer->Print(*vars, "const ::grpc::RpcMethod rpcmethod_$Method$_;\n"); +} + +void PrintHeaderServerMethodSync(Printer *printer, const Method *method, + std::map *vars) { + (*vars)["Method"] = method->name(); + (*vars)["Request"] = method->input_type_name(); + (*vars)["Response"] = method->output_type_name(); + if (method->NoStreaming()) { + printer->Print(*vars, + "virtual ::grpc::Status $Method$(" + "::grpc::ServerContext* context, const $Request$* request, " + "$Response$* response);\n"); + } else if (method->ClientOnlyStreaming()) { + printer->Print(*vars, + "virtual ::grpc::Status $Method$(" + "::grpc::ServerContext* context, " + "::grpc::ServerReader< $Request$>* reader, " + "$Response$* response);\n"); + } else if (method->ServerOnlyStreaming()) { + printer->Print(*vars, + "virtual ::grpc::Status $Method$(" + "::grpc::ServerContext* context, const $Request$* request, " + "::grpc::ServerWriter< $Response$>* writer);\n"); + } else if (method->BidiStreaming()) { + printer->Print( + *vars, + "virtual ::grpc::Status $Method$(" + "::grpc::ServerContext* context, " + "::grpc::ServerReaderWriter< $Response$, $Request$>* stream);" + "\n"); + } +} + +void PrintHeaderServerMethodAsync( + Printer *printer, + const Method *method, + std::map *vars) { + (*vars)["Method"] = method->name(); + (*vars)["Request"] = method->input_type_name(); + (*vars)["Response"] = method->output_type_name(); + printer->Print(*vars, "template \n"); + printer->Print(*vars, + "class WithAsyncMethod_$Method$ : public BaseClass {\n"); + printer->Print( + " private:\n" + " void BaseClassMustBeDerivedFromService(const Service *service) {}\n"); + printer->Print(" public:\n"); + printer->Indent(); + printer->Print(*vars, + "WithAsyncMethod_$Method$() {\n" + " ::grpc::Service::MarkMethodAsync($Idx$);\n" + "}\n"); + printer->Print(*vars, + "~WithAsyncMethod_$Method$() GRPC_OVERRIDE {\n" + " BaseClassMustBeDerivedFromService(this);\n" + "}\n"); + if (method->NoStreaming()) { + printer->Print( + *vars, + "// disable synchronous version of this method\n" + "::grpc::Status $Method$(" + "::grpc::ServerContext* context, const $Request$* request, " + "$Response$* response) GRPC_FINAL GRPC_OVERRIDE {\n" + " abort();\n" + " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n" + "}\n"); + printer->Print( + *vars, + "void Request$Method$(" + "::grpc::ServerContext* context, $Request$* request, " + "::grpc::ServerAsyncResponseWriter< $Response$>* response, " + "::grpc::CompletionQueue* new_call_cq, " + "::grpc::ServerCompletionQueue* notification_cq, void *tag) {\n"); + printer->Print(*vars, + " ::grpc::Service::RequestAsyncUnary($Idx$, context, " + "request, response, new_call_cq, notification_cq, tag);\n"); + printer->Print("}\n"); + } else if (method->ClientOnlyStreaming()) { + printer->Print( + *vars, + "// disable synchronous version of this method\n" + "::grpc::Status $Method$(" + "::grpc::ServerContext* context, " + "::grpc::ServerReader< $Request$>* reader, " + "$Response$* response) GRPC_FINAL GRPC_OVERRIDE {\n" + " abort();\n" + " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n" + "}\n"); + printer->Print( + *vars, + "void Request$Method$(" + "::grpc::ServerContext* context, " + "::grpc::ServerAsyncReader< $Response$, $Request$>* reader, " + "::grpc::CompletionQueue* new_call_cq, " + "::grpc::ServerCompletionQueue* notification_cq, void *tag) {\n"); + printer->Print(*vars, + " ::grpc::Service::RequestAsyncClientStreaming($Idx$, " + "context, reader, new_call_cq, notification_cq, tag);\n"); + printer->Print("}\n"); + } else if (method->ServerOnlyStreaming()) { + printer->Print( + *vars, + "// disable synchronous version of this method\n" + "::grpc::Status $Method$(" + "::grpc::ServerContext* context, const $Request$* request, " + "::grpc::ServerWriter< $Response$>* writer) GRPC_FINAL GRPC_OVERRIDE " + "{\n" + " abort();\n" + " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n" + "}\n"); + printer->Print( + *vars, + "void Request$Method$(" + "::grpc::ServerContext* context, $Request$* request, " + "::grpc::ServerAsyncWriter< $Response$>* writer, " + "::grpc::CompletionQueue* new_call_cq, " + "::grpc::ServerCompletionQueue* notification_cq, void *tag) {\n"); + printer->Print( + *vars, + " ::grpc::Service::RequestAsyncServerStreaming($Idx$, " + "context, request, writer, new_call_cq, notification_cq, tag);\n"); + printer->Print("}\n"); + } else if (method->BidiStreaming()) { + printer->Print( + *vars, + "// disable synchronous version of this method\n" + "::grpc::Status $Method$(" + "::grpc::ServerContext* context, " + "::grpc::ServerReaderWriter< $Response$, $Request$>* stream) " + "GRPC_FINAL GRPC_OVERRIDE {\n" + " abort();\n" + " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n" + "}\n"); + printer->Print( + *vars, + "void Request$Method$(" + "::grpc::ServerContext* context, " + "::grpc::ServerAsyncReaderWriter< $Response$, $Request$>* stream, " + "::grpc::CompletionQueue* new_call_cq, " + "::grpc::ServerCompletionQueue* notification_cq, void *tag) {\n"); + printer->Print(*vars, + " ::grpc::Service::RequestAsyncBidiStreaming($Idx$, " + "context, stream, new_call_cq, notification_cq, tag);\n"); + printer->Print("}\n"); + } + printer->Outdent(); + printer->Print(*vars, "};\n"); +} + +void PrintHeaderServerMethodGeneric( + Printer *printer, + const Method *method, + std::map *vars) { + (*vars)["Method"] = method->name(); + (*vars)["Request"] = method->input_type_name(); + (*vars)["Response"] = method->output_type_name(); + printer->Print(*vars, "template \n"); + printer->Print(*vars, + "class WithGenericMethod_$Method$ : public BaseClass {\n"); + printer->Print( + " private:\n" + " void BaseClassMustBeDerivedFromService(const Service *service) {}\n"); + printer->Print(" public:\n"); + printer->Indent(); + printer->Print(*vars, + "WithGenericMethod_$Method$() {\n" + " ::grpc::Service::MarkMethodGeneric($Idx$);\n" + "}\n"); + printer->Print(*vars, + "~WithGenericMethod_$Method$() GRPC_OVERRIDE {\n" + " BaseClassMustBeDerivedFromService(this);\n" + "}\n"); + if (method->NoStreaming()) { + printer->Print( + *vars, + "// disable synchronous version of this method\n" + "::grpc::Status $Method$(" + "::grpc::ServerContext* context, const $Request$* request, " + "$Response$* response) GRPC_FINAL GRPC_OVERRIDE {\n" + " abort();\n" + " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n" + "}\n"); + } else if (method->ClientOnlyStreaming()) { + printer->Print( + *vars, + "// disable synchronous version of this method\n" + "::grpc::Status $Method$(" + "::grpc::ServerContext* context, " + "::grpc::ServerReader< $Request$>* reader, " + "$Response$* response) GRPC_FINAL GRPC_OVERRIDE {\n" + " abort();\n" + " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n" + "}\n"); + } else if (method->ServerOnlyStreaming()) { + printer->Print( + *vars, + "// disable synchronous version of this method\n" + "::grpc::Status $Method$(" + "::grpc::ServerContext* context, const $Request$* request, " + "::grpc::ServerWriter< $Response$>* writer) GRPC_FINAL GRPC_OVERRIDE " + "{\n" + " abort();\n" + " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n" + "}\n"); + } else if (method->BidiStreaming()) { + printer->Print( + *vars, + "// disable synchronous version of this method\n" + "::grpc::Status $Method$(" + "::grpc::ServerContext* context, " + "::grpc::ServerReaderWriter< $Response$, $Request$>* stream) " + "GRPC_FINAL GRPC_OVERRIDE {\n" + " abort();\n" + " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n" + "}\n"); + } + printer->Outdent(); + printer->Print(*vars, "};\n"); +} + +void PrintHeaderService(Printer *printer, + const Service *service, + std::map *vars) { + (*vars)["Service"] = service->name(); + + printer->Print(*vars, + "class $Service$ GRPC_FINAL {\n" + " public:\n"); + printer->Indent(); + + // Client side + printer->Print( + "class StubInterface {\n" + " public:\n"); + printer->Indent(); + printer->Print("virtual ~StubInterface() {}\n"); + for (int i = 0; i < service->method_count(); ++i) { + PrintHeaderClientMethodInterfaces(printer, service->method(i).get(), vars, true); + } + printer->Outdent(); + printer->Print("private:\n"); + printer->Indent(); + for (int i = 0; i < service->method_count(); ++i) { + PrintHeaderClientMethodInterfaces(printer, service->method(i).get(), vars, false); + } + printer->Outdent(); + printer->Print("};\n"); + printer->Print( + "class Stub GRPC_FINAL : public StubInterface" + " {\n public:\n"); + printer->Indent(); + printer->Print("Stub(const std::shared_ptr< ::grpc::ChannelInterface>& channel);\n"); + for (int i = 0; i < service->method_count(); ++i) { + PrintHeaderClientMethod(printer, service->method(i).get(), vars, true); + } + printer->Outdent(); + printer->Print("\n private:\n"); + printer->Indent(); + printer->Print("std::shared_ptr< ::grpc::ChannelInterface> channel_;\n"); + for (int i = 0; i < service->method_count(); ++i) { + PrintHeaderClientMethod(printer, service->method(i).get(), vars, false); + } + for (int i = 0; i < service->method_count(); ++i) { + PrintHeaderClientMethodData(printer, service->method(i).get(), vars); + } + printer->Outdent(); + printer->Print("};\n"); + printer->Print( + "static std::unique_ptr NewStub(const std::shared_ptr< " + "::grpc::ChannelInterface>& channel, " + "const ::grpc::StubOptions& options = ::grpc::StubOptions());\n"); + + printer->Print("\n"); + + // Server side - base + printer->Print( + "class Service : public ::grpc::Service {\n" + " public:\n"); + printer->Indent(); + printer->Print("Service();\n"); + printer->Print("virtual ~Service();\n"); + for (int i = 0; i < service->method_count(); ++i) { + PrintHeaderServerMethodSync(printer, service->method(i).get(), vars); + } + printer->Outdent(); + printer->Print("};\n"); + + // Server side - Asynchronous + for (int i = 0; i < service->method_count(); ++i) { + (*vars)["Idx"] = as_string(i); + PrintHeaderServerMethodAsync(printer, service->method(i).get(), vars); + } + + printer->Print("typedef "); + + for (int i = 0; i < service->method_count(); ++i) { + (*vars)["method_name"] = service->method(i).get()->name(); + printer->Print(*vars, "WithAsyncMethod_$method_name$<"); + } + printer->Print("Service"); + for (int i = 0; i < service->method_count(); ++i) { + printer->Print(" >"); + } + printer->Print(" AsyncService;\n"); + + // Server side - Generic + for (int i = 0; i < service->method_count(); ++i) { + (*vars)["Idx"] = as_string(i); + PrintHeaderServerMethodGeneric(printer, service->method(i).get(), vars); + } + + printer->Outdent(); + printer->Print("};\n"); +} + +grpc::string GetHeaderServices(File *file, + const Parameters ¶ms) { + grpc::string output; + { + // Scope the output stream so it closes and finalizes output to the string. + auto printer = file->CreatePrinter(&output); + std::map vars; + // Package string is empty or ends with a dot. It is used to fully qualify + // method names. + vars["Package"] = file->package(); + if (!file->package().empty()) { + vars["Package"].append("."); + } + + if (!params.services_namespace.empty()) { + vars["services_namespace"] = params.services_namespace; + printer->Print(vars, "\nnamespace $services_namespace$ {\n\n"); + } + + for (int i = 0; i < file->service_count(); ++i) { + PrintHeaderService(printer.get(), file->service(i).get(), &vars); + printer->Print("\n"); + } + + if (!params.services_namespace.empty()) { + printer->Print(vars, "} // namespace $services_namespace$\n\n"); + } + } + return output; +} + +grpc::string GetHeaderEpilogue(File *file, const Parameters & /*params*/) { + grpc::string output; + { + // Scope the output stream so it closes and finalizes output to the string. + auto printer = file->CreatePrinter(&output); + std::map vars; + + vars["filename"] = file->filename(); + vars["filename_identifier"] = FilenameIdentifier(file->filename()); + + if (!file->package().empty()) { + std::vector parts = file->package_parts(); + + for (auto part = parts.rbegin(); part != parts.rend(); part++) { + vars["part"] = *part; + printer->Print(vars, "} // namespace $part$\n"); + } + printer->Print(vars, "\n"); + } + + printer->Print(vars, "\n"); + printer->Print(vars, "#endif // GRPC_$filename_identifier$__INCLUDED\n"); + } + return output; +} + +grpc::string GetSourcePrologue(File *file, const Parameters & /*params*/) { + grpc::string output; + { + // Scope the output stream so it closes and finalizes output to the string. + auto printer = file->CreatePrinter(&output); + std::map vars; + + vars["filename"] = file->filename(); + vars["filename_base"] = file->filename_without_ext(); + vars["message_header_ext"] = file->message_header_ext(); + vars["service_header_ext"] = file->service_header_ext(); + + printer->Print(vars, "// Generated by the gRPC protobuf plugin.\n"); + printer->Print(vars, + "// If you make any local change, they will be lost.\n"); + printer->Print(vars, "// source: $filename$\n\n"); + printer->Print(vars, "#include \"$filename_base$$message_header_ext$\"\n"); + printer->Print(vars, "#include \"$filename_base$$service_header_ext$\"\n"); + printer->Print(vars, file->additional_headers().c_str()); + printer->Print(vars, "\n"); + } + return output; +} + +grpc::string GetSourceIncludes(File *file, + const Parameters ¶ms) { + grpc::string output; + { + // Scope the output stream so it closes and finalizes output to the string. + auto printer = file->CreatePrinter(&output); + std::map vars; + + static const char *headers_strs[] = { + "grpc++/impl/codegen/async_stream.h", + "grpc++/impl/codegen/async_unary_call.h", + "grpc++/impl/codegen/channel_interface.h", + "grpc++/impl/codegen/client_unary_call.h", + "grpc++/impl/codegen/method_handler_impl.h", + "grpc++/impl/codegen/rpc_service_method.h", + "grpc++/impl/codegen/service_type.h", + "grpc++/impl/codegen/sync_stream.h" + }; + std::vector headers(headers_strs, array_end(headers_strs)); + PrintIncludes(printer.get(), headers, params); + + if (!file->package().empty()) { + std::vector parts = file->package_parts(); + + for (auto part = parts.begin(); part != parts.end(); part++) { + vars["part"] = *part; + printer->Print(vars, "namespace $part$ {\n"); + } + } + + printer->Print(vars, "\n"); + } + return output; +} + +void PrintSourceClientMethod(Printer *printer, + const Method *method, + std::map *vars) { + (*vars)["Method"] = method->name(); + (*vars)["Request"] = method->input_type_name(); + (*vars)["Response"] = method->output_type_name(); + if (method->NoStreaming()) { + printer->Print(*vars, + "::grpc::Status $ns$$Service$::Stub::$Method$(" + "::grpc::ClientContext* context, " + "const $Request$& request, $Response$* response) {\n"); + printer->Print(*vars, + " return ::grpc::BlockingUnaryCall(channel_.get(), " + "rpcmethod_$Method$_, " + "context, request, response);\n" + "}\n\n"); + printer->Print( + *vars, + "::grpc::ClientAsyncResponseReader< $Response$>* " + "$ns$$Service$::Stub::Async$Method$Raw(::grpc::ClientContext* context, " + "const $Request$& request, " + "::grpc::CompletionQueue* cq) {\n"); + printer->Print(*vars, + " return new " + "::grpc::ClientAsyncResponseReader< $Response$>(" + "channel_.get(), cq, " + "rpcmethod_$Method$_, " + "context, request);\n" + "}\n\n"); + } else if (method->ClientOnlyStreaming()) { + printer->Print(*vars, + "::grpc::ClientWriter< $Request$>* " + "$ns$$Service$::Stub::$Method$Raw(" + "::grpc::ClientContext* context, $Response$* response) {\n"); + printer->Print(*vars, + " return new ::grpc::ClientWriter< $Request$>(" + "channel_.get(), " + "rpcmethod_$Method$_, " + "context, response);\n" + "}\n\n"); + printer->Print(*vars, + "::grpc::ClientAsyncWriter< $Request$>* " + "$ns$$Service$::Stub::Async$Method$Raw(" + "::grpc::ClientContext* context, $Response$* response, " + "::grpc::CompletionQueue* cq, void* tag) {\n"); + printer->Print(*vars, + " return new ::grpc::ClientAsyncWriter< $Request$>(" + "channel_.get(), cq, " + "rpcmethod_$Method$_, " + "context, response, tag);\n" + "}\n\n"); + } else if (method->ServerOnlyStreaming()) { + printer->Print( + *vars, + "::grpc::ClientReader< $Response$>* " + "$ns$$Service$::Stub::$Method$Raw(" + "::grpc::ClientContext* context, const $Request$& request) {\n"); + printer->Print(*vars, + " return new ::grpc::ClientReader< $Response$>(" + "channel_.get(), " + "rpcmethod_$Method$_, " + "context, request);\n" + "}\n\n"); + printer->Print(*vars, + "::grpc::ClientAsyncReader< $Response$>* " + "$ns$$Service$::Stub::Async$Method$Raw(" + "::grpc::ClientContext* context, const $Request$& request, " + "::grpc::CompletionQueue* cq, void* tag) {\n"); + printer->Print(*vars, + " return new ::grpc::ClientAsyncReader< $Response$>(" + "channel_.get(), cq, " + "rpcmethod_$Method$_, " + "context, request, tag);\n" + "}\n\n"); + } else if (method->BidiStreaming()) { + printer->Print( + *vars, + "::grpc::ClientReaderWriter< $Request$, $Response$>* " + "$ns$$Service$::Stub::$Method$Raw(::grpc::ClientContext* context) {\n"); + printer->Print(*vars, + " return new ::grpc::ClientReaderWriter< " + "$Request$, $Response$>(" + "channel_.get(), " + "rpcmethod_$Method$_, " + "context);\n" + "}\n\n"); + printer->Print( + *vars, + "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>* " + "$ns$$Service$::Stub::Async$Method$Raw(::grpc::ClientContext* context, " + "::grpc::CompletionQueue* cq, void* tag) {\n"); + printer->Print(*vars, + " return new " + "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>(" + "channel_.get(), cq, " + "rpcmethod_$Method$_, " + "context, tag);\n" + "}\n\n"); + } +} + +void PrintSourceServerMethod(Printer *printer, + const Method *method, + std::map *vars) { + (*vars)["Method"] = method->name(); + (*vars)["Request"] = method->input_type_name(); + (*vars)["Response"] = method->output_type_name(); + if (method->NoStreaming()) { + printer->Print(*vars, + "::grpc::Status $ns$$Service$::Service::$Method$(" + "::grpc::ServerContext* context, " + "const $Request$* request, $Response$* response) {\n"); + printer->Print(" (void) context;\n"); + printer->Print(" (void) request;\n"); + printer->Print(" (void) response;\n"); + printer->Print( + " return ::grpc::Status(" + "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"); + printer->Print("}\n\n"); + } else if (method->ClientOnlyStreaming()) { + printer->Print(*vars, + "::grpc::Status $ns$$Service$::Service::$Method$(" + "::grpc::ServerContext* context, " + "::grpc::ServerReader< $Request$>* reader, " + "$Response$* response) {\n"); + printer->Print(" (void) context;\n"); + printer->Print(" (void) reader;\n"); + printer->Print(" (void) response;\n"); + printer->Print( + " return ::grpc::Status(" + "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"); + printer->Print("}\n\n"); + } else if (method->ServerOnlyStreaming()) { + printer->Print(*vars, + "::grpc::Status $ns$$Service$::Service::$Method$(" + "::grpc::ServerContext* context, " + "const $Request$* request, " + "::grpc::ServerWriter< $Response$>* writer) {\n"); + printer->Print(" (void) context;\n"); + printer->Print(" (void) request;\n"); + printer->Print(" (void) writer;\n"); + printer->Print( + " return ::grpc::Status(" + "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"); + printer->Print("}\n\n"); + } else if (method->BidiStreaming()) { + printer->Print(*vars, + "::grpc::Status $ns$$Service$::Service::$Method$(" + "::grpc::ServerContext* context, " + "::grpc::ServerReaderWriter< $Response$, $Request$>* " + "stream) {\n"); + printer->Print(" (void) context;\n"); + printer->Print(" (void) stream;\n"); + printer->Print( + " return ::grpc::Status(" + "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"); + printer->Print("}\n\n"); + } +} + +void PrintSourceService(Printer *printer, + const Service *service, + std::map *vars) { + (*vars)["Service"] = service->name(); + + printer->Print(*vars, + "static const char* $prefix$$Service$_method_names[] = {\n"); + for (int i = 0; i < service->method_count(); ++i) { + (*vars)["Method"] = service->method(i).get()->name(); + printer->Print(*vars, " \"/$Package$$Service$/$Method$\",\n"); + } + printer->Print(*vars, "};\n\n"); + + printer->Print(*vars, + "std::unique_ptr< $ns$$Service$::Stub> $ns$$Service$::NewStub(" + "const std::shared_ptr< ::grpc::ChannelInterface>& channel, " + "const ::grpc::StubOptions& options) {\n" + " std::unique_ptr< $ns$$Service$::Stub> stub(new " + "$ns$$Service$::Stub(channel));\n" + " return stub;\n" + "}\n\n"); + printer->Print(*vars, + "$ns$$Service$::Stub::Stub(const std::shared_ptr< " + "::grpc::ChannelInterface>& channel)\n"); + printer->Indent(); + printer->Print(": channel_(channel)"); + for (int i = 0; i < service->method_count(); ++i) { + auto method = service->method(i); + (*vars)["Method"] = method->name(); + (*vars)["Idx"] = as_string(i); + if (method->NoStreaming()) { + (*vars)["StreamingType"] = "NORMAL_RPC"; + } else if (method->ClientOnlyStreaming()) { + (*vars)["StreamingType"] = "CLIENT_STREAMING"; + } else if (method->ServerOnlyStreaming()) { + (*vars)["StreamingType"] = "SERVER_STREAMING"; + } else { + (*vars)["StreamingType"] = "BIDI_STREAMING"; + } + printer->Print(*vars, + ", rpcmethod_$Method$_(" + "$prefix$$Service$_method_names[$Idx$], " + "::grpc::RpcMethod::$StreamingType$, " + "channel" + ")\n"); + } + printer->Print("{}\n\n"); + printer->Outdent(); + + for (int i = 0; i < service->method_count(); ++i) { + (*vars)["Idx"] = as_string(i); + PrintSourceClientMethod(printer, service->method(i).get(), vars); + } + + printer->Print(*vars, "$ns$$Service$::Service::Service() {\n"); + printer->Indent(); + printer->Print(*vars, "(void)$prefix$$Service$_method_names;\n"); + for (int i = 0; i < service->method_count(); ++i) { + auto method = service->method(i); + (*vars)["Idx"] = as_string(i); + (*vars)["Method"] = method->name(); + (*vars)["Request"] = method->input_type_name(); + (*vars)["Response"] = method->output_type_name(); + if (method->NoStreaming()) { + printer->Print( + *vars, + "AddMethod(new ::grpc::RpcServiceMethod(\n" + " $prefix$$Service$_method_names[$Idx$],\n" + " ::grpc::RpcMethod::NORMAL_RPC,\n" + " new ::grpc::RpcMethodHandler< $ns$$Service$::Service, " + "$Request$, " + "$Response$>(\n" + " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n"); + } else if (method->ClientOnlyStreaming()) { + printer->Print( + *vars, + "AddMethod(new ::grpc::RpcServiceMethod(\n" + " $prefix$$Service$_method_names[$Idx$],\n" + " ::grpc::RpcMethod::CLIENT_STREAMING,\n" + " new ::grpc::ClientStreamingHandler< " + "$ns$$Service$::Service, $Request$, $Response$>(\n" + " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n"); + } else if (method->ServerOnlyStreaming()) { + printer->Print( + *vars, + "AddMethod(new ::grpc::RpcServiceMethod(\n" + " $prefix$$Service$_method_names[$Idx$],\n" + " ::grpc::RpcMethod::SERVER_STREAMING,\n" + " new ::grpc::ServerStreamingHandler< " + "$ns$$Service$::Service, $Request$, $Response$>(\n" + " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n"); + } else if (method->BidiStreaming()) { + printer->Print( + *vars, + "AddMethod(new ::grpc::RpcServiceMethod(\n" + " $prefix$$Service$_method_names[$Idx$],\n" + " ::grpc::RpcMethod::BIDI_STREAMING,\n" + " new ::grpc::BidiStreamingHandler< " + "$ns$$Service$::Service, $Request$, $Response$>(\n" + " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n"); + } + } + printer->Outdent(); + printer->Print(*vars, "}\n\n"); + printer->Print(*vars, + "$ns$$Service$::Service::~Service() {\n" + "}\n\n"); + for (int i = 0; i < service->method_count(); ++i) { + (*vars)["Idx"] = as_string(i); + PrintSourceServerMethod(printer, service->method(i).get(), vars); + } +} + +grpc::string GetSourceServices(File *file, + const Parameters ¶ms) { + grpc::string output; + { + // Scope the output stream so it closes and finalizes output to the string. + auto printer = file->CreatePrinter(&output); + std::map vars; + // Package string is empty or ends with a dot. It is used to fully qualify + // method names. + vars["Package"] = file->package(); + if (!file->package().empty()) { + vars["Package"].append("."); + } + if (!params.services_namespace.empty()) { + vars["ns"] = params.services_namespace + "::"; + vars["prefix"] = params.services_namespace; + } else { + vars["ns"] = ""; + vars["prefix"] = ""; + } + + for (int i = 0; i < file->service_count(); ++i) { + PrintSourceService(printer.get(), file->service(i).get(), &vars); + printer->Print("\n"); + } + } + return output; +} + +grpc::string GetSourceEpilogue(File *file, const Parameters & /*params*/) { + grpc::string temp; + + if (!file->package().empty()) { + std::vector parts = file->package_parts(); + + for (auto part = parts.begin(); part != parts.end(); part++) { + temp.append("} // namespace "); + temp.append(*part); + temp.append("\n"); + } + temp.append("\n"); + } + + return temp; +} + +} // namespace grpc_cpp_generator diff --git a/grpc/src/compiler/cpp_generator.h b/grpc/src/compiler/cpp_generator.h new file mode 100644 index 000000000..953ddfd56 --- /dev/null +++ b/grpc/src/compiler/cpp_generator.h @@ -0,0 +1,147 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef GRPC_INTERNAL_COMPILER_CPP_GENERATOR_H +#define GRPC_INTERNAL_COMPILER_CPP_GENERATOR_H + +// cpp_generator.h/.cc do not directly depend on GRPC/ProtoBuf, such that they +// can be used to generate code for other serialization systems, such as +// FlatBuffers. + +#include +#include + +#ifndef GRPC_CUSTOM_STRING +#include +#define GRPC_CUSTOM_STRING std::string +#endif + +namespace grpc { + +typedef GRPC_CUSTOM_STRING string; + +} // namespace grpc + +namespace grpc_cpp_generator { + +// Contains all the parameters that are parsed from the command line. +struct Parameters { + // Puts the service into a namespace + grpc::string services_namespace; + // Use system includes (<>) or local includes ("") + bool use_system_headers; + // Prefix to any grpc include + grpc::string grpc_search_path; +}; + +// An abstract interface representing a method. +struct Method { + virtual ~Method() {} + + virtual grpc::string name() const = 0; + + virtual grpc::string input_type_name() const = 0; + virtual grpc::string output_type_name() const = 0; + + virtual bool NoStreaming() const = 0; + virtual bool ClientOnlyStreaming() const = 0; + virtual bool ServerOnlyStreaming() const = 0; + virtual bool BidiStreaming() const = 0; +}; + +// An abstract interface representing a service. +struct Service { + virtual ~Service() {} + + virtual grpc::string name() const = 0; + + virtual int method_count() const = 0; + virtual std::unique_ptr method(int i) const = 0; +}; + +struct Printer { + virtual ~Printer() {} + + virtual void Print(const std::map &vars, + const char *template_string) = 0; + virtual void Print(const char *string) = 0; + virtual void Indent() = 0; + virtual void Outdent() = 0; +}; + +// An interface that allows the source generated to be output using various +// libraries/idls/serializers. +struct File { + virtual ~File() {} + + virtual grpc::string filename() const = 0; + virtual grpc::string filename_without_ext() const = 0; + virtual grpc::string message_header_ext() const = 0; + virtual grpc::string service_header_ext() const = 0; + virtual grpc::string package() const = 0; + virtual std::vector package_parts() const = 0; + virtual grpc::string additional_headers() const = 0; + + virtual int service_count() const = 0; + virtual std::unique_ptr service(int i) const = 0; + + virtual std::unique_ptr CreatePrinter(grpc::string *str) const = 0; +}; + +// Return the prologue of the generated header file. +grpc::string GetHeaderPrologue(File *file, const Parameters ¶ms); + +// Return the includes needed for generated header file. +grpc::string GetHeaderIncludes(File *file, const Parameters ¶ms); + +// Return the includes needed for generated source file. +grpc::string GetSourceIncludes(File *file, const Parameters ¶ms); + +// Return the epilogue of the generated header file. +grpc::string GetHeaderEpilogue(File *file, const Parameters ¶ms); + +// Return the prologue of the generated source file. +grpc::string GetSourcePrologue(File *file, const Parameters ¶ms); + +// Return the services for generated header file. +grpc::string GetHeaderServices(File *file, const Parameters ¶ms); + +// Return the services for generated source file. +grpc::string GetSourceServices(File *file, const Parameters ¶ms); + +// Return the epilogue of the generated source file. +grpc::string GetSourceEpilogue(File *file, const Parameters ¶ms); + +} // namespace grpc_cpp_generator + +#endif // GRPC_INTERNAL_COMPILER_CPP_GENERATOR_H diff --git a/grpc/tests/grpctest.cpp b/grpc/tests/grpctest.cpp new file mode 100644 index 000000000..d1b53776b --- /dev/null +++ b/grpc/tests/grpctest.cpp @@ -0,0 +1,124 @@ +/* + * Copyright 2014 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include + +#include "monster_test_generated.h" +#include "monster_test.grpc.fb.h" + +using namespace MyGame::Example; + +// The callback implementation of our server, that derives from the generated +// code. It implements all rpcs specified in the FlatBuffers schema. +class ServiceImpl final : public MyGame::Example::MonsterStorage::Service { + virtual ::grpc::Status Store(::grpc::ServerContext* context, + const flatbuffers::BufferRef *request, + flatbuffers::BufferRef *response) + override { + // Create a response from the incoming request name. + fbb_.Clear(); + auto stat_offset = CreateStat(fbb_, fbb_.CreateString("Hello, " + + request->GetRoot()->name()->str())); + fbb_.Finish(stat_offset); + // Since we keep reusing the same FlatBufferBuilder, the memory it owns + // remains valid until the next call (this BufferRef doesn't own the + // memory it points to). + *response = flatbuffers::BufferRef(fbb_.GetBufferPointer(), + fbb_.GetSize()); + return grpc::Status::OK; + } + virtual ::grpc::Status Retrieve(::grpc::ServerContext *context, + const flatbuffers::BufferRef *request, + flatbuffers::BufferRef *response) + override { + assert(false); // We're not actually using this RPC. + return grpc::Status::CANCELLED; + } + + private: + flatbuffers::FlatBufferBuilder fbb_; +}; + +// Track the server instance, so we can terminate it later. +grpc::Server *server_instance = nullptr; +// Mutex to protec this variable. +std::mutex wait_for_server; +std::condition_variable server_instance_cv; + +// This function implements the server thread. +void RunServer() { + auto server_address = "0.0.0.0:50051"; + // Callback interface we implemented above. + ServiceImpl service; + grpc::ServerBuilder builder; + builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); + builder.RegisterService(&service); + + // Start the server. Lock to change the variable we're changing. + wait_for_server.lock(); + server_instance = builder.BuildAndStart().release(); + wait_for_server.unlock(); + server_instance_cv.notify_one(); + + std::cout << "Server listening on " << server_address << std::endl; + // This will block the thread and serve requests. + server_instance->Wait(); +} + +int main(int /*argc*/, const char * /*argv*/[]) { + // Launch server. + std::thread server_thread(RunServer); + + // wait for server to spin up. + std::unique_lock lock(wait_for_server); + while (!server_instance) server_instance_cv.wait(lock); + + // Now connect the client. + auto channel = grpc::CreateChannel("localhost:50051", + grpc::InsecureChannelCredentials()); + auto stub = MyGame::Example::MonsterStorage::NewStub(channel); + + grpc::ClientContext context; + + // Build a request with the name set. + flatbuffers::FlatBufferBuilder fbb; + auto monster_offset = CreateMonster(fbb, 0, 0, 0, fbb.CreateString("Fred")); + fbb.Finish(monster_offset); + auto request = flatbuffers::BufferRef(fbb.GetBufferPointer(), + fbb.GetSize()); + flatbuffers::BufferRef response; + + // The actual RPC. + auto status = stub->Store(&context, request, &response); + + if (status.ok()) { + auto resp = response.GetRoot()->id(); + std::cout << "RPC response: " << resp->str() << std::endl; + } else { + std::cout << "RPC failed" << std::endl; + } + + server_instance->Shutdown(); + + server_thread.join(); + + delete server_instance; + + return 0; +} + diff --git a/include/flatbuffers/code_generators.h b/include/flatbuffers/code_generators.h index 95fa0c1ac..e7450a7a0 100644 --- a/include/flatbuffers/code_generators.h +++ b/include/flatbuffers/code_generators.h @@ -24,11 +24,12 @@ class BaseGenerator { virtual bool generate() = 0; static const std::string NamespaceDir(const Parser &parser, - const std::string &path) { + const std::string &path, + const Namespace &ns) { EnsureDirExists(path.c_str()); if (parser.opts.one_file) return path; std::string namespace_dir = path; // Either empty or ends in separator. - auto &namespaces = parser.namespaces_.back()->components; + auto &namespaces = ns.components; for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { namespace_dir += *it + kPathSeparator; EnsureDirExists(namespace_dir.c_str()); @@ -38,17 +39,24 @@ class BaseGenerator { protected: BaseGenerator(const Parser &parser, const std::string &path, - const std::string &file_name) + const std::string &file_name, + const std::string qualifying_start, + const std::string qualifying_separator) : parser_(parser), path_(path), file_name_(file_name), - namespace_dir_(BaseGenerator::NamespaceDir(parser, path)){}; + qualifying_start_(qualifying_start), + qualifying_separator_(qualifying_separator){}; virtual ~BaseGenerator(){}; // No copy/assign. BaseGenerator &operator=(const BaseGenerator &); BaseGenerator(const BaseGenerator &); + const std::string NamespaceDir(const Namespace &ns) { + return BaseGenerator::NamespaceDir(parser_, path_, ns); + } + const char *FlatBuffersGeneratedWarning() { return "automatically generated by the FlatBuffers compiler," " do not modify\n\n"; @@ -66,9 +74,9 @@ class BaseGenerator { return true; } - std::string FullNamespace(const char *separator) { + std::string FullNamespace(const char *separator, const Namespace &ns) { std::string namespace_name; - auto &namespaces = parser_.namespaces_.back()->components; + auto &namespaces = ns.components; for (auto it = namespaces.begin(); it != namespaces.end(); ++it) { if (namespace_name.length()) namespace_name += separator; namespace_name += *it; @@ -76,15 +84,36 @@ class BaseGenerator { return namespace_name; } - const std::string LastNamespacePart() { - auto &namespaces = parser_.namespaces_.back()->components; + const std::string LastNamespacePart(const Namespace &ns) { + auto &namespaces = ns.components; if (namespaces.size()) return *(namespaces.end() - 1); else return std::string(""); } + // tracks the current namespace for early exit in WrapInNameSpace + // c++, java and csharp returns a different namespace from + // the following default (no early exit, always fully qualify), + // which works for js and php + virtual const Namespace *CurrentNameSpace() { return nullptr; } + + // Ensure that a type is prefixed with its namespace whenever it is used + // outside of its namespace. + std::string WrapInNameSpace(const Namespace *ns, const std::string &name) { + if (CurrentNameSpace() == ns) return name; + std::string qualified_name = qualifying_start_; + for (auto it = ns->components.begin(); it != ns->components.end(); ++it) + qualified_name += *it + qualifying_separator_; + return qualified_name + name; + } + + std::string WrapInNameSpace(const Definition &def) { + return WrapInNameSpace(def.defined_namespace, def.name); + } + const Parser &parser_; const std::string &path_; const std::string &file_name_; - const std::string namespace_dir_; + const std::string qualifying_start_; + const std::string qualifying_separator_; }; } // namespace flatbuffers diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 272901163..6755ec279 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -36,16 +36,17 @@ (!defined(_MSC_VER) || _MSC_VER < 1600) && \ (!defined(__GNUC__) || \ (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40400)) - #error A C++11 compatible compiler with support for the auto typing is required for FlatBuffers. + #error A C++11 compatible compiler with support for the auto typing is \ + required for FlatBuffers. #error __cplusplus _MSC_VER __GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__ #endif #if !defined(__clang__) && \ defined(__GNUC__) && \ (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__ < 40600) - // Backwards compatability for g++ 4.4, and 4.5 which don't have the nullptr and constexpr - // keywords. Note the __clang__ check is needed, because clang presents itself as an older GNUC - // compiler. + // Backwards compatability for g++ 4.4, and 4.5 which don't have the nullptr + // and constexpr keywords. Note the __clang__ check is needed, because clang + // presents itself as an older GNUC compiler. #ifndef nullptr_t const class nullptr_t { public: @@ -207,6 +208,7 @@ template size_t AlignOf() { // (avoiding the need for a trailing return decltype) template struct IndirectHelper { typedef T return_type; + typedef T mutable_return_type; static const size_t element_stride = sizeof(T); static return_type Read(const uint8_t *p, uoffset_t i) { return EndianScalar((reinterpret_cast(p))[i]); @@ -214,6 +216,7 @@ template struct IndirectHelper { }; template struct IndirectHelper> { typedef const T *return_type; + typedef T *mutable_return_type; static const size_t element_stride = sizeof(uoffset_t); static return_type Read(const uint8_t *p, uoffset_t i) { p += i * sizeof(uoffset_t); @@ -222,6 +225,7 @@ template struct IndirectHelper> { }; template struct IndirectHelper { typedef const T *return_type; + typedef T *mutable_return_type; static const size_t element_stride = sizeof(T); static return_type Read(const uint8_t *p, uoffset_t i) { return reinterpret_cast(p + i * sizeof(T)); @@ -306,6 +310,7 @@ public: uoffset_t Length() const { return size(); } typedef typename IndirectHelper::return_type return_type; + typedef typename IndirectHelper::mutable_return_type mutable_return_type; return_type Get(uoffset_t i) const { assert(i < size()); @@ -347,6 +352,12 @@ public: WriteScalar(data() + i, val - (Data() + i * sizeof(uoffset_t))); } + // Get a mutable pointer to tables/strings inside this vector. + mutable_return_type GetMutableObject(uoffset_t i) const { + assert(i < size()); + return const_cast(IndirectHelper::Read(Data(), i)); + } + // The raw data in little endian format. Use with care. const uint8_t *Data() const { return reinterpret_cast(&length_ + 1); @@ -973,6 +984,33 @@ FLATBUFFERS_FINAL_CLASS return CreateVector(v.data(), v.size()); } + /// @brief Serialize values returned by a function into a FlatBuffer `vector`. + /// This is a convenience function that takes care of iteration for you. + /// @tparam T The data type of the `std::vector` elements. + /// @param f A function that takes the current iteration 0..vector_size-1 and + /// returns any type that you can construct a FlatBuffers vector out of. + /// @return Returns a typed `Offset` into the serialized data indicating + /// where the vector is stored. + template Offset> CreateVector(size_t vector_size, + const std::function &f) { + std::vector elems(vector_size); + for (size_t i = 0; i < vector_size; i++) elems[i] = f(i); + return CreateVector(elems.data(), elems.size()); + } + + /// @brief Serialize a `std::vector` into a FlatBuffer `vector`. + /// This is a convenience function for a common case. + /// @param v A const reference to the `std::vector` to serialize into the + /// buffer as a `vector`. + /// @return Returns a typed `Offset` into the serialized data indicating + /// where the vector is stored. + Offset>> CreateVectorOfStrings( + const std::vector &v) { + std::vector> offsets(v.size()); + for (size_t i = 0; i < v.size(); i++) offsets[i] = CreateString(v[i]); + return CreateVector(offsets.data(), offsets.size()); + } + /// @brief Serialize an array of structs into a FlatBuffer `vector`. /// @tparam T The data type of the struct array elements. /// @param[in] v A pointer to the array of type `T` to serialize into the @@ -981,7 +1019,7 @@ FLATBUFFERS_FINAL_CLASS /// @return Returns a typed `Offset` into the serialized data indicating /// where the vector is stored. template Offset> CreateVectorOfStructs( - const T *v, size_t len) { + const T *v, size_t len) { StartVector(len * sizeof(T) / AlignOf(), AlignOf()); PushBytes(reinterpret_cast(v), sizeof(T) * len); return Offset>(EndVector(len)); @@ -994,7 +1032,7 @@ FLATBUFFERS_FINAL_CLASS /// @return Returns a typed `Offset` into the serialized data indicating /// where the vector is stored. template Offset> CreateVectorOfStructs( - const std::vector &v) { + const std::vector &v) { return CreateVectorOfStructs(v.data(), v.size()); } @@ -1023,7 +1061,7 @@ FLATBUFFERS_FINAL_CLASS /// @return Returns a typed `Offset` into the serialized data indicating /// where the vector is stored. template Offset>> CreateVectorOfSortedTables( - Offset *v, size_t len) { + Offset *v, size_t len) { std::sort(v, v + len, TableKeyComparator(buf_)); return CreateVector(v, len); } @@ -1036,7 +1074,7 @@ FLATBUFFERS_FINAL_CLASS /// @return Returns a typed `Offset` into the serialized data indicating /// where the vector is stored. template Offset>> CreateVectorOfSortedTables( - std::vector> *v) { + std::vector> *v) { return CreateVectorOfSortedTables(v->data(), v->size()); } @@ -1067,7 +1105,7 @@ FLATBUFFERS_FINAL_CLASS /// written to at a later time to serialize the data into a `vector` /// in the buffer. template Offset> CreateUninitializedVector( - size_t len, T **buf) { + size_t len, T **buf) { return CreateUninitializedVector(len, sizeof(T), reinterpret_cast(buf)); } @@ -1152,13 +1190,16 @@ template const T *GetRoot(const void *buf) { } /// Helpers to get a typed pointer to objects that are currently beeing built. -/// @warning Creating new objects will lead to reallocations and invalidates the pointer! -template T *GetMutableTemporaryPointer(FlatBufferBuilder &fbb, Offset offset) { +/// @warning Creating new objects will lead to reallocations and invalidates +/// the pointer! +template T *GetMutableTemporaryPointer(FlatBufferBuilder &fbb, + Offset offset) { return reinterpret_cast(fbb.GetCurrentBufferPointer() + fbb.GetSize() - offset.o); } -template const T *GetTemporaryPointer(FlatBufferBuilder &fbb, Offset offset) { +template const T *GetTemporaryPointer(FlatBufferBuilder &fbb, + Offset offset) { return GetMutableTemporaryPointer(fbb, offset); } @@ -1293,6 +1334,29 @@ class Verifier FLATBUFFERS_FINAL_CLASS { size_t max_tables_; }; +// Convenient way to bundle a buffer and its length, to pass it around +// typed by its root. +// A BufferRef does not own its buffer. +struct BufferRefBase {}; // for std::is_base_of +template struct BufferRef : BufferRefBase { + BufferRef() : buf(nullptr), len(0), must_free(false) {} + BufferRef(uint8_t *_buf, uoffset_t _len) + : buf(_buf), len(_len), must_free(false) {} + + ~BufferRef() { if (must_free) free(buf); } + + const T *GetRoot() const { return flatbuffers::GetRoot(buf); } + + bool Verify() { + Verifier verifier(buf, len); + return verifier.VerifyBuffer(); + } + + uint8_t *buf; + uoffset_t len; + bool must_free; +}; + // "structs" are flat structures that do not have an offset table, thus // always have all members present and do not support forwards/backwards // compatible extensions. diff --git a/include/flatbuffers/grpc.h b/include/flatbuffers/grpc.h new file mode 100644 index 000000000..52a64408d --- /dev/null +++ b/include/flatbuffers/grpc.h @@ -0,0 +1,72 @@ +/* + * Copyright 2014 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLATBUFFERS_GRPC_H_ +#define FLATBUFFERS_GRPC_H_ + +// Helper functionality to glue FlatBuffers and GRPC. + +#include "grpc++/support/byte_buffer.h" +#include "grpc/byte_buffer_reader.h" + +namespace grpc { + +template +class SerializationTraits::value>::type> { + public: + // The type we're passing here is a BufferRef, which is already serialized + // FlatBuffer data, which then gets passed to GRPC. + static grpc::Status Serialize(const T& msg, + grpc_byte_buffer **buffer, + bool *own_buffer) { + // TODO(wvo): make this work without copying. + auto slice = gpr_slice_from_copied_buffer( + reinterpret_cast(msg.buf), msg.len); + *buffer = grpc_raw_byte_buffer_create(&slice, 1); + *own_buffer = true; + return grpc::Status(); + } + + // There is no de-serialization step in FlatBuffers, so we just receive + // the data from GRPC. + static grpc::Status Deserialize(grpc_byte_buffer *buffer, + T *msg, + int max_message_size) { + // TODO(wvo): make this more efficient / zero copy when possible. + auto len = grpc_byte_buffer_length(buffer); + msg->buf = reinterpret_cast(malloc(len)); + msg->len = static_cast(len); + msg->must_free = true; + uint8_t *current = msg->buf; + grpc_byte_buffer_reader reader; + grpc_byte_buffer_reader_init(&reader, buffer); + gpr_slice slice; + while (grpc_byte_buffer_reader_next(&reader, &slice)) { + memcpy(current, GPR_SLICE_START_PTR(slice), GPR_SLICE_LENGTH(slice)); + current += GPR_SLICE_LENGTH(slice); + gpr_slice_unref(slice); + } + GPR_ASSERT(current == msg->buf + msg->len); + grpc_byte_buffer_reader_destroy(&reader); + grpc_byte_buffer_destroy(buffer); + return grpc::Status(); + } +}; + +} // namespace grpc; + +#endif // FLATBUFFERS_GRPC_H_ diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index dc385efa3..b22bc08a1 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -359,6 +359,19 @@ struct IDLOptions { lang(IDLOptions::kJava) {} }; +// This encapsulates where the parser is in the current source file. +struct ParserState { + ParserState() : cursor_(nullptr), line_(1), token_(-1) {} + + protected: + const char *cursor_; + int line_; // the current line being parsed + int token_; + + std::string attribute_; + std::vector doc_comment_; +}; + // A way to make error propagation less error prone by requiring values to be // checked. // Once you create a value of this type you must either: @@ -400,14 +413,12 @@ class CheckedError { #define FLATBUFFERS_CHECKED_ERROR CheckedError #endif -class Parser { +class Parser : public ParserState { public: explicit Parser(const IDLOptions &options = IDLOptions()) : root_struct_def_(nullptr), opts(options), source_(nullptr), - cursor_(nullptr), - line_(1), anonymous_counter(0) { // Just in case none are declared: namespaces_.push_back(new Namespace()); @@ -421,7 +432,7 @@ class Parser { known_attributes_["original_order"] = true; known_attributes_["nested_flatbuffer"] = true; known_attributes_["csharp_partial"] = true; - known_attributes_["stream"] = true; + known_attributes_["streaming"] = true; known_attributes_["idempotent"] = true; } @@ -478,7 +489,8 @@ private: FieldDef **dest); FLATBUFFERS_CHECKED_ERROR ParseField(StructDef &struct_def); FLATBUFFERS_CHECKED_ERROR ParseAnyValue(Value &val, FieldDef *field, - size_t parent_fieldn); + size_t parent_fieldn, + const StructDef *parent_struct_def); FLATBUFFERS_CHECKED_ERROR ParseTable(const StructDef &struct_def, std::string *value, uoffset_t *ovalue); void SerializeStruct(const StructDef &struct_def, const Value &val); @@ -538,13 +550,9 @@ private: IDLOptions opts; private: - const char *source_, *cursor_; - int line_; // the current line being parsed - int token_; - std::string file_being_parsed_; + const char *source_; - std::string attribute_; - std::vector doc_comment_; + std::string file_being_parsed_; std::vector> field_stack_; @@ -671,6 +679,12 @@ extern std::string BinaryMakeRule(const Parser &parser, const std::string &path, const std::string &file_name); +// Generate GRPC interfaces. +// See idl_gen_grpc.cpp. +bool GenerateGRPC(const Parser &parser, + const std::string &path, + const std::string &file_name); + } // namespace flatbuffers #endif // FLATBUFFERS_IDL_H_ diff --git a/include/flatbuffers/reflection.h b/include/flatbuffers/reflection.h index ababe6ad4..87091438a 100644 --- a/include/flatbuffers/reflection.h +++ b/include/flatbuffers/reflection.h @@ -335,6 +335,8 @@ template pointer_inside_vector piv(T *ptr, return pointer_inside_vector(ptr, vec); } +inline const char *UnionTypeFieldSuffix() { return "_type"; } + // Helper to figure out the actual table type a union refers to. inline const reflection::Object &GetUnionType( const reflection::Schema &schema, const reflection::Object &parent, @@ -342,7 +344,7 @@ inline const reflection::Object &GetUnionType( auto enumdef = schema.enums()->Get(unionfield.type()->index()); // TODO: this is clumsy and slow, but no other way to find it? auto type_field = parent.fields()->LookupByKey( - (unionfield.name()->str() + "_type").c_str()); + (unionfield.name()->str() + UnionTypeFieldSuffix()).c_str()); assert(type_field); auto union_type = GetFieldI(table, *type_field); auto enumval = enumdef->values()->LookupByKey(union_type); diff --git a/include/flatbuffers/util.h b/include/flatbuffers/util.h index cfb211f09..7bd7513bb 100644 --- a/include/flatbuffers/util.h +++ b/include/flatbuffers/util.h @@ -35,9 +35,10 @@ #include #include #else -#include #include #endif +#include +#include #include "flatbuffers/flatbuffers.h" @@ -124,6 +125,9 @@ FileExistsFunction SetFileExistsFunction(FileExistsFunction // Check if file "name" exists. bool FileExists(const char *name); +// Check if "name" exists and it is also a directory. +bool DirExists(const char *name); + // Load file "name" into "buf" returning true if successful // false otherwise. If "binary" is false data is read // using ifstream's text mode, otherwise data is read with diff --git a/java/com/google/flatbuffers/Constants.java b/java/com/google/flatbuffers/Constants.java index ac0593ae6..f59063145 100644 --- a/java/com/google/flatbuffers/Constants.java +++ b/java/com/google/flatbuffers/Constants.java @@ -23,10 +23,18 @@ package com.google.flatbuffers; */ public class Constants { // Java doesn't seem to have these. + /** The number of bytes in an `byte`. */ + static final int SIZEOF_BYTE = 1; /** The number of bytes in a `short`. */ static final int SIZEOF_SHORT = 2; /** The number of bytes in an `int`. */ static final int SIZEOF_INT = 4; + /** The number of bytes in an `float`. */ + static final int SIZEOF_FLOAT = 4; + /** The number of bytes in an `long`. */ + static final int SIZEOF_LONG = 8; + /** The number of bytes in an `double`. */ + static final int SIZEOF_DOUBLE = 8; /** The number of bytes in a file identifier. */ static final int FILE_IDENTIFIER_LENGTH = 4; } diff --git a/java/com/google/flatbuffers/FlatBufferBuilder.java b/java/com/google/flatbuffers/FlatBufferBuilder.java index fecb213f6..c2186fa7b 100644 --- a/java/com/google/flatbuffers/FlatBufferBuilder.java +++ b/java/com/google/flatbuffers/FlatBufferBuilder.java @@ -188,7 +188,7 @@ public class FlatBufferBuilder { * * @param x A `boolean` to put into the buffer. */ - public void putBoolean(boolean x) { bb.put (space -= 1, (byte)(x ? 1 : 0)); } + public void putBoolean(boolean x) { bb.put (space -= Constants.SIZEOF_BYTE, (byte)(x ? 1 : 0)); } /** * Add a `byte` to the buffer, backwards from the current location. Doesn't align nor @@ -196,7 +196,7 @@ public class FlatBufferBuilder { * * @param x A `byte` to put into the buffer. */ - public void putByte (byte x) { bb.put (space -= 1, x); } + public void putByte (byte x) { bb.put (space -= Constants.SIZEOF_BYTE, x); } /** * Add a `short` to the buffer, backwards from the current location. Doesn't align nor @@ -204,7 +204,7 @@ public class FlatBufferBuilder { * * @param x A `short` to put into the buffer. */ - public void putShort (short x) { bb.putShort (space -= 2, x); } + public void putShort (short x) { bb.putShort (space -= Constants.SIZEOF_SHORT, x); } /** * Add an `int` to the buffer, backwards from the current location. Doesn't align nor @@ -212,7 +212,7 @@ public class FlatBufferBuilder { * * @param x An `int` to put into the buffer. */ - public void putInt (int x) { bb.putInt (space -= 4, x); } + public void putInt (int x) { bb.putInt (space -= Constants.SIZEOF_INT, x); } /** * Add a `long` to the buffer, backwards from the current location. Doesn't align nor @@ -220,7 +220,7 @@ public class FlatBufferBuilder { * * @param x A `long` to put into the buffer. */ - public void putLong (long x) { bb.putLong (space -= 8, x); } + public void putLong (long x) { bb.putLong (space -= Constants.SIZEOF_LONG, x); } /** * Add a `float` to the buffer, backwards from the current location. Doesn't align nor @@ -228,7 +228,7 @@ public class FlatBufferBuilder { * * @param x A `float` to put into the buffer. */ - public void putFloat (float x) { bb.putFloat (space -= 4, x); } + public void putFloat (float x) { bb.putFloat (space -= Constants.SIZEOF_FLOAT, x); } /** * Add a `double` to the buffer, backwards from the current location. Doesn't align nor @@ -236,7 +236,7 @@ public class FlatBufferBuilder { * * @param x A `double` to put into the buffer. */ - public void putDouble (double x) { bb.putDouble(space -= 8, x); } + public void putDouble (double x) { bb.putDouble(space -= Constants.SIZEOF_DOUBLE, x); } /// @endcond /** @@ -244,49 +244,49 @@ public class FlatBufferBuilder { * * @param x A `boolean` to put into the buffer. */ - public void addBoolean(boolean x) { prep(1, 0); putBoolean(x); } + public void addBoolean(boolean x) { prep(Constants.SIZEOF_BYTE, 0); putBoolean(x); } /** * Add a `byte` to the buffer, properly aligned, and grows the buffer (if necessary). * * @param x A `byte` to put into the buffer. */ - public void addByte (byte x) { prep(1, 0); putByte (x); } + public void addByte (byte x) { prep(Constants.SIZEOF_BYTE, 0); putByte (x); } /** * Add a `short` to the buffer, properly aligned, and grows the buffer (if necessary). * * @param x A `short` to put into the buffer. */ - public void addShort (short x) { prep(2, 0); putShort (x); } + public void addShort (short x) { prep(Constants.SIZEOF_SHORT, 0); putShort (x); } /** * Add an `int` to the buffer, properly aligned, and grows the buffer (if necessary). * * @param x An `int` to put into the buffer. */ - public void addInt (int x) { prep(4, 0); putInt (x); } + public void addInt (int x) { prep(Constants.SIZEOF_INT, 0); putInt (x); } /** * Add a `long` to the buffer, properly aligned, and grows the buffer (if necessary). * * @param x A `long` to put into the buffer. */ - public void addLong (long x) { prep(8, 0); putLong (x); } + public void addLong (long x) { prep(Constants.SIZEOF_LONG, 0); putLong (x); } /** * Add a `float` to the buffer, properly aligned, and grows the buffer (if necessary). * * @param x A `float` to put into the buffer. */ - public void addFloat (float x) { prep(4, 0); putFloat (x); } + public void addFloat (float x) { prep(Constants.SIZEOF_FLOAT, 0); putFloat (x); } /** * Add a `double` to the buffer, properly aligned, and grows the buffer (if necessary). * * @param x A `double` to put into the buffer. */ - public void addDouble (double x) { prep(8, 0); putDouble (x); } + public void addDouble (double x) { prep(Constants.SIZEOF_DOUBLE, 0); putDouble (x); } /** * Adds on offset, relative to where it will be written. diff --git a/samples/android/jni/Application.mk b/samples/android/jni/Application.mk index 56952fc96..2fc9c7379 100755 --- a/samples/android/jni/Application.mk +++ b/samples/android/jni/Application.mk @@ -18,5 +18,5 @@ APP_PROJECT_PATH := $(call my-dir)/.. APP_STL := gnustl_static APP_ABI := armeabi-v7a -NDK_TOOLCHAIN_VERSION := 4.8 + APP_CPPFLAGS += -std=c++11 diff --git a/src/flatc.cpp b/src/flatc.cpp index d4be151c8..c7ba29e86 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -74,10 +74,14 @@ const Generator generators[] = { flatbuffers::IDLOptions::kMAX, "Generate Python files for tables/structs", flatbuffers::GeneralMakeRule }, - { flatbuffers::GeneratePhp, nullptr, "--php", "PHP", + { flatbuffers::GeneratePhp, nullptr, "--php", "PHP", flatbuffers::IDLOptions::kMAX, "Generate PHP files for tables/structs", flatbuffers::GeneralMakeRule }, + { flatbuffers::GenerateGRPC, nullptr, "--grpc", "GRPC", + flatbuffers::IDLOptions::kMAX, + "Generate GRPC interfaces", + flatbuffers::CPPMakeRule }, }; const char *program_name = nullptr; diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 7efa1334e..c690240d6 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -22,706 +22,6 @@ #include "flatbuffers/code_generators.h" namespace flatbuffers { -namespace cpp { - -// This tracks the current namespace so we can insert namespace declarations. -// TODO(wvo): this needs to be moved into a code generator context object. -static const Namespace *code_generator_cur_name_space = nullptr; - -// Ensure that a type is prefixed with its namespace whenever it is used -// outside of its namespace. -static std::string WrapInNameSpace(const Namespace *ns, - const std::string &name) { - if (code_generator_cur_name_space != ns) { - std::string qualified_name; - for (auto it = ns->components.begin(); - it != ns->components.end(); ++it) { - qualified_name += *it + "::"; - } - return qualified_name + name; - } else { - return name; - } -} - -static std::string WrapInNameSpace(const Definition &def) { - return WrapInNameSpace(def.defined_namespace, def.name); -} - -// Translates a qualified name in flatbuffer text format to the same name in -// the equivalent C++ namespace. -static std::string TranslateNameSpace(const std::string &qualified_name) { - std::string cpp_qualified_name = qualified_name; - size_t start_pos = 0; - while((start_pos = cpp_qualified_name.find(".", start_pos)) != - std::string::npos) { - cpp_qualified_name.replace(start_pos, 1, "::"); - } - return cpp_qualified_name; -} - - -// Return a C++ type from the table in idl.h -static std::string GenTypeBasic(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(*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(*type.struct_def); - } - case BASE_TYPE_UNION: - // fall through - default: - return "void"; - } -} - -// 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(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) that reflects its -// serialized size. -static std::string GenTypeSize(const Parser &parser, const Type &type) { - return IsScalar(type.base_type) - ? GenTypeBasic(type, false) - : IsStruct(type) - ? GenTypePointer(parser, type) - : "flatbuffers::uoffset_t"; -} - -// 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(type, user_facing_type) + afterbasic - : beforeptr + GenTypePointer(parser, type) + afterptr; -} - -static std::string GenEnumDecl(const EnumDef &enum_def, - const IDLOptions &opts) { - return (opts.scoped_enums ? "enum class " : "enum ") + enum_def.name; -} - -static std::string GenEnumVal(const EnumDef &enum_def, - const std::string &enum_val, - const IDLOptions &opts) { - return opts.prefixed_enums ? enum_def.name + "_" + enum_val : enum_val; -} - -static std::string GetEnumVal(const EnumDef &enum_def, const EnumVal &enum_val, - const IDLOptions &opts) { - if (opts.scoped_enums) { - return enum_def.name + "::" + enum_val.name; - } else if (opts.prefixed_enums) { - return enum_def.name + "_" + enum_val.name; - } else { - return enum_val.name; - } -} - -std::string EnumSignature(EnumDef &enum_def) { - return "inline bool Verify" + enum_def.name + - "(flatbuffers::Verifier &verifier, " + - "const void *union_obj, " + enum_def.name + " type)"; -} - -// Generate an enum declaration and an enum string lookup table. -static void GenEnum(const Parser &parser, EnumDef &enum_def, - std::string *code_ptr) { - std::string &code = *code_ptr; - GenComment(enum_def.doc_comment, code_ptr, nullptr); - code += GenEnumDecl(enum_def, parser.opts); - if (parser.opts.scoped_enums) - code += " : " + GenTypeBasic(enum_def.underlying_type, false); - code += " {\n"; - int64_t anyv = 0; - EnumVal *minv = nullptr, *maxv = nullptr; - for (auto it = enum_def.vals.vec.begin(); - it != enum_def.vals.vec.end(); - ++it) { - auto &ev = **it; - GenComment(ev.doc_comment, code_ptr, nullptr, " "); - code += " " + GenEnumVal(enum_def, ev.name, parser.opts) + " = "; - code += NumToString(ev.value) + ",\n"; - minv = !minv || minv->value > ev.value ? &ev : minv; - maxv = !maxv || maxv->value < ev.value ? &ev : maxv; - anyv |= ev.value; - } - if (parser.opts.scoped_enums || parser.opts.prefixed_enums) { - assert(minv && maxv); - if (enum_def.attributes.Lookup("bit_flags")) { - if (minv->value != 0) // If the user didn't defined NONE value - code += " " + GenEnumVal(enum_def, "NONE", parser.opts) + " = 0,\n"; - if (maxv->value != anyv) // If the user didn't defined ANY value - code += " " + GenEnumVal(enum_def, "ANY", parser.opts) + " = " + NumToString(anyv) + "\n"; - } else { // MIN & MAX are useless for bit_flags - code += " " + GenEnumVal(enum_def, "MIN", parser.opts) + " = "; - code += GenEnumVal(enum_def, minv->name, parser.opts) + ",\n"; - code += " " + GenEnumVal(enum_def, "MAX", parser.opts) + " = "; - code += GenEnumVal(enum_def, maxv->name, parser.opts) + "\n"; - } - } - code += "};\n"; - if (parser.opts.scoped_enums && enum_def.attributes.Lookup("bit_flags")) - code += "DEFINE_BITMASK_OPERATORS(" + enum_def.name + ", " + GenTypeBasic(enum_def.underlying_type, false) + ")\n"; - code += "\n"; - - // Generate a generate string table for enum values. - // Problem is, if values are very sparse that could generate really big - // tables. Ideally in that case we generate a map lookup instead, but for - // the moment we simply don't output a table at all. - auto range = enum_def.vals.vec.back()->value - - enum_def.vals.vec.front()->value + 1; - // Average distance between values above which we consider a table - // "too sparse". Change at will. - static const int kMaxSparseness = 5; - if (range / static_cast(enum_def.vals.vec.size()) < kMaxSparseness) { - code += "inline const char **EnumNames" + enum_def.name + "() {\n"; - code += " static const char *names[] = { "; - auto val = enum_def.vals.vec.front()->value; - for (auto it = enum_def.vals.vec.begin(); - it != enum_def.vals.vec.end(); - ++it) { - 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(), parser.opts) +")"; - } - code += "]; }\n\n"; - } - - if (enum_def.is_union) { - code += EnumSignature(enum_def) + ";\n\n"; - } -} - -static void GenEnumPost(const Parser &parser, EnumDef &enum_def, - std::string *code_ptr_post) { - // Generate a verifier function for this union that can be called by the - // table verifier functions. It uses a switch case to select a specific - // verifier function to call, this should be safe even if the union type - // has been corrupted, since the verifiers will simply fail when called - // on the wrong type. - std::string &code_post = *code_ptr_post; - code_post += EnumSignature(enum_def) + " {\n switch (type) {\n"; - for (auto it = enum_def.vals.vec.begin(); - it != enum_def.vals.vec.end(); - ++it) { - auto &ev = **it; - code_post += " case " + GetEnumVal(enum_def, ev, parser.opts); - if (!ev.value) { - code_post += ": return true;\n"; // "NONE" enum value. - } else { - code_post += ": return verifier.VerifyTable(reinterpret_cast(" + val + ")"; - } else { - return val; - } -} - -std::string GenFieldOffsetName(const FieldDef &field) { - std::string uname = field.name; - std::transform(uname.begin(), uname.end(), uname.begin(), ::toupper); - 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"; - } -} - -std::string GenDefaultConstant(const FieldDef &field) { - return field.value.type.base_type == BASE_TYPE_FLOAT - ? field.value.constant + "f" - : field.value.constant; -} - -// Generate an accessor struct, builder structs & function for a table. -static void GenTable(const Parser &parser, StructDef &struct_def, - std::string *code_ptr) { - 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 GetFullyQualifiedName - GenFullyQualifiedNameGetter(parser, struct_def.name, code); - // Generate field id constants. - if (struct_def.fields.vec.size() > 0) { - code += " enum {\n"; - bool is_first_field = true; // track the first field that's not deprecated - 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. - if (!is_first_field) { - // Add trailing comma and newline to previous element. Don't add trailing comma to - // last element since older versions of gcc complain about this. - code += ",\n"; - } else { - is_first_field = false; - } - code += " " + GenFieldOffsetName(field) + " = "; - code += NumToString(field.value.offset); - } - } - code += "\n };\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 += ", " + GenDefaultConstant(field); - call += ")"; - code += GenUnderlyingCast(field, true, call); - code += "; }\n"; - if (parser.opts.mutable_buffer) { - if (is_scalar) { - code += " bool mutate_" + field.name + "("; - code += GenTypeBasic(field.value.type, true); - code += " _" + field.name + ") { return SetField(" + offsetstr + ", "; - code += GenUnderlyingCast(field, false, "_" + field.name); - code += "); }\n"; - } else { - auto type = GenTypeGet(parser, field.value.type, " ", "", " *", true); - code += " " + type + "mutable_" + field.name + "() { return "; - code += GenUnderlyingCast(field, true, - accessor + type + ">(" + offsetstr + ")"); - code += "; }\n"; - } - } - auto nested = field.attributes.Lookup("nested_flatbuffer"); - if (nested) { - std::string qualified_name = - parser.namespaces_.back()->GetFullyQualifiedName(nested->constant); - auto nested_root = parser.structs_.Lookup(qualified_name); - assert(nested_root); // Guaranteed to exist by parser. - (void)nested_root; - std::string cpp_qualified_name = TranslateNameSpace(qualified_name); - - code += " const " + cpp_qualified_name + " *" + field.name; - code += "_nested_root() const { return flatbuffers::GetRoot<"; - code += cpp_qualified_name + ">(" + field.name + "()->Data()); }\n"; - } - // Generate a comparison function for this field if it is a key. - if (field.key) { - code += " bool KeyCompareLessThan(const " + struct_def.name; - code += " *o) const { return "; - if (field.value.type.base_type == BASE_TYPE_STRING) code += "*"; - code += field.name + "() < "; - if (field.value.type.base_type == BASE_TYPE_STRING) code += "*"; - code += "o->" + field.name + "(); }\n"; - code += " int KeyCompareWithValue("; - if (field.value.type.base_type == BASE_TYPE_STRING) { - code += "const char *val) const { return strcmp(" + field.name; - code += "()->c_str(), val); }\n"; - } else { - if (parser.opts.scoped_enums && - field.value.type.enum_def && - IsScalar(field.value.type.base_type)) { - code += GenTypeGet(parser, field.value.type, " ", "const ", " *", - true); - } else { - code += GenTypeBasic(field.value.type, false); - } - code += " val) const { return " + field.name + "() < val ? -1 : "; - code += field.name + "() > val; }\n"; - } - } - } - } - // 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; - code += "())"; - break; - } - case BASE_TYPE_STRUCT: { - if (!field.value.type.struct_def->fixed) { - code += prefix + "verifier.VerifyVectorOfTables(" + field.name; - code += "())"; - } - break; - } - default: - break; - } - break; - default: - break; - } - } - } - code += prefix + "verifier.EndTable()"; - code += ";\n }\n"; - code += "};\n\n"; - - // Generate a builder struct, with methods of the form: - // void add_name(type name) { fbb_.AddElement(offset, name, default); } - code += "struct " + struct_def.name; - code += "Builder {\n flatbuffers::FlatBufferBuilder &fbb_;\n"; - code += " flatbuffers::uoffset_t start_;\n"; - for (auto it = struct_def.fields.vec.begin(); - it != struct_def.fields.vec.end(); - ++it) { - auto &field = **it; - if (!field.deprecated) { - code += " void add_" + field.name + "("; - code += GenTypeWire(parser, field.value.type, " ", true) + field.name; - code += ") { fbb_.Add"; - if (IsScalar(field.value.type.base_type)) { - code += "Element<" + GenTypeWire(parser, field.value.type, "", false); - code += ">"; - } else if (IsStruct(field.value.type)) { - code += "Struct"; - } else { - code += "Offset"; - } - code += "(" + struct_def.name + "::" + GenFieldOffsetName(field) + ", "; - code += GenUnderlyingCast(field, false, field.name); - if (IsScalar(field.value.type.base_type)) - code += ", " + GenDefaultConstant(field); - 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 += " return o;\n }\n};\n\n"; - - // Generate a convenient CreateX function that uses the above builder - // to create a table in one go. - code += "inline flatbuffers::Offset<" + struct_def.name + "> Create"; - code += struct_def.name; - code += "(flatbuffers::FlatBufferBuilder &_fbb"; - for (auto it = struct_def.fields.vec.begin(); - it != struct_def.fields.vec.end(); - ++it) { - auto &field = **it; - if (!field.deprecated) { - code += ",\n " + GenTypeWire(parser, field.value.type, " ", true); - code += field.name + " = "; - if (field.value.type.enum_def && IsScalar(field.value.type.base_type)) { - auto ev = field.value.type.enum_def->ReverseLookup( - static_cast(StringToInt(field.value.constant.c_str())), false); - if (ev) { - code += WrapInNameSpace(field.value.type.enum_def->defined_namespace, - GetEnumVal(*field.value.type.enum_def, *ev, - parser.opts)); - } else { - code += GenUnderlyingCast(field, true, field.value.constant); - } - } else if (field.value.type.base_type == BASE_TYPE_BOOL) { - code += field.value.constant == "0" ? "false" : "true"; - } else { - code += GenDefaultConstant(field); - } - } - } - code += ") {\n " + struct_def.name + "Builder builder_(_fbb);\n"; - for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1; - size; - size /= 2) { - for (auto it = struct_def.fields.vec.rbegin(); - it != struct_def.fields.vec.rend(); - ++it) { - auto &field = **it; - if (!field.deprecated && - (!struct_def.sortbysize || - size == SizeOf(field.value.type.base_type))) { - code += " builder_.add_" + field.name + "(" + field.name + ");\n"; - } - } - } - code += " return builder_.Finish();\n}\n\n"; -} - -static void GenPadding(const FieldDef &field, std::string &code, - int &padding_id, - 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, code, padding_id); - assert(!(field.padding & ~0xF)); - } -} - -static void PaddingDefinition(int bits, std::string &code, int &padding_id) { - code += " int" + NumToString(bits) + - "_t __padding" + NumToString(padding_id++) + ";\n"; -} - -static void PaddingDeclaration(int bits, std::string &code, int &padding_id) { - (void)bits; - code += " (void)__padding" + NumToString(padding_id++) + ";"; -} - -static void PaddingInitializer(int bits, std::string &code, int &padding_id) { - (void)bits; - code += ", __padding" + NumToString(padding_id++) + "(0)"; -} - -// Generate an accessor struct with constructor for a flatbuffers struct. -static void GenStruct(const Parser &parser, StructDef &struct_def, - 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, PaddingDefinition); - } - - // Generate GetFullyQualifiedName - code += "\n public:\n"; - GenFullyQualifiedNameGetter(parser, struct_def.name, code); - - // Generate a constructor that takes all fields as arguments. - code += " " + 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(field, false, "_" + field.name); - code += "))"; - } else { - code += "_" + field.name + ")"; - } - GenPadding(field, code, padding_id, PaddingInitializer); - } - - 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, PaddingDeclaration); - } - 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(field, true, - is_scalar - ? "flatbuffers::EndianScalar(" + field.name + "_)" - : field.name + "_"); - code += "; }\n"; - if (parser.opts.mutable_buffer) { - if (is_scalar) { - code += " void mutate_" + field.name + "("; - code += GenTypeBasic(field.value.type, true); - code += " _" + field.name + ") { flatbuffers::WriteScalar(&"; - code += field.name + "_, "; - code += GenUnderlyingCast(field, false, "_" + field.name); - code += "); }\n"; - } else { - code += " "; - code += GenTypeGet(parser, field.value.type, "", "", " &", true); - code += "mutable_" + field.name + "() { return " + field.name; - code += "_; }\n"; - } - } - } - code += "};\nSTRUCT_END(" + struct_def.name + ", "; - code += NumToString(struct_def.bytesize) + ");\n\n"; -} - -void GenerateNestedNameSpaces(const Namespace *ns, std::string *code_ptr) { - for (auto it = ns->components.begin(); it != ns->components.end(); ++it) { - *code_ptr += "namespace " + *it + " {\n"; - } -} - -void CloseNestedNameSpaces(const Namespace *ns, std::string *code_ptr) { - for (auto it = ns->components.rbegin(); it != ns->components.rend(); ++it) { - *code_ptr += "} // namespace " + *it + "\n"; - } -} - -void CheckNameSpace(const Definition &def, std::string *code_ptr) { - // Set up the correct namespace. Only open a namespace if - // the existing one is different. - // TODO: this could be done more intelligently, by sorting to - // namespace path and only opening/closing what is necessary, but that's - // quite a bit more complexity. - if (code_generator_cur_name_space != def.defined_namespace) { - if (code_generator_cur_name_space) { - CloseNestedNameSpaces(code_generator_cur_name_space, code_ptr); - if (code_generator_cur_name_space->components.size()) *code_ptr += "\n"; - } - GenerateNestedNameSpaces(def.defined_namespace, code_ptr); - code_generator_cur_name_space = def.defined_namespace; - if (code_generator_cur_name_space->components.size()) *code_ptr += "\n"; - } -} - -} // namespace cpp struct IsAlnum { bool operator()(char c) { @@ -739,7 +39,7 @@ class CppGenerator : public BaseGenerator { public: CppGenerator(const Parser &parser, const std::string &path, const std::string &file_name) - : BaseGenerator(parser, path, file_name){}; + : BaseGenerator(parser, path, file_name, "", "::"){}; // Iterate through all definitions we haven't generate code for (enums, // structs, // and tables) and output them to a single file. @@ -786,15 +86,15 @@ class CppGenerator : public BaseGenerator { if (num_includes) code += "\n"; } - assert(!code_generator_cur_name_space); + assert(!cur_name_space_); // Generate forward declarations for all structs/tables, since they may // have circular references. - for (auto it = parser_.structs_.vec.begin(); it != parser_.structs_.vec.end(); - ++it) { + for (auto it = parser_.structs_.vec.begin(); + it != parser_.structs_.vec.end(); ++it) { auto &struct_def = **it; if (!struct_def.generated) { - CheckNameSpace(struct_def, &code); + SetNameSpace(struct_def.defined_namespace, &code); code += "struct " + struct_def.name + ";\n\n"; } } @@ -804,26 +104,26 @@ class CppGenerator : public BaseGenerator { ++it) { auto &enum_def = **it; if (!enum_def.generated) { - CheckNameSpace(**it, &code); - GenEnum(parser_, **it, &code); + SetNameSpace((**it).defined_namespace, &code); + GenEnum(**it, &code); } } // Generate code for all structs, then all tables. - for (auto it = parser_.structs_.vec.begin(); it != parser_.structs_.vec.end(); - ++it) { + for (auto it = parser_.structs_.vec.begin(); + it != parser_.structs_.vec.end(); ++it) { auto &struct_def = **it; if (struct_def.fixed && !struct_def.generated) { - CheckNameSpace(struct_def, &code); - GenStruct(parser_, struct_def, &code); + SetNameSpace(struct_def.defined_namespace, &code); + GenStruct(struct_def, &code); } } - for (auto it = parser_.structs_.vec.begin(); it != parser_.structs_.vec.end(); - ++it) { + for (auto it = parser_.structs_.vec.begin(); + it != parser_.structs_.vec.end(); ++it) { auto &struct_def = **it; if (!struct_def.fixed && !struct_def.generated) { - CheckNameSpace(struct_def, &code); - GenTable(parser_, struct_def, &code); + SetNameSpace(struct_def.defined_namespace, &code); + GenTable(struct_def, &code); } } @@ -832,14 +132,14 @@ class CppGenerator : public BaseGenerator { ++it) { auto &enum_def = **it; if (enum_def.is_union && !enum_def.generated) { - CheckNameSpace(enum_def, &code); - GenEnumPost(parser_, enum_def, &code); + SetNameSpace(enum_def.defined_namespace, &code); + GenEnumPost(enum_def, &code); } } // Generate convenient global helper functions: if (parser_.root_struct_def_) { - CheckNameSpace(*parser_.root_struct_def_, &code); + SetNameSpace((*parser_.root_struct_def_).defined_namespace, &code); auto &name = parser_.root_struct_def_->name; std::string qualified_name = parser_.namespaces_.back()->GetFullyQualifiedName(name); @@ -895,16 +195,688 @@ class CppGenerator : public BaseGenerator { code += "); }\n\n"; } - assert(code_generator_cur_name_space); - CloseNestedNameSpaces(code_generator_cur_name_space, &code); - - code_generator_cur_name_space = nullptr; + assert(cur_name_space_); + SetNameSpace(nullptr, &code); // Close the include guard. - code += "\n#endif // " + include_guard + "\n"; + code += "#endif // " + include_guard + "\n"; return SaveFile(GeneratedFileName(path_, file_name_).c_str(), code, false); } + + private: + // This tracks the current namespace so we can insert namespace declarations. + const Namespace *cur_name_space_ = nullptr; + + const Namespace *CurrentNameSpace() { return cur_name_space_; } + + // Translates a qualified name in flatbuffer text format to the same name in + // the equivalent C++ namespace. + static std::string TranslateNameSpace(const std::string &qualified_name) { + std::string cpp_qualified_name = qualified_name; + size_t start_pos = 0; + while ((start_pos = cpp_qualified_name.find(".", start_pos)) != + std::string::npos) { + cpp_qualified_name.replace(start_pos, 1, "::"); + } + return cpp_qualified_name; + } + + // Return a C++ type from the table in idl.h + std::string GenTypeBasic(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(*type.enum_def); + if (type.base_type == BASE_TYPE_BOOL) return "bool"; + } + return ctypename[type.base_type]; + } + + // Return a C++ pointer type, specialized to the actual struct/table types, + // and vector element types. + std::string GenTypePointer(const Type &type) { + switch (type.base_type) { + case BASE_TYPE_STRING: + return "flatbuffers::String"; + case BASE_TYPE_VECTOR: + return "flatbuffers::Vector<" + + GenTypeWire(type.VectorType(), "", false) + ">"; + case BASE_TYPE_STRUCT: { + return WrapInNameSpace(*type.struct_def); + } + case BASE_TYPE_UNION: + // fall through + default: + return "void"; + } + } + + // Return a C++ type for any type (scalar/pointer) specifically for + // building a flatbuffer. + std::string GenTypeWire(const Type &type, + const char *postfix, bool user_facing_type) { + return IsScalar(type.base_type) + ? GenTypeBasic(type, user_facing_type) + postfix + : IsStruct(type) + ? "const " + GenTypePointer(type) + " *" + : "flatbuffers::Offset<" + GenTypePointer(type) + + ">" + postfix; + } + + // Return a C++ type for any type (scalar/pointer) that reflects its + // serialized size. + std::string GenTypeSize(const Type &type) { + return IsScalar(type.base_type) + ? GenTypeBasic(type, false) + : IsStruct(type) ? GenTypePointer(type) + : "flatbuffers::uoffset_t"; + } + + // Return a C++ type for any type (scalar/pointer) specifically for + // using a flatbuffer. + std::string GenTypeGet(const Type &type, + const char *afterbasic, const char *beforeptr, + const char *afterptr, bool user_facing_type) { + return IsScalar(type.base_type) + ? GenTypeBasic(type, user_facing_type) + afterbasic + : beforeptr + GenTypePointer(type) + afterptr; + } + + static std::string GenEnumDecl(const EnumDef &enum_def, + const IDLOptions &opts) { + return (opts.scoped_enums ? "enum class " : "enum ") + enum_def.name; + } + + static std::string GenEnumVal(const EnumDef &enum_def, + const std::string &enum_val, + const IDLOptions &opts) { + return opts.prefixed_enums ? enum_def.name + "_" + enum_val : enum_val; + } + + static std::string GetEnumVal(const EnumDef &enum_def, + const EnumVal &enum_val, + const IDLOptions &opts) { + if (opts.scoped_enums) { + return enum_def.name + "::" + enum_val.name; + } else if (opts.prefixed_enums) { + return enum_def.name + "_" + enum_val.name; + } else { + return enum_val.name; + } + } + + std::string EnumSignature(EnumDef &enum_def) { + return "inline bool Verify" + enum_def.name + + "(flatbuffers::Verifier &verifier, " + "const void *union_obj, " + + enum_def.name + " type)"; + } + + // Generate an enum declaration and an enum string lookup table. + void GenEnum(EnumDef &enum_def, std::string *code_ptr) { + std::string &code = *code_ptr; + GenComment(enum_def.doc_comment, code_ptr, nullptr); + code += GenEnumDecl(enum_def, parser_.opts); + if (parser_.opts.scoped_enums) + code += " : " + GenTypeBasic(enum_def.underlying_type, false); + code += " {\n"; + int64_t anyv = 0; + EnumVal *minv = nullptr, *maxv = nullptr; + for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); + ++it) { + auto &ev = **it; + GenComment(ev.doc_comment, code_ptr, nullptr, " "); + code += " " + GenEnumVal(enum_def, ev.name, parser_.opts) + " = "; + code += NumToString(ev.value) + ",\n"; + minv = !minv || minv->value > ev.value ? &ev : minv; + maxv = !maxv || maxv->value < ev.value ? &ev : maxv; + anyv |= ev.value; + } + if (parser_.opts.scoped_enums || parser_.opts.prefixed_enums) { + assert(minv && maxv); + if (enum_def.attributes.Lookup("bit_flags")) { + if (minv->value != 0) // If the user didn't defined NONE value + code += " " + GenEnumVal(enum_def, "NONE", parser_.opts) + " = 0,\n"; + if (maxv->value != anyv) // If the user didn't defined ANY value + code += " " + GenEnumVal(enum_def, "ANY", parser_.opts) + " = " + + NumToString(anyv) + "\n"; + } else { // MIN & MAX are useless for bit_flags + code += " " + GenEnumVal(enum_def, "MIN", parser_.opts) + " = "; + code += GenEnumVal(enum_def, minv->name, parser_.opts) + ",\n"; + code += " " + GenEnumVal(enum_def, "MAX", parser_.opts) + " = "; + code += GenEnumVal(enum_def, maxv->name, parser_.opts) + "\n"; + } + } + code += "};\n"; + if (parser_.opts.scoped_enums && enum_def.attributes.Lookup("bit_flags")) + code += "DEFINE_BITMASK_OPERATORS(" + enum_def.name + ", " + + GenTypeBasic(enum_def.underlying_type, false) + ")\n"; + code += "\n"; + + // Generate a generate string table for enum values. + // Problem is, if values are very sparse that could generate really big + // tables. Ideally in that case we generate a map lookup instead, but for + // the moment we simply don't output a table at all. + auto range = + enum_def.vals.vec.back()->value - enum_def.vals.vec.front()->value + 1; + // Average distance between values above which we consider a table + // "too sparse". Change at will. + static const int kMaxSparseness = 5; + if (range / static_cast(enum_def.vals.vec.size()) < + kMaxSparseness) { + code += "inline const char **EnumNames" + enum_def.name + "() {\n"; + code += " static const char *names[] = { "; + auto val = enum_def.vals.vec.front()->value; + for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); + ++it) { + 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(), parser_.opts) + ")"; + } + code += "]; }\n\n"; + } + + if (enum_def.is_union) { + code += EnumSignature(enum_def) + ";\n\n"; + } + } + + void GenEnumPost(EnumDef &enum_def, std::string *code_ptr_post) { + // Generate a verifier function for this union that can be called by the + // table verifier functions. It uses a switch case to select a specific + // verifier function to call, this should be safe even if the union type + // has been corrupted, since the verifiers will simply fail when called + // on the wrong type. + std::string &code_post = *code_ptr_post; + code_post += EnumSignature(enum_def) + " {\n switch (type) {\n"; + for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); + ++it) { + auto &ev = **it; + code_post += " case " + GetEnumVal(enum_def, ev, parser_.opts); + if (!ev.value) { + code_post += ": return true;\n"; // "NONE" enum value. + } else { + code_post += ": return verifier.VerifyTable(reinterpret_cast(" + + val + ")"; + } else { + return val; + } + } + + std::string GenFieldOffsetName(const FieldDef &field) { + std::string uname = field.name; + std::transform(uname.begin(), uname.end(), uname.begin(), ::toupper); + return "VT_" + uname; + } + + void GenFullyQualifiedNameGetter(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"; + } + } + + std::string GenDefaultConstant(const FieldDef &field) { + return field.value.type.base_type == BASE_TYPE_FLOAT + ? field.value.constant + "f" + : field.value.constant; + } + + // Generate an accessor struct, builder structs & function for a table. + void GenTable(StructDef &struct_def, std::string *code_ptr) { + 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 GetFullyQualifiedName + GenFullyQualifiedNameGetter(struct_def.name, code); + // Generate field id constants. + if (struct_def.fields.vec.size() > 0) { + code += " enum {\n"; + bool is_first_field = + true; // track the first field that's not deprecated + 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. + if (!is_first_field) { + // Add trailing comma and newline to previous element. Don't add + // trailing comma to + // last element since older versions of gcc complain about this. + code += ",\n"; + } else { + is_first_field = false; + } + code += " " + GenFieldOffsetName(field) + " = "; + code += NumToString(field.value.offset); + } + } + code += "\n };\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(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(field.value.type, "", + "const ", " *", false) + + ">(" + offsetstr; + // Default value as second arg for non-pointer types. + if (IsScalar(field.value.type.base_type)) + call += ", " + GenDefaultConstant(field); + call += ")"; + code += GenUnderlyingCast(field, true, call); + code += "; }\n"; + if (parser_.opts.mutable_buffer) { + if (is_scalar) { + code += " bool mutate_" + field.name + "("; + code += GenTypeBasic(field.value.type, true); + code += + " _" + field.name + ") { return SetField(" + offsetstr + ", "; + code += GenUnderlyingCast(field, false, "_" + field.name); + code += "); }\n"; + } else { + auto type = + GenTypeGet(field.value.type, " ", "", " *", true); + code += " " + type + "mutable_" + field.name + "() { return "; + code += GenUnderlyingCast(field, true, + accessor + type + ">(" + offsetstr + ")"); + code += "; }\n"; + } + } + auto nested = field.attributes.Lookup("nested_flatbuffer"); + if (nested) { + std::string qualified_name = + parser_.namespaces_.back()->GetFullyQualifiedName( + nested->constant); + auto nested_root = parser_.structs_.Lookup(qualified_name); + assert(nested_root); // Guaranteed to exist by parser. + (void)nested_root; + std::string cpp_qualified_name = TranslateNameSpace(qualified_name); + + code += " const " + cpp_qualified_name + " *" + field.name; + code += "_nested_root() const { return flatbuffers::GetRoot<"; + code += cpp_qualified_name + ">(" + field.name + "()->Data()); }\n"; + } + // Generate a comparison function for this field if it is a key. + if (field.key) { + code += " bool KeyCompareLessThan(const " + struct_def.name; + code += " *o) const { return "; + if (field.value.type.base_type == BASE_TYPE_STRING) code += "*"; + code += field.name + "() < "; + if (field.value.type.base_type == BASE_TYPE_STRING) code += "*"; + code += "o->" + field.name + "(); }\n"; + code += " int KeyCompareWithValue("; + if (field.value.type.base_type == BASE_TYPE_STRING) { + code += "const char *val) const { return strcmp(" + field.name; + code += "()->c_str(), val); }\n"; + } else { + if (parser_.opts.scoped_enums && field.value.type.enum_def && + IsScalar(field.value.type.base_type)) { + code += GenTypeGet(field.value.type, " ", "const ", " *", true); + } else { + code += GenTypeBasic(field.value.type, false); + } + code += " val) const { return " + field.name + "() < val ? -1 : "; + code += field.name + "() > val; }\n"; + } + } + } + } + // 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(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 + + UnionTypeFieldSuffix() + "())"; + 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; + code += "())"; + break; + } + case BASE_TYPE_STRUCT: { + if (!field.value.type.struct_def->fixed) { + code += + prefix + "verifier.VerifyVectorOfTables(" + field.name; + code += "())"; + } + break; + } + default: + break; + } + break; + default: + break; + } + } + } + code += prefix + "verifier.EndTable()"; + code += ";\n }\n"; + code += "};\n\n"; + + // Generate a builder struct, with methods of the form: + // void add_name(type name) { fbb_.AddElement(offset, name, default); + // } + code += "struct " + struct_def.name; + code += "Builder {\n flatbuffers::FlatBufferBuilder &fbb_;\n"; + code += " flatbuffers::uoffset_t start_;\n"; + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); ++it) { + auto &field = **it; + if (!field.deprecated) { + code += " void add_" + field.name + "("; + code += GenTypeWire(field.value.type, " ", true) + field.name; + code += ") { fbb_.Add"; + if (IsScalar(field.value.type.base_type)) { + code += "Element<" + GenTypeWire(field.value.type, "", false); + code += ">"; + } else if (IsStruct(field.value.type)) { + code += "Struct"; + } else { + code += "Offset"; + } + code += "(" + struct_def.name + "::" + GenFieldOffsetName(field) + ", "; + code += GenUnderlyingCast(field, false, field.name); + if (IsScalar(field.value.type.base_type)) + code += ", " + GenDefaultConstant(field); + 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 += " return o;\n }\n};\n\n"; + + // Generate a convenient CreateX function that uses the above builder + // to create a table in one go. + code += "inline flatbuffers::Offset<" + struct_def.name + "> Create"; + code += struct_def.name; + code += "(flatbuffers::FlatBufferBuilder &_fbb"; + for (auto it = struct_def.fields.vec.begin(); + it != struct_def.fields.vec.end(); ++it) { + auto &field = **it; + if (!field.deprecated) { + code += ",\n " + GenTypeWire(field.value.type, " ", true); + code += field.name + " = "; + if (field.value.type.enum_def && IsScalar(field.value.type.base_type)) { + auto ev = field.value.type.enum_def->ReverseLookup( + static_cast(StringToInt(field.value.constant.c_str())), + false); + if (ev) { + code += WrapInNameSpace( + field.value.type.enum_def->defined_namespace, + GetEnumVal(*field.value.type.enum_def, *ev, parser_.opts)); + } else { + code += GenUnderlyingCast(field, true, field.value.constant); + } + } else if (field.value.type.base_type == BASE_TYPE_BOOL) { + code += field.value.constant == "0" ? "false" : "true"; + } else { + code += GenDefaultConstant(field); + } + } + } + code += ") {\n " + struct_def.name + "Builder builder_(_fbb);\n"; + for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1; + size; size /= 2) { + for (auto it = struct_def.fields.vec.rbegin(); + it != struct_def.fields.vec.rend(); ++it) { + auto &field = **it; + if (!field.deprecated && (!struct_def.sortbysize || + size == SizeOf(field.value.type.base_type))) { + code += " builder_.add_" + field.name + "(" + field.name + ");\n"; + } + } + } + code += " return builder_.Finish();\n}\n\n"; + } + + static void GenPadding(const FieldDef &field, std::string &code, + int &padding_id, + 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, code, padding_id); + assert(!(field.padding & ~0xF)); + } + } + + static void PaddingDefinition(int bits, std::string &code, int &padding_id) { + code += " int" + NumToString(bits) + "_t __padding" + + NumToString(padding_id++) + ";\n"; + } + + static void PaddingDeclaration(int bits, std::string &code, int &padding_id) { + (void)bits; + code += " (void)__padding" + NumToString(padding_id++) + ";"; + } + + static void PaddingInitializer(int bits, std::string &code, int &padding_id) { + (void)bits; + code += ", __padding" + NumToString(padding_id++) + "(0)"; + } + + // Generate an accessor struct with constructor for a flatbuffers struct. + void GenStruct(StructDef &struct_def, 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(field.value.type, " ", "", " ", false); + code += field.name + "_;\n"; + GenPadding(field, code, padding_id, PaddingDefinition); + } + + // Generate GetFullyQualifiedName + code += "\n public:\n"; + GenFullyQualifiedNameGetter(struct_def.name, code); + + // Generate a constructor that takes all fields as arguments. + code += " " + 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(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(field, false, "_" + field.name); + code += "))"; + } else { + code += "_" + field.name + ")"; + } + GenPadding(field, code, padding_id, PaddingInitializer); + } + + 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, PaddingDeclaration); + } + 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(field.value.type, " ", "const ", " &", true); + code += field.name + "() const { return "; + code += GenUnderlyingCast( + field, true, is_scalar + ? "flatbuffers::EndianScalar(" + field.name + "_)" + : field.name + "_"); + code += "; }\n"; + if (parser_.opts.mutable_buffer) { + if (is_scalar) { + code += " void mutate_" + field.name + "("; + code += GenTypeBasic(field.value.type, true); + code += " _" + field.name + ") { flatbuffers::WriteScalar(&"; + code += field.name + "_, "; + code += GenUnderlyingCast(field, false, "_" + field.name); + code += "); }\n"; + } else { + code += " "; + code += GenTypeGet(field.value.type, "", "", " &", true); + code += "mutable_" + field.name + "() { return " + field.name; + code += "_; }\n"; + } + } + } + code += "};\nSTRUCT_END(" + struct_def.name + ", "; + code += NumToString(struct_def.bytesize) + ");\n\n"; + } + + // Set up the correct namespace. Only open a namespace if + // the existing one is different (closing/opening only what is necessary) : + // + // the file must start and end with an empty (or null) namespace + // so that namespaces are properly opened and closed + void SetNameSpace(const Namespace *ns, std::string *code_ptr) { + if (cur_name_space_ == ns) return; + // compute the size of the longest common namespace prefix. + // if cur_name_space is A::B::C::D and ns is A::B::E::F::G, + // the common prefix is A::B:: and we have old_size = 4, new_size = 5 + // and common_prefix_size = 2 + auto old_size = + cur_name_space_ == nullptr ? 0 : cur_name_space_->components.size(); + auto new_size = ns == nullptr ? 0 : ns->components.size(); + std::vector::size_type common_prefix_size = 0; + while (common_prefix_size < old_size && common_prefix_size < new_size && + ns->components[common_prefix_size] == + cur_name_space_->components[common_prefix_size]) + common_prefix_size++; + // close cur_name_space in reverse order to reach the common prefix + // in the previous example, D then C are closed + for (auto j = old_size; j > common_prefix_size; --j) + *code_ptr += + "} // namespace " + cur_name_space_->components[j - 1] + "\n"; + if (old_size != common_prefix_size) *code_ptr += "\n"; + // open namespace parts to reach the ns namespace + // in the previous example, E, then F, then G are opened + for (auto j = common_prefix_size; j != new_size; ++j) + *code_ptr += "namespace " + ns->components[j] + " {\n"; + if (new_size != common_prefix_size) *code_ptr += "\n"; + cur_name_space_ = ns; + } }; } // namespace cpp diff --git a/src/idl_gen_general.cpp b/src/idl_gen_general.cpp index a0b837a20..a96609c54 100644 --- a/src/idl_gen_general.cpp +++ b/src/idl_gen_general.cpp @@ -188,41 +188,83 @@ static_assert(sizeof(language_parameters) / sizeof(LanguageParameters) == IDLOptions::kMAX, "Please add extra elements to the arrays above."); -static std::string FunctionStart(const LanguageParameters &lang, char upper) { - return std::string() + - (lang.language == IDLOptions::kJava - ? static_cast(tolower(upper)) - : upper); +namespace general { +class GeneralGenerator : public BaseGenerator { + public: + GeneralGenerator(const Parser &parser, const std::string &path, + const std::string &file_name) + : BaseGenerator(parser, path, file_name, "", "."){ + assert(parser_.opts.lang <= IDLOptions::kMAX); + }; + bool generate() { + std::string one_file_code; + + for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); + ++it) { + std::string enumcode; + auto &enum_def = **it; + GenEnum(enum_def, &enumcode); + if (parser_.opts.one_file) { + one_file_code += enumcode; + } else { + if (!SaveType(enum_def.name, *enum_def.defined_namespace, + enumcode, false)) return false; + } + } + + for (auto it = parser_.structs_.vec.begin(); + it != parser_.structs_.vec.end(); ++it) { + std::string declcode; + auto &struct_def = **it; + GenStruct(struct_def, &declcode); + if (parser_.opts.one_file) { + one_file_code += declcode; + } else { + if (!SaveType(struct_def.name, *struct_def.defined_namespace, + declcode, true)) return false; + } + } + + if (parser_.opts.one_file) { + return SaveType(file_name_, *parser_.namespaces_.back(), + one_file_code, true); + } + return true; + } + + // Save out the generated code for a single class while adding + // declaration boilerplate. + bool SaveType(const std::string &defname, const Namespace &ns, + const std::string &classcode, bool needs_includes) { + if (!classcode.length()) return true; + + std::string code; + code = code + "// " + FlatBuffersGeneratedWarning(); + std::string namespace_name = FullNamespace(".", ns); + if (!namespace_name.empty()) { + code += lang_.namespace_ident + namespace_name + lang_.namespace_begin; + code += "\n\n"; + } + if (needs_includes) code += lang_.includes; + code += classcode; + if (!namespace_name.empty()) code += lang_.namespace_end; + auto filename = NamespaceDir(ns) + defname + lang_.file_extension; + return SaveFile(filename.c_str(), code, false); + } + + const Namespace *CurrentNameSpace() { return parser_.namespaces_.back(); } + + std::string FunctionStart(char upper) { + return std::string() + (lang_.language == IDLOptions::kJava + ? static_cast(tolower(upper)) + : upper); } static bool IsEnum(const Type& type) { return type.enum_def != nullptr && IsInteger(type.base_type); } -// Ensure that a type is prefixed with its namespace whenever it is used -// outside of its namespace. -static std::string WrapInNameSpace(const Parser &parser, const Namespace *ns, - const std::string &name) { - if (parser.namespaces_.back() != ns) { - std::string qualified_name; - for (auto it = ns->components.begin(); - it != ns->components.end(); ++it) { - qualified_name += *it + "."; - } - return qualified_name + name; - } else { - return name; - } -} - -static std::string WrapInNameSpace(const Parser &parser, - const Definition &def) { - return WrapInNameSpace(parser, def.defined_namespace, def.name); -} - -static std::string GenTypeBasic(const LanguageParameters &lang, const Parser &parser, - const Type &type, - bool enableLangOverrides) { +std::string GenTypeBasic(const Type &type, bool enableLangOverrides) { static const char *gtypename[] = { #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \ #JTYPE, #NTYPE, #GTYPE, @@ -231,55 +273,48 @@ static std::string GenTypeBasic(const LanguageParameters &lang, const Parser &pa }; if (enableLangOverrides) { - if (lang.language == IDLOptions::kCSharp) { - if (IsEnum(type)) return WrapInNameSpace(parser, *type.enum_def); + if (lang_.language == IDLOptions::kCSharp) { + if (IsEnum(type)) return WrapInNameSpace(*type.enum_def); if (type.base_type == BASE_TYPE_STRUCT) { - return "Offset<" + WrapInNameSpace(parser, *type.struct_def) + ">"; + return "Offset<" + WrapInNameSpace(*type.struct_def) + ">"; } } } - return gtypename[type.base_type * IDLOptions::kMAX + lang.language]; + return gtypename[type.base_type * IDLOptions::kMAX + lang_.language]; } -static std::string GenTypeBasic(const LanguageParameters &lang, const Parser &parser, - const Type &type) { - return GenTypeBasic(lang, parser, type, true); +std::string GenTypeBasic(const Type &type) { + return GenTypeBasic(type, true); } -static std::string GenTypeGet(const LanguageParameters &lang, const Parser &parser, - const Type &type); - -static std::string GenTypePointer(const LanguageParameters &lang, const Parser &parser, - const Type &type) { +std::string GenTypePointer(const Type &type) { switch (type.base_type) { case BASE_TYPE_STRING: - return lang.string_type; + return lang_.string_type; case BASE_TYPE_VECTOR: - return GenTypeGet(lang, parser, type.VectorType()); + return GenTypeGet(type.VectorType()); case BASE_TYPE_STRUCT: - return WrapInNameSpace(parser, *type.struct_def); + return WrapInNameSpace(*type.struct_def); case BASE_TYPE_UNION: // Unions in C# use a generic Table-derived type for better type safety - if (lang.language == IDLOptions::kCSharp) return "TTable"; + if (lang_.language == IDLOptions::kCSharp) return "TTable"; // fall through default: return "Table"; } } -static std::string GenTypeGet(const LanguageParameters &lang, const Parser &parser, - const Type &type) { +std::string GenTypeGet(const Type &type) { return IsScalar(type.base_type) - ? GenTypeBasic(lang, parser, type) - : GenTypePointer(lang, parser, type); + ? GenTypeBasic(type) + : GenTypePointer(type); } // Find the destination type the user wants to receive the value in (e.g. // one size higher signed types for unsigned serialized values in Java). -static Type DestinationType(const LanguageParameters &lang, const Parser &parser, const Type &type, - bool vectorelem) { - if (lang.language != IDLOptions::kJava) return type; +Type DestinationType(const Type &type, bool vectorelem) { + if (lang_.language != IDLOptions::kJava) return type; switch (type.base_type) { // We use int for both uchar/ushort, since that generally means less casting // than using short for uchar. @@ -288,33 +323,31 @@ static Type DestinationType(const LanguageParameters &lang, const Parser &parser case BASE_TYPE_UINT: return Type(BASE_TYPE_LONG); case BASE_TYPE_VECTOR: if (vectorelem) - return DestinationType(lang, parser, type.VectorType(), vectorelem); + return DestinationType(type.VectorType(), vectorelem); // else fall thru: default: return type; } } -static std::string GenOffsetType(const LanguageParameters &lang, const Parser &parser, - const StructDef &struct_def) { - if(lang.language == IDLOptions::kCSharp) { - return "Offset<" + WrapInNameSpace(parser, struct_def) + ">"; +std::string GenOffsetType(const StructDef &struct_def) { + if(lang_.language == IDLOptions::kCSharp) { + return "Offset<" + WrapInNameSpace(struct_def) + ">"; } else { return "int"; } } -static std::string GenOffsetConstruct(const LanguageParameters &lang, const Parser &parser, - const StructDef &struct_def, +std::string GenOffsetConstruct(const StructDef &struct_def, const std::string &variable_name) { - if(lang.language == IDLOptions::kCSharp) { - return "new Offset<" + WrapInNameSpace(parser, struct_def) + ">(" + variable_name + ")"; + if(lang_.language == IDLOptions::kCSharp) { + return "new Offset<" + WrapInNameSpace(struct_def) + ">(" + variable_name + ")"; } return variable_name; } -static std::string GenVectorOffsetType(const LanguageParameters &lang) { - if(lang.language == IDLOptions::kCSharp) { +std::string GenVectorOffsetType() { + if(lang_.language == IDLOptions::kCSharp) { return "VectorOffset"; } else { return "int"; @@ -322,34 +355,32 @@ static std::string GenVectorOffsetType(const LanguageParameters &lang) { } // Generate destination type name -static std::string GenTypeNameDest(const LanguageParameters &lang, const Parser &parser, const Type &type) +std::string GenTypeNameDest(const Type &type) { - return GenTypeGet(lang, parser, DestinationType(lang, parser, type, true)); + return GenTypeGet(DestinationType(type, true)); } // Mask to turn serialized value into destination type value. -static std::string DestinationMask(const LanguageParameters &lang, - const Type &type, bool vectorelem) { - if (lang.language != IDLOptions::kJava) return ""; +std::string DestinationMask(const Type &type, bool vectorelem) { + if (lang_.language != IDLOptions::kJava) return ""; switch (type.base_type) { case BASE_TYPE_UCHAR: return " & 0xFF"; case BASE_TYPE_USHORT: return " & 0xFFFF"; case BASE_TYPE_UINT: return " & 0xFFFFFFFFL"; case BASE_TYPE_VECTOR: if (vectorelem) - return DestinationMask(lang, type.VectorType(), vectorelem); + return DestinationMask(type.VectorType(), vectorelem); // else fall thru: default: return ""; } } // Casts necessary to correctly read serialized data -static std::string DestinationCast(const LanguageParameters &lang, const Parser &parser, - const Type &type) { +std::string DestinationCast(const Type &type) { if (type.base_type == BASE_TYPE_VECTOR) { - return DestinationCast(lang, parser, type.VectorType()); + return DestinationCast(type.VectorType()); } else { - switch (lang.language) { + switch (lang_.language) { case IDLOptions::kJava: // Cast necessary to correctly read serialized unsigned values. if (type.base_type == BASE_TYPE_UINT) return "(long)"; @@ -357,7 +388,7 @@ static std::string DestinationCast(const LanguageParameters &lang, const Parser case IDLOptions::kCSharp: // Cast from raw integral types to enum. - if (IsEnum(type)) return "(" + WrapInNameSpace(parser, *type.enum_def) + ")"; + if (IsEnum(type)) return "(" + WrapInNameSpace(*type.enum_def) + ")"; break; default: @@ -371,13 +402,11 @@ static std::string DestinationCast(const LanguageParameters &lang, const Parser // In Java, parameters representing unsigned numbers need to be cast down to their respective type. // For example, a long holding an unsigned int value would be cast down to int before being put onto the buffer. // In C#, one cast directly cast an Enum to its underlying type, which is essential before putting it onto the buffer. -static std::string SourceCast(const LanguageParameters &lang, const Parser &parser, - const Type &type, - bool castFromDest) { +std::string SourceCast(const Type &type, bool castFromDest) { if (type.base_type == BASE_TYPE_VECTOR) { - return SourceCast(lang, parser, type.VectorType(), castFromDest); + return SourceCast(type.VectorType(), castFromDest); } else { - switch (lang.language) { + switch (lang_.language) { case IDLOptions::kJava: if (castFromDest) { if (type.base_type == BASE_TYPE_UINT) return "(int)"; @@ -386,7 +415,7 @@ static std::string SourceCast(const LanguageParameters &lang, const Parser &pars } break; case IDLOptions::kCSharp: - if (IsEnum(type)) return "(" + GenTypeBasic(lang, parser, type, false) + ")"; + if (IsEnum(type)) return "(" + GenTypeBasic(type, false) + ")"; break; default: break; @@ -395,24 +424,20 @@ static std::string SourceCast(const LanguageParameters &lang, const Parser &pars return ""; } -static std::string SourceCast(const LanguageParameters &lang, const Parser &parser, - const Type &type) { - return SourceCast(lang, parser, type, true); +std::string SourceCast(const Type &type) { + return SourceCast(type, true); } -static std::string SourceCastBasic(const LanguageParameters &lang, const Parser &parser, - const Type &type, - bool castFromDest) { - return IsScalar(type.base_type) ? SourceCast(lang, parser, type, castFromDest) : ""; +std::string SourceCastBasic(const Type &type, bool castFromDest) { + return IsScalar(type.base_type) ? SourceCast(type, castFromDest) : ""; } -static std::string SourceCastBasic(const LanguageParameters &lang, const Parser &parser, - const Type &type) { - return SourceCastBasic(lang, parser, type, true); +std::string SourceCastBasic(const Type &type) { + return SourceCastBasic(type, true); } -static std::string GenEnumDefaultValue(const Parser &parser, const Value &value) { +std::string GenEnumDefaultValue(const Value &value) { auto enum_def = value.type.enum_def; auto vec = enum_def->vals.vec; auto default_value = StringToInt(value.constant.c_str()); @@ -421,7 +446,7 @@ static std::string GenEnumDefaultValue(const Parser &parser, const Value &value) for (auto it = vec.begin(); it != vec.end(); ++it) { auto enum_val = **it; if (enum_val.value == default_value) { - result = WrapInNameSpace(parser, *enum_def) + "." + enum_val.name; + result = WrapInNameSpace(*enum_def) + "." + enum_val.name; break; } } @@ -429,14 +454,13 @@ static std::string GenEnumDefaultValue(const Parser &parser, const Value &value) return result; } -static std::string GenDefaultValue(const LanguageParameters &lang, const Parser &parser, - const Value &value, bool enableLangOverrides) { +std::string GenDefaultValue(const Value &value, bool enableLangOverrides) { if (enableLangOverrides) { // handles both enum case and vector of enum case - if (lang.language == IDLOptions::kCSharp && + if (lang_.language == IDLOptions::kCSharp && value.type.enum_def != nullptr && value.type.base_type != BASE_TYPE_UNION) { - return GenEnumDefaultValue(parser, value); + return GenEnumDefaultValue(value); } } switch (value.type.base_type) { @@ -446,21 +470,19 @@ static std::string GenDefaultValue(const LanguageParameters &lang, const Parser } } -static std::string GenDefaultValue(const LanguageParameters &lang, const Parser &parser, - const Value &value) { - return GenDefaultValue(lang, parser, value, true); +std::string GenDefaultValue(const Value &value) { + return GenDefaultValue(value, true); } -static std::string GenDefaultValueBasic(const LanguageParameters &lang, const Parser &parser, - const Value &value, bool enableLangOverrides) { +std::string GenDefaultValueBasic(const Value &value, bool enableLangOverrides) { if (!IsScalar(value.type.base_type)) { if (enableLangOverrides) { - if (lang.language == IDLOptions::kCSharp) { + if (lang_.language == IDLOptions::kCSharp) { switch (value.type.base_type) { case BASE_TYPE_STRING: return "default(StringOffset)"; case BASE_TYPE_STRUCT: - return "default(Offset<" + WrapInNameSpace(parser, *value.type.struct_def) + ">)"; + return "default(Offset<" + WrapInNameSpace(*value.type.struct_def) + ">)"; case BASE_TYPE_VECTOR: return "default(VectorOffset)"; default: @@ -470,16 +492,14 @@ static std::string GenDefaultValueBasic(const LanguageParameters &lang, const Pa } return "0"; } - return GenDefaultValue(lang, parser, value, enableLangOverrides); + return GenDefaultValue(value, enableLangOverrides); } -static std::string GenDefaultValueBasic(const LanguageParameters &lang, const Parser &parser, - const Value &value) { - return GenDefaultValueBasic(lang, parser, value, true); +std::string GenDefaultValueBasic(const Value &value) { + return GenDefaultValueBasic(value, true); } -static void GenEnum(const LanguageParameters &lang, const Parser &parser, EnumDef &enum_def, - std::string *code_ptr) { +void GenEnum(EnumDef &enum_def, std::string *code_ptr) { std::string &code = *code_ptr; if (enum_def.generated) return; @@ -488,33 +508,33 @@ static void GenEnum(const LanguageParameters &lang, const Parser &parser, EnumDe // In Java, we use ints rather than the Enum feature, because we want them // to map directly to how they're used in C/C++ and file formats. // That, and Java Enums are expensive, and not universally liked. - GenComment(enum_def.doc_comment, code_ptr, &lang.comment_config); - code += std::string("public ") + lang.enum_decl + enum_def.name; - if (lang.language == IDLOptions::kCSharp) { - code += lang.inheritance_marker + GenTypeBasic(lang, parser, enum_def.underlying_type, false); + GenComment(enum_def.doc_comment, code_ptr, &lang_.comment_config); + code += std::string("public ") + lang_.enum_decl + enum_def.name; + if (lang_.language == IDLOptions::kCSharp) { + code += lang_.inheritance_marker + GenTypeBasic(enum_def.underlying_type, false); } - code += lang.open_curly; - if (lang.language == IDLOptions::kJava) { + code += lang_.open_curly; + if (lang_.language == IDLOptions::kJava) { code += " private " + enum_def.name + "() { }\n"; } for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end(); ++it) { auto &ev = **it; - GenComment(ev.doc_comment, code_ptr, &lang.comment_config, " "); - if (lang.language != IDLOptions::kCSharp) { + GenComment(ev.doc_comment, code_ptr, &lang_.comment_config, " "); + if (lang_.language != IDLOptions::kCSharp) { code += " public static"; - code += lang.const_decl; - code += GenTypeBasic(lang, parser, enum_def.underlying_type, false); + code += lang_.const_decl; + code += GenTypeBasic(enum_def.underlying_type, false); } code += " " + ev.name + " = "; code += NumToString(ev.value); - code += lang.enum_separator; + code += lang_.enum_separator; } // Generate a generate string table for enum values. // We do not do that for C# where this functionality is native. - if (lang.language != IDLOptions::kCSharp) { + if (lang_.language != IDLOptions::kCSharp) { // Problem is, if values are very sparse that could generate really big // tables. Ideally in that case we generate a map lookup instead, but for // the moment we simply don't output a table at all. @@ -525,8 +545,8 @@ static void GenEnum(const LanguageParameters &lang, const Parser &parser, EnumDe static const int kMaxSparseness = 5; if (range / static_cast(enum_def.vals.vec.size()) < kMaxSparseness) { code += "\n private static"; - code += lang.const_decl; - code += lang.string_type; + code += lang_.const_decl; + code += lang_.string_type; code += "[] names = { "; auto val = enum_def.vals.vec.front()->value; for (auto it = enum_def.vals.vec.begin(); @@ -537,8 +557,8 @@ static void GenEnum(const LanguageParameters &lang, const Parser &parser, EnumDe } code += "};\n\n"; code += " public static "; - code += lang.string_type; - code += " " + MakeCamel("name", lang.first_camel_upper); + code += lang_.string_type; + code += " " + MakeCamel("name", lang_.first_camel_upper); code += "(int e) { return names[e"; if (enum_def.vals.vec.front()->value) code += " - " + enum_def.vals.vec.front()->name; @@ -551,19 +571,18 @@ static void GenEnum(const LanguageParameters &lang, const Parser &parser, EnumDe } // Returns the function name that is able to read a value of the given type. -static std::string GenGetter(const LanguageParameters &lang, const Parser &parser, - const Type &type) { +std::string GenGetter(const Type &type) { switch (type.base_type) { case BASE_TYPE_STRING: return "__string"; case BASE_TYPE_STRUCT: return "__struct"; case BASE_TYPE_UNION: return "__union"; - case BASE_TYPE_VECTOR: return GenGetter(lang, parser, type.VectorType()); + case BASE_TYPE_VECTOR: return GenGetter(type.VectorType()); default: { - std::string getter = "bb." + FunctionStart(lang, 'G') + "et"; + std::string getter = "bb." + FunctionStart('G') + "et"; if (type.base_type == BASE_TYPE_BOOL) { getter = "0!=" + getter; - } else if (GenTypeBasic(lang, parser, type, false) != "byte") { - getter += MakeCamel(GenTypeBasic(lang, parser, type, false)); + } else if (GenTypeBasic(type, false) != "byte") { + getter += MakeCamel(GenTypeBasic(type, false)); } return getter; } @@ -572,13 +591,12 @@ static std::string GenGetter(const LanguageParameters &lang, const Parser &parse // Direct mutation is only allowed for scalar fields. // Hence a setter method will only be generated for such fields. -static std::string GenSetter(const LanguageParameters &lang, const Parser &parser, - const Type &type) { +std::string GenSetter(const Type &type) { if (IsScalar(type.base_type)) { - std::string setter = "bb." + FunctionStart(lang, 'P') + "ut"; - if (GenTypeBasic(lang, parser, type, false) != "byte" && + std::string setter = "bb." + FunctionStart('P') + "ut"; + if (GenTypeBasic(type, false) != "byte" && type.base_type != BASE_TYPE_BOOL) { - setter += MakeCamel(GenTypeBasic(lang, parser, type, false)); + setter += MakeCamel(GenTypeBasic(type, false)); } return setter; } else { @@ -587,17 +605,15 @@ static std::string GenSetter(const LanguageParameters &lang, const Parser &parse } // Returns the method name for use with add/put calls. -static std::string GenMethod(const LanguageParameters &lang, const Parser &parser, const Type &type) { +std::string GenMethod(const Type &type) { return IsScalar(type.base_type) - ? MakeCamel(GenTypeBasic(lang, parser, type, false)) + ? MakeCamel(GenTypeBasic(type, false)) : (IsStruct(type) ? "Struct" : "Offset"); } // Recursively generate arguments for a constructor, to deal with nested // structs. -static void GenStructArgs(const LanguageParameters &lang, const Parser &parser, - const StructDef &struct_def, - std::string *code_ptr, const char *nameprefix) { +void GenStructArgs(const StructDef &struct_def, std::string *code_ptr, const char *nameprefix) { std::string &code = *code_ptr; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); @@ -607,14 +623,14 @@ static void GenStructArgs(const LanguageParameters &lang, const Parser &parser, // Generate arguments for a struct inside a struct. To ensure names // don't clash, and to make it obvious these arguments are constructing // a nested struct, prefix the name with the field name. - GenStructArgs(lang, parser, *field.value.type.struct_def, code_ptr, + GenStructArgs(*field.value.type.struct_def, code_ptr, (nameprefix + (field.name + "_")).c_str()); } else { code += ", "; - code += GenTypeBasic(lang, parser, DestinationType(lang, parser, field.value.type, false)); + code += GenTypeBasic(DestinationType(field.value.type, false)); code += " "; code += nameprefix; - code += MakeCamel(field.name, lang.first_camel_upper); + code += MakeCamel(field.name, lang_.first_camel_upper); } } } @@ -622,36 +638,33 @@ static void GenStructArgs(const LanguageParameters &lang, const Parser &parser, // Recusively generate struct construction statements of the form: // builder.putType(name); // and insert manual padding. -static void GenStructBody(const LanguageParameters &lang, const Parser &parser, - const StructDef &struct_def, - std::string *code_ptr, const char *nameprefix) { +void GenStructBody(const StructDef &struct_def, std::string *code_ptr, const char *nameprefix) { std::string &code = *code_ptr; - code += " builder." + FunctionStart(lang, 'P') + "rep("; + code += " builder." + FunctionStart('P') + "rep("; code += NumToString(struct_def.minalign) + ", "; code += NumToString(struct_def.bytesize) + ");\n"; for (auto it = struct_def.fields.vec.rbegin(); it != struct_def.fields.vec.rend(); ++it) { auto &field = **it; if (field.padding) { - code += " builder." + FunctionStart(lang, 'P') + "ad("; + code += " builder." + FunctionStart('P') + "ad("; code += NumToString(field.padding) + ");\n"; } if (IsStruct(field.value.type)) { - GenStructBody(lang, parser, *field.value.type.struct_def, code_ptr, + GenStructBody(*field.value.type.struct_def, code_ptr, (nameprefix + (field.name + "_")).c_str()); } else { - code += " builder." + FunctionStart(lang, 'P') + "ut"; - code += GenMethod(lang, parser, field.value.type) + "("; - code += SourceCast(lang, parser, field.value.type); - auto argname = nameprefix + MakeCamel(field.name, lang.first_camel_upper); + code += " builder." + FunctionStart('P') + "ut"; + code += GenMethod(field.value.type) + "("; + code += SourceCast(field.value.type); + auto argname = nameprefix + MakeCamel(field.name, lang_.first_camel_upper); code += argname; code += ");\n"; } } } -static void GenStruct(const LanguageParameters &lang, const Parser &parser, - StructDef &struct_def, std::string *code_ptr) { +void GenStruct(StructDef &struct_def, std::string *code_ptr) { if (struct_def.generated) return; std::string &code = *code_ptr; @@ -661,23 +674,23 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, // public type name() { // int o = __offset(offset); return o != 0 ? bb.getType(o + i) : default; // } - GenComment(struct_def.doc_comment, code_ptr, &lang.comment_config); + GenComment(struct_def.doc_comment, code_ptr, &lang_.comment_config); code += "public "; - if (lang.language == IDLOptions::kCSharp && + if (lang_.language == IDLOptions::kCSharp && struct_def.attributes.Lookup("csharp_partial")) { // generate a partial class for this C# struct/table code += "partial "; } else { - code += lang.unsubclassable_decl; + code += lang_.unsubclassable_decl; } - code += "class " + struct_def.name + lang.inheritance_marker; + code += "class " + struct_def.name + lang_.inheritance_marker; code += struct_def.fixed ? "Struct" : "Table"; code += " {\n"; if (!struct_def.fixed) { // Generate a special accessor for the table that when used as the root // of a FlatBuffer - std::string method_name = FunctionStart(lang, 'G') + "etRootAs" + struct_def.name; + std::string method_name = FunctionStart('G') + "etRootAs" + struct_def.name; std::string method_signature = " public static " + struct_def.name + " " + method_name; // create convenience method that doesn't require an existing object @@ -686,20 +699,20 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, // create method that allows object reuse code += method_signature + "(ByteBuffer _bb, " + struct_def.name + " obj) { "; - code += lang.set_bb_byteorder; - code += "return (obj.__init(_bb." + FunctionStart(lang, 'G'); + code += lang_.set_bb_byteorder; + code += "return (obj.__init(_bb." + FunctionStart('G'); code += "etInt(_bb."; - code += lang.get_bb_position; + code += lang_.get_bb_position; code += ") + _bb."; - code += lang.get_bb_position; + code += lang_.get_bb_position; code += ", _bb)); }\n"; - if (parser.root_struct_def_ == &struct_def) { - if (parser.file_identifier_.length()) { + if (parser_.root_struct_def_ == &struct_def) { + if (parser_.file_identifier_.length()) { // Check if a buffer has the identifier. code += " public static "; - code += lang.bool_type + struct_def.name; + code += lang_.bool_type + struct_def.name; code += "BufferHasIdentifier(ByteBuffer _bb) { return "; - code += "__has_identifier(_bb, \"" + parser.file_identifier_; + code += "__has_identifier(_bb, \"" + parser_.file_identifier_; code += "\"); }\n"; } } @@ -714,14 +727,14 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, ++it) { auto &field = **it; if (field.deprecated) continue; - GenComment(field.doc_comment, code_ptr, &lang.comment_config, " "); - std::string type_name = GenTypeGet(lang, parser, field.value.type); - std::string type_name_dest = GenTypeNameDest(lang, parser, field.value.type); - std::string dest_mask = DestinationMask(lang, field.value.type, true); - std::string dest_cast = DestinationCast(lang, parser, field.value.type); - std::string src_cast = SourceCast(lang, parser, field.value.type); + GenComment(field.doc_comment, code_ptr, &lang_.comment_config, " "); + std::string type_name = GenTypeGet(field.value.type); + std::string type_name_dest = GenTypeNameDest(field.value.type); + std::string dest_mask = DestinationMask(field.value.type, true); + std::string dest_cast = DestinationCast(field.value.type); + std::string src_cast = SourceCast(field.value.type); std::string method_start = " public " + type_name_dest + " " + - MakeCamel(field.name, lang.first_camel_upper); + MakeCamel(field.name, lang_.first_camel_upper); // Most field accessors need to retrieve and test the field offset first, // this is the prefix code for that: @@ -731,16 +744,16 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, // Generate the accessors that don't do object reuse. if (field.value.type.base_type == BASE_TYPE_STRUCT) { // Calls the accessor that takes an accessor object with a new object. - if (lang.language == IDLOptions::kCSharp) { + if (lang_.language == IDLOptions::kCSharp) { code += method_start + " { get { return Get"; - code += MakeCamel(field.name, lang.first_camel_upper); + code += MakeCamel(field.name, lang_.first_camel_upper); code += "(new "; code += type_name + "()); } }\n"; - method_start = " public " + type_name_dest + " Get" + MakeCamel(field.name, lang.first_camel_upper); + method_start = " public " + type_name_dest + " Get" + MakeCamel(field.name, lang_.first_camel_upper); } else { code += method_start + "() { return "; - code += MakeCamel(field.name, lang.first_camel_upper); + code += MakeCamel(field.name, lang_.first_camel_upper); code += "(new "; code += type_name + "()); }\n"; } @@ -748,32 +761,32 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, field.value.type.element == BASE_TYPE_STRUCT) { // Accessors for vectors of structs also take accessor objects, this // generates a variant without that argument. - if (lang.language == IDLOptions::kCSharp) { - method_start = " public " + type_name_dest + " Get" + MakeCamel(field.name, lang.first_camel_upper); + if (lang_.language == IDLOptions::kCSharp) { + method_start = " public " + type_name_dest + " Get" + MakeCamel(field.name, lang_.first_camel_upper); code += method_start + "(int j) { return Get"; } else { code += method_start + "(int j) { return "; } - code += MakeCamel(field.name, lang.first_camel_upper); + code += MakeCamel(field.name, lang_.first_camel_upper); code += "(new "; code += type_name + "(), j); }\n"; } else if (field.value.type.base_type == BASE_TYPE_VECTOR) { - if (lang.language == IDLOptions::kCSharp) { - method_start = " public " + type_name_dest + " Get" + MakeCamel(field.name, lang.first_camel_upper); + if (lang_.language == IDLOptions::kCSharp) { + method_start = " public " + type_name_dest + " Get" + MakeCamel(field.name, lang_.first_camel_upper); } } else if (field.value.type.base_type == BASE_TYPE_UNION) { - if (lang.language == IDLOptions::kCSharp) { + if (lang_.language == IDLOptions::kCSharp) { // union types in C# use generic Table-derived type for better type safety - method_start = " public " + type_name_dest + " Get" + MakeCamel(field.name, lang.first_camel_upper) + ""; + method_start = " public " + type_name_dest + " Get" + MakeCamel(field.name, lang_.first_camel_upper) + ""; offset_prefix = " where TTable : Table" + offset_prefix; type_name = type_name_dest; } } - std::string getter = dest_cast + GenGetter(lang, parser, field.value.type); + std::string getter = dest_cast + GenGetter(field.value.type); code += method_start; std::string default_cast = ""; // only create default casts for c# scalars or vectors of scalars - if (lang.language == IDLOptions::kCSharp && + if (lang_.language == IDLOptions::kCSharp && (IsScalar(field.value.type.base_type) || (field.value.type.base_type == BASE_TYPE_VECTOR && IsScalar(field.value.type.element)))) { // For scalars, default value will be returned by GetDefaultValue(). If the scalar is an enum, GetDefaultValue() @@ -785,8 +798,8 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, } std::string member_suffix = ""; if (IsScalar(field.value.type.base_type)) { - code += lang.getter_prefix; - member_suffix = lang.getter_suffix; + code += lang_.getter_prefix; + member_suffix = lang_.getter_suffix; if (struct_def.fixed) { code += " { return " + getter; code += "(bb_pos + " + NumToString(field.value.offset) + ")"; @@ -794,7 +807,7 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, } else { code += offset_prefix + getter; code += "(o + bb_pos)" + dest_mask + " : " + default_cast; - code += GenDefaultValue(lang, parser, field.value); + code += GenDefaultValue(field.value); } } else { switch (field.value.type.base_type) { @@ -814,8 +827,8 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, } break; case BASE_TYPE_STRING: - code += lang.getter_prefix; - member_suffix = lang.getter_suffix; + code += lang_.getter_prefix; + member_suffix = lang_.getter_suffix; code += offset_prefix + getter + "(o + bb_pos) : null"; break; case BASE_TYPE_VECTOR: { @@ -854,22 +867,22 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, code += member_suffix; code += "}\n"; if (field.value.type.base_type == BASE_TYPE_VECTOR) { - code += " public int " + MakeCamel(field.name, lang.first_camel_upper); + code += " public int " + MakeCamel(field.name, lang_.first_camel_upper); code += "Length"; - code += lang.getter_prefix; + code += lang_.getter_prefix; code += offset_prefix; code += "__vector_len(o) : 0; "; - code += lang.getter_suffix; + code += lang_.getter_suffix; code += "}\n"; } // Generate a ByteBuffer accessor for strings & vectors of scalars. if ((field.value.type.base_type == BASE_TYPE_VECTOR && IsScalar(field.value.type.VectorType().base_type)) || field.value.type.base_type == BASE_TYPE_STRING) { - switch (lang.language) { + switch (lang_.language) { case IDLOptions::kJava: code += " public ByteBuffer "; - code += MakeCamel(field.name, lang.first_camel_upper); + code += MakeCamel(field.name, lang_.first_camel_upper); code += "AsByteBuffer() { return __vector_as_bytebuffer("; code += NumToString(field.value.offset) + ", "; code += NumToString(field.value.type.base_type == BASE_TYPE_STRING ? 1 : @@ -878,7 +891,7 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, break; case IDLOptions::kCSharp: code += " public ArraySegment? Get"; - code += MakeCamel(field.name, lang.first_camel_upper); + code += MakeCamel(field.name, lang_.first_camel_upper); code += "Bytes() { return __vector_as_arraysegment("; code += NumToString(field.value.offset); code += "); }\n"; @@ -891,13 +904,13 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, auto nested = field.attributes.Lookup("nested_flatbuffer"); if (nested) { auto nested_qualified_name = - parser.namespaces_.back()->GetFullyQualifiedName(nested->constant); - auto nested_type = parser.structs_.Lookup(nested_qualified_name); - auto nested_type_name = WrapInNameSpace(parser, *nested_type); - auto nestedMethodName = MakeCamel(field.name, lang.first_camel_upper) + parser_.namespaces_.back()->GetFullyQualifiedName(nested->constant); + auto nested_type = parser_.structs_.Lookup(nested_qualified_name); + auto nested_type_name = WrapInNameSpace(*nested_type); + auto nestedMethodName = MakeCamel(field.name, lang_.first_camel_upper) + "As" + nested_type_name; auto getNestedMethodName = nestedMethodName; - if (lang.language == IDLOptions::kCSharp) { + if (lang_.language == IDLOptions::kCSharp) { getNestedMethodName = "Get" + nestedMethodName; } code += " public " + nested_type_name + " "; @@ -909,16 +922,16 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, code += "return o != 0 ? obj.__init(__indirect(__vector(o)), bb) : null; }\n"; } // generate mutators for scalar fields or vectors of scalars - if (parser.opts.mutable_buffer) { + if (parser_.opts.mutable_buffer) { auto underlying_type = field.value.type.base_type == BASE_TYPE_VECTOR ? field.value.type.VectorType() : field.value.type; // boolean parameters have to be explicitly converted to byte representation auto setter_parameter = underlying_type.base_type == BASE_TYPE_BOOL ? "(byte)(" + field.name + " ? 1 : 0)" : field.name; - auto mutator_prefix = MakeCamel("mutate", lang.first_camel_upper); + auto mutator_prefix = MakeCamel("mutate", lang_.first_camel_upper); //a vector mutator also needs the index of the vector element it should mutate auto mutator_params = (field.value.type.base_type == BASE_TYPE_VECTOR ? "(int j, " : "(") + - GenTypeNameDest(lang, parser, underlying_type) + " " + + GenTypeNameDest(underlying_type) + " " + field.name + ") { "; auto setter_index = field.value.type.base_type == BASE_TYPE_VECTOR ? "__vector(o) + j * " + NumToString(InlineSize(underlying_type)) @@ -927,15 +940,15 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, (field.value.type.base_type == BASE_TYPE_VECTOR && IsScalar(field.value.type.VectorType().base_type))) { code += " public "; - code += struct_def.fixed ? "void " : lang.bool_type; + code += struct_def.fixed ? "void " : lang_.bool_type; code += mutator_prefix + MakeCamel(field.name, true); code += mutator_params; if (struct_def.fixed) { - code += GenSetter(lang, parser, underlying_type) + "(" + setter_index + ", "; + code += GenSetter(underlying_type) + "(" + setter_index + ", "; code += src_cast + setter_parameter + "); }\n"; } else { code += "int o = __offset(" + NumToString(field.value.offset) + ");"; - code += " if (o != 0) { " + GenSetter(lang, parser, underlying_type); + code += " if (o != 0) { " + GenSetter(underlying_type); code += "(" + setter_index + ", " + src_cast + setter_parameter + "); return true; } else { return false; } }\n"; } } @@ -944,14 +957,14 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, code += "\n"; if (struct_def.fixed) { // create a struct constructor function - code += " public static " + GenOffsetType(lang, parser, struct_def) + " "; - code += FunctionStart(lang, 'C') + "reate"; + code += " public static " + GenOffsetType(struct_def) + " "; + code += FunctionStart('C') + "reate"; code += struct_def.name + "(FlatBufferBuilder builder"; - GenStructArgs(lang, parser, struct_def, code_ptr, ""); + GenStructArgs(struct_def, code_ptr, ""); code += ") {\n"; - GenStructBody(lang, parser, struct_def, code_ptr, ""); + GenStructBody(struct_def, code_ptr, ""); code += " return "; - code += GenOffsetConstruct(lang, parser, struct_def, "builder." + std::string(lang.get_fbb_offset)); + code += GenOffsetConstruct(struct_def, "builder." + std::string(lang_.get_fbb_offset)); code += ";\n }\n"; } else { // Generate a method that creates a table in one go. This is only possible @@ -972,28 +985,28 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, if (has_no_struct_fields && num_fields) { // Generate a table constructor of the form: // public static int createName(FlatBufferBuilder builder, args...) - code += " public static " + GenOffsetType(lang, parser, struct_def) + " "; - code += FunctionStart(lang, 'C') + "reate" + struct_def.name; + code += " public static " + GenOffsetType(struct_def) + " "; + code += FunctionStart('C') + "reate" + struct_def.name; code += "(FlatBufferBuilder builder"; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { auto &field = **it; if (field.deprecated) continue; code += ",\n "; - code += GenTypeBasic(lang, parser, DestinationType(lang, parser, field.value.type, false)); + code += GenTypeBasic(DestinationType(field.value.type, false)); code += " "; code += field.name; if (!IsScalar(field.value.type.base_type)) code += "Offset"; // Java doesn't have defaults, which means this method must always // supply all arguments, and thus won't compile when fields are added. - if (lang.language != IDLOptions::kJava) { + if (lang_.language != IDLOptions::kJava) { code += " = "; - code += GenDefaultValueBasic(lang, parser, field.value); + code += GenDefaultValueBasic(field.value); } } code += ") {\n builder."; - code += FunctionStart(lang, 'S') + "tartObject("; + code += FunctionStart('S') + "tartObject("; code += NumToString(struct_def.fields.vec.size()) + ");\n"; for (size_t size = struct_def.sortbysize ? sizeof(largest_scalar_t) : 1; size; @@ -1005,7 +1018,7 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, (!struct_def.sortbysize || size == SizeOf(field.value.type.base_type))) { code += " " + struct_def.name + "."; - code += FunctionStart(lang, 'A') + "dd"; + code += FunctionStart('A') + "dd"; code += MakeCamel(field.name) + "(builder, " + field.name; if (!IsScalar(field.value.type.base_type)) code += "Offset"; code += ");\n"; @@ -1013,7 +1026,7 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, } } code += " return " + struct_def.name + "."; - code += FunctionStart(lang, 'E') + "nd" + struct_def.name; + code += FunctionStart('E') + "nd" + struct_def.name; code += "(builder);\n }\n\n"; } // Generate a set of static methods that allow table construction, @@ -1021,30 +1034,30 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, // public static void addName(FlatBufferBuilder builder, short name) // { builder.addShort(id, name, default); } // Unlike the Create function, these always work. - code += " public static void " + FunctionStart(lang, 'S') + "tart"; + code += " public static void " + FunctionStart('S') + "tart"; code += struct_def.name; code += "(FlatBufferBuilder builder) { builder."; - code += FunctionStart(lang, 'S') + "tartObject("; + code += FunctionStart('S') + "tartObject("; 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) continue; - code += " public static void " + FunctionStart(lang, 'A') + "dd"; + code += " public static void " + FunctionStart('A') + "dd"; code += MakeCamel(field.name); code += "(FlatBufferBuilder builder, "; - code += GenTypeBasic(lang, parser, DestinationType(lang, parser, field.value.type, false)); + code += GenTypeBasic(DestinationType(field.value.type, false)); auto argname = MakeCamel(field.name, false); if (!IsScalar(field.value.type.base_type)) argname += "Offset"; - code += " " + argname + ") { builder." + FunctionStart(lang, 'A') + "dd"; - code += GenMethod(lang, parser, field.value.type) + "("; + code += " " + argname + ") { builder." + FunctionStart('A') + "dd"; + code += GenMethod(field.value.type) + "("; code += NumToString(it - struct_def.fields.vec.begin()) + ", "; - code += SourceCastBasic(lang, parser, field.value.type); + code += SourceCastBasic(field.value.type); code += argname; - if(!IsScalar(field.value.type.base_type) && field.value.type.base_type != BASE_TYPE_UNION && lang.language == IDLOptions::kCSharp) { + if(!IsScalar(field.value.type.base_type) && field.value.type.base_type != BASE_TYPE_UNION && lang_.language == IDLOptions::kCSharp) { code += ".Value"; } - code += ", " + GenDefaultValue(lang, parser, field.value, false); + code += ", " + GenDefaultValue(field.value, false); code += "); }\n"; if (field.value.type.base_type == BASE_TYPE_VECTOR) { auto vector_type = field.value.type.VectorType(); @@ -1052,127 +1065,69 @@ static void GenStruct(const LanguageParameters &lang, const Parser &parser, auto elem_size = InlineSize(vector_type); if (!IsStruct(vector_type)) { // Generate a method to create a vector from a Java array. - code += " public static " + GenVectorOffsetType(lang) + " " + FunctionStart(lang, 'C') + "reate"; + code += " public static " + GenVectorOffsetType() + " " + FunctionStart('C') + "reate"; code += MakeCamel(field.name); code += "Vector(FlatBufferBuilder builder, "; - code += GenTypeBasic(lang, parser, vector_type) + "[] data) "; - code += "{ builder." + FunctionStart(lang, 'S') + "tartVector("; + code += GenTypeBasic(vector_type) + "[] data) "; + code += "{ builder." + FunctionStart('S') + "tartVector("; code += NumToString(elem_size); - code += ", data." + FunctionStart(lang, 'L') + "ength, "; + code += ", data." + FunctionStart('L') + "ength, "; code += NumToString(alignment); code += "); for (int i = data."; - code += FunctionStart(lang, 'L') + "ength - 1; i >= 0; i--) builder."; - code += FunctionStart(lang, 'A') + "dd"; - code += GenMethod(lang, parser, vector_type); + code += FunctionStart('L') + "ength - 1; i >= 0; i--) builder."; + code += FunctionStart('A') + "dd"; + code += GenMethod(vector_type); code += "("; - code += SourceCastBasic(lang, parser, vector_type, false); + code += SourceCastBasic(vector_type, false); code += "data[i]"; - if (lang.language == IDLOptions::kCSharp && + if (lang_.language == IDLOptions::kCSharp && (vector_type.base_type == BASE_TYPE_STRUCT || vector_type.base_type == BASE_TYPE_STRING)) code += ".Value"; code += "); return "; - code += "builder." + FunctionStart(lang, 'E') + "ndVector(); }\n"; + code += "builder." + FunctionStart('E') + "ndVector(); }\n"; } // Generate a method to start a vector, data to be added manually after. - code += " public static void " + FunctionStart(lang, 'S') + "tart"; + code += " public static void " + FunctionStart('S') + "tart"; code += MakeCamel(field.name); code += "Vector(FlatBufferBuilder builder, int numElems) "; - code += "{ builder." + FunctionStart(lang, 'S') + "tartVector("; + code += "{ builder." + FunctionStart('S') + "tartVector("; code += NumToString(elem_size); code += ", numElems, " + NumToString(alignment); code += "); }\n"; } } - code += " public static " + GenOffsetType(lang, parser, struct_def) + " "; - code += FunctionStart(lang, 'E') + "nd" + struct_def.name; + code += " public static " + GenOffsetType(struct_def) + " "; + code += FunctionStart('E') + "nd" + struct_def.name; code += "(FlatBufferBuilder builder) {\n int o = builder."; - code += FunctionStart(lang, 'E') + "ndObject();\n"; + code += FunctionStart('E') + "ndObject();\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 += " builder." + FunctionStart(lang, 'R') + "equired(o, "; + code += " builder." + FunctionStart('R') + "equired(o, "; code += NumToString(field.value.offset); code += "); // " + field.name + "\n"; } } - code += " return " + GenOffsetConstruct(lang, parser, struct_def, "o") + ";\n }\n"; - if (parser.root_struct_def_ == &struct_def) { + code += " return " + GenOffsetConstruct(struct_def, "o") + ";\n }\n"; + if (parser_.root_struct_def_ == &struct_def) { code += " public static void "; - code += FunctionStart(lang, 'F') + "inish" + struct_def.name; - code += "Buffer(FlatBufferBuilder builder, " + GenOffsetType(lang, parser, struct_def) + " offset) {"; - code += " builder." + FunctionStart(lang, 'F') + "inish(offset"; - if (lang.language == IDLOptions::kCSharp) { + code += FunctionStart('F') + "inish" + struct_def.name; + code += "Buffer(FlatBufferBuilder builder, " + GenOffsetType(struct_def) + " offset) {"; + code += " builder." + FunctionStart('F') + "inish(offset"; + if (lang_.language == IDLOptions::kCSharp) { code += ".Value"; } - if (parser.file_identifier_.length()) - code += ", \"" + parser.file_identifier_ + "\""; + if (parser_.file_identifier_.length()) + code += ", \"" + parser_.file_identifier_ + "\""; code += "); }\n"; } } code += "};\n\n"; -} - -namespace general { -class GeneralGenerator : public BaseGenerator { - public: - GeneralGenerator(const Parser &parser, const std::string &path, - const std::string &file_name) - : BaseGenerator(parser, path, file_name){}; - bool generate() { - assert(parser_.opts.lang <= IDLOptions::kMAX); - auto lang = language_parameters[parser_.opts.lang]; - std::string one_file_code; - - for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); - ++it) { - std::string enumcode; - GenEnum(lang, parser_, **it, &enumcode); - if (parser_.opts.one_file) { - one_file_code += enumcode; - } else { - if (!SaveType(lang, (**it).name, enumcode, false)) return false; - } - } - - for (auto it = parser_.structs_.vec.begin(); - it != parser_.structs_.vec.end(); ++it) { - std::string declcode; - GenStruct(lang, parser_, **it, &declcode); - if (parser_.opts.one_file) { - one_file_code += declcode; - } else { - if (!SaveType(lang, (**it).name, declcode, true)) return false; - } - } - - if (parser_.opts.one_file) { - return SaveType(lang, file_name_, one_file_code, true); - } - return true; - } - - // Save out the generated code for a single class while adding - // declaration boilerplate. - bool SaveType(const LanguageParameters &lang, const std::string &defname, - const std::string &classcode, bool needs_includes) { - if (!classcode.length()) return true; - - std::string code; - code = code + "// " + FlatBuffersGeneratedWarning(); - std::string namespace_name = FullNamespace("."); - if (!namespace_name.empty()) { - code += lang.namespace_ident + namespace_name + lang.namespace_begin; - code += "\n\n"; - } - if (needs_includes) code += lang.includes; - code += classcode; - if (!namespace_name.empty()) code += lang.namespace_end; - auto filename = namespace_dir_ + defname + lang.file_extension; - return SaveFile(filename.c_str(), code, false); - } +} + const LanguageParameters & lang_ = language_parameters[parser_.opts.lang]; }; } // namespace general @@ -1188,19 +1143,25 @@ std::string GeneralMakeRule(const Parser &parser, const std::string &path, auto lang = language_parameters[parser.opts.lang]; std::string make_rule; - std::string directory = - BaseGenerator::NamespaceDir(parser, path) + kPathSeparator; for (auto it = parser.enums_.vec.begin(); it != parser.enums_.vec.end(); ++it) { + auto &enum_def = **it; if (make_rule != "") make_rule += " "; - make_rule += directory + (**it).name + lang.file_extension; + std::string directory = + BaseGenerator::NamespaceDir(parser, path, *enum_def.defined_namespace) + + kPathSeparator; + make_rule += directory + enum_def.name + lang.file_extension; } for (auto it = parser.structs_.vec.begin(); it != parser.structs_.vec.end(); ++it) { + auto &struct_def = **it; if (make_rule != "") make_rule += " "; - make_rule += directory + (**it).name + lang.file_extension; + std::string directory = + BaseGenerator::NamespaceDir(parser, path, *struct_def.defined_namespace) + + kPathSeparator; + make_rule += directory + struct_def.name + lang.file_extension; } make_rule += ": "; diff --git a/src/idl_gen_go.cpp b/src/idl_gen_go.cpp index 5841e121c..a04b6ba20 100644 --- a/src/idl_gen_go.cpp +++ b/src/idl_gen_go.cpp @@ -625,7 +625,8 @@ class GoGenerator : public BaseGenerator { public: GoGenerator(const Parser &parser, const std::string &path, const std::string &file_name) - : BaseGenerator(parser, path, file_name){}; + : BaseGenerator(parser, path, file_name, "" /* not used*/, + "" /* not used */){}; bool generate() { for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); ++it) { @@ -664,9 +665,10 @@ class GoGenerator : public BaseGenerator { if (!classcode.length()) return true; std::string code = ""; - BeginFile(LastNamespacePart(), needs_imports, &code); + BeginFile(LastNamespacePart(*def.defined_namespace), needs_imports, &code); code += classcode; - std::string filename = namespace_dir_ + def.name + ".go"; + std::string filename = + NamespaceDir(*def.defined_namespace) + def.name + ".go"; return SaveFile(filename.c_str(), code, false); } }; diff --git a/src/idl_gen_grpc.cpp b/src/idl_gen_grpc.cpp new file mode 100644 index 000000000..2bf0e911d --- /dev/null +++ b/src/idl_gen_grpc.cpp @@ -0,0 +1,216 @@ +/* + * Copyright 2014 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// independent from idl_parser, since this code is not needed for most clients + +#include "flatbuffers/flatbuffers.h" +#include "flatbuffers/idl.h" +#include "flatbuffers/util.h" + +#include "src/compiler/cpp_generator.h" + +namespace flatbuffers { + +class FlatBufMethod : public grpc_cpp_generator::Method { + public: + enum Streaming { kNone, kClient, kServer, kBiDi }; + + FlatBufMethod(const RPCCall *method) + : method_(method) { + streaming_ = kNone; + auto val = method_->attributes.Lookup("streaming"); + if (val) { + if (val->constant == "client") streaming_ = kClient; + if (val->constant == "server") streaming_ = kServer; + if (val->constant == "bidi") streaming_ = kBiDi; + } + } + + std::string name() const { return method_->name; } + + std::string GRPCType(const StructDef &sd) const { + return "flatbuffers::BufferRef<" + sd.name + ">"; + } + + std::string input_type_name() const { + return GRPCType(*method_->request); + } + std::string output_type_name() const { + return GRPCType(*method_->response); + } + + bool NoStreaming() const { return streaming_ == kNone; } + bool ClientOnlyStreaming() const { return streaming_ == kClient; } + bool ServerOnlyStreaming() const { return streaming_ == kServer; } + bool BidiStreaming() const { return streaming_ == kBiDi; } + + private: + const RPCCall *method_; + Streaming streaming_; +}; + +class FlatBufService : public grpc_cpp_generator::Service { + public: + FlatBufService(const ServiceDef *service) : service_(service) {} + + std::string name() const { return service_->name; } + + int method_count() const { + return static_cast(service_->calls.vec.size()); + }; + + std::unique_ptr method(int i) const { + return std::unique_ptr( + new FlatBufMethod(service_->calls.vec[i])); + }; + + private: + const ServiceDef *service_; +}; + +class FlatBufPrinter : public grpc_cpp_generator::Printer { + public: + FlatBufPrinter(std::string *str) + : str_(str), escape_char_('$'), indent_(0) {} + + void Print(const std::map &vars, + const char *string_template) { + std::string s = string_template; + // Replace any occurrences of strings in "vars" that are surrounded + // by the escape character by what they're mapped to. + size_t pos; + while ((pos = s.find(escape_char_)) != std::string::npos) { + // Found an escape char, must also find the closing one. + size_t pos2 = s.find(escape_char_, pos + 1); + // If placeholder not closed, ignore. + if (pos2 == std::string::npos) break; + auto it = vars.find(s.substr(pos + 1, pos2 - pos - 1)); + // If unknown placeholder, ignore. + if (it == vars.end()) break; + // Subtitute placeholder. + s.replace(pos, pos2 - pos + 1, it->second); + } + Print(s.c_str()); + } + + void Print(const char *s) { + // Add this string, but for each part separated by \n, add indentation. + for (;;) { + // Current indentation. + str_->insert(str_->end(), indent_ * 2, ' '); + // See if this contains more than one line. + const char * lf = strchr(s, '\n'); + if (lf) { + (*str_) += std::string(s, lf + 1); + s = lf + 1; + if (!*s) break; // Only continue if there's more lines. + } else { + (*str_) += s; + break; + } + } + } + + void Indent() { indent_++; } + void Outdent() { indent_--; assert(indent_ >= 0); } + + private: + std::string *str_; + char escape_char_; + int indent_; +}; + +class FlatBufFile : public grpc_cpp_generator::File { + public: + FlatBufFile(const Parser &parser, const std::string &file_name) + : parser_(parser), file_name_(file_name) {} + + std::string filename() const { return file_name_; } + std::string filename_without_ext() const { + return StripExtension(file_name_); + } + + std::string message_header_ext() const { return "_generated.h"; } + std::string service_header_ext() const { return ".grpc.fb.h"; } + + std::string package() const { + return parser_.namespaces_.back()->GetFullyQualifiedName(""); + } + + std::vector package_parts() const { + return parser_.namespaces_.back()->components; + } + + std::string additional_headers() const { + return "#include \"flatbuffers/grpc.h\"\n"; + } + + int service_count() const { + return static_cast(parser_.services_.vec.size()); + }; + + std::unique_ptr service(int i) const { + return std::unique_ptr ( + new FlatBufService(parser_.services_.vec[i])); + } + + std::unique_ptr CreatePrinter(std::string *str) const { + return std::unique_ptr( + new FlatBufPrinter(str)); + } + + private: + const Parser &parser_; + const std::string &file_name_; +}; + +bool GenerateGRPC(const Parser &parser, + const std::string &/*path*/, + const std::string &file_name) { + + int nservices = 0; + for (auto it = parser.services_.vec.begin(); + it != parser.services_.vec.end(); ++it) { + if (!(*it)->generated) nservices++; + } + if (!nservices) return true; + + grpc_cpp_generator::Parameters generator_parameters; + // TODO(wvo): make the other parameters in this struct configurable. + generator_parameters.use_system_headers = true; + + FlatBufFile fbfile(parser, file_name); + + std::string header_code = + grpc_cpp_generator::GetHeaderPrologue(&fbfile, generator_parameters) + + grpc_cpp_generator::GetHeaderIncludes(&fbfile, generator_parameters) + + grpc_cpp_generator::GetHeaderServices(&fbfile, generator_parameters) + + grpc_cpp_generator::GetHeaderEpilogue(&fbfile, generator_parameters); + + std::string source_code = + grpc_cpp_generator::GetSourcePrologue(&fbfile, generator_parameters) + + grpc_cpp_generator::GetSourceIncludes(&fbfile, generator_parameters) + + grpc_cpp_generator::GetSourceServices(&fbfile, generator_parameters) + + grpc_cpp_generator::GetSourceEpilogue(&fbfile, generator_parameters); + + return flatbuffers::SaveFile((file_name + ".grpc.fb.h").c_str(), + header_code, false) && + flatbuffers::SaveFile((file_name + ".grpc.fb.cc").c_str(), + source_code, false); +} + +} // namespace flatbuffers + diff --git a/src/idl_gen_js.cpp b/src/idl_gen_js.cpp index d724b80f4..32a06f1ef 100644 --- a/src/idl_gen_js.cpp +++ b/src/idl_gen_js.cpp @@ -22,14 +22,71 @@ #include "flatbuffers/code_generators.h" namespace flatbuffers { -namespace js { -static void GenNamespaces(const Parser &parser, std::string *code_ptr, - std::string *exports_ptr) { +static std::string GeneratedFileName(const std::string &path, + const std::string &file_name) { + return path + file_name + "_generated.js"; +} + +namespace js { +// Iterate through all definitions we haven't generate code for (enums, structs, +// and tables) and output them to a single file. +class JsGenerator : public BaseGenerator { + public: + JsGenerator(const Parser &parser, const std::string &path, + const std::string &file_name) + : BaseGenerator(parser, path, file_name, "", "."){}; + // Iterate through all definitions we haven't generate code for (enums, + // structs, and tables) and output them to a single file. + bool generate() { + if (IsEverythingGenerated()) return true; + + std::string enum_code, struct_code, exports_code, code; + generateEnums(&enum_code, &exports_code); + generateStructs(&struct_code, &exports_code); + + code = code + "// " + FlatBuffersGeneratedWarning(); + + // Generate code for all the namespace declarations. + GenNamespaces(&code, &exports_code); + + // Output the main declaration code from above. + code += enum_code; + code += struct_code; + + if (!exports_code.empty() && !parser_.opts.skip_js_exports) { + code += "// Exports for Node.js and RequireJS\n"; + code += exports_code; + } + + return SaveFile(GeneratedFileName(path_, file_name_).c_str(), code, false); + } + + private: + // Generate code for all enums. + void generateEnums(std::string *enum_code_ptr, + std::string *exports_code_ptr) { + for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); + ++it) { + auto &enum_def = **it; + GenEnum(enum_def, enum_code_ptr, exports_code_ptr); + } + } + + // Generate code for all structs. + void generateStructs(std::string *decl_code_ptr, + std::string *exports_code_ptr) { + for (auto it = parser_.structs_.vec.begin(); + it != parser_.structs_.vec.end(); ++it) { + auto &struct_def = **it; + GenStruct(struct_def, decl_code_ptr, exports_code_ptr); + } + } + void GenNamespaces(std::string *code_ptr, std::string *exports_ptr) { std::set namespaces; - for (auto it = parser.namespaces_.begin(); - it != parser.namespaces_.end(); ++it) { + for (auto it = parser_.namespaces_.begin(); + it != parser_.namespaces_.end(); ++it) { std::string namespace_so_far; // Gather all parent namespaces for this namespace @@ -62,22 +119,6 @@ static void GenNamespaces(const Parser &parser, std::string *code_ptr, } } -// Ensure that a type is prefixed with its namespace whenever it is used -// outside of its namespace. -static std::string WrapInNameSpace(const Namespace *ns, - const std::string &name) { - std::string qualified_name; - for (auto it = ns->components.begin(); - it != ns->components.end(); ++it) { - qualified_name += *it + "."; - } - return qualified_name + name; -} - -static std::string WrapInNameSpace(const Definition &def) { - return WrapInNameSpace(def.defined_namespace, def.name); -} - // Generate a documentation comment, if available. static void GenDocComment(const std::vector &dc, std::string *code_ptr, @@ -123,7 +164,7 @@ static void GenDocComment(std::string *code_ptr, } // Generate an enum declaration and an enum string lookup table. -static void GenEnum(EnumDef &enum_def, std::string *code_ptr, +void GenEnum(EnumDef &enum_def, std::string *code_ptr, std::string *exports_ptr) { if (enum_def.generated) return; std::string &code = *code_ptr; @@ -170,7 +211,7 @@ static std::string GenType(const Type &type) { } } -static std::string GenGetter(const Type &type, const std::string &arguments) { +std::string GenGetter(const Type &type, const std::string &arguments) { switch (type.base_type) { case BASE_TYPE_STRING: return "this.bb.__string" + arguments; case BASE_TYPE_STRUCT: return "this.bb.__struct" + arguments; @@ -190,7 +231,7 @@ static std::string GenGetter(const Type &type, const std::string &arguments) { } } -static std::string GenDefaultValue(const Value &value, const std::string &context) { +std::string GenDefaultValue(const Value &value, const std::string &context) { if (value.type.enum_def) { if (auto val = value.type.enum_def->ReverseLookup( atoi(value.constant.c_str()), false)) { @@ -217,7 +258,7 @@ static std::string GenDefaultValue(const Value &value, const std::string &contex } } -static std::string GenTypeName(const Type &type, bool input) { +std::string GenTypeName(const Type &type, bool input) { if (!input) { if (type.base_type == BASE_TYPE_STRING) { return "string|Uint8Array"; @@ -269,7 +310,7 @@ static std::string MaybeScale(T value) { return value != 1 ? " * " + NumToString(value) : ""; } -static void GenStructArgs(const StructDef &struct_def, +void GenStructArgs(const StructDef &struct_def, std::string *annotations, std::string *arguments, const std::string &nameprefix) { @@ -320,8 +361,7 @@ static void GenStructBody(const StructDef &struct_def, } // Generate an accessor struct with constructor for a flatbuffers struct. -static void GenStruct(const Parser &parser, StructDef &struct_def, - std::string *code_ptr, std::string *exports_ptr) { +void GenStruct(StructDef &struct_def, std::string *code_ptr, std::string *exports_ptr) { if (struct_def.generated) return; std::string &code = *code_ptr; std::string &exports = *exports_ptr; @@ -375,13 +415,13 @@ static void GenStruct(const Parser &parser, StructDef &struct_def, code += "};\n\n"; // Generate the identifier check method - if (parser.root_struct_def_ == &struct_def && - !parser.file_identifier_.empty()) { + if (parser_.root_struct_def_ == &struct_def && + !parser_.file_identifier_.empty()) { GenDocComment(code_ptr, "@param {flatbuffers.ByteBuffer} bb\n" "@returns {boolean}"); code += object_name + ".bufferHasIdentifier = function(bb) {\n"; - code += " return bb.__has_identifier('" + parser.file_identifier_; + code += " return bb.__has_identifier('" + parser_.file_identifier_; code += "');\n};\n\n"; } } @@ -644,83 +684,21 @@ static void GenStruct(const Parser &parser, StructDef &struct_def, code += "};\n\n"; // Generate the method to complete buffer construction - if (parser.root_struct_def_ == &struct_def) { + if (parser_.root_struct_def_ == &struct_def) { GenDocComment(code_ptr, "@param {flatbuffers.Builder} builder\n" "@param {flatbuffers.Offset} offset"); code += object_name + ".finish" + struct_def.name + "Buffer"; code += " = function(builder, offset) {\n"; code += " builder.finish(offset"; - if (!parser.file_identifier_.empty()) { - code += ", '" + parser.file_identifier_ + "'"; + if (!parser_.file_identifier_.empty()) { + code += ", '" + parser_.file_identifier_ + "'"; } code += ");\n"; code += "};\n\n"; } } } - -} // namespace js - -static std::string GeneratedFileName(const std::string &path, - const std::string &file_name) { - return path + file_name + "_generated.js"; -} - -namespace js { -// Iterate through all definitions we haven't generate code for (enums, structs, -// and tables) and output them to a single file. -class JsGenerator : public BaseGenerator { - public: - JsGenerator(const Parser &parser, const std::string &path, - const std::string &file_name) - : BaseGenerator(parser, path, file_name){}; - // Iterate through all definitions we haven't generate code for (enums, - // structs, and tables) and output them to a single file. - bool generate() { - if (IsEverythingGenerated()) return true; - - std::string enum_code, struct_code, exports_code, code; - generateEnums(&enum_code, &exports_code); - generateStructs(&struct_code, &exports_code); - - code = code + "// " + FlatBuffersGeneratedWarning(); - - // Generate code for all the namespace declarations. - GenNamespaces(parser_, &code, &exports_code); - - // Output the main declaration code from above. - code += enum_code; - code += struct_code; - - if (!exports_code.empty() && !parser_.opts.skip_js_exports) { - code += "// Exports for Node.js and RequireJS\n"; - code += exports_code; - } - - return SaveFile(GeneratedFileName(path_, file_name_).c_str(), code, false); - } - - private: - // Generate code for all enums. - void generateEnums(std::string *enum_code_ptr, - std::string *exports_code_ptr) { - for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end(); - ++it) { - auto &enum_def = **it; - GenEnum(enum_def, enum_code_ptr, exports_code_ptr); - } - } - - // Generate code for all structs. - void generateStructs(std::string *decl_code_ptr, - std::string *exports_code_ptr) { - for (auto it = parser_.structs_.vec.begin(); - it != parser_.structs_.vec.end(); ++it) { - auto &struct_def = **it; - GenStruct(parser_, struct_def, decl_code_ptr, exports_code_ptr); - } - } }; } // namespace js diff --git a/src/idl_gen_php.cpp b/src/idl_gen_php.cpp index 599d5571d..af3b06d57 100644 --- a/src/idl_gen_php.cpp +++ b/src/idl_gen_php.cpp @@ -25,35 +25,74 @@ namespace flatbuffers { namespace php { - - static std::string GenGetter(const Type &type); - static std::string GenDefaultValue(const Value &value); - static std::string GenMethod(const FieldDef &field); - static void GenStructBuilder(const StructDef &struct_def, - std::string *code_ptr); - static std::string GenTypeBasic(const Type &type); - static std::string GenTypeGet(const Type &type); - - // Ensure that a type is prefixed with its namespace whenever it is used - // outside of its namespace. - static std::string WrapInNameSpace(const Namespace *ns, - const std::string &name) { - std::string qualified_name = "\\"; - for (auto it = ns->components.begin(); - it != ns->components.end(); ++it) { - qualified_name += *it + "\\"; - } - return qualified_name + name; - } - - static std::string WrapInNameSpace(const Definition &def) { - return WrapInNameSpace(def.defined_namespace, def.name); - } - - // Hardcode spaces per indentation. const std::string Indent = " "; + class PhpGenerator : public BaseGenerator { + public: + PhpGenerator(const Parser &parser, const std::string &path, + const std::string &file_name) + : BaseGenerator(parser, path, file_name, "\\", "\\"){}; + bool generate() { + if (!generateEnums()) return false; + if (!generateStructs()) return false; + return true; + } + private: + bool generateEnums() { + for (auto it = parser_.enums_.vec.begin(); + it != parser_.enums_.vec.end(); ++it) { + auto &enum_def = **it; + std::string enumcode; + GenEnum(enum_def, &enumcode); + if (!SaveType(enum_def, enumcode, false)) return false; + } + return true; + } + + bool generateStructs() { + for (auto it = parser_.structs_.vec.begin(); + it != parser_.structs_.vec.end(); ++it) { + auto &struct_def = **it; + std::string declcode; + GenStruct(struct_def, &declcode); + if (!SaveType(struct_def, declcode, true)) return false; + } + return true; + } + + // Begin by declaring namespace and imports. + void BeginFile(const std::string name_space_name, + const bool needs_imports, std::string *code_ptr) { + std::string &code = *code_ptr; + code += "finish($offset"; - if (parser.file_identifier_.length()) - code += ", \"" + parser.file_identifier_ + "\""; + if (parser_.file_identifier_.length()) + code += ", \"" + parser_.file_identifier_ + "\""; code += ");\n"; code += Indent + "}\n"; } } // Generate a struct field, conditioned on its child type(s). - static void GenStructAccessor(const StructDef &struct_def, - const FieldDef &field, + void GenStructAccessor(const StructDef &struct_def, const FieldDef &field, std::string *code_ptr) { GenComment(field.doc_comment, code_ptr, nullptr); @@ -696,9 +726,7 @@ namespace php { } // Generate table constructors, conditioned on its members' types. - static void GenTableBuilders(const Parser &parser, - const StructDef &struct_def, - std::string *code_ptr) { + void GenTableBuilders(const StructDef &struct_def, std::string *code_ptr) { GetStartOfTable(struct_def, code_ptr); for (auto it = struct_def.fields.vec.begin(); @@ -725,11 +753,11 @@ namespace php { } } - GetEndOffsetOnTable(parser, struct_def, code_ptr); + GetEndOffsetOnTable(struct_def, code_ptr); } // Generate struct or table methods. - static void GenStruct(const Parser &parser, const StructDef &struct_def, + void GenStruct(const StructDef &struct_def, std::string *code_ptr) { if (struct_def.generated) return; @@ -744,13 +772,13 @@ namespace php { std::string &code = *code_ptr; if (!struct_def.fixed) { - if (parser.file_identifier_.length()) { + if (parser_.file_identifier_.length()) { // Return the identifier code += Indent + "public static function " + struct_def.name; code += "Identifier()\n"; code += Indent + "{\n"; code += Indent + Indent + "return \""; - code += parser.file_identifier_ + "\";\n"; + code += parser_.file_identifier_ + "\";\n"; code += Indent + "}\n\n"; // Check if a buffer has the identifier. @@ -763,12 +791,12 @@ namespace php { code += Indent + "}\n\n"; } - if (parser.file_extension_.length()) { + if (parser_.file_extension_.length()) { // Return the extension code += Indent + "public static function " + struct_def.name; code += "Extension()\n"; code += Indent + "{\n"; - code += Indent + Indent + "return \"" + parser.file_extension_; + code += Indent + Indent + "return \"" + parser_.file_extension_; code += "\";\n"; code += Indent + "}\n\n"; } @@ -791,7 +819,7 @@ namespace php { GenStructBuilder(struct_def, code_ptr); } else { // Create a set of functions that allow table construction. - GenTableBuilders(parser, struct_def, code_ptr); + GenTableBuilders(struct_def, code_ptr); } EndClass(code_ptr); } @@ -859,7 +887,7 @@ namespace php { return ctypename[type.base_type]; } - static std::string GenDefaultValue(const Value &value) { + std::string GenDefaultValue(const Value &value) { if (value.type.enum_def) { if (auto val = value.type.enum_def->ReverseLookup( atoi(value.constant.c_str()), false)) { @@ -927,71 +955,7 @@ namespace php { code += Indent + Indent + "return $builder->offset();\n"; code += Indent + "}\n"; } - - class PhpGenerator : public BaseGenerator { - public: - PhpGenerator(const Parser &parser, const std::string &path, - const std::string &file_name) - : BaseGenerator(parser, path, file_name){}; - bool generate() { - if (!generateEnums()) return false; - if (!generateStructs()) return false; - return true; - } - - private: - bool generateEnums() { - for (auto it = parser_.enums_.vec.begin(); - it != parser_.enums_.vec.end(); ++it) { - auto &enum_def = **it; - std::string enumcode; - GenEnum(enum_def, &enumcode); - if (!SaveType(enum_def, enumcode, false)) return false; - } - return true; - } - - bool generateStructs() { - for (auto it = parser_.structs_.vec.begin(); - it != parser_.structs_.vec.end(); ++it) { - auto &struct_def = **it; - std::string declcode; - GenStruct(parser_, struct_def, &declcode); - if (!SaveType(struct_def, declcode, true)) return false; - } - return true; - } - - // Begin by declaring namespace and imports. - void BeginFile(const std::string name_space_name, - const bool needs_imports, std::string *code_ptr) { - std::string &code = *code_ptr; - code += " #include +#ifdef _WIN32 +#if !defined(_USE_MATH_DEFINES) +#define _USE_MATH_DEFINES // For M_PI. +#endif // !defined(_USE_MATH_DEFINES) +#endif // _WIN32 + +#include + #include "flatbuffers/idl.h" #include "flatbuffers/util.h" @@ -575,9 +583,9 @@ CheckedError Parser::ParseField(StructDef &struct_def) { FieldDef *typefield = nullptr; if (type.base_type == BASE_TYPE_UNION) { // For union fields, add a second auto-generated field to hold the type, - // with _type appended as the name. - ECHECK(AddField(struct_def, name + "_type", type.enum_def->underlying_type, - &typefield)); + // with a special suffix. + ECHECK(AddField(struct_def, name + UnionTypeFieldSuffix(), + type.enum_def->underlying_type, &typefield)); } FieldDef *field; @@ -678,17 +686,45 @@ CheckedError Parser::ParseField(StructDef &struct_def) { } CheckedError Parser::ParseAnyValue(Value &val, FieldDef *field, - size_t parent_fieldn) { + size_t parent_fieldn, + const StructDef *parent_struct_def) { switch (val.type.base_type) { case BASE_TYPE_UNION: { assert(field); + std::string constant; if (!parent_fieldn || - field_stack_.back().second->value.type.base_type != BASE_TYPE_UTYPE) - return Error("missing type field before this union value: " + - field->name); + field_stack_.back().second->value.type.base_type != BASE_TYPE_UTYPE) { + // We haven't seen the type field yet. Sadly a lot of JSON writers + // output these in alphabetical order, meaning it comes after this + // value. So we scan past the value to find it, then come back here. + auto type_name = field->name + UnionTypeFieldSuffix(); + assert(parent_struct_def); + auto type_field = parent_struct_def->fields.Lookup(type_name); + assert(type_field); // Guaranteed by ParseField(). + // Remember where we are in the source file, so we can come back here. + auto backup = *static_cast(this); + ECHECK(SkipAnyJsonValue()); // The table. + EXPECT(','); + auto next_name = attribute_; + if (Is(kTokenStringConstant)) { + NEXT(); + } else { + EXPECT(kTokenIdentifier); + } + if (next_name != type_name) + return Error("missing type field after this union value: " + + type_name); + EXPECT(':'); + Value type_val = type_field->value; + ECHECK(ParseAnyValue(type_val, type_field, 0, nullptr)); + constant = type_val.constant; + // Got the information we needed, now rewind: + *static_cast(this) = backup; + } else { + constant = field_stack_.back().first.constant; + } uint8_t enum_idx; - ECHECK(atot(field_stack_.back().first.constant.c_str(), *this, - &enum_idx)); + ECHECK(atot(constant.c_str(), *this, &enum_idx)); auto enum_val = val.type.enum_def->ReverseLookup(enum_idx); if (!enum_val) return Error("illegal type id for: " + field->name); ECHECK(ParseTable(*enum_val->struct_def, &val.constant, nullptr)); @@ -763,7 +799,7 @@ CheckedError Parser::ParseTable(const StructDef &struct_def, std::string *value, NEXT(); // Ignore this field. } else { Value val = field->value; - ECHECK(ParseAnyValue(val, field, fieldn)); + ECHECK(ParseAnyValue(val, field, fieldn, &struct_def)); size_t i = field_stack_.size(); // Hardcoded insertion-sort with error-check. // If fields are specified in order, then this loop exits immediately. @@ -862,7 +898,7 @@ CheckedError Parser::ParseVector(const Type &type, uoffset_t *ovalue) { if ((!opts.strict_json || !count) && Is(']')) { NEXT(); break; } Value val; val.type = type; - ECHECK(ParseAnyValue(val, nullptr, 0)); + ECHECK(ParseAnyValue(val, nullptr, 0, nullptr)); field_stack_.push_back(std::make_pair(val, nullptr)); count++; if (Is(']')) { NEXT(); break; } @@ -1004,8 +1040,30 @@ CheckedError Parser::ParseHash(Value &e, FieldDef* field) { } CheckedError Parser::ParseSingleValue(Value &e) { - // First check if this could be a string/identifier enum value: - if (e.type.base_type != BASE_TYPE_STRING && + // First see if this could be a conversion function: + if (token_ == kTokenIdentifier && *cursor_ == '(') { + auto functionname = attribute_; + NEXT(); + EXPECT('('); + ECHECK(ParseSingleValue(e)); + EXPECT(')'); + #define FLATBUFFERS_FN_DOUBLE(name, op) \ + if (functionname == name) { \ + auto x = strtod(e.constant.c_str(), nullptr); \ + e.constant = NumToString(op); \ + } + FLATBUFFERS_FN_DOUBLE("deg", x / M_PI * 180); + FLATBUFFERS_FN_DOUBLE("rad", x * M_PI / 180); + FLATBUFFERS_FN_DOUBLE("sin", sin(x)); + FLATBUFFERS_FN_DOUBLE("cos", cos(x)); + FLATBUFFERS_FN_DOUBLE("tan", tan(x)); + FLATBUFFERS_FN_DOUBLE("asin", asin(x)); + FLATBUFFERS_FN_DOUBLE("acos", acos(x)); + FLATBUFFERS_FN_DOUBLE("atan", atan(x)); + // TODO(wvo): add more useful conversion functions here. + #undef FLATBUFFERS_FN_DOUBLE + // Then check if this could be a string/identifier enum value: + } else if (e.type.base_type != BASE_TYPE_STRING && e.type.base_type != BASE_TYPE_NONE && (token_ == kTokenIdentifier || token_ == kTokenStringConstant)) { if (IsIdentifierStart(attribute_[0])) { // Enum value. @@ -1150,7 +1208,13 @@ CheckedError Parser::ParseEnum(bool is_union, EnumDef **dest) { auto full_name = value_name; std::vector value_comment = doc_comment_; EXPECT(kTokenIdentifier); - if (is_union) ECHECK(ParseNamespacing(&full_name, &value_name)); + if (is_union) { + ECHECK(ParseNamespacing(&full_name, &value_name)); + // Since we can't namespace the actual enum identifiers, turn + // namespace parts into part of the identifier. + value_name = full_name; + std::replace(value_name.begin(), value_name.end(), '.', '_'); + } auto prevsize = enum_def.vals.vec.size(); auto value = enum_def.vals.vec.size() ? enum_def.vals.vec.back()->value + 1 @@ -1288,7 +1352,8 @@ CheckedError Parser::ParseDecl() { } } - ECHECK(CheckClash(fields, struct_def, "_type", BASE_TYPE_UNION)); + ECHECK(CheckClash(fields, struct_def, UnionTypeFieldSuffix(), + BASE_TYPE_UNION)); ECHECK(CheckClash(fields, struct_def, "Type", BASE_TYPE_UNION)); ECHECK(CheckClash(fields, struct_def, "_length", BASE_TYPE_VECTOR)); ECHECK(CheckClash(fields, struct_def, "Length", BASE_TYPE_VECTOR)); @@ -1358,6 +1423,10 @@ void Parser::MarkGenerated() { it != structs_.vec.end(); ++it) { (*it)->generated = true; } + for (auto it = services_.vec.begin(); + it != services_.vec.end(); ++it) { + (*it)->generated = true; + } } CheckedError Parser::ParseNamespace() { diff --git a/src/util.cpp b/src/util.cpp index 452e9eae5..f4aecb5c3 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -24,12 +24,14 @@ bool FileExistsRaw(const char *name) { } bool LoadFileRaw(const char *name, bool binary, std::string *buf) { + if (DirExists(name)) return false; std::ifstream ifs(name, binary ? std::ifstream::binary : std::ifstream::in); if (!ifs.is_open()) return false; if (binary) { // The fastest way to read a file into a string. ifs.seekg(0, std::ios::end); - (*buf).resize(static_cast(ifs.tellg())); + auto size = ifs.tellg(); + (*buf).resize(static_cast(size)); ifs.seekg(0, std::ios::beg); ifs.read(&(*buf)[0], (*buf).size()); } else { @@ -54,6 +56,19 @@ bool FileExists(const char *name) { return g_file_exists_function(name); } +bool DirExists(const char *name) { + #ifdef _WIN32 + #define flatbuffers_stat _stat + #define FLATBUFFERS_S_IFDIR _S_IFDIR + #else + #define flatbuffers_stat stat + #define FLATBUFFERS_S_IFDIR S_IFDIR + #endif + struct flatbuffers_stat file_info; + if (flatbuffers_stat(name, &file_info) != 0) return false; + return (file_info.st_mode & FLATBUFFERS_S_IFDIR) != 0; +} + LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function) { LoadFileFunction previous_function = g_load_file_function; g_load_file_function = load_file_function ? load_file_function : LoadFileRaw; diff --git a/tests/MyGame/Example/Any.cs b/tests/MyGame/Example/Any.cs index 1f018ad89..8fdc2fca1 100644 --- a/tests/MyGame/Example/Any.cs +++ b/tests/MyGame/Example/Any.cs @@ -8,6 +8,7 @@ public enum Any : byte NONE = 0, Monster = 1, TestSimpleTableWithEnum = 2, + MyGame_Example2_Monster = 3, }; diff --git a/tests/MyGame/Example/Any.go b/tests/MyGame/Example/Any.go index 0322364c6..df9f823fc 100644 --- a/tests/MyGame/Example/Any.go +++ b/tests/MyGame/Example/Any.go @@ -6,4 +6,5 @@ const ( AnyNONE = 0 AnyMonster = 1 AnyTestSimpleTableWithEnum = 2 + AnyMyGame_Example2_Monster = 3 ) diff --git a/tests/MyGame/Example/Any.java b/tests/MyGame/Example/Any.java index 27dc1bcb9..25d74af55 100644 --- a/tests/MyGame/Example/Any.java +++ b/tests/MyGame/Example/Any.java @@ -7,8 +7,9 @@ public final class Any { public static final byte NONE = 0; public static final byte Monster = 1; public static final byte TestSimpleTableWithEnum = 2; + public static final byte MyGame_Example2_Monster = 3; - private static final String[] names = { "NONE", "Monster", "TestSimpleTableWithEnum", }; + private static final String[] names = { "NONE", "Monster", "TestSimpleTableWithEnum", "MyGame_Example2_Monster", }; public static String name(int e) { return names[e]; } }; diff --git a/tests/MyGame/Example/Any.php b/tests/MyGame/Example/Any.php index f04f4ad3f..da691760c 100644 --- a/tests/MyGame/Example/Any.php +++ b/tests/MyGame/Example/Any.php @@ -8,11 +8,13 @@ class Any const NONE = 0; const Monster = 1; const TestSimpleTableWithEnum = 2; + const MyGame_Example2_Monster = 3; private static $names = array( "NONE", "Monster", "TestSimpleTableWithEnum", + "MyGame_Example2_Monster", ); public static function Name($e) diff --git a/tests/MyGame/Example/Any.py b/tests/MyGame/Example/Any.py index b642ede01..f1b8d519d 100644 --- a/tests/MyGame/Example/Any.py +++ b/tests/MyGame/Example/Any.py @@ -6,4 +6,5 @@ class Any(object): NONE = 0 Monster = 1 TestSimpleTableWithEnum = 2 + MyGame_Example2_Monster = 3 diff --git a/tests/MyGame/Example/Monster.cs b/tests/MyGame/Example/Monster.cs index 6101f464c..fdfd2b0a3 100644 --- a/tests/MyGame/Example/Monster.cs +++ b/tests/MyGame/Example/Monster.cs @@ -78,8 +78,10 @@ public sealed class Monster : Table { public bool MutateTestf2(float testf2) { int o = __offset(56); if (o != 0) { bb.PutFloat(o + bb_pos, testf2); return true; } else { return false; } } public float Testf3 { get { int o = __offset(58); return o != 0 ? bb.GetFloat(o + bb_pos) : (float)0.0f; } } public bool MutateTestf3(float testf3) { int o = __offset(58); if (o != 0) { bb.PutFloat(o + bb_pos, testf3); return true; } else { return false; } } + public string GetTestarrayofstring2(int j) { int o = __offset(60); return o != 0 ? __string(__vector(o) + j * 4) : null; } + public int Testarrayofstring2Length { get { int o = __offset(60); return o != 0 ? __vector_len(o) : 0; } } - public static void StartMonster(FlatBufferBuilder builder) { builder.StartObject(28); } + public static void StartMonster(FlatBufferBuilder builder) { builder.StartObject(29); } public static void AddPos(FlatBufferBuilder builder, Offset posOffset) { builder.AddStruct(0, posOffset.Value, 0); } public static void AddMana(FlatBufferBuilder builder, short mana) { builder.AddShort(1, mana, 150); } public static void AddHp(FlatBufferBuilder builder, short hp) { builder.AddShort(2, hp, 100); } @@ -118,6 +120,9 @@ public sealed class Monster : Table { public static void AddTestf(FlatBufferBuilder builder, float testf) { builder.AddFloat(25, testf, 3.14159f); } public static void AddTestf2(FlatBufferBuilder builder, float testf2) { builder.AddFloat(26, testf2, 3.0f); } public static void AddTestf3(FlatBufferBuilder builder, float testf3) { builder.AddFloat(27, testf3, 0.0f); } + public static void AddTestarrayofstring2(FlatBufferBuilder builder, VectorOffset testarrayofstring2Offset) { builder.AddOffset(28, testarrayofstring2Offset.Value, 0); } + public static VectorOffset CreateTestarrayofstring2Vector(FlatBufferBuilder builder, StringOffset[] data) { builder.StartVector(4, data.Length, 4); for (int i = data.Length - 1; i >= 0; i--) builder.AddOffset(data[i].Value); return builder.EndVector(); } + public static void StartTestarrayofstring2Vector(FlatBufferBuilder builder, int numElems) { builder.StartVector(4, numElems, 4); } public static Offset EndMonster(FlatBufferBuilder builder) { int o = builder.EndObject(); builder.Required(o, 10); // name diff --git a/tests/MyGame/Example/Monster.go b/tests/MyGame/Example/Monster.go index fbd849e81..0c42f45a5 100644 --- a/tests/MyGame/Example/Monster.go +++ b/tests/MyGame/Example/Monster.go @@ -337,7 +337,24 @@ func (rcv *Monster) Testf3() float32 { return 0.0 } -func MonsterStart(builder *flatbuffers.Builder) { builder.StartObject(28) } +func (rcv *Monster) Testarrayofstring2(j int) []byte { + o := flatbuffers.UOffsetT(rcv._tab.Offset(60)) + if o != 0 { + a := rcv._tab.Vector(o) + return rcv._tab.ByteVector(a + flatbuffers.UOffsetT(j * 4)) + } + return nil +} + +func (rcv *Monster) Testarrayofstring2Length() int { + o := flatbuffers.UOffsetT(rcv._tab.Offset(60)) + if o != 0 { + return rcv._tab.VectorLen(o) + } + return 0 +} + +func MonsterStart(builder *flatbuffers.Builder) { builder.StartObject(29) } func MonsterAddPos(builder *flatbuffers.Builder, pos flatbuffers.UOffsetT) { builder.PrependStructSlot(0, flatbuffers.UOffsetT(pos), 0) } func MonsterAddMana(builder *flatbuffers.Builder, mana int16) { builder.PrependInt16Slot(1, mana, 150) } func MonsterAddHp(builder *flatbuffers.Builder, hp int16) { builder.PrependInt16Slot(2, hp, 100) } @@ -377,4 +394,7 @@ func MonsterStartTestarrayofboolsVector(builder *flatbuffers.Builder, numElems i func MonsterAddTestf(builder *flatbuffers.Builder, testf float32) { builder.PrependFloat32Slot(25, testf, 3.14159) } func MonsterAddTestf2(builder *flatbuffers.Builder, testf2 float32) { builder.PrependFloat32Slot(26, testf2, 3.0) } func MonsterAddTestf3(builder *flatbuffers.Builder, testf3 float32) { builder.PrependFloat32Slot(27, testf3, 0.0) } +func MonsterAddTestarrayofstring2(builder *flatbuffers.Builder, testarrayofstring2 flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(28, flatbuffers.UOffsetT(testarrayofstring2), 0) } +func MonsterStartTestarrayofstring2Vector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT { return builder.StartVector(4, numElems, 4) +} func MonsterEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() } diff --git a/tests/MyGame/Example/Monster.java b/tests/MyGame/Example/Monster.java index d85e4baac..bfd4be1c9 100644 --- a/tests/MyGame/Example/Monster.java +++ b/tests/MyGame/Example/Monster.java @@ -84,8 +84,10 @@ public final class Monster extends Table { public boolean mutateTestf2(float testf2) { int o = __offset(56); if (o != 0) { bb.putFloat(o + bb_pos, testf2); return true; } else { return false; } } public float testf3() { int o = __offset(58); return o != 0 ? bb.getFloat(o + bb_pos) : 0.0f; } public boolean mutateTestf3(float testf3) { int o = __offset(58); if (o != 0) { bb.putFloat(o + bb_pos, testf3); return true; } else { return false; } } + public String testarrayofstring2(int j) { int o = __offset(60); return o != 0 ? __string(__vector(o) + j * 4) : null; } + public int testarrayofstring2Length() { int o = __offset(60); return o != 0 ? __vector_len(o) : 0; } - public static void startMonster(FlatBufferBuilder builder) { builder.startObject(28); } + public static void startMonster(FlatBufferBuilder builder) { builder.startObject(29); } public static void addPos(FlatBufferBuilder builder, int posOffset) { builder.addStruct(0, posOffset, 0); } public static void addMana(FlatBufferBuilder builder, short mana) { builder.addShort(1, mana, 150); } public static void addHp(FlatBufferBuilder builder, short hp) { builder.addShort(2, hp, 100); } @@ -124,6 +126,9 @@ public final class Monster extends Table { public static void addTestf(FlatBufferBuilder builder, float testf) { builder.addFloat(25, testf, 3.14159f); } public static void addTestf2(FlatBufferBuilder builder, float testf2) { builder.addFloat(26, testf2, 3.0f); } public static void addTestf3(FlatBufferBuilder builder, float testf3) { builder.addFloat(27, testf3, 0.0f); } + public static void addTestarrayofstring2(FlatBufferBuilder builder, int testarrayofstring2Offset) { builder.addOffset(28, testarrayofstring2Offset, 0); } + public static int createTestarrayofstring2Vector(FlatBufferBuilder builder, int[] data) { builder.startVector(4, data.length, 4); for (int i = data.length - 1; i >= 0; i--) builder.addOffset(data[i]); return builder.endVector(); } + public static void startTestarrayofstring2Vector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); } public static int endMonster(FlatBufferBuilder builder) { int o = builder.endObject(); builder.required(o, 10); // name diff --git a/tests/MyGame/Example/Monster.php b/tests/MyGame/Example/Monster.php index 9f82d5a9c..c06cffbc6 100644 --- a/tests/MyGame/Example/Monster.php +++ b/tests/MyGame/Example/Monster.php @@ -360,22 +360,41 @@ class Monster extends Table return $o != 0 ? $this->bb->getFloat($o + $this->bb_pos) : 0.0; } + /** + * @param int offset + * @return string + */ + public function getTestarrayofstring2($j) + { + $o = $this->__offset(60); + return $o != 0 ? $this->__string($this->__vector($o) + $j * 4) : 0; + } + + /** + * @return int + */ + public function getTestarrayofstring2Length() + { + $o = $this->__offset(60); + return $o != 0 ? $this->__vector_len($o) : 0; + } + /** * @param FlatBufferBuilder $builder * @return void */ public static function startMonster(FlatBufferBuilder $builder) { - $builder->StartObject(28); + $builder->StartObject(29); } /** * @param FlatBufferBuilder $builder * @return Monster */ - public static function createMonster(FlatBufferBuilder $builder, $pos, $mana, $hp, $name, $inventory, $color, $test_type, $test, $test4, $testarrayofstring, $testarrayoftables, $enemy, $testnestedflatbuffer, $testempty, $testbool, $testhashs32_fnv1, $testhashu32_fnv1, $testhashs64_fnv1, $testhashu64_fnv1, $testhashs32_fnv1a, $testhashu32_fnv1a, $testhashs64_fnv1a, $testhashu64_fnv1a, $testarrayofbools, $testf, $testf2, $testf3) + public static function createMonster(FlatBufferBuilder $builder, $pos, $mana, $hp, $name, $inventory, $color, $test_type, $test, $test4, $testarrayofstring, $testarrayoftables, $enemy, $testnestedflatbuffer, $testempty, $testbool, $testhashs32_fnv1, $testhashu32_fnv1, $testhashs64_fnv1, $testhashu64_fnv1, $testhashs32_fnv1a, $testhashu32_fnv1a, $testhashs64_fnv1a, $testhashu64_fnv1a, $testarrayofbools, $testf, $testf2, $testf3, $testarrayofstring2) { - $builder->startObject(28); + $builder->startObject(29); self::addPos($builder, $pos); self::addMana($builder, $mana); self::addHp($builder, $hp); @@ -403,6 +422,7 @@ class Monster extends Table self::addTestf($builder, $testf); self::addTestf2($builder, $testf2); self::addTestf3($builder, $testf3); + self::addTestarrayofstring2($builder, $testarrayofstring2); $o = $builder->endObject(); $builder->required($o, 10); // name return $o; @@ -817,6 +837,40 @@ class Monster extends Table $builder->addFloatX(27, $testf3, 0.0); } + /** + * @param FlatBufferBuilder $builder + * @param VectorOffset + * @return void + */ + public static function addTestarrayofstring2(FlatBufferBuilder $builder, $testarrayofstring2) + { + $builder->addOffsetX(28, $testarrayofstring2, 0); + } + + /** + * @param FlatBufferBuilder $builder + * @param array offset array + * @return int vector offset + */ + public static function createTestarrayofstring2Vector(FlatBufferBuilder $builder, array $data) + { + $builder->startVector(4, count($data), 4); + for ($i = count($data) - 1; $i >= 0; $i--) { + $builder->addOffset($data[$i]); + } + return $builder->endVector(); + } + + /** + * @param FlatBufferBuilder $builder + * @param int $numElems + * @return void + */ + public static function startTestarrayofstring2Vector(FlatBufferBuilder $builder, $numElems) + { + $builder->startVector(4, $numElems, 4); + } + /** * @param FlatBufferBuilder $builder * @return int table offset diff --git a/tests/MyGame/Example/Monster.py b/tests/MyGame/Example/Monster.py index 4f1ca2dfa..9d2909e89 100644 --- a/tests/MyGame/Example/Monster.py +++ b/tests/MyGame/Example/Monster.py @@ -283,7 +283,22 @@ class Monster(object): return self._tab.Get(flatbuffers.number_types.Float32Flags, o + self._tab.Pos) return 0.0 -def MonsterStart(builder): builder.StartObject(28) + # Monster + def Testarrayofstring2(self, j): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(60)) + if o != 0: + a = self._tab.Vector(o) + return self._tab.String(a + flatbuffers.number_types.UOffsetTFlags.py_type(j * 4)) + return "" + + # Monster + def Testarrayofstring2Length(self): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(60)) + if o != 0: + return self._tab.VectorLen(o) + return 0 + +def MonsterStart(builder): builder.StartObject(29) def MonsterAddPos(builder, pos): builder.PrependStructSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(pos), 0) def MonsterAddMana(builder, mana): builder.PrependInt16Slot(1, mana, 150) def MonsterAddHp(builder, hp): builder.PrependInt16Slot(2, hp, 100) @@ -317,4 +332,6 @@ def MonsterStartTestarrayofboolsVector(builder, numElems): return builder.StartV def MonsterAddTestf(builder, testf): builder.PrependFloat32Slot(25, testf, 3.14159) def MonsterAddTestf2(builder, testf2): builder.PrependFloat32Slot(26, testf2, 3.0) def MonsterAddTestf3(builder, testf3): builder.PrependFloat32Slot(27, testf3, 0.0) +def MonsterAddTestarrayofstring2(builder, testarrayofstring2): builder.PrependUOffsetTRelativeSlot(28, flatbuffers.number_types.UOffsetTFlags.py_type(testarrayofstring2), 0) +def MonsterStartTestarrayofstring2Vector(builder, numElems): return builder.StartVector(4, numElems, 4) def MonsterEnd(builder): return builder.EndObject() diff --git a/tests/MyGame/Example2/Monster.cs b/tests/MyGame/Example2/Monster.cs new file mode 100644 index 000000000..e6c512abc --- /dev/null +++ b/tests/MyGame/Example2/Monster.cs @@ -0,0 +1,23 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +namespace MyGame.Example2 +{ + +using System; +using FlatBuffers; + +public sealed class Monster : Table { + public static Monster GetRootAsMonster(ByteBuffer _bb) { return GetRootAsMonster(_bb, new Monster()); } + public static Monster GetRootAsMonster(ByteBuffer _bb, Monster obj) { return (obj.__init(_bb.GetInt(_bb.Position) + _bb.Position, _bb)); } + public Monster __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; } + + + public static void StartMonster(FlatBufferBuilder builder) { builder.StartObject(0); } + public static Offset EndMonster(FlatBufferBuilder builder) { + int o = builder.EndObject(); + return new Offset(o); + } +}; + + +} diff --git a/tests/MyGame/Example2/Monster.go b/tests/MyGame/Example2/Monster.go new file mode 100644 index 000000000..e12b05453 --- /dev/null +++ b/tests/MyGame/Example2/Monster.go @@ -0,0 +1,18 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +package Example2 + +import ( + flatbuffers "github.com/google/flatbuffers/go" +) +type Monster struct { + _tab flatbuffers.Table +} + +func (rcv *Monster) Init(buf []byte, i flatbuffers.UOffsetT) { + rcv._tab.Bytes = buf + rcv._tab.Pos = i +} + +func MonsterStart(builder *flatbuffers.Builder) { builder.StartObject(0) } +func MonsterEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() } diff --git a/tests/MyGame/Example2/Monster.java b/tests/MyGame/Example2/Monster.java new file mode 100644 index 000000000..6432494f3 --- /dev/null +++ b/tests/MyGame/Example2/Monster.java @@ -0,0 +1,23 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +package MyGame.Example2; + +import java.nio.*; +import java.lang.*; +import java.util.*; +import com.google.flatbuffers.*; + +@SuppressWarnings("unused") +public final class Monster extends Table { + public static Monster getRootAsMonster(ByteBuffer _bb) { return getRootAsMonster(_bb, new Monster()); } + public static Monster getRootAsMonster(ByteBuffer _bb, Monster obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__init(_bb.getInt(_bb.position()) + _bb.position(), _bb)); } + public Monster __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; } + + + public static void startMonster(FlatBufferBuilder builder) { builder.startObject(0); } + public static int endMonster(FlatBufferBuilder builder) { + int o = builder.endObject(); + return o; + } +}; + diff --git a/tests/MyGame/Example2/Monster.php b/tests/MyGame/Example2/Monster.php new file mode 100644 index 000000000..b00f150e5 --- /dev/null +++ b/tests/MyGame/Example2/Monster.php @@ -0,0 +1,79 @@ +init($bb->getInt($bb->getPosition()) + $bb->getPosition(), $bb)); + } + + public static function MonsterIdentifier() + { + return "MONS"; + } + + public static function MonsterBufferHasIdentifier(ByteBuffer $buf) + { + return self::__has_identifier($buf, self::MonsterIdentifier()); + } + + public static function MonsterExtension() + { + return "mon"; + } + + /** + * @param int $_i offset + * @param ByteBuffer $_bb + * @return Monster + **/ + public function init($_i, ByteBuffer $_bb) + { + $this->bb_pos = $_i; + $this->bb = $_bb; + return $this; + } + + /** + * @param FlatBufferBuilder $builder + * @return void + */ + public static function startMonster(FlatBufferBuilder $builder) + { + $builder->StartObject(0); + } + + /** + * @param FlatBufferBuilder $builder + * @return Monster + */ + public static function createMonster(FlatBufferBuilder $builder, ) + { + $builder->startObject(0); + $o = $builder->endObject(); + return $o; + } + + /** + * @param FlatBufferBuilder $builder + * @return int table offset + */ + public static function endMonster(FlatBufferBuilder $builder) + { + $o = $builder->endObject(); + return $o; + } +} diff --git a/tests/MyGame/Example2/Monster.py b/tests/MyGame/Example2/Monster.py new file mode 100644 index 000000000..0b98211e4 --- /dev/null +++ b/tests/MyGame/Example2/Monster.py @@ -0,0 +1,15 @@ +# automatically generated by the FlatBuffers compiler, do not modify + +# namespace: Example2 + +import flatbuffers + +class Monster(object): + __slots__ = ['_tab'] + + # Monster + def Init(self, buf, pos): + self._tab = flatbuffers.table.Table(buf, pos) + +def MonsterStart(builder): builder.StartObject(0) +def MonsterEnd(builder): return builder.EndObject() diff --git a/tests/fuzzer/build_fuzzer.sh b/tests/fuzzer/build_fuzzer.sh new file mode 100644 index 000000000..48eb3b7c9 --- /dev/null +++ b/tests/fuzzer/build_fuzzer.sh @@ -0,0 +1,20 @@ +#!/bin/bash +# +# Copyright 2015 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +git clone https://chromium.googlesource.com/chromium/llvm-project/llvm/lib/Fuzzer +clang++ -c -g -O2 -std=c++11 Fuzzer/*.cpp -IFuzzer +ar ruv libFuzzer.a Fuzzer*.o +rm -rf Fuzzer *.o diff --git a/tests/fuzzer/build_run_parser_test.sh b/tests/fuzzer/build_run_parser_test.sh new file mode 100644 index 000000000..3a053e762 --- /dev/null +++ b/tests/fuzzer/build_run_parser_test.sh @@ -0,0 +1,20 @@ +#!/bin/bash +# +# Copyright 2015 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +clang++ -fsanitize-coverage=edge -fsanitize=address -std=c++11 -stdlib=libstdc++ -I.. -I../../include flatbuffers_parser_fuzzer.cc ../../src/idl_parser.cpp ../../src/util.cpp libFuzzer.a -o fuzz_parser +mkdir -p parser_corpus +cp ../*.json ../*.fbs parser_corpus +./fuzz_parser parser_corpus diff --git a/tests/fuzzer/build_run_verifier_test.sh b/tests/fuzzer/build_run_verifier_test.sh new file mode 100644 index 000000000..67616496a --- /dev/null +++ b/tests/fuzzer/build_run_verifier_test.sh @@ -0,0 +1,20 @@ +#!/bin/bash +# +# Copyright 2015 Google Inc. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +clang++ -fsanitize-coverage=edge -fsanitize=address -std=c++11 -stdlib=libstdc++ -I.. -I../../include flatbuffers_verifier_fuzzer.cc libFuzzer.a -o fuzz_verifier +mkdir -p verifier_corpus +cp ../*.mon verifier_corpus +./fuzz_verifier verifier_corpus diff --git a/tests/fuzzer/flatbuffers_parser_fuzzer.cc b/tests/fuzzer/flatbuffers_parser_fuzzer.cc new file mode 100644 index 000000000..632b3b18d --- /dev/null +++ b/tests/fuzzer/flatbuffers_parser_fuzzer.cc @@ -0,0 +1,16 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +#include +#include +#include + +#include "flatbuffers/idl.h" + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { + flatbuffers::Parser parser; + // Guarantee 0-termination. + std::string s(reinterpret_cast(data), size); + parser.Parse(s.c_str()); + return 0; +} diff --git a/tests/fuzzer/flatbuffers_verifier_fuzzer.cc b/tests/fuzzer/flatbuffers_verifier_fuzzer.cc new file mode 100644 index 000000000..d7d453c82 --- /dev/null +++ b/tests/fuzzer/flatbuffers_verifier_fuzzer.cc @@ -0,0 +1,14 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +#include +#include +#include + +#include "monster_test_generated.h" + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { + flatbuffers::Verifier verifier(data, size); + MyGame::Example::VerifyMonsterBuffer(verifier); + return 0; +} diff --git a/tests/generate_code.bat b/tests/generate_code.bat index 00b16d900..53d54707b 100644 --- a/tests/generate_code.bat +++ b/tests/generate_code.bat @@ -12,6 +12,6 @@ :: See the License for the specific language governing permissions and :: limitations under the License. -..\flatc.exe --cpp --java --csharp --go --binary --python --js --php --gen-mutable --no-includes monster_test.fbs monsterdata_test.json +..\flatc.exe --cpp --java --csharp --go --binary --python --js --php --grpc --gen-mutable --no-includes monster_test.fbs monsterdata_test.json ..\flatc.exe --cpp --java --csharp --go --binary --python --js --php --gen-mutable -o namespace_test namespace_test\namespace_test1.fbs namespace_test\namespace_test2.fbs ..\flatc.exe --binary --schema monster_test.fbs diff --git a/tests/generate_code.sh b/tests/generate_code.sh index b74c2bec5..3436d8586 100644 --- a/tests/generate_code.sh +++ b/tests/generate_code.sh @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -../flatc --cpp --java --csharp --go --binary --python --js --php --gen-mutable --no-includes monster_test.fbs monsterdata_test.json +../flatc --cpp --java --csharp --go --binary --python --js --php --grpc --gen-mutable --no-includes monster_test.fbs monsterdata_test.json ../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 diff --git a/tests/monster_test.bfbs b/tests/monster_test.bfbs index e16b2bd13..e131ac8c2 100644 Binary files a/tests/monster_test.bfbs and b/tests/monster_test.bfbs differ diff --git a/tests/monster_test.fbs b/tests/monster_test.fbs index f302ce444..3fecd33cf 100755 --- a/tests/monster_test.fbs +++ b/tests/monster_test.fbs @@ -2,13 +2,17 @@ include "include_test1.fbs"; +namespace MyGame.Example2; + +table Monster {} // Test having same name as below, but in different namespace. + namespace MyGame.Example; attribute "priority"; enum Color:byte (bit_flags) { Red = 0, Green, Blue = 3, } -union Any { Monster, TestSimpleTableWithEnum } // TODO: add more elements +union Any { Monster, TestSimpleTableWithEnum, MyGame.Example2.Monster } struct Test { a:short; b:byte; } @@ -44,6 +48,7 @@ table Monster { /// multiline too testarrayoftables:[Monster] (id: 11); testarrayofstring:[string] (id: 10); + testarrayofstring2:[string] (id: 28); testarrayofbools:[bool] (id: 24); enemy:MyGame.Example.Monster (id:12); // Test referring by full namespace. test:Any (id: 8); @@ -65,7 +70,7 @@ table Monster { } rpc_service MonsterStorage { - Store(Monster):Stat (stream); + Store(Monster):Stat (streaming: "none"); Retrieve(Stat):Monster (idempotent); } diff --git a/tests/monster_test.grpc.fb.cc b/tests/monster_test.grpc.fb.cc new file mode 100644 index 000000000..71dbd2d4f --- /dev/null +++ b/tests/monster_test.grpc.fb.cc @@ -0,0 +1,85 @@ +// Generated by the gRPC protobuf plugin. +// If you make any local change, they will be lost. +// source: monster_test + +#include "monster_test_generated.h" +#include "monster_test.grpc.fb.h" +#include "flatbuffers/grpc.h" + +#include +#include +#include +#include +#include +#include +#include +#include +namespace MyGame { +namespace Example { + +static const char* MonsterStorage_method_names[] = { + "/MyGame.Example..MonsterStorage/Store", + "/MyGame.Example..MonsterStorage/Retrieve", +}; + +std::unique_ptr< MonsterStorage::Stub> MonsterStorage::NewStub(const std::shared_ptr< ::grpc::ChannelInterface>& channel, const ::grpc::StubOptions& options) { + std::unique_ptr< MonsterStorage::Stub> stub(new MonsterStorage::Stub(channel)); + return stub; +} + +MonsterStorage::Stub::Stub(const std::shared_ptr< ::grpc::ChannelInterface>& channel) + : channel_(channel) , rpcmethod_Store_(MonsterStorage_method_names[0], ::grpc::RpcMethod::NORMAL_RPC, channel) + , rpcmethod_Retrieve_(MonsterStorage_method_names[1], ::grpc::RpcMethod::NORMAL_RPC, channel) + {} + +::grpc::Status MonsterStorage::Stub::Store(::grpc::ClientContext* context, const flatbuffers::BufferRef& request, flatbuffers::BufferRef* response) { + return ::grpc::BlockingUnaryCall(channel_.get(), rpcmethod_Store_, context, request, response); +} + +::grpc::ClientAsyncResponseReader< flatbuffers::BufferRef>* MonsterStorage::Stub::AsyncStoreRaw(::grpc::ClientContext* context, const flatbuffers::BufferRef& request, ::grpc::CompletionQueue* cq) { + return new ::grpc::ClientAsyncResponseReader< flatbuffers::BufferRef>(channel_.get(), cq, rpcmethod_Store_, context, request); +} + +::grpc::Status MonsterStorage::Stub::Retrieve(::grpc::ClientContext* context, const flatbuffers::BufferRef& request, flatbuffers::BufferRef* response) { + return ::grpc::BlockingUnaryCall(channel_.get(), rpcmethod_Retrieve_, context, request, response); +} + +::grpc::ClientAsyncResponseReader< flatbuffers::BufferRef>* MonsterStorage::Stub::AsyncRetrieveRaw(::grpc::ClientContext* context, const flatbuffers::BufferRef& request, ::grpc::CompletionQueue* cq) { + return new ::grpc::ClientAsyncResponseReader< flatbuffers::BufferRef>(channel_.get(), cq, rpcmethod_Retrieve_, context, request); +} + +MonsterStorage::Service::Service() { + (void)MonsterStorage_method_names; + AddMethod(new ::grpc::RpcServiceMethod( + MonsterStorage_method_names[0], + ::grpc::RpcMethod::NORMAL_RPC, + new ::grpc::RpcMethodHandler< MonsterStorage::Service, flatbuffers::BufferRef, flatbuffers::BufferRef>( + std::mem_fn(&MonsterStorage::Service::Store), this))); + AddMethod(new ::grpc::RpcServiceMethod( + MonsterStorage_method_names[1], + ::grpc::RpcMethod::NORMAL_RPC, + new ::grpc::RpcMethodHandler< MonsterStorage::Service, flatbuffers::BufferRef, flatbuffers::BufferRef>( + std::mem_fn(&MonsterStorage::Service::Retrieve), this))); +} + +MonsterStorage::Service::~Service() { +} + +::grpc::Status MonsterStorage::Service::Store(::grpc::ServerContext* context, const flatbuffers::BufferRef* request, flatbuffers::BufferRef* response) { + (void) context; + (void) request; + (void) response; + return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, ""); +} + +::grpc::Status MonsterStorage::Service::Retrieve(::grpc::ServerContext* context, const flatbuffers::BufferRef* request, flatbuffers::BufferRef* response) { + (void) context; + (void) request; + (void) response; + return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, ""); +} + + +} // namespace MyGame +} // namespace Example + diff --git a/tests/monster_test.grpc.fb.h b/tests/monster_test.grpc.fb.h new file mode 100644 index 000000000..4269234f3 --- /dev/null +++ b/tests/monster_test.grpc.fb.h @@ -0,0 +1,155 @@ +// Generated by the gRPC protobuf plugin. +// If you make any local change, they will be lost. +// source: monster_test +#ifndef GRPC_monster_5ftest__INCLUDED +#define GRPC_monster_5ftest__INCLUDED + +#include "monster_test_generated.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace grpc { +class CompletionQueue; +class Channel; +class RpcService; +class ServerCompletionQueue; +class ServerContext; +} // namespace grpc + +namespace MyGame { +namespace Example { + +class MonsterStorage GRPC_FINAL { + public: + class StubInterface { + public: + virtual ~StubInterface() {} + virtual ::grpc::Status Store(::grpc::ClientContext* context, const flatbuffers::BufferRef& request, flatbuffers::BufferRef* response) = 0; + std::unique_ptr< ::grpc::ClientAsyncResponseReaderInterface< flatbuffers::BufferRef>> AsyncStore(::grpc::ClientContext* context, const flatbuffers::BufferRef& request, ::grpc::CompletionQueue* cq) { + return std::unique_ptr< ::grpc::ClientAsyncResponseReaderInterface< flatbuffers::BufferRef>>(AsyncStoreRaw(context, request, cq)); + } + virtual ::grpc::Status Retrieve(::grpc::ClientContext* context, const flatbuffers::BufferRef& request, flatbuffers::BufferRef* response) = 0; + std::unique_ptr< ::grpc::ClientAsyncResponseReaderInterface< flatbuffers::BufferRef>> AsyncRetrieve(::grpc::ClientContext* context, const flatbuffers::BufferRef& request, ::grpc::CompletionQueue* cq) { + return std::unique_ptr< ::grpc::ClientAsyncResponseReaderInterface< flatbuffers::BufferRef>>(AsyncRetrieveRaw(context, request, cq)); + } + private: + virtual ::grpc::ClientAsyncResponseReaderInterface< flatbuffers::BufferRef>* AsyncStoreRaw(::grpc::ClientContext* context, const flatbuffers::BufferRef& request, ::grpc::CompletionQueue* cq) = 0; + virtual ::grpc::ClientAsyncResponseReaderInterface< flatbuffers::BufferRef>* AsyncRetrieveRaw(::grpc::ClientContext* context, const flatbuffers::BufferRef& request, ::grpc::CompletionQueue* cq) = 0; + }; + class Stub GRPC_FINAL : public StubInterface { + public: + Stub(const std::shared_ptr< ::grpc::ChannelInterface>& channel); + ::grpc::Status Store(::grpc::ClientContext* context, const flatbuffers::BufferRef& request, flatbuffers::BufferRef* response) GRPC_OVERRIDE; + std::unique_ptr< ::grpc::ClientAsyncResponseReader< flatbuffers::BufferRef>> AsyncStore(::grpc::ClientContext* context, const flatbuffers::BufferRef& request, ::grpc::CompletionQueue* cq) { + return std::unique_ptr< ::grpc::ClientAsyncResponseReader< flatbuffers::BufferRef>>(AsyncStoreRaw(context, request, cq)); + } + ::grpc::Status Retrieve(::grpc::ClientContext* context, const flatbuffers::BufferRef& request, flatbuffers::BufferRef* response) GRPC_OVERRIDE; + std::unique_ptr< ::grpc::ClientAsyncResponseReader< flatbuffers::BufferRef>> AsyncRetrieve(::grpc::ClientContext* context, const flatbuffers::BufferRef& request, ::grpc::CompletionQueue* cq) { + return std::unique_ptr< ::grpc::ClientAsyncResponseReader< flatbuffers::BufferRef>>(AsyncRetrieveRaw(context, request, cq)); + } + + private: + std::shared_ptr< ::grpc::ChannelInterface> channel_; + ::grpc::ClientAsyncResponseReader< flatbuffers::BufferRef>* AsyncStoreRaw(::grpc::ClientContext* context, const flatbuffers::BufferRef& request, ::grpc::CompletionQueue* cq) GRPC_OVERRIDE; + ::grpc::ClientAsyncResponseReader< flatbuffers::BufferRef>* AsyncRetrieveRaw(::grpc::ClientContext* context, const flatbuffers::BufferRef& request, ::grpc::CompletionQueue* cq) GRPC_OVERRIDE; + const ::grpc::RpcMethod rpcmethod_Store_; + const ::grpc::RpcMethod rpcmethod_Retrieve_; + }; + static std::unique_ptr NewStub(const std::shared_ptr< ::grpc::ChannelInterface>& channel, const ::grpc::StubOptions& options = ::grpc::StubOptions()); + + class Service : public ::grpc::Service { + public: + Service(); + virtual ~Service(); + virtual ::grpc::Status Store(::grpc::ServerContext* context, const flatbuffers::BufferRef* request, flatbuffers::BufferRef* response); + virtual ::grpc::Status Retrieve(::grpc::ServerContext* context, const flatbuffers::BufferRef* request, flatbuffers::BufferRef* response); + }; + template + class WithAsyncMethod_Store : public BaseClass { + private: + void BaseClassMustBeDerivedFromService(const Service *service) {} + public: + WithAsyncMethod_Store() { + ::grpc::Service::MarkMethodAsync(0); + } + ~WithAsyncMethod_Store() GRPC_OVERRIDE { + BaseClassMustBeDerivedFromService(this); + } + // disable synchronous version of this method + ::grpc::Status Store(::grpc::ServerContext* context, const flatbuffers::BufferRef* request, flatbuffers::BufferRef* response) GRPC_FINAL GRPC_OVERRIDE { + abort(); + return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, ""); + } + void RequestStore(::grpc::ServerContext* context, flatbuffers::BufferRef* request, ::grpc::ServerAsyncResponseWriter< flatbuffers::BufferRef>* response, ::grpc::CompletionQueue* new_call_cq, ::grpc::ServerCompletionQueue* notification_cq, void *tag) { + ::grpc::Service::RequestAsyncUnary(0, context, request, response, new_call_cq, notification_cq, tag); + } + }; + template + class WithAsyncMethod_Retrieve : public BaseClass { + private: + void BaseClassMustBeDerivedFromService(const Service *service) {} + public: + WithAsyncMethod_Retrieve() { + ::grpc::Service::MarkMethodAsync(1); + } + ~WithAsyncMethod_Retrieve() GRPC_OVERRIDE { + BaseClassMustBeDerivedFromService(this); + } + // disable synchronous version of this method + ::grpc::Status Retrieve(::grpc::ServerContext* context, const flatbuffers::BufferRef* request, flatbuffers::BufferRef* response) GRPC_FINAL GRPC_OVERRIDE { + abort(); + return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, ""); + } + void RequestRetrieve(::grpc::ServerContext* context, flatbuffers::BufferRef* request, ::grpc::ServerAsyncResponseWriter< flatbuffers::BufferRef>* response, ::grpc::CompletionQueue* new_call_cq, ::grpc::ServerCompletionQueue* notification_cq, void *tag) { + ::grpc::Service::RequestAsyncUnary(1, context, request, response, new_call_cq, notification_cq, tag); + } + }; + typedef WithAsyncMethod_Store< WithAsyncMethod_Retrieve< Service > > AsyncService; + template + class WithGenericMethod_Store : public BaseClass { + private: + void BaseClassMustBeDerivedFromService(const Service *service) {} + public: + WithGenericMethod_Store() { + ::grpc::Service::MarkMethodGeneric(0); + } + ~WithGenericMethod_Store() GRPC_OVERRIDE { + BaseClassMustBeDerivedFromService(this); + } + // disable synchronous version of this method + ::grpc::Status Store(::grpc::ServerContext* context, const flatbuffers::BufferRef* request, flatbuffers::BufferRef* response) GRPC_FINAL GRPC_OVERRIDE { + abort(); + return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, ""); + } + }; + template + class WithGenericMethod_Retrieve : public BaseClass { + private: + void BaseClassMustBeDerivedFromService(const Service *service) {} + public: + WithGenericMethod_Retrieve() { + ::grpc::Service::MarkMethodGeneric(1); + } + ~WithGenericMethod_Retrieve() GRPC_OVERRIDE { + BaseClassMustBeDerivedFromService(this); + } + // disable synchronous version of this method + ::grpc::Status Retrieve(::grpc::ServerContext* context, const flatbuffers::BufferRef* request, flatbuffers::BufferRef* response) GRPC_FINAL GRPC_OVERRIDE { + abort(); + return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, ""); + } + }; +}; + +} // namespace Example +} // namespace MyGame + + +#endif // GRPC_monster_5ftest__INCLUDED diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 594c6f7c8..50fbc28ce 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -6,6 +6,12 @@ #include "flatbuffers/flatbuffers.h" namespace MyGame { +namespace Example2 { + +struct Monster; + +} // namespace Example2 + namespace Example { struct Test; @@ -22,8 +28,8 @@ enum Color { Color_Red = 1, Color_Green = 2, Color_Blue = 8, - Color_MIN = Color_Red, - Color_MAX = Color_Blue + Color_NONE = 0, + Color_ANY = 11 }; inline const char **EnumNamesColor() { @@ -37,12 +43,13 @@ enum Any { Any_NONE = 0, Any_Monster = 1, Any_TestSimpleTableWithEnum = 2, + Any_MyGame_Example2_Monster = 3, Any_MIN = Any_NONE, - Any_MAX = Any_TestSimpleTableWithEnum + Any_MAX = Any_MyGame_Example2_Monster }; inline const char **EnumNamesAny() { - static const char *names[] = { "NONE", "Monster", "TestSimpleTableWithEnum", nullptr }; + static const char *names[] = { "NONE", "Monster", "TestSimpleTableWithEnum", "MyGame_Example2_Monster", nullptr }; return names; } @@ -98,6 +105,37 @@ MANUALLY_ALIGNED_STRUCT(16) Vec3 FLATBUFFERS_FINAL_CLASS { }; STRUCT_END(Vec3, 32); +} // namespace Example + +namespace Example2 { + +struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { + bool Verify(flatbuffers::Verifier &verifier) const { + return VerifyTableStart(verifier) && + verifier.EndTable(); + } +}; + +struct MonsterBuilder { + flatbuffers::FlatBufferBuilder &fbb_; + flatbuffers::uoffset_t start_; + MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } + MonsterBuilder &operator=(const MonsterBuilder &); + flatbuffers::Offset Finish() { + auto o = flatbuffers::Offset(fbb_.EndTable(start_, 0)); + return o; + } +}; + +inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder &_fbb) { + MonsterBuilder builder_(_fbb); + return builder_.Finish(); +} + +} // namespace Example2 + +namespace Example { + struct TestSimpleTableWithEnum FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { enum { VT_COLOR = 4 @@ -206,7 +244,8 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { VT_TESTARRAYOFBOOLS = 52, VT_TESTF = 54, VT_TESTF2 = 56, - VT_TESTF3 = 58 + VT_TESTF3 = 58, + VT_TESTARRAYOFSTRING2 = 60 }; const Vec3 *pos() const { return GetStruct(VT_POS); } Vec3 *mutable_pos() { return GetStruct(VT_POS); } @@ -267,6 +306,8 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool mutate_testf2(float _testf2) { return SetField(VT_TESTF2, _testf2); } float testf3() const { return GetField(VT_TESTF3, 0.0f); } 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); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField(verifier, VT_POS) && @@ -308,6 +349,9 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { VerifyField(verifier, VT_TESTF) && VerifyField(verifier, VT_TESTF2) && VerifyField(verifier, VT_TESTF3) && + VerifyField(verifier, VT_TESTARRAYOFSTRING2) && + verifier.Verify(testarrayofstring2()) && + verifier.VerifyVectorOfStrings(testarrayofstring2()) && verifier.EndTable(); } }; @@ -342,10 +386,11 @@ struct MonsterBuilder { void add_testf(float testf) { fbb_.AddElement(Monster::VT_TESTF, testf, 3.14159f); } void add_testf2(float testf2) { fbb_.AddElement(Monster::VT_TESTF2, testf2, 3.0f); } void add_testf3(float testf3) { fbb_.AddElement(Monster::VT_TESTF3, testf3, 0.0f); } + void add_testarrayofstring2(flatbuffers::Offset>> testarrayofstring2) { fbb_.AddOffset(Monster::VT_TESTARRAYOFSTRING2, testarrayofstring2); } MonsterBuilder(flatbuffers::FlatBufferBuilder &_fbb) : fbb_(_fbb) { start_ = fbb_.StartTable(); } MonsterBuilder &operator=(const MonsterBuilder &); flatbuffers::Offset Finish() { - auto o = flatbuffers::Offset(fbb_.EndTable(start_, 28)); + auto o = flatbuffers::Offset(fbb_.EndTable(start_, 29)); fbb_.Required(o, Monster::VT_NAME); // name return o; } @@ -378,12 +423,14 @@ inline flatbuffers::Offset CreateMonster(flatbuffers::FlatBufferBuilder flatbuffers::Offset> testarrayofbools = 0, float testf = 3.14159f, float testf2 = 3.0f, - float testf3 = 0.0f) { + float testf3 = 0.0f, + flatbuffers::Offset>> testarrayofstring2 = 0) { MonsterBuilder builder_(_fbb); builder_.add_testhashu64_fnv1a(testhashu64_fnv1a); builder_.add_testhashs64_fnv1a(testhashs64_fnv1a); builder_.add_testhashu64_fnv1(testhashu64_fnv1); builder_.add_testhashs64_fnv1(testhashs64_fnv1); + builder_.add_testarrayofstring2(testarrayofstring2); builder_.add_testf3(testf3); builder_.add_testf2(testf2); builder_.add_testf(testf); @@ -415,6 +462,7 @@ inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, An case Any_NONE: return true; case Any_Monster: return verifier.VerifyTable(reinterpret_cast(union_obj)); case Any_TestSimpleTableWithEnum: return verifier.VerifyTable(reinterpret_cast(union_obj)); + case Any_MyGame_Example2_Monster: return verifier.VerifyTable(reinterpret_cast(union_obj)); default: return false; } } diff --git a/tests/monster_test_generated.js b/tests/monster_test_generated.js index 8316fbc69..f67e84802 100644 --- a/tests/monster_test_generated.js +++ b/tests/monster_test_generated.js @@ -10,6 +10,11 @@ var MyGame = MyGame || {}; */ MyGame.Example = MyGame.Example || {}; +/** + * @const +*/ +MyGame.Example2 = MyGame.Example2 || {}; + /** * @const */ @@ -30,7 +35,59 @@ MyGame.Example.Color = { MyGame.Example.Any = { NONE: 0, Monster: 1, - TestSimpleTableWithEnum: 2 + TestSimpleTableWithEnum: 2, + MyGame_Example2_Monster: 3 +}; + +/** + * @constructor + */ +MyGame.Example2.Monster = function() { + /** + * @type {flatbuffers.ByteBuffer} + */ + this.bb = null; + + /** + * @type {number} + */ + this.bb_pos = 0; +}; + +/** + * @param {number} i + * @param {flatbuffers.ByteBuffer} bb + * @returns {MyGame.Example2.Monster} + */ +MyGame.Example2.Monster.prototype.__init = function(i, bb) { + this.bb_pos = i; + this.bb = bb; + return this; +}; + +/** + * @param {flatbuffers.ByteBuffer} bb + * @param {MyGame.Example2.Monster=} obj + * @returns {MyGame.Example2.Monster} + */ +MyGame.Example2.Monster.getRootAsMonster = function(bb, obj) { + return (obj || new MyGame.Example2.Monster).__init(bb.readInt32(bb.position()) + bb.position(), bb); +}; + +/** + * @param {flatbuffers.Builder} builder + */ +MyGame.Example2.Monster.startMonster = function(builder) { + builder.startObject(0); +}; + +/** + * @param {flatbuffers.Builder} builder + * @returns {flatbuffers.Offset} + */ +MyGame.Example2.Monster.endMonster = function(builder) { + var offset = builder.endObject(); + return offset; }; /** @@ -701,11 +758,29 @@ MyGame.Example.Monster.prototype.testf3 = function() { return offset ? this.bb.readFloat32(this.bb_pos + offset) : 0.0; }; +/** + * @param {number} index + * @param {flatbuffers.Encoding=} optionalEncoding + * @returns {string|Uint8Array} + */ +MyGame.Example.Monster.prototype.testarrayofstring2 = function(index, optionalEncoding) { + var offset = this.bb.__offset(this.bb_pos, 60); + return offset ? this.bb.__string(this.bb.__vector(this.bb_pos + offset) + index * 4, optionalEncoding) : null; +}; + +/** + * @returns {number} + */ +MyGame.Example.Monster.prototype.testarrayofstring2Length = function() { + var offset = this.bb.__offset(this.bb_pos, 60); + return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0; +}; + /** * @param {flatbuffers.Builder} builder */ MyGame.Example.Monster.startMonster = function(builder) { - builder.startObject(28); + builder.startObject(29); }; /** @@ -1037,6 +1112,35 @@ MyGame.Example.Monster.addTestf3 = function(builder, testf3) { builder.addFieldFloat32(27, testf3, 0.0); }; +/** + * @param {flatbuffers.Builder} builder + * @param {flatbuffers.Offset} testarrayofstring2Offset + */ +MyGame.Example.Monster.addTestarrayofstring2 = function(builder, testarrayofstring2Offset) { + builder.addFieldOffset(28, testarrayofstring2Offset, 0); +}; + +/** + * @param {flatbuffers.Builder} builder + * @param {Array.} data + * @returns {flatbuffers.Offset} + */ +MyGame.Example.Monster.createTestarrayofstring2Vector = function(builder, data) { + builder.startVector(4, data.length, 4); + for (var i = data.length - 1; i >= 0; i--) { + builder.addOffset(data[i]); + } + return builder.endVector(); +}; + +/** + * @param {flatbuffers.Builder} builder + * @param {number} numElems + */ +MyGame.Example.Monster.startTestarrayofstring2Vector = function(builder, numElems) { + builder.startVector(4, numElems, 4); +}; + /** * @param {flatbuffers.Builder} builder * @returns {flatbuffers.Offset} diff --git a/tests/monsterdata_test.mon b/tests/monsterdata_test.mon index 7a7fbf707..01bd52794 100644 Binary files a/tests/monsterdata_test.mon and b/tests/monsterdata_test.mon differ diff --git a/tests/namespace_test/NamespaceC/TableInC.cs b/tests/namespace_test/NamespaceC/TableInC.cs new file mode 100644 index 000000000..0f75dfec9 --- /dev/null +++ b/tests/namespace_test/NamespaceC/TableInC.cs @@ -0,0 +1,38 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +namespace NamespaceC +{ + +using System; +using FlatBuffers; + +public sealed class TableInC : Table { + public static TableInC GetRootAsTableInC(ByteBuffer _bb) { return GetRootAsTableInC(_bb, new TableInC()); } + public static TableInC GetRootAsTableInC(ByteBuffer _bb, TableInC obj) { return (obj.__init(_bb.GetInt(_bb.Position) + _bb.Position, _bb)); } + public TableInC __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; } + + public NamespaceA.TableInFirstNS ReferToA1 { get { return GetReferToA1(new NamespaceA.TableInFirstNS()); } } + public NamespaceA.TableInFirstNS GetReferToA1(NamespaceA.TableInFirstNS obj) { int o = __offset(4); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; } + public SecondTableInA ReferToA2 { get { return GetReferToA2(new SecondTableInA()); } } + public SecondTableInA GetReferToA2(SecondTableInA obj) { int o = __offset(6); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; } + + public static Offset CreateTableInC(FlatBufferBuilder builder, + Offset refer_to_a1Offset = default(Offset), + Offset refer_to_a2Offset = default(Offset)) { + builder.StartObject(2); + TableInC.AddReferToA2(builder, refer_to_a2Offset); + TableInC.AddReferToA1(builder, refer_to_a1Offset); + return TableInC.EndTableInC(builder); + } + + public static void StartTableInC(FlatBufferBuilder builder) { builder.StartObject(2); } + public static void AddReferToA1(FlatBufferBuilder builder, Offset referToA1Offset) { builder.AddOffset(0, referToA1Offset.Value, 0); } + public static void AddReferToA2(FlatBufferBuilder builder, Offset referToA2Offset) { builder.AddOffset(1, referToA2Offset.Value, 0); } + public static Offset EndTableInC(FlatBufferBuilder builder) { + int o = builder.EndObject(); + return new Offset(o); + } +}; + + +} diff --git a/tests/namespace_test/NamespaceC/TableInC.go b/tests/namespace_test/NamespaceC/TableInC.go new file mode 100644 index 000000000..dc75c45c2 --- /dev/null +++ b/tests/namespace_test/NamespaceC/TableInC.go @@ -0,0 +1,46 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +package NamespaceC + +import ( + flatbuffers "github.com/google/flatbuffers/go" +) +type TableInC struct { + _tab flatbuffers.Table +} + +func (rcv *TableInC) Init(buf []byte, i flatbuffers.UOffsetT) { + rcv._tab.Bytes = buf + rcv._tab.Pos = i +} + +func (rcv *TableInC) ReferToA1(obj *TableInFirstNS) *TableInFirstNS { + o := flatbuffers.UOffsetT(rcv._tab.Offset(4)) + if o != 0 { + x := rcv._tab.Indirect(o + rcv._tab.Pos) + if obj == nil { + obj = new(TableInFirstNS) + } + obj.Init(rcv._tab.Bytes, x) + return obj + } + return nil +} + +func (rcv *TableInC) ReferToA2(obj *SecondTableInA) *SecondTableInA { + o := flatbuffers.UOffsetT(rcv._tab.Offset(6)) + if o != 0 { + x := rcv._tab.Indirect(o + rcv._tab.Pos) + if obj == nil { + obj = new(SecondTableInA) + } + obj.Init(rcv._tab.Bytes, x) + return obj + } + return nil +} + +func TableInCStart(builder *flatbuffers.Builder) { builder.StartObject(2) } +func TableInCAddReferToA1(builder *flatbuffers.Builder, referToA1 flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(referToA1), 0) } +func TableInCAddReferToA2(builder *flatbuffers.Builder, referToA2 flatbuffers.UOffsetT) { builder.PrependUOffsetTSlot(1, flatbuffers.UOffsetT(referToA2), 0) } +func TableInCEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT { return builder.EndObject() } diff --git a/tests/namespace_test/NamespaceC/TableInC.java b/tests/namespace_test/NamespaceC/TableInC.java new file mode 100644 index 000000000..cf1bca18c --- /dev/null +++ b/tests/namespace_test/NamespaceC/TableInC.java @@ -0,0 +1,38 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +package NamespaceC; + +import java.nio.*; +import java.lang.*; +import java.util.*; +import com.google.flatbuffers.*; + +@SuppressWarnings("unused") +public final class TableInC extends Table { + public static TableInC getRootAsTableInC(ByteBuffer _bb) { return getRootAsTableInC(_bb, new TableInC()); } + public static TableInC getRootAsTableInC(ByteBuffer _bb, TableInC obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__init(_bb.getInt(_bb.position()) + _bb.position(), _bb)); } + public TableInC __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; return this; } + + public NamespaceA.TableInFirstNS referToA1() { return referToA1(new NamespaceA.TableInFirstNS()); } + public NamespaceA.TableInFirstNS referToA1(NamespaceA.TableInFirstNS obj) { int o = __offset(4); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; } + public SecondTableInA referToA2() { return referToA2(new SecondTableInA()); } + public SecondTableInA referToA2(SecondTableInA obj) { int o = __offset(6); return o != 0 ? obj.__init(__indirect(o + bb_pos), bb) : null; } + + public static int createTableInC(FlatBufferBuilder builder, + int refer_to_a1Offset, + int refer_to_a2Offset) { + builder.startObject(2); + TableInC.addReferToA2(builder, refer_to_a2Offset); + TableInC.addReferToA1(builder, refer_to_a1Offset); + return TableInC.endTableInC(builder); + } + + public static void startTableInC(FlatBufferBuilder builder) { builder.startObject(2); } + public static void addReferToA1(FlatBufferBuilder builder, int referToA1Offset) { builder.addOffset(0, referToA1Offset, 0); } + public static void addReferToA2(FlatBufferBuilder builder, int referToA2Offset) { builder.addOffset(1, referToA2Offset, 0); } + public static int endTableInC(FlatBufferBuilder builder) { + int o = builder.endObject(); + return o; + } +}; + diff --git a/tests/namespace_test/NamespaceC/TableInC.php b/tests/namespace_test/NamespaceC/TableInC.php new file mode 100644 index 000000000..116aea1f9 --- /dev/null +++ b/tests/namespace_test/NamespaceC/TableInC.php @@ -0,0 +1,100 @@ +init($bb->getInt($bb->getPosition()) + $bb->getPosition(), $bb)); + } + + /** + * @param int $_i offset + * @param ByteBuffer $_bb + * @return TableInC + **/ + public function init($_i, ByteBuffer $_bb) + { + $this->bb_pos = $_i; + $this->bb = $_bb; + return $this; + } + + public function getReferToA1() + { + $obj = new TableInFirstNS(); + $o = $this->__offset(4); + return $o != 0 ? $obj->init($this->__indirect($o + $this->bb_pos), $this->bb) : 0; + } + + public function getReferToA2() + { + $obj = new SecondTableInA(); + $o = $this->__offset(6); + return $o != 0 ? $obj->init($this->__indirect($o + $this->bb_pos), $this->bb) : 0; + } + + /** + * @param FlatBufferBuilder $builder + * @return void + */ + public static function startTableInC(FlatBufferBuilder $builder) + { + $builder->StartObject(2); + } + + /** + * @param FlatBufferBuilder $builder + * @return TableInC + */ + public static function createTableInC(FlatBufferBuilder $builder, $refer_to_a1, $refer_to_a2) + { + $builder->startObject(2); + self::addReferToA1($builder, $refer_to_a1); + self::addReferToA2($builder, $refer_to_a2); + $o = $builder->endObject(); + return $o; + } + + /** + * @param FlatBufferBuilder $builder + * @param int + * @return void + */ + public static function addReferToA1(FlatBufferBuilder $builder, $referToA1) + { + $builder->addOffsetX(0, $referToA1, 0); + } + + /** + * @param FlatBufferBuilder $builder + * @param int + * @return void + */ + public static function addReferToA2(FlatBufferBuilder $builder, $referToA2) + { + $builder->addOffsetX(1, $referToA2, 0); + } + + /** + * @param FlatBufferBuilder $builder + * @return int table offset + */ + public static function endTableInC(FlatBufferBuilder $builder) + { + $o = $builder->endObject(); + return $o; + } +} diff --git a/tests/namespace_test/NamespaceC/TableInC.py b/tests/namespace_test/NamespaceC/TableInC.py new file mode 100644 index 000000000..5a4376ed8 --- /dev/null +++ b/tests/namespace_test/NamespaceC/TableInC.py @@ -0,0 +1,39 @@ +# automatically generated by the FlatBuffers compiler, do not modify + +# namespace: NamespaceC + +import flatbuffers + +class TableInC(object): + __slots__ = ['_tab'] + + # TableInC + def Init(self, buf, pos): + self._tab = flatbuffers.table.Table(buf, pos) + + # TableInC + def ReferToA1(self): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4)) + if o != 0: + x = self._tab.Indirect(o + self._tab.Pos) + from .TableInFirstNS import TableInFirstNS + obj = TableInFirstNS() + obj.Init(self._tab.Bytes, x) + return obj + return None + + # TableInC + def ReferToA2(self): + o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(6)) + if o != 0: + x = self._tab.Indirect(o + self._tab.Pos) + from .SecondTableInA import SecondTableInA + obj = SecondTableInA() + obj.Init(self._tab.Bytes, x) + return obj + return None + +def TableInCStart(builder): builder.StartObject(2) +def TableInCAddReferToA1(builder, referToA1): builder.PrependUOffsetTRelativeSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(referToA1), 0) +def TableInCAddReferToA2(builder, referToA2): builder.PrependUOffsetTRelativeSlot(1, flatbuffers.number_types.UOffsetTFlags.py_type(referToA2), 0) +def TableInCEnd(builder): return builder.EndObject() diff --git a/tests/namespace_test/namespace_test2_generated.h b/tests/namespace_test/namespace_test2_generated.h index d0cc5fab2..8e6b3ada8 100644 --- a/tests/namespace_test/namespace_test2_generated.h +++ b/tests/namespace_test/namespace_test2_generated.h @@ -23,10 +23,6 @@ namespace NamespaceA { struct SecondTableInA; -} // namespace NamespaceA - -namespace NamespaceA { - struct TableInFirstNS FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { enum { VT_FOO_TABLE = 4, diff --git a/tests/py_test.py b/tests/py_test.py index d12cfb43d..82f839005 100644 --- a/tests/py_test.py +++ b/tests/py_test.py @@ -162,9 +162,10 @@ class TestFuzz(unittest.TestCase): ''' Low level stress/fuzz test: serialize/deserialize a variety of different kinds of data in different combinations ''' - ofInt32Bytes = compat.binary_type([0x83, 0x33, 0x33, 0x33]) - ofInt64Bytes = compat.binary_type([0x84, 0x44, 0x44, 0x44, - 0x44, 0x44, 0x44, 0x44]) + binary_type = compat.binary_types[0] # this will always exist + ofInt32Bytes = binary_type([0x83, 0x33, 0x33, 0x33]) + ofInt64Bytes = binary_type([0x84, 0x44, 0x44, 0x44, + 0x44, 0x44, 0x44, 0x44]) overflowingInt32Val = flatbuffers.encode.Get(flatbuffers.packer.int32, ofInt32Bytes, 0) overflowingInt64Val = flatbuffers.encode.Get(flatbuffers.packer.int64, diff --git a/tests/test.cpp b/tests/test.cpp index df9cf08b9..6f3b06299 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -108,18 +108,23 @@ flatbuffers::unique_ptr_t CreateFlatBufferTest(std::string &buffer) { mlocs[0] = mb1.Finish(); MonsterBuilder mb2(builder); mb2.add_name(barney); + mb2.add_hp(1000); mlocs[1] = mb2.Finish(); MonsterBuilder mb3(builder); mb3.add_name(wilma); mlocs[2] = mb3.Finish(); - // Create an array of strings. Also test string pooling. - flatbuffers::Offset strings[4]; - strings[0] = builder.CreateSharedString("bob"); - strings[1] = builder.CreateSharedString("fred"); - strings[2] = builder.CreateSharedString("bob"); - strings[3] = builder.CreateSharedString("fred"); - auto vecofstrings = builder.CreateVector(strings, 4); + // Create an array of strings. Also test string pooling, and lambdas. + const char *names[] = { "bob", "fred", "bob", "fred" }; + auto vecofstrings = + builder.CreateVector>(4, + [&](size_t i) { + return builder.CreateSharedString(names[i]); + }); + + // Creating vectors of strings in one convenient call. + std::vector names2 = { "jane", "mary" }; + auto vecofstrings2 = builder.CreateVectorOfStrings(names2); // Create an array of sorted tables, can be used with binary search when read: auto vecoftables = builder.CreateVectorOfSortedTables(mlocs, 3); @@ -127,7 +132,9 @@ flatbuffers::unique_ptr_t CreateFlatBufferTest(std::string &buffer) { // shortcut for creating monster with all fields set: auto mloc = CreateMonster(builder, &vec, 150, 80, name, inventory, Color_Blue, Any_Monster, mlocs[1].Union(), // Store a union. - testv, vecofstrings, vecoftables, 0); + testv, vecofstrings, vecoftables, 0, 0, 0, false, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 3.14159f, 3.0f, 0.0f, + vecofstrings2); FinishMonsterBuffer(builder, mloc); @@ -197,12 +204,20 @@ void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length) { TEST_EQ(vecofstrings->Get(0)->c_str(), vecofstrings->Get(2)->c_str()); TEST_EQ(vecofstrings->Get(1)->c_str(), vecofstrings->Get(3)->c_str()); + auto vecofstrings2 = monster->testarrayofstring2(); + if (vecofstrings2) { + TEST_EQ(vecofstrings2->Length(), 2U); + TEST_EQ_STR(vecofstrings2->Get(0)->c_str(), "jane"); + TEST_EQ_STR(vecofstrings2->Get(1)->c_str(), "mary"); + } + // Example of accessing a vector of tables: auto vecoftables = monster->testarrayoftables(); TEST_EQ(vecoftables->Length(), 3U); for (auto it = vecoftables->begin(); it != vecoftables->end(); ++it) TEST_EQ(strlen(it->name()->c_str()) >= 4, true); TEST_EQ_STR(vecoftables->Get(0)->name()->c_str(), "Barney"); + TEST_EQ(vecoftables->Get(0)->hp(), 1000); TEST_EQ_STR(vecoftables->Get(1)->name()->c_str(), "Fred"); TEST_EQ_STR(vecoftables->Get(2)->name()->c_str(), "Wilma"); TEST_NOTNULL(vecoftables->LookupByKey("Barney")); @@ -260,6 +275,13 @@ void MutateFlatBuffersTest(uint8_t *flatbuf, std::size_t length) { TEST_EQ(inventory->Get(9), 100); inventory->Mutate(9, 9); + auto tables = monster->mutable_testarrayoftables(); + auto first = tables->GetMutableObject(0); + TEST_EQ(first->hp(), 1000); + first->mutate_hp(0); + TEST_EQ(first->hp(), 0); + first->mutate_hp(1000); + // Run the verifier and the regular test to make sure we didn't trample on // anything. AccessFlatBufferTest(flatbuf, length); @@ -761,7 +783,7 @@ void ErrorTest() { TestError("table X { Y:int; Y:int; }", "field already"); TestError("struct X { Y:string; }", "only scalar"); TestError("struct X { Y:int (deprecated); }", "deprecate"); - TestError("union Z { X } table X { Y:Z; } root_type X; { Y: {", + TestError("union Z { X } table X { Y:Z; } root_type X; { Y: {}, A:1 }", "missing type field"); TestError("union Z { X } table X { Y:Z; } root_type X; { Y_type: 99, Y: {", "type id"); @@ -792,20 +814,29 @@ void ErrorTest() { TestError("table X { Y:byte; } root_type X; { Y:1, Y:2 }", "more than once"); } -// Additional parser testing not covered elsewhere. -void ScientificTest() { +float TestValue(const char *json) { flatbuffers::Parser parser; // Simple schema. TEST_EQ(parser.Parse("table X { Y:float; } root_type X;"), true); - // Test scientific notation numbers. - TEST_EQ(parser.Parse("{ Y:0.0314159e+2 }"), true); + TEST_EQ(parser.Parse(json), true); auto root = flatbuffers::GetRoot(parser.builder_.GetBufferPointer()); // root will point to the table, which is a 32bit vtable offset followed // by a float: - TEST_EQ(sizeof(flatbuffers::soffset_t) == 4 && // Test assumes 32bit offsets - fabs(root[1] - 3.14159) < 0.001, true); + TEST_EQ(sizeof(flatbuffers::soffset_t), 4); // Test assumes 32bit offsets + return root[1]; +} + +bool FloatCompare(float a, float b) { return fabs(a - b) < 0.001; } + +// Additional parser testing not covered elsewhere. +void ValueTest() { + // Test scientific notation numbers. + TEST_EQ(FloatCompare(TestValue("{ Y:0.0314159e+2 }"), 3.14159), true); + + // Test conversion functions. + TEST_EQ(FloatCompare(TestValue("{ Y:cos(rad(180)) }"), -1), true); } void EnumStringsTest() { @@ -920,6 +951,16 @@ void UnknownFieldsTest() { TEST_EQ(jsongen == "{str: \"test\",i: 10}", true); } +void ParseUnionTest() { + // Unions must be parseable with the type field following the object. + flatbuffers::Parser parser; + TEST_EQ(parser.Parse("table T { A:int; }" + "union U { T }" + "table V { X:U; }" + "root_type V;" + "{ X:{ A:1 }, X_type: T }"), true); +} + int main(int /*argc*/, const char * /*argv*/[]) { // Run our various test suites: @@ -941,13 +982,14 @@ int main(int /*argc*/, const char * /*argv*/[]) { FuzzTest2(); ErrorTest(); - ScientificTest(); + ValueTest(); EnumStringsTest(); IntegerOutOfRangeTest(); UnicodeTest(); UnicodeSurrogatesTest(); UnicodeInvalidSurrogatesTest(); UnknownFieldsTest(); + ParseUnionTest(); if (!testing_fails) { TEST_OUTPUT_LINE("ALL TESTS PASSED");