From 7e72bba1627f2563c1a8d3c5c8135f7f286ce456 Mon Sep 17 00:00:00 2001 From: Vijay Pai Date: Thu, 15 Oct 2020 04:41:59 -0700 Subject: [PATCH] Reduce templating for unary callback client RPC --- include/grpcpp/impl/codegen/client_callback.h | 16 +++++++++++++--- src/compiler/cpp_generator.cc | 2 ++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/include/grpcpp/impl/codegen/client_callback.h b/include/grpcpp/impl/codegen/client_callback.h index 90c817ceaa7..6ab0dfbc746 100644 --- a/include/grpcpp/impl/codegen/client_callback.h +++ b/include/grpcpp/impl/codegen/client_callback.h @@ -35,15 +35,25 @@ class ClientContext; namespace internal { class RpcMethod; -/// Perform a callback-based unary call +/// Perform a callback-based unary call. May optionally specify the base +/// class of the Request and Response so that the internal calls and structures +/// below this may be based on those base classes and thus achieve code reuse +/// across different RPCs (e.g., for protobuf, MessageLite would be a base +/// class). /// TODO(vjpai): Combine as much as possible with the blocking unary call code -template +template void CallbackUnaryCall(::grpc::ChannelInterface* channel, const ::grpc::internal::RpcMethod& method, ::grpc::ClientContext* context, const InputMessage* request, OutputMessage* result, std::function on_completion) { - CallbackUnaryCallImpl x( + static_assert(std::is_base_of::value, + "Invalid input message specification"); + static_assert(std::is_base_of::value, + "Invalid output message specification"); + CallbackUnaryCallImpl x( channel, method, context, request, result, on_completion); } diff --git a/src/compiler/cpp_generator.cc b/src/compiler/cpp_generator.cc index 9a20a5a5711..7ed74eb2302 100644 --- a/src/compiler/cpp_generator.cc +++ b/src/compiler/cpp_generator.cc @@ -1897,6 +1897,8 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer, "std::function f) {\n"); printer->Print(*vars, " ::grpc::internal::CallbackUnaryCall" + "< $Request$, $Response$, ::grpc::protobuf::MessageLite, " + "::grpc::protobuf::MessageLite>" "(stub_->channel_.get(), stub_->rpcmethod_$Method$_, " "context, request, response, std::move(f));\n}\n\n");