diff --git a/src/compiler/cpp_generator.cc b/src/compiler/cpp_generator.cc index 2908b639f39..44578e3fe1e 100644 --- a/src/compiler/cpp_generator.cc +++ b/src/compiler/cpp_generator.cc @@ -40,6 +40,9 @@ namespace grpc_cpp_generator { namespace { +grpc::string message_header_ext() { return ".pb.h"; } +grpc::string service_header_ext() { return ".grpc.pb.h"; } + template grpc::string as_string(T x) { std::ostringstream out; @@ -69,7 +72,7 @@ T *array_end(T (&array)[N]) { return array + N; } -void PrintIncludes(Printer *printer, const std::vector &headers, +void PrintIncludes(grpc_generator::Printer *printer, const std::vector &headers, const Parameters ¶ms) { std::map vars; @@ -90,7 +93,8 @@ void PrintIncludes(Printer *printer, const std::vector &headers, } } -grpc::string GetHeaderPrologue(File *file, const Parameters & /*params*/) { +grpc::string GetHeaderPrologue(grpc_generator::File *file, + const Parameters & /*params*/) { grpc::string output; { // Scope the output stream so it closes and finalizes output to the string. @@ -100,13 +104,13 @@ grpc::string GetHeaderPrologue(File *file, const Parameters & /*params*/) { 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(); + vars["message_header_ext"] = message_header_ext(); printer->Print(vars, "// Generated by the gRPC C++ plugin.\n"); printer->Print(vars, "// If you make any local change, they will be lost.\n"); printer->Print(vars, "// source: $filename$\n"); - grpc::string leading_comments = file->GetLeadingComments(); + grpc::string leading_comments = file->GetLeadingComments("//"); if (!leading_comments.empty()) { printer->Print(vars, "// Original file comments:\n"); printer->Print(leading_comments.c_str()); @@ -120,7 +124,8 @@ grpc::string GetHeaderPrologue(File *file, const Parameters & /*params*/) { return output; } -grpc::string GetHeaderIncludes(File *file, const Parameters ¶ms) { +grpc::string GetHeaderIncludes(grpc_generator::File *file, + const Parameters ¶ms) { grpc::string output; { // Scope the output stream so it closes and finalizes output to the string. @@ -162,7 +167,7 @@ grpc::string GetHeaderIncludes(File *file, const Parameters ¶ms) { } void PrintHeaderClientMethodInterfaces( - Printer *printer, const Method *method, + grpc_generator::Printer *printer, const grpc_generator::Method *method, std::map *vars, bool is_public) { (*vars)["Method"] = method->name(); (*vars)["Request"] = method->input_type_name(); @@ -187,7 +192,7 @@ void PrintHeaderClientMethodInterfaces( "Async$Method$Raw(context, request, cq));\n"); printer->Outdent(); printer->Print("}\n"); - } else if (method->ClientOnlyStreaming()) { + } else if (method->ClientStreaming()) { printer->Print( *vars, "std::unique_ptr< ::grpc::ClientWriterInterface< $Request$>>" @@ -213,7 +218,7 @@ void PrintHeaderClientMethodInterfaces( "Async$Method$Raw(context, response, cq, tag));\n"); printer->Outdent(); printer->Print("}\n"); - } else if (method->ServerOnlyStreaming()) { + } else if (method->ServerStreaming()) { printer->Print( *vars, "std::unique_ptr< ::grpc::ClientReaderInterface< $Response$>>" @@ -275,7 +280,7 @@ void PrintHeaderClientMethodInterfaces( "Async$Method$Raw(::grpc::ClientContext* context, " "const $Request$& request, " "::grpc::CompletionQueue* cq) = 0;\n"); - } else if (method->ClientOnlyStreaming()) { + } else if (method->ClientStreaming()) { printer->Print( *vars, "virtual ::grpc::ClientWriterInterface< $Request$>*" @@ -286,7 +291,7 @@ void PrintHeaderClientMethodInterfaces( " Async$Method$Raw(::grpc::ClientContext* context, " "$Response$* response, " "::grpc::CompletionQueue* cq, void* tag) = 0;\n"); - } else if (method->ServerOnlyStreaming()) { + } else if (method->ServerStreaming()) { printer->Print( *vars, "virtual ::grpc::ClientReaderInterface< $Response$>* $Method$Raw(" @@ -311,9 +316,10 @@ void PrintHeaderClientMethodInterfaces( } } -void PrintHeaderClientMethod(Printer *printer, const Method *method, - std::map *vars, - bool is_public) { +void PrintHeaderClientMethod(grpc_generator::Printer *printer, + const grpc_generator::Method *method, + std::map *vars, + bool is_public) { (*vars)["Method"] = method->name(); (*vars)["Request"] = method->input_type_name(); (*vars)["Response"] = method->output_type_name(); @@ -336,7 +342,7 @@ void PrintHeaderClientMethod(Printer *printer, const Method *method, "Async$Method$Raw(context, request, cq));\n"); printer->Outdent(); printer->Print("}\n"); - } else if (method->ClientOnlyStreaming()) { + } else if (method->ClientStreaming()) { printer->Print( *vars, "std::unique_ptr< ::grpc::ClientWriter< $Request$>>" @@ -360,7 +366,7 @@ void PrintHeaderClientMethod(Printer *printer, const Method *method, "Async$Method$Raw(context, response, cq, tag));\n"); printer->Outdent(); printer->Print("}\n"); - } else if (method->ServerOnlyStreaming()) { + } else if (method->ServerStreaming()) { printer->Print( *vars, "std::unique_ptr< ::grpc::ClientReader< $Response$>>" @@ -418,7 +424,7 @@ void PrintHeaderClientMethod(Printer *printer, const Method *method, "Async$Method$Raw(::grpc::ClientContext* context, " "const $Request$& request, " "::grpc::CompletionQueue* cq) override;\n"); - } else if (method->ClientOnlyStreaming()) { + } else if (method->ClientStreaming()) { printer->Print(*vars, "::grpc::ClientWriter< $Request$>* $Method$Raw(" "::grpc::ClientContext* context, $Response$* response) " @@ -427,7 +433,7 @@ void PrintHeaderClientMethod(Printer *printer, const Method *method, "::grpc::ClientAsyncWriter< $Request$>* Async$Method$Raw(" "::grpc::ClientContext* context, $Response$* response, " "::grpc::CompletionQueue* cq, void* tag) override;\n"); - } else if (method->ServerOnlyStreaming()) { + } else if (method->ServerStreaming()) { printer->Print(*vars, "::grpc::ClientReader< $Response$>* $Method$Raw(" "::grpc::ClientContext* context, const $Request$& request)" @@ -449,30 +455,32 @@ void PrintHeaderClientMethod(Printer *printer, const Method *method, } } -void PrintHeaderClientMethodData(Printer *printer, const Method *method, +void PrintHeaderClientMethodData(grpc_generator::Printer *printer, + const grpc_generator::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, +void PrintHeaderServerMethodSync(grpc_generator::Printer *printer, + const grpc_generator::Method *method, std::map *vars) { (*vars)["Method"] = method->name(); (*vars)["Request"] = method->input_type_name(); (*vars)["Response"] = method->output_type_name(); - printer->Print(method->GetLeadingComments().c_str()); + printer->Print(method->GetLeadingComments("//").c_str()); if (method->NoStreaming()) { printer->Print(*vars, "virtual ::grpc::Status $Method$(" "::grpc::ServerContext* context, const $Request$* request, " "$Response$* response);\n"); - } else if (method->ClientOnlyStreaming()) { + } else if (method->ClientStreaming()) { printer->Print(*vars, "virtual ::grpc::Status $Method$(" "::grpc::ServerContext* context, " "::grpc::ServerReader< $Request$>* reader, " "$Response$* response);\n"); - } else if (method->ServerOnlyStreaming()) { + } else if (method->ServerStreaming()) { printer->Print(*vars, "virtual ::grpc::Status $Method$(" "::grpc::ServerContext* context, const $Request$* request, " @@ -485,10 +493,11 @@ void PrintHeaderServerMethodSync(Printer *printer, const Method *method, "::grpc::ServerReaderWriter< $Response$, $Request$>* stream);" "\n"); } - printer->Print(method->GetTrailingComments().c_str()); + printer->Print(method->GetTrailingComments("//").c_str()); } -void PrintHeaderServerMethodAsync(Printer *printer, const Method *method, +void PrintHeaderServerMethodAsync(grpc_generator::Printer *printer, + const grpc_generator::Method *method, std::map *vars) { (*vars)["Method"] = method->name(); (*vars)["Request"] = method->input_type_name(); @@ -530,7 +539,7 @@ void PrintHeaderServerMethodAsync(Printer *printer, const Method *method, " ::grpc::Service::RequestAsyncUnary($Idx$, context, " "request, response, new_call_cq, notification_cq, tag);\n"); printer->Print("}\n"); - } else if (method->ClientOnlyStreaming()) { + } else if (method->ClientStreaming()) { printer->Print( *vars, "// disable synchronous version of this method\n" @@ -552,7 +561,7 @@ void PrintHeaderServerMethodAsync(Printer *printer, const Method *method, " ::grpc::Service::RequestAsyncClientStreaming($Idx$, " "context, reader, new_call_cq, notification_cq, tag);\n"); printer->Print("}\n"); - } else if (method->ServerOnlyStreaming()) { + } else if (method->ServerStreaming()) { printer->Print( *vars, "// disable synchronous version of this method\n" @@ -603,7 +612,7 @@ void PrintHeaderServerMethodAsync(Printer *printer, const Method *method, } void PrintHeaderServerMethodStreamedUnary( - Printer *printer, const Method *method, + grpc_generator::Printer *printer, const grpc_generator::Method *method, std::map *vars) { (*vars)["Method"] = method->name(); (*vars)["Request"] = method->input_type_name(); @@ -654,12 +663,12 @@ void PrintHeaderServerMethodStreamedUnary( } void PrintHeaderServerMethodSplitStreaming( - Printer *printer, const Method *method, + grpc_generator::Printer *printer, const grpc_generator::Method *method, std::map *vars) { (*vars)["Method"] = method->name(); (*vars)["Request"] = method->input_type_name(); (*vars)["Response"] = method->output_type_name(); - if (method->ServerOnlyStreaming()) { + if (method->ServerStreaming()) { printer->Print(*vars, "template \n"); printer->Print(*vars, "class WithSplitStreamingMethod_$Method$ : " @@ -706,7 +715,7 @@ void PrintHeaderServerMethodSplitStreaming( } void PrintHeaderServerMethodGeneric( - Printer *printer, const Method *method, + grpc_generator::Printer *printer, const grpc_generator::Method *method, std::map *vars) { (*vars)["Method"] = method->name(); (*vars)["Request"] = method->input_type_name(); @@ -737,7 +746,7 @@ void PrintHeaderServerMethodGeneric( " abort();\n" " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n" "}\n"); - } else if (method->ClientOnlyStreaming()) { + } else if (method->ClientStreaming()) { printer->Print( *vars, "// disable synchronous version of this method\n" @@ -748,7 +757,7 @@ void PrintHeaderServerMethodGeneric( " abort();\n" " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n" "}\n"); - } else if (method->ServerOnlyStreaming()) { + } else if (method->ServerStreaming()) { printer->Print( *vars, "// disable synchronous version of this method\n" @@ -775,11 +784,12 @@ void PrintHeaderServerMethodGeneric( printer->Print(*vars, "};\n"); } -void PrintHeaderService(Printer *printer, const Service *service, +void PrintHeaderService(grpc_generator::Printer *printer, + const grpc_generator::Service *service, std::map *vars) { (*vars)["Service"] = service->name(); - printer->Print(service->GetLeadingComments().c_str()); + printer->Print(service->GetLeadingComments("//").c_str()); printer->Print(*vars, "class $Service$ final {\n" " public:\n"); @@ -792,10 +802,10 @@ void PrintHeaderService(Printer *printer, const Service *service, printer->Indent(); printer->Print("virtual ~StubInterface() {}\n"); for (int i = 0; i < service->method_count(); ++i) { - printer->Print(service->method(i)->GetLeadingComments().c_str()); + printer->Print(service->method(i)->GetLeadingComments("//").c_str()); PrintHeaderClientMethodInterfaces(printer, service->method(i).get(), vars, true); - printer->Print(service->method(i)->GetTrailingComments().c_str()); + printer->Print(service->method(i)->GetTrailingComments("//").c_str()); } printer->Outdent(); printer->Print("private:\n"); @@ -903,13 +913,13 @@ void PrintHeaderService(Printer *printer, const Service *service, printer->Print("typedef "); for (int i = 0; i < service->method_count(); ++i) { (*vars)["method_name"] = service->method(i).get()->name(); - if (service->method(i)->ServerOnlyStreaming()) { + if (service->method(i)->ServerStreaming()) { printer->Print(*vars, "WithSplitStreamingMethod_$method_name$<"); } } printer->Print("Service"); for (int i = 0; i < service->method_count(); ++i) { - if (service->method(i)->ServerOnlyStreaming()) { + if (service->method(i)->ServerStreaming()) { printer->Print(" >"); } } @@ -919,7 +929,7 @@ void PrintHeaderService(Printer *printer, const Service *service, printer->Print("typedef "); for (int i = 0; i < service->method_count(); ++i) { (*vars)["method_name"] = service->method(i).get()->name(); - if (service->method(i)->ServerOnlyStreaming()) { + if (service->method(i)->ServerStreaming()) { printer->Print(*vars, "WithSplitStreamingMethod_$method_name$<"); } if (service->method(i)->NoStreaming()) { @@ -929,7 +939,7 @@ void PrintHeaderService(Printer *printer, const Service *service, printer->Print("Service"); for (int i = 0; i < service->method_count(); ++i) { if (service->method(i)->NoStreaming() || - service->method(i)->ServerOnlyStreaming()) { + service->method(i)->ServerStreaming()) { printer->Print(" >"); } } @@ -937,10 +947,11 @@ void PrintHeaderService(Printer *printer, const Service *service, printer->Outdent(); printer->Print("};\n"); - printer->Print(service->GetTrailingComments().c_str()); + printer->Print(service->GetTrailingComments("//").c_str()); } -grpc::string GetHeaderServices(File *file, const Parameters ¶ms) { +grpc::string GetHeaderServices(grpc_generator::File *file, + const Parameters ¶ms) { grpc::string output; { // Scope the output stream so it closes and finalizes output to the string. @@ -970,7 +981,8 @@ grpc::string GetHeaderServices(File *file, const Parameters ¶ms) { return output; } -grpc::string GetHeaderEpilogue(File *file, const Parameters & /*params*/) { +grpc::string GetHeaderEpilogue(grpc_generator::File *file, + const Parameters & /*params*/) { grpc::string output; { // Scope the output stream so it closes and finalizes output to the string. @@ -993,12 +1005,13 @@ grpc::string GetHeaderEpilogue(File *file, const Parameters & /*params*/) { printer->Print(vars, "\n"); printer->Print(vars, "#endif // GRPC_$filename_identifier$__INCLUDED\n"); - printer->Print(file->GetTrailingComments().c_str()); + printer->Print(file->GetTrailingComments("//").c_str()); } return output; } -grpc::string GetSourcePrologue(File *file, const Parameters & /*params*/) { +grpc::string GetSourcePrologue(grpc_generator::File *file, + const Parameters & /*params*/) { grpc::string output; { // Scope the output stream so it closes and finalizes output to the string. @@ -1007,8 +1020,8 @@ grpc::string GetSourcePrologue(File *file, const Parameters & /*params*/) { 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(); + vars["message_header_ext"] = message_header_ext(); + vars["service_header_ext"] = service_header_ext(); printer->Print(vars, "// Generated by the gRPC C++ plugin.\n"); printer->Print(vars, @@ -1023,7 +1036,8 @@ grpc::string GetSourcePrologue(File *file, const Parameters & /*params*/) { return output; } -grpc::string GetSourceIncludes(File *file, const Parameters ¶ms) { +grpc::string GetSourceIncludes(grpc_generator::File *file, + const Parameters ¶ms) { grpc::string output; { // Scope the output stream so it closes and finalizes output to the string. @@ -1056,7 +1070,8 @@ grpc::string GetSourceIncludes(File *file, const Parameters ¶ms) { return output; } -void PrintSourceClientMethod(Printer *printer, const Method *method, +void PrintSourceClientMethod(grpc_generator::Printer *printer, + const grpc_generator::Method *method, std::map *vars) { (*vars)["Method"] = method->name(); (*vars)["Request"] = method->input_type_name(); @@ -1084,7 +1099,7 @@ void PrintSourceClientMethod(Printer *printer, const Method *method, "rpcmethod_$Method$_, " "context, request);\n" "}\n\n"); - } else if (method->ClientOnlyStreaming()) { + } else if (method->ClientStreaming()) { printer->Print(*vars, "::grpc::ClientWriter< $Request$>* " "$ns$$Service$::Stub::$Method$Raw(" @@ -1106,7 +1121,7 @@ void PrintSourceClientMethod(Printer *printer, const Method *method, "rpcmethod_$Method$_, " "context, response, tag);\n" "}\n\n"); - } else if (method->ServerOnlyStreaming()) { + } else if (method->ServerStreaming()) { printer->Print( *vars, "::grpc::ClientReader< $Response$>* " @@ -1156,7 +1171,8 @@ void PrintSourceClientMethod(Printer *printer, const Method *method, } } -void PrintSourceServerMethod(Printer *printer, const Method *method, +void PrintSourceServerMethod(grpc_generator::Printer *printer, + const grpc_generator::Method *method, std::map *vars) { (*vars)["Method"] = method->name(); (*vars)["Request"] = method->input_type_name(); @@ -1173,7 +1189,7 @@ void PrintSourceServerMethod(Printer *printer, const Method *method, " return ::grpc::Status(" "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"); printer->Print("}\n\n"); - } else if (method->ClientOnlyStreaming()) { + } else if (method->ClientStreaming()) { printer->Print(*vars, "::grpc::Status $ns$$Service$::Service::$Method$(" "::grpc::ServerContext* context, " @@ -1186,7 +1202,7 @@ void PrintSourceServerMethod(Printer *printer, const Method *method, " return ::grpc::Status(" "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"); printer->Print("}\n\n"); - } else if (method->ServerOnlyStreaming()) { + } else if (method->ServerStreaming()) { printer->Print(*vars, "::grpc::Status $ns$$Service$::Service::$Method$(" "::grpc::ServerContext* context, " @@ -1214,7 +1230,8 @@ void PrintSourceServerMethod(Printer *printer, const Method *method, } } -void PrintSourceService(Printer *printer, const Service *service, +void PrintSourceService(grpc_generator::Printer *printer, + const grpc_generator::Service *service, std::map *vars) { (*vars)["Service"] = service->name(); @@ -1250,9 +1267,9 @@ void PrintSourceService(Printer *printer, const Service *service, // NOTE: There is no reason to consider streamed-unary as a separate // category here since this part is setting up the client-side stub // and this appears as a NORMAL_RPC from the client-side. - } else if (method->ClientOnlyStreaming()) { + } else if (method->ClientStreaming()) { (*vars)["StreamingType"] = "CLIENT_STREAMING"; - } else if (method->ServerOnlyStreaming()) { + } else if (method->ServerStreaming()) { (*vars)["StreamingType"] = "SERVER_STREAMING"; } else { (*vars)["StreamingType"] = "BIDI_STREAMING"; @@ -1290,7 +1307,7 @@ void PrintSourceService(Printer *printer, const Service *service, "$Request$, " "$Response$>(\n" " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n"); - } else if (method->ClientOnlyStreaming()) { + } else if (method->ClientStreaming()) { printer->Print( *vars, "AddMethod(new ::grpc::RpcServiceMethod(\n" @@ -1299,7 +1316,7 @@ void PrintSourceService(Printer *printer, const Service *service, " new ::grpc::ClientStreamingHandler< " "$ns$$Service$::Service, $Request$, $Response$>(\n" " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n"); - } else if (method->ServerOnlyStreaming()) { + } else if (method->ServerStreaming()) { printer->Print( *vars, "AddMethod(new ::grpc::RpcServiceMethod(\n" @@ -1330,7 +1347,8 @@ void PrintSourceService(Printer *printer, const Service *service, } } -grpc::string GetSourceServices(File *file, const Parameters ¶ms) { +grpc::string GetSourceServices(grpc_generator::File *file, + const Parameters ¶ms) { grpc::string output; { // Scope the output stream so it closes and finalizes output to the string. @@ -1358,7 +1376,8 @@ grpc::string GetSourceServices(File *file, const Parameters ¶ms) { return output; } -grpc::string GetSourceEpilogue(File *file, const Parameters & /*params*/) { +grpc::string GetSourceEpilogue(grpc_generator::File *file, + const Parameters & /*params*/) { grpc::string temp; if (!file->package().empty()) { diff --git a/src/compiler/cpp_generator.h b/src/compiler/cpp_generator.h index d0343e9978b..dde72025c8a 100644 --- a/src/compiler/cpp_generator.h +++ b/src/compiler/cpp_generator.h @@ -42,6 +42,7 @@ #include #include "src/compiler/config.h" +#include "src/compiler/schema_interface.h" #ifndef GRPC_CUSTOM_STRING #include @@ -66,91 +67,29 @@ struct Parameters { grpc::string grpc_search_path; }; -// A common interface for objects having comments in the source. -// Return formatted comments to be inserted in generated code. -struct CommentHolder { - virtual ~CommentHolder() {} - virtual grpc::string GetLeadingComments() const = 0; - virtual grpc::string GetTrailingComments() const = 0; -}; - -// An abstract interface representing a method. -struct Method : public CommentHolder { - 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 : public CommentHolder { - 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 : public CommentHolder { - 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); +grpc::string GetHeaderPrologue(grpc_generator::File *file, const Parameters ¶ms); // Return the includes needed for generated header file. -grpc::string GetHeaderIncludes(File *file, const Parameters ¶ms); +grpc::string GetHeaderIncludes(grpc_generator::File *file, const Parameters ¶ms); // Return the includes needed for generated source file. -grpc::string GetSourceIncludes(File *file, const Parameters ¶ms); +grpc::string GetSourceIncludes(grpc_generator::File *file, const Parameters ¶ms); // Return the epilogue of the generated header file. -grpc::string GetHeaderEpilogue(File *file, const Parameters ¶ms); +grpc::string GetHeaderEpilogue(grpc_generator::File *file, const Parameters ¶ms); // Return the prologue of the generated source file. -grpc::string GetSourcePrologue(File *file, const Parameters ¶ms); +grpc::string GetSourcePrologue(grpc_generator::File *file, const Parameters ¶ms); // Return the services for generated header file. -grpc::string GetHeaderServices(File *file, const Parameters ¶ms); +grpc::string GetHeaderServices(grpc_generator::File *file, const Parameters ¶ms); // Return the services for generated source file. -grpc::string GetSourceServices(File *file, const Parameters ¶ms); +grpc::string GetSourceServices(grpc_generator::File *file, const Parameters ¶ms); // Return the epilogue of the generated source file. -grpc::string GetSourceEpilogue(File *file, const Parameters ¶ms); +grpc::string GetSourceEpilogue(grpc_generator::File *file, const Parameters ¶ms); } // namespace grpc_cpp_generator diff --git a/src/compiler/cpp_generator_helpers.h b/src/compiler/cpp_generator_helpers.h index 87e278f1b9f..83932a09bb8 100644 --- a/src/compiler/cpp_generator_helpers.h +++ b/src/compiler/cpp_generator_helpers.h @@ -35,6 +35,7 @@ #define GRPC_INTERNAL_COMPILER_CPP_GENERATOR_HELPERS_H #include + #include "src/compiler/config.h" #include "src/compiler/generator_helpers.h" diff --git a/src/compiler/cpp_plugin.cc b/src/compiler/cpp_plugin.cc index 38f8f738ed9..4ee05ee0370 100644 --- a/src/compiler/cpp_plugin.cc +++ b/src/compiler/cpp_plugin.cc @@ -40,139 +40,8 @@ #include "src/compiler/config.h" #include "src/compiler/cpp_generator.h" -#include "src/compiler/cpp_generator_helpers.h" #include "src/compiler/generator_helpers.h" - -using grpc_cpp_generator::GetCppComments; - -class ProtoBufMethod : public grpc_cpp_generator::Method { - public: - ProtoBufMethod(const grpc::protobuf::MethodDescriptor *method) - : method_(method) {} - - grpc::string name() const { return method_->name(); } - - grpc::string input_type_name() const { - return grpc_cpp_generator::ClassName(method_->input_type(), true); - } - grpc::string output_type_name() const { - return grpc_cpp_generator::ClassName(method_->output_type(), true); - } - - bool NoStreaming() const { - return !method_->client_streaming() && !method_->server_streaming(); - } - - bool ClientOnlyStreaming() const { - return method_->client_streaming() && !method_->server_streaming(); - } - - bool ServerOnlyStreaming() const { - return !method_->client_streaming() && method_->server_streaming(); - } - - bool BidiStreaming() const { - return method_->client_streaming() && method_->server_streaming(); - } - - grpc::string GetLeadingComments() const { - return GetCppComments(method_, true); - } - - grpc::string GetTrailingComments() const { - return GetCppComments(method_, false); - } - - private: - const grpc::protobuf::MethodDescriptor *method_; -}; - -class ProtoBufService : public grpc_cpp_generator::Service { - public: - ProtoBufService(const grpc::protobuf::ServiceDescriptor *service) - : service_(service) {} - - grpc::string name() const { return service_->name(); } - - int method_count() const { return service_->method_count(); }; - std::unique_ptr method(int i) const { - return std::unique_ptr( - new ProtoBufMethod(service_->method(i))); - }; - - grpc::string GetLeadingComments() const { - return GetCppComments(service_, true); - } - - grpc::string GetTrailingComments() const { - return GetCppComments(service_, false); - } - - private: - const grpc::protobuf::ServiceDescriptor *service_; -}; - -class ProtoBufPrinter : public grpc_cpp_generator::Printer { - public: - ProtoBufPrinter(grpc::string *str) - : output_stream_(str), printer_(&output_stream_, '$') {} - - void Print(const std::map &vars, - const char *string_template) { - printer_.Print(vars, string_template); - } - - void Print(const char *string) { printer_.Print(string); } - void Indent() { printer_.Indent(); } - void Outdent() { printer_.Outdent(); } - - private: - grpc::protobuf::io::StringOutputStream output_stream_; - grpc::protobuf::io::Printer printer_; -}; - -class ProtoBufFile : public grpc_cpp_generator::File { - public: - ProtoBufFile(const grpc::protobuf::FileDescriptor *file) : file_(file) {} - - grpc::string filename() const { return file_->name(); } - grpc::string filename_without_ext() const { - return grpc_generator::StripProto(filename()); - } - - grpc::string message_header_ext() const { return ".pb.h"; } - grpc::string service_header_ext() const { return ".grpc.pb.h"; } - - grpc::string package() const { return file_->package(); } - std::vector package_parts() const { - return grpc_generator::tokenize(package(), "."); - } - - grpc::string additional_headers() const { return ""; } - - int service_count() const { return file_->service_count(); }; - std::unique_ptr service(int i) const { - return std::unique_ptr( - new ProtoBufService(file_->service(i))); - } - - std::unique_ptr CreatePrinter( - grpc::string *str) const { - return std::unique_ptr( - new ProtoBufPrinter(str)); - } - - grpc::string GetLeadingComments() const { - return GetCppComments(file_, true); - } - - grpc::string GetTrailingComments() const { - return GetCppComments(file_, false); - } - - private: - const grpc::protobuf::FileDescriptor *file_; -}; +#include "src/compiler/protobuf_plugin.h" class CppGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { public: diff --git a/src/compiler/protobuf_plugin.h b/src/compiler/protobuf_plugin.h new file mode 100644 index 00000000000..23ba49c7a77 --- /dev/null +++ b/src/compiler/protobuf_plugin.h @@ -0,0 +1,146 @@ + +#ifndef GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H +#define GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H + +#include "src/compiler/config.h" +#include "src/compiler/schema_interface.h" +#include "src/compiler/cpp_generator_helpers.h" + +#include + +// Get leading or trailing comments in a string. +template +inline grpc::string GetCommentsHelper(const DescriptorType *desc, bool leading, + const grpc::string &prefix) { + return grpc_generator::GetPrefixedComments(desc, leading, prefix); +} + +class ProtoBufMethod : public grpc_generator::Method { + public: + ProtoBufMethod(const grpc::protobuf::MethodDescriptor *method) + : method_(method) {} + + grpc::string name() const { return method_->name(); } + + grpc::string input_type_name() const { + return grpc_cpp_generator::ClassName(method_->input_type(), true); + } + grpc::string output_type_name() const { + return grpc_cpp_generator::ClassName(method_->output_type(), true); + } + + bool NoStreaming() const { + return !method_->client_streaming() && !method_->server_streaming(); + } + + bool ClientStreaming() const { + return method_->client_streaming() && !method_->server_streaming(); + } + + bool ServerStreaming() const { + return !method_->client_streaming() && method_->server_streaming(); + } + + bool BidiStreaming() const { + return method_->client_streaming() && method_->server_streaming(); + } + + grpc::string GetLeadingComments(const grpc::string prefix) const { + return GetCommentsHelper(method_, true, prefix); + } + + grpc::string GetTrailingComments(const grpc::string prefix) const { + return GetCommentsHelper(method_, false, prefix); + } + + private: + const grpc::protobuf::MethodDescriptor *method_; + }; + +class ProtoBufService : public grpc_generator::Service { + public: + ProtoBufService(const grpc::protobuf::ServiceDescriptor *service) + : service_(service) {} + + grpc::string name() const { return service_->name(); } + + int method_count() const { return service_->method_count(); }; + std::unique_ptr method(int i) const { + return std::unique_ptr( + new ProtoBufMethod(service_->method(i))); + }; + + grpc::string GetLeadingComments(const grpc::string prefix) const { + return GetCommentsHelper(service_, true, prefix); + } + + grpc::string GetTrailingComments(const grpc::string prefix) const { + return GetCommentsHelper(service_, false, prefix); + } + + private: + const grpc::protobuf::ServiceDescriptor *service_; +}; + +class ProtoBufPrinter : public grpc_generator::Printer { + public: + ProtoBufPrinter(grpc::string *str) + : output_stream_(str), printer_(&output_stream_, '$') {} + + void Print(const std::map &vars, + const char *string_template) { + printer_.Print(vars, string_template); + } + + void Print(const char *string) { printer_.Print(string); } + void Indent() { printer_.Indent(); } + void Outdent() { printer_.Outdent(); } + + private: + grpc::protobuf::io::StringOutputStream output_stream_; + grpc::protobuf::io::Printer printer_; + }; + +class ProtoBufFile : public grpc_generator::File { + public: + ProtoBufFile(const grpc::protobuf::FileDescriptor *file) : file_(file) {} + + grpc::string filename() const { return file_->name(); } + grpc::string filename_without_ext() const { + return grpc_generator::StripProto(filename()); + } + + grpc::string package() const { return file_->package(); } + std::vector package_parts() const { + return grpc_generator::tokenize(package(), "."); + } + + grpc::string additional_headers() const { return ""; } + + int service_count() const { return file_->service_count(); }; + std::unique_ptr service(int i) const { + return std::unique_ptr( + new ProtoBufService(file_->service(i))); + } + + std::unique_ptr CreatePrinter( + grpc::string *str) const { + return std::unique_ptr( + new ProtoBufPrinter(str)); + } + + grpc::string GetLeadingComments(const grpc::string prefix) const { + return GetCommentsHelper(file_, true, prefix); + } + + grpc::string GetTrailingComments(const grpc::string prefix) const { + return GetCommentsHelper(file_, false, prefix); + } + + private: + const grpc::protobuf::FileDescriptor *file_; +}; + + + +#endif // GRPC_INTERNAL_COMPILER_PROTOBUF_PLUGIN_H \ No newline at end of file diff --git a/src/compiler/python_generator.h b/src/compiler/python_generator.h index b91059fad77..0c0d7449b56 100644 --- a/src/compiler/python_generator.h +++ b/src/compiler/python_generator.h @@ -37,6 +37,7 @@ #include #include "src/compiler/config.h" +#include "src/compiler/schema_interface.h" namespace grpc_python_generator { diff --git a/src/compiler/python_plugin.cc b/src/compiler/python_plugin.cc index 3457aa631a3..f34abcc3139 100644 --- a/src/compiler/python_plugin.cc +++ b/src/compiler/python_plugin.cc @@ -34,6 +34,7 @@ // Generates a Python gRPC service interface out of Protobuf IDL. #include "src/compiler/config.h" +#include "src/compiler/protobuf_plugin.h" #include "src/compiler/python_generator.h" int main(int argc, char* argv[]) { diff --git a/src/compiler/schema_interface.h b/src/compiler/schema_interface.h new file mode 100644 index 00000000000..9c2ee5467e8 --- /dev/null +++ b/src/compiler/schema_interface.h @@ -0,0 +1,114 @@ +/* + * + * 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_SCHEMA_INTERFACE_H +#define GRPC_INTERNAL_COMPILER_SCHEMA_INTERFACE_H + +#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_generator { + + // A common interface for objects having comments in the source. + // Return formatted comments to be inserted in generated code. + struct CommentHolder { + virtual ~CommentHolder() {} + virtual grpc::string GetLeadingComments(const grpc::string prefix) const = 0; + virtual grpc::string GetTrailingComments(const grpc::string prefix) const = 0; + }; + + // An abstract interface representing a method. + struct Method : public CommentHolder { + 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 ClientStreaming() const = 0; + virtual bool ServerStreaming() const = 0; + virtual bool BidiStreaming() const = 0; + }; + + // An abstract interface representing a service. + struct Service : public CommentHolder { + 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 : public CommentHolder { + virtual ~File() {} + + virtual grpc::string filename() const = 0; + virtual grpc::string filename_without_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; + }; +} // namespace grpc_generator + +#endif // GRPC_INTERNAL_COMPILER_SCHEMA_INTERFACE_H