From bd6c61860a4b930f8e59807af653b7c759f24091 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Fri, 10 Apr 2015 17:08:15 -0700 Subject: Precreate RpcMethod instances --- src/compiler/cpp_generator.cc | 64 ++++++++++++++++++++++++++++++++----------- 1 file changed, 48 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/compiler/cpp_generator.cc b/src/compiler/cpp_generator.cc index bd8bf65349..defb44aae9 100644 --- a/src/compiler/cpp_generator.cc +++ b/src/compiler/cpp_generator.cc @@ -154,6 +154,7 @@ grpc::string GetHeaderIncludes(const grpc::protobuf::FileDescriptor *file, const Parameters ¶ms) { grpc::string temp = "#include \n" + "#include \n" "#include \n" "#include \n" "\n" @@ -264,6 +265,13 @@ void PrintHeaderClientMethod(grpc::protobuf::io::Printer *printer, } } +void PrintHeaderClientMethodData(grpc::protobuf::io::Printer *printer, + const grpc::protobuf::MethodDescriptor *method, + std::map *vars) { + (*vars)["Method"] = method->name(); + printer->Print(*vars, "const ::grpc::RpcMethod rpcmethod_$Method$_;\n"); +} + void PrintHeaderServerMethodSync( grpc::protobuf::io::Printer *printer, const grpc::protobuf::MethodDescriptor *method, @@ -351,10 +359,17 @@ void PrintHeaderService(grpc::protobuf::io::Printer *printer, "class Stub GRPC_FINAL : public ::grpc::InternalStub {\n" " public:\n"); printer->Indent(); + printer->Print("Stub();\n"); for (int i = 0; i < service->method_count(); ++i) { PrintHeaderClientMethod(printer, service->method(i), vars); } printer->Outdent(); + printer->Print(" private:\n"); + printer->Indent(); + for (int i = 0; i < service->method_count(); ++i) { + PrintHeaderClientMethodData(printer, service->method(i), vars); + } + printer->Outdent(); printer->Print("};\n"); printer->Print( "static std::unique_ptr NewStub(const std::shared_ptr< " @@ -479,7 +494,6 @@ grpc::string GetSourceIncludes(const grpc::protobuf::FileDescriptor *file, printer.Print(vars, "#include \n"); printer.Print(vars, "#include \n"); printer.Print(vars, "#include \n"); - printer.Print(vars, "#include \n"); printer.Print(vars, "#include \n"); printer.Print(vars, "#include \n"); printer.Print(vars, "#include \n"); @@ -513,8 +527,8 @@ void PrintSourceClientMethod(grpc::protobuf::io::Printer *printer, "::grpc::ClientContext* context, " "const $Request$& request, $Response$* response) {\n"); printer->Print(*vars, - " return ::grpc::BlockingUnaryCall(channel()," - "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$]), " + " return ::grpc::BlockingUnaryCall(channel(), " + "rpcmethod_$Method$_, " "context, request, response);\n" "}\n\n"); printer->Print( @@ -528,7 +542,7 @@ void PrintSourceClientMethod(grpc::protobuf::io::Printer *printer, "::grpc::ClientAsyncResponseReader< $Response$>>(new " "::grpc::ClientAsyncResponseReader< $Response$>(" "channel(), cq, " - "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$]), " + "rpcmethod_$Method$_, " "context, request, tag));\n" "}\n\n"); } else if (ClientOnlyStreaming(method)) { @@ -540,8 +554,7 @@ void PrintSourceClientMethod(grpc::protobuf::io::Printer *printer, " return std::unique_ptr< ::grpc::ClientWriter< " "$Request$>>(new ::grpc::ClientWriter< $Request$>(" "channel()," - "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$], " - "::grpc::RpcMethod::RpcType::CLIENT_STREAMING), " + "rpcmethod_$Method$_, " "context, response));\n" "}\n\n"); printer->Print(*vars, @@ -553,8 +566,7 @@ void PrintSourceClientMethod(grpc::protobuf::io::Printer *printer, " return std::unique_ptr< ::grpc::ClientAsyncWriter< " "$Request$>>(new ::grpc::ClientAsyncWriter< $Request$>(" "channel(), cq, " - "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$], " - "::grpc::RpcMethod::RpcType::CLIENT_STREAMING), " + "rpcmethod_$Method$_, " "context, response, tag));\n" "}\n\n"); } else if (ServerOnlyStreaming(method)) { @@ -567,8 +579,7 @@ void PrintSourceClientMethod(grpc::protobuf::io::Printer *printer, " return std::unique_ptr< ::grpc::ClientReader< " "$Response$>>(new ::grpc::ClientReader< $Response$>(" "channel()," - "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$], " - "::grpc::RpcMethod::RpcType::SERVER_STREAMING), " + "rpcmethod_$Method$_, " "context, request));\n" "}\n\n"); printer->Print(*vars, @@ -580,8 +591,7 @@ void PrintSourceClientMethod(grpc::protobuf::io::Printer *printer, " return std::unique_ptr< ::grpc::ClientAsyncReader< " "$Response$>>(new ::grpc::ClientAsyncReader< $Response$>(" "channel(), cq, " - "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$], " - "::grpc::RpcMethod::RpcType::SERVER_STREAMING), " + "rpcmethod_$Method$_, " "context, request, tag));\n" "}\n\n"); } else if (BidiStreaming(method)) { @@ -594,8 +604,7 @@ void PrintSourceClientMethod(grpc::protobuf::io::Printer *printer, "$Request$, $Response$>>(new ::grpc::ClientReaderWriter< " "$Request$, $Response$>(" "channel()," - "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$], " - "::grpc::RpcMethod::RpcType::BIDI_STREAMING), " + "rpcmethod_$Method$_, " "context));\n" "}\n\n"); printer->Print(*vars, @@ -608,8 +617,7 @@ void PrintSourceClientMethod(grpc::protobuf::io::Printer *printer, "$Request$, $Response$>>(new " "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>(" "channel(), cq, " - "::grpc::RpcMethod($prefix$$Service$_method_names[$Idx$], " - "::grpc::RpcMethod::RpcType::BIDI_STREAMING), " + "rpcmethod_$Method$_, " "context, tag));\n" "}\n\n"); } @@ -740,6 +748,30 @@ void PrintSourceService(grpc::protobuf::io::Printer *printer, " stub->set_channel(channel);\n" " return stub;\n" "}\n\n"); + printer->Print(*vars, "$ns$$Service$::Stub::Stub()"); + printer->Indent(); + for (int i = 0; i < service->method_count(); ++i) { + const grpc::protobuf::MethodDescriptor *method = service->method(i); + (*vars)["Sep"] = (i==0) ? ":" : ","; + (*vars)["Method"] = method->name(); + (*vars)["Idx"] = as_string(i); + if (NoStreaming(method)) { + (*vars)["StreamingType"] = "NORMAL_RPC"; + } else if (ClientOnlyStreaming(method)) { + (*vars)["StreamingType"] = "CLIENT_STREAMING"; + } else if (ServerOnlyStreaming(method)) { + (*vars)["StreamingType"] = "SERVER_STREAMING"; + } else { + (*vars)["StreamingType"] = "BIDI_STREAMING"; + } + printer->Print(*vars, "$Sep$ rpcmethod_$Method$_(" + "$prefix$$Service$_method_names[$Idx$], " + "::grpc::RpcMethod::$StreamingType$" + ")\n"); + } + printer->Print("{}\n"); + printer->Outdent(); + for (int i = 0; i < service->method_count(); ++i) { (*vars)["Idx"] = as_string(i); PrintSourceClientMethod(printer, service->method(i), vars); -- cgit v1.2.3