diff options
author | ncteisen <ncteisen@gmail.com> | 2018-06-18 18:39:56 -0700 |
---|---|---|
committer | ncteisen <ncteisen@gmail.com> | 2018-06-20 07:59:05 -0700 |
commit | 93a90e6132d4c6cdf06ae3b0d7176c2ee3944967 (patch) | |
tree | e873881aea9d20bdb4c74fc389ffbc2176658821 /src/compiler | |
parent | 7fc8bf5833334a8bbe41d940c9e6038012eb694c (diff) |
Support codegen generic on server side
Diffstat (limited to 'src/compiler')
-rw-r--r-- | src/compiler/cpp_generator.cc | 129 |
1 files changed, 91 insertions, 38 deletions
diff --git a/src/compiler/cpp_generator.cc b/src/compiler/cpp_generator.cc index 30d66b444f..f01f279a44 100644 --- a/src/compiler/cpp_generator.cc +++ b/src/compiler/cpp_generator.cc @@ -128,6 +128,7 @@ grpc::string GetHeaderIncludes(grpc_generator::File* file, ""); } static const char* headers_strs[] = { + "grpcpp/impl/codegen/async_generic_service.h", "grpcpp/impl/codegen/async_stream.h", "grpcpp/impl/codegen/async_unary_call.h", "grpcpp/impl/codegen/method_handler_impl.h", @@ -588,43 +589,27 @@ void PrintHeaderServerMethodSync(grpc_generator::Printer* printer, printer->Print(method->GetTrailingComments("//").c_str()); } -void PrintHeaderServerMethodAsync(grpc_generator::Printer* printer, - const grpc_generator::Method* method, - std::map<grpc::string, grpc::string>* vars) { - (*vars)["Method"] = method->name(); - (*vars)["Request"] = method->input_type_name(); - (*vars)["Response"] = method->output_type_name(); - printer->Print(*vars, "template <class BaseClass>\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$() override {\n" - " BaseClassMustBeDerivedFromService(this);\n" - "}\n"); +// Helper generator. Disabled the sync API for Request and Response, then adds +// in an async API for RealRequest and RealResponse types. This is to be used +// to generate async and codegen generic APIs. +void PrintHeaderServerAsyncMethodsHelper( + grpc_generator::Printer* printer, const grpc_generator::Method* method, + std::map<grpc::string, grpc::string>* vars) { if (method->NoStreaming()) { printer->Print( *vars, "// disable synchronous version of this method\n" "::grpc::Status $Method$(" "::grpc::ServerContext* context, const $Request$* request, " - "$Response$* response) final override {\n" + "$Response$* response) 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::ServerContext* context, $RealRequest$* request, " + "::grpc::ServerAsyncResponseWriter< $RealResponse$>* response, " "::grpc::CompletionQueue* new_call_cq, " "::grpc::ServerCompletionQueue* notification_cq, void *tag) {\n"); printer->Print(*vars, @@ -638,7 +623,7 @@ void PrintHeaderServerMethodAsync(grpc_generator::Printer* printer, "::grpc::Status $Method$(" "::grpc::ServerContext* context, " "::grpc::ServerReader< $Request$>* reader, " - "$Response$* response) final override {\n" + "$Response$* response) override {\n" " abort();\n" " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n" "}\n"); @@ -646,7 +631,7 @@ void PrintHeaderServerMethodAsync(grpc_generator::Printer* printer, *vars, "void Request$Method$(" "::grpc::ServerContext* context, " - "::grpc::ServerAsyncReader< $Response$, $Request$>* reader, " + "::grpc::ServerAsyncReader< $RealResponse$, $RealRequest$>* reader, " "::grpc::CompletionQueue* new_call_cq, " "::grpc::ServerCompletionQueue* notification_cq, void *tag) {\n"); printer->Print(*vars, @@ -659,7 +644,7 @@ void PrintHeaderServerMethodAsync(grpc_generator::Printer* printer, "// disable synchronous version of this method\n" "::grpc::Status $Method$(" "::grpc::ServerContext* context, const $Request$* request, " - "::grpc::ServerWriter< $Response$>* writer) final override " + "::grpc::ServerWriter< $Response$>* writer) override " "{\n" " abort();\n" " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n" @@ -667,8 +652,8 @@ void PrintHeaderServerMethodAsync(grpc_generator::Printer* printer, printer->Print( *vars, "void Request$Method$(" - "::grpc::ServerContext* context, $Request$* request, " - "::grpc::ServerAsyncWriter< $Response$>* writer, " + "::grpc::ServerContext* context, $RealRequest$* request, " + "::grpc::ServerAsyncWriter< $RealResponse$>* writer, " "::grpc::CompletionQueue* new_call_cq, " "::grpc::ServerCompletionQueue* notification_cq, void *tag) {\n"); printer->Print( @@ -683,7 +668,7 @@ void PrintHeaderServerMethodAsync(grpc_generator::Printer* printer, "::grpc::Status $Method$(" "::grpc::ServerContext* context, " "::grpc::ServerReaderWriter< $Response$, $Request$>* stream) " - "final override {\n" + " override {\n" " abort();\n" " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n" "}\n"); @@ -691,7 +676,8 @@ void PrintHeaderServerMethodAsync(grpc_generator::Printer* printer, *vars, "void Request$Method$(" "::grpc::ServerContext* context, " - "::grpc::ServerAsyncReaderWriter< $Response$, $Request$>* stream, " + "::grpc::ServerAsyncReaderWriter< $RealResponse$, $RealRequest$>* " + "stream, " "::grpc::CompletionQueue* new_call_cq, " "::grpc::ServerCompletionQueue* notification_cq, void *tag) {\n"); printer->Print(*vars, @@ -699,6 +685,35 @@ void PrintHeaderServerMethodAsync(grpc_generator::Printer* printer, "context, stream, new_call_cq, notification_cq, tag);\n"); printer->Print("}\n"); } +} + +void PrintHeaderServerMethodAsync(grpc_generator::Printer* printer, + const grpc_generator::Method* method, + std::map<grpc::string, grpc::string>* vars) { + (*vars)["Method"] = method->name(); + // These will be disabled + (*vars)["Request"] = method->input_type_name(); + (*vars)["Response"] = method->output_type_name(); + // These will be used for the async API + (*vars)["RealRequest"] = method->input_type_name(); + (*vars)["RealResponse"] = method->output_type_name(); + printer->Print(*vars, "template <class BaseClass>\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$() override {\n" + " BaseClassMustBeDerivedFromService(this);\n" + "}\n"); + PrintHeaderServerAsyncMethodsHelper(printer, method, vars); printer->Outdent(); printer->Print(*vars, "};\n"); } @@ -738,7 +753,7 @@ void PrintHeaderServerMethodStreamedUnary( "// disable regular version of this method\n" "::grpc::Status $Method$(" "::grpc::ServerContext* context, const $Request$* request, " - "$Response$* response) final override {\n" + "$Response$* response) override {\n" " abort();\n" " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n" "}\n"); @@ -790,7 +805,7 @@ void PrintHeaderServerMethodSplitStreaming( "// disable regular version of this method\n" "::grpc::Status $Method$(" "::grpc::ServerContext* context, const $Request$* request, " - "::grpc::ServerWriter< $Response$>* writer) final override " + "::grpc::ServerWriter< $Response$>* writer) override " "{\n" " abort();\n" " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n" @@ -835,7 +850,7 @@ void PrintHeaderServerMethodGeneric( "// disable synchronous version of this method\n" "::grpc::Status $Method$(" "::grpc::ServerContext* context, const $Request$* request, " - "$Response$* response) final override {\n" + "$Response$* response) override {\n" " abort();\n" " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n" "}\n"); @@ -846,7 +861,7 @@ void PrintHeaderServerMethodGeneric( "::grpc::Status $Method$(" "::grpc::ServerContext* context, " "::grpc::ServerReader< $Request$>* reader, " - "$Response$* response) final override {\n" + "$Response$* response) override {\n" " abort();\n" " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n" "}\n"); @@ -856,7 +871,7 @@ void PrintHeaderServerMethodGeneric( "// disable synchronous version of this method\n" "::grpc::Status $Method$(" "::grpc::ServerContext* context, const $Request$* request, " - "::grpc::ServerWriter< $Response$>* writer) final override " + "::grpc::ServerWriter< $Response$>* writer) override " "{\n" " abort();\n" " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n" @@ -868,7 +883,7 @@ void PrintHeaderServerMethodGeneric( "::grpc::Status $Method$(" "::grpc::ServerContext* context, " "::grpc::ServerReaderWriter< $Response$, $Request$>* stream) " - "final override {\n" + " override {\n" " abort();\n" " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n" "}\n"); @@ -877,6 +892,37 @@ void PrintHeaderServerMethodGeneric( printer->Print(*vars, "};\n"); } +void PrintHeaderServerMethodCodegenGeneric( + grpc_generator::Printer* printer, const grpc_generator::Method* method, + std::map<grpc::string, grpc::string>* vars) { + (*vars)["Method"] = method->name(); + // These will be disabled + (*vars)["Request"] = method->input_type_name(); + (*vars)["Response"] = method->output_type_name(); + // These will be used for codegen generic API + (*vars)["RealRequest"] = "::grpc::ByteBuffer"; + (*vars)["RealResponse"] = "::grpc::ByteBuffer"; + printer->Print(*vars, "template <class BaseClass>\n"); + printer->Print( + *vars, "class WithCodegenGenericMethod_$Method$ : public BaseClass {\n"); + printer->Print( + " private:\n" + " void BaseClassMustBeDerivedFromService(const Service *service) {}\n"); + printer->Print(" public:\n"); + printer->Indent(); + printer->Print(*vars, + "WithCodegenGenericMethod_$Method$() {\n" + " ::grpc::Service::MarkMethodCodegenGeneric($Idx$);\n" + "}\n"); + printer->Print(*vars, + "~WithCodegenGenericMethod_$Method$() override {\n" + " BaseClassMustBeDerivedFromService(this);\n" + "}\n"); + PrintHeaderServerAsyncMethodsHelper(printer, method, vars); + printer->Outdent(); + printer->Print(*vars, "};\n"); +} + void PrintHeaderService(grpc_generator::Printer* printer, const grpc_generator::Service* service, std::map<grpc::string, grpc::string>* vars) { @@ -981,6 +1027,13 @@ void PrintHeaderService(grpc_generator::Printer* printer, PrintHeaderServerMethodGeneric(printer, service->method(i).get(), vars); } + // Server side - CodegenGeneric + for (int i = 0; i < service->method_count(); ++i) { + (*vars)["Idx"] = as_string(i); + PrintHeaderServerMethodCodegenGeneric(printer, service->method(i).get(), + vars); + } + // Server side - Streamed Unary for (int i = 0; i < service->method_count(); ++i) { (*vars)["Idx"] = as_string(i); |