Add options to omit CQ or sync server APIs in generated code

PiperOrigin-RevId: 625490214
pull/36345/head
Yousuk Seung 12 months ago committed by Yash Tibrewal
parent c3a077e184
commit c40ea61261
  1. 254
      src/compiler/cpp_generator.cc
  2. 4
      src/compiler/cpp_generator.h
  3. 20
      src/compiler/cpp_plugin.h

@ -20,6 +20,7 @@
#include <map>
#include <sstream>
#include <vector>
namespace grpc_cpp_generator {
namespace {
@ -189,6 +190,7 @@ std::string GetHeaderIncludes(grpc_generator::File* file,
void PrintHeaderClientMethodInterfaces(grpc_generator::Printer* printer,
const grpc_generator::Method* method,
const Parameters& params,
std::map<std::string, std::string>* vars,
bool is_public) {
(*vars)["Method"] = method->name();
@ -208,7 +210,8 @@ void PrintHeaderClientMethodInterfaces(grpc_generator::Printer* printer,
*vars,
"virtual ::grpc::Status $Method$(::grpc::ClientContext* context, "
"const $Request$& request, $Response$* response) = 0;\n");
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
printer->Print(
*vars,
@ -226,6 +229,7 @@ void PrintHeaderClientMethodInterfaces(grpc_generator::Printer* printer,
printer->Outdent();
printer->Print("}\n");
}
}
} else if (ClientOnlyStreaming(method)) {
printer->Print(
*vars,
@ -239,7 +243,8 @@ void PrintHeaderClientMethodInterfaces(grpc_generator::Printer* printer,
"($Method$Raw(context, response));\n");
printer->Outdent();
printer->Print("}\n");
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
@ -259,6 +264,7 @@ void PrintHeaderClientMethodInterfaces(grpc_generator::Printer* printer,
printer->Outdent();
printer->Print("}\n");
}
}
} else if (ServerOnlyStreaming(method)) {
printer->Print(
*vars,
@ -272,25 +278,28 @@ void PrintHeaderClientMethodInterfaces(grpc_generator::Printer* printer,
"($Method$Raw(context, request));\n");
printer->Outdent();
printer->Print("}\n");
for (auto& async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
printer->Print(
*vars,
"std::unique_ptr< ::grpc::ClientAsyncReaderInterface< $Response$>> "
"std::unique_ptr< ::grpc::ClientAsyncReaderInterface< "
"$Response$>> "
"$AsyncPrefix$$Method$("
"::grpc::ClientContext* context, const $Request$& request, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) {\n");
printer->Indent();
printer->Print(
*vars,
printer->Print(*vars,
"return std::unique_ptr< "
"::grpc::ClientAsyncReaderInterface< $Response$>>("
"$AsyncPrefix$$Method$Raw(context, request, cq$AsyncRawArgs$));\n");
"$AsyncPrefix$$Method$Raw(context, request, "
"cq$AsyncRawArgs$));\n");
printer->Outdent();
printer->Print("}\n");
}
}
} else if (method->BidiStreaming()) {
printer->Print(*vars,
"std::unique_ptr< ::grpc::ClientReaderWriterInterface< "
@ -304,44 +313,52 @@ void PrintHeaderClientMethodInterfaces(grpc_generator::Printer* printer,
"$Method$Raw(context));\n");
printer->Outdent();
printer->Print("}\n");
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
printer->Print(
*vars,
"std::unique_ptr< "
"::grpc::ClientAsyncReaderWriterInterface< $Request$, $Response$>> "
"::grpc::ClientAsyncReaderWriterInterface< $Request$, "
"$Response$>> "
"$AsyncPrefix$$Method$(::grpc::ClientContext* context, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) {\n");
printer->Indent();
printer->Print(
*vars,
"return std::unique_ptr< "
"::grpc::ClientAsyncReaderWriterInterface< $Request$, $Response$>>("
"::grpc::ClientAsyncReaderWriterInterface< $Request$, "
"$Response$>>("
"$AsyncPrefix$$Method$Raw(context, cq$AsyncRawArgs$));\n");
printer->Outdent();
printer->Print("}\n");
}
}
}
} else {
if (method->NoStreaming()) {
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
printer->Print(
*vars,
"virtual ::grpc::ClientAsyncResponseReaderInterface< $Response$>* "
"virtual ::grpc::ClientAsyncResponseReaderInterface< "
"$Response$>* "
"$AsyncPrefix$$Method$Raw(::grpc::ClientContext* context, "
"const $Request$& request, "
"::grpc::CompletionQueue* cq) = 0;\n");
}
}
} else if (ClientOnlyStreaming(method)) {
printer->Print(
*vars,
"virtual ::grpc::ClientWriterInterface< $Request$>*"
" $Method$Raw("
"::grpc::ClientContext* context, $Response$* response) = 0;\n");
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
printer->Print(
@ -351,13 +368,15 @@ void PrintHeaderClientMethodInterfaces(grpc_generator::Printer* printer,
"$Response$* response, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) = 0;\n");
}
}
} else if (ServerOnlyStreaming(method)) {
printer->Print(
*vars,
"virtual ::grpc::ClientReaderInterface< $Response$>* "
"$Method$Raw("
"::grpc::ClientContext* context, const $Request$& request) = 0;\n");
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
printer->Print(
@ -367,12 +386,14 @@ void PrintHeaderClientMethodInterfaces(grpc_generator::Printer* printer,
"::grpc::ClientContext* context, const $Request$& request, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) = 0;\n");
}
}
} else if (method->BidiStreaming()) {
printer->Print(*vars,
"virtual ::grpc::ClientReaderWriterInterface< $Request$, "
"$Response$>* "
"$Method$Raw(::grpc::ClientContext* context) = 0;\n");
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
printer->Print(
@ -384,10 +405,12 @@ void PrintHeaderClientMethodInterfaces(grpc_generator::Printer* printer,
}
}
}
}
}
void PrintHeaderClientMethod(grpc_generator::Printer* printer,
const grpc_generator::Method* method,
const Parameters& params,
std::map<std::string, std::string>* vars,
bool is_public) {
(*vars)["Method"] = method->name();
@ -406,11 +429,13 @@ void PrintHeaderClientMethod(grpc_generator::Printer* printer,
*vars,
"::grpc::Status $Method$(::grpc::ClientContext* context, "
"const $Request$& request, $Response$* response) override;\n");
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
printer->Print(
*vars,
"std::unique_ptr< ::grpc::ClientAsyncResponseReader< $Response$>> "
"std::unique_ptr< ::grpc::ClientAsyncResponseReader< "
"$Response$>> "
"$AsyncPrefix$$Method$(::grpc::ClientContext* context, "
"const $Request$& request, "
"::grpc::CompletionQueue* cq) {\n");
@ -422,6 +447,7 @@ void PrintHeaderClientMethod(grpc_generator::Printer* printer,
printer->Outdent();
printer->Print("}\n");
}
}
} else if (ClientOnlyStreaming(method)) {
printer->Print(
*vars,
@ -434,11 +460,13 @@ void PrintHeaderClientMethod(grpc_generator::Printer* printer,
"($Method$Raw(context, response));\n");
printer->Outdent();
printer->Print("}\n");
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
printer->Print(*vars,
printer->Print(
*vars,
"std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>>"
" $AsyncPrefix$$Method$(::grpc::ClientContext* context, "
"$Response$* response, "
@ -452,6 +480,7 @@ void PrintHeaderClientMethod(grpc_generator::Printer* printer,
printer->Outdent();
printer->Print("}\n");
}
}
} else if (ServerOnlyStreaming(method)) {
printer->Print(
*vars,
@ -465,7 +494,8 @@ void PrintHeaderClientMethod(grpc_generator::Printer* printer,
"($Method$Raw(context, request));\n");
printer->Outdent();
printer->Print("}\n");
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
@ -479,10 +509,12 @@ void PrintHeaderClientMethod(grpc_generator::Printer* printer,
printer->Print(
*vars,
"return std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>>("
"$AsyncPrefix$$Method$Raw(context, request, cq$AsyncRawArgs$));\n");
"$AsyncPrefix$$Method$Raw(context, request, "
"cq$AsyncRawArgs$));\n");
printer->Outdent();
printer->Print("}\n");
}
}
} else if (method->BidiStreaming()) {
printer->Print(
*vars,
@ -495,11 +527,13 @@ void PrintHeaderClientMethod(grpc_generator::Printer* printer,
"$Method$Raw(context));\n");
printer->Outdent();
printer->Print("}\n");
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
printer->Print(*vars,
printer->Print(
*vars,
"std::unique_ptr< ::grpc::ClientAsyncReaderWriter< "
"$Request$, $Response$>> "
"$AsyncPrefix$$Method$(::grpc::ClientContext* context, "
@ -514,9 +548,11 @@ void PrintHeaderClientMethod(grpc_generator::Printer* printer,
printer->Print("}\n");
}
}
}
} else {
if (method->NoStreaming()) {
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
printer->Print(
*vars,
@ -525,12 +561,14 @@ void PrintHeaderClientMethod(grpc_generator::Printer* printer,
"const $Request$& request, "
"::grpc::CompletionQueue* cq) override;\n");
}
}
} else if (ClientOnlyStreaming(method)) {
printer->Print(*vars,
"::grpc::ClientWriter< $Request$>* $Method$Raw("
"::grpc::ClientContext* context, $Response$* response) "
"override;\n");
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
@ -540,26 +578,31 @@ void PrintHeaderClientMethod(grpc_generator::Printer* printer,
"::grpc::ClientContext* context, $Response$* response, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) override;\n");
}
}
} else if (ServerOnlyStreaming(method)) {
printer->Print(*vars,
"::grpc::ClientReader< $Response$>* $Method$Raw("
"::grpc::ClientContext* context, const $Request$& request)"
" override;\n");
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
printer->Print(
*vars,
"::grpc::ClientAsyncReader< $Response$>* $AsyncPrefix$$Method$Raw("
"::grpc::ClientAsyncReader< $Response$>* "
"$AsyncPrefix$$Method$Raw("
"::grpc::ClientContext* context, const $Request$& request, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$) override;\n");
}
}
} else if (method->BidiStreaming()) {
printer->Print(*vars,
"::grpc::ClientReaderWriter< $Request$, $Response$>* "
"$Method$Raw(::grpc::ClientContext* context) override;\n");
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncRawArgs"] = async_prefix.raw_args;
@ -571,6 +614,7 @@ void PrintHeaderClientMethod(grpc_generator::Printer* printer,
}
}
}
}
}
void PrintHeaderClientMethodCallbackInterfacesStart(
@ -724,31 +768,35 @@ void PrintHeaderClientMethodData(grpc_generator::Printer* printer,
void PrintHeaderServerMethodSync(grpc_generator::Printer* printer,
const grpc_generator::Method* method,
const Parameters& params,
std::map<std::string, std::string>* vars) {
(*vars)["Method"] = method->name();
(*vars)["Request"] = method->input_type_name();
(*vars)["Response"] = method->output_type_name();
printer->Print(method->GetLeadingComments("//").c_str());
if (params.allow_sync_server_api) {
printer->Print("virtual ");
}
if (method->NoStreaming()) {
printer->Print(*vars,
"virtual ::grpc::Status $Method$("
"::grpc::Status $Method$("
"::grpc::ServerContext* context, const $Request$* request, "
"$Response$* response);\n");
} else if (ClientOnlyStreaming(method)) {
printer->Print(*vars,
"virtual ::grpc::Status $Method$("
"::grpc::Status $Method$("
"::grpc::ServerContext* context, "
"::grpc::ServerReader< $Request$>* reader, "
"$Response$* response);\n");
} else if (ServerOnlyStreaming(method)) {
printer->Print(*vars,
"virtual ::grpc::Status $Method$("
"::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::Status $Method$("
"::grpc::ServerContext* context, "
"::grpc::ServerReaderWriter< $Response$, $Request$>* stream);"
"\n");
@ -761,8 +809,9 @@ void PrintHeaderServerMethodSync(grpc_generator::Printer* printer,
// to generate async and raw async APIs.
void PrintHeaderServerAsyncMethodsHelper(
grpc_generator::Printer* printer, const grpc_generator::Method* method,
std::map<std::string, std::string>* vars) {
const Parameters& params, std::map<std::string, std::string>* vars) {
if (method->NoStreaming()) {
if (params.allow_sync_server_api) {
printer->Print(
*vars,
"// disable synchronous version of this method\n"
@ -772,6 +821,7 @@ void PrintHeaderServerAsyncMethodsHelper(
" abort();\n"
" return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
"}\n");
}
printer->Print(
*vars,
"void Request$Method$("
@ -784,6 +834,7 @@ void PrintHeaderServerAsyncMethodsHelper(
"request, response, new_call_cq, notification_cq, tag);\n");
printer->Print("}\n");
} else if (ClientOnlyStreaming(method)) {
if (params.allow_sync_server_api) {
printer->Print(
*vars,
"// disable synchronous version of this method\n"
@ -794,6 +845,7 @@ void PrintHeaderServerAsyncMethodsHelper(
" abort();\n"
" return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
"}\n");
}
printer->Print(
*vars,
"void Request$Method$("
@ -806,6 +858,7 @@ void PrintHeaderServerAsyncMethodsHelper(
"context, reader, new_call_cq, notification_cq, tag);\n");
printer->Print("}\n");
} else if (ServerOnlyStreaming(method)) {
if (params.allow_sync_server_api) {
printer->Print(
*vars,
"// disable synchronous version of this method\n"
@ -816,6 +869,7 @@ void PrintHeaderServerAsyncMethodsHelper(
" abort();\n"
" return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
"}\n");
}
printer->Print(
*vars,
"void Request$Method$("
@ -829,6 +883,7 @@ void PrintHeaderServerAsyncMethodsHelper(
"context, request, writer, new_call_cq, notification_cq, tag);\n");
printer->Print("}\n");
} else if (method->BidiStreaming()) {
if (params.allow_sync_server_api) {
printer->Print(
*vars,
"// disable synchronous version of this method\n"
@ -839,6 +894,7 @@ void PrintHeaderServerAsyncMethodsHelper(
" abort();\n"
" return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
"}\n");
}
printer->Print(
*vars,
"void Request$Method$("
@ -856,6 +912,7 @@ void PrintHeaderServerAsyncMethodsHelper(
void PrintHeaderServerMethodAsync(grpc_generator::Printer* printer,
const grpc_generator::Method* method,
const Parameters& params,
std::map<std::string, std::string>* vars) {
(*vars)["Method"] = method->name();
// These will be disabled
@ -881,7 +938,7 @@ void PrintHeaderServerMethodAsync(grpc_generator::Printer* printer,
"~WithAsyncMethod_$Method$() override {\n"
" BaseClassMustBeDerivedFromService(this);\n"
"}\n");
PrintHeaderServerAsyncMethodsHelper(printer, method, vars);
PrintHeaderServerAsyncMethodsHelper(printer, method, params, vars);
printer->Outdent();
printer->Print(*vars, "};\n");
}
@ -891,8 +948,9 @@ void PrintHeaderServerMethodAsync(grpc_generator::Printer* printer,
// to generate callback and raw callback APIs.
void PrintHeaderServerCallbackMethodsHelper(
grpc_generator::Printer* printer, const grpc_generator::Method* method,
std::map<std::string, std::string>* vars) {
const Parameters& params, std::map<std::string, std::string>* vars) {
if (method->NoStreaming()) {
if (params.allow_sync_server_api) {
printer->Print(
*vars,
"// disable synchronous version of this method\n"
@ -902,6 +960,7 @@ void PrintHeaderServerCallbackMethodsHelper(
" abort();\n"
" return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
"}\n");
}
printer->Print(*vars,
"virtual ::grpc::ServerUnaryReactor* $Method$(\n"
" ::grpc::CallbackServerContext* /*context*/, "
@ -909,6 +968,7 @@ void PrintHeaderServerCallbackMethodsHelper(
"$RealResponse$* /*response*/)"
" { return nullptr; }\n");
} else if (ClientOnlyStreaming(method)) {
if (params.allow_sync_server_api) {
printer->Print(
*vars,
"// disable synchronous version of this method\n"
@ -919,6 +979,7 @@ void PrintHeaderServerCallbackMethodsHelper(
" abort();\n"
" return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
"}\n");
}
printer->Print(*vars,
"virtual ::grpc::ServerReadReactor< "
"$RealRequest$>* $Method$(\n"
@ -926,6 +987,7 @@ void PrintHeaderServerCallbackMethodsHelper(
"/*context*/, $RealResponse$* /*response*/)"
" { return nullptr; }\n");
} else if (ServerOnlyStreaming(method)) {
if (params.allow_sync_server_api) {
printer->Print(
*vars,
"// disable synchronous version of this method\n"
@ -936,6 +998,7 @@ void PrintHeaderServerCallbackMethodsHelper(
" abort();\n"
" return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
"}\n");
}
printer->Print(
*vars,
"virtual ::grpc::ServerWriteReactor< $RealResponse$>* $Method$(\n"
@ -943,6 +1006,7 @@ void PrintHeaderServerCallbackMethodsHelper(
"/*context*/, const $RealRequest$* /*request*/)"
" { return nullptr; }\n");
} else if (method->BidiStreaming()) {
if (params.allow_sync_server_api) {
printer->Print(
*vars,
"// disable synchronous version of this method\n"
@ -953,6 +1017,7 @@ void PrintHeaderServerCallbackMethodsHelper(
" abort();\n"
" return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
"}\n");
}
printer->Print(
*vars,
"virtual ::grpc::ServerBidiReactor< $RealRequest$, $RealResponse$>* "
@ -964,6 +1029,7 @@ void PrintHeaderServerCallbackMethodsHelper(
void PrintHeaderServerMethodCallback(grpc_generator::Printer* printer,
const grpc_generator::Method* method,
const Parameters& params,
std::map<std::string, std::string>* vars) {
(*vars)["Method"] = method->name();
// These will be disabled
@ -1039,14 +1105,14 @@ void PrintHeaderServerMethodCallback(grpc_generator::Printer* printer,
"~WithCallbackMethod_$Method$() override {\n"
" BaseClassMustBeDerivedFromService(this);\n"
"}\n");
PrintHeaderServerCallbackMethodsHelper(printer, method, vars);
PrintHeaderServerCallbackMethodsHelper(printer, method, params, vars);
printer->Outdent();
printer->Print(*vars, "};\n");
}
void PrintHeaderServerMethodRawCallback(
grpc_generator::Printer* printer, const grpc_generator::Method* method,
std::map<std::string, std::string>* vars) {
const Parameters& params, std::map<std::string, std::string>* vars) {
(*vars)["Method"] = method->name();
// These will be disabled
(*vars)["Request"] = method->input_type_name();
@ -1111,7 +1177,7 @@ void PrintHeaderServerMethodRawCallback(
"~WithRawCallbackMethod_$Method$() override {\n"
" BaseClassMustBeDerivedFromService(this);\n"
"}\n");
PrintHeaderServerCallbackMethodsHelper(printer, method, vars);
PrintHeaderServerCallbackMethodsHelper(printer, method, params, vars);
printer->Outdent();
printer->Print(*vars, "};\n");
}
@ -1227,6 +1293,7 @@ void PrintHeaderServerMethodSplitStreaming(
void PrintHeaderServerMethodGeneric(grpc_generator::Printer* printer,
const grpc_generator::Method* method,
const Parameters& params,
std::map<std::string, std::string>* vars) {
(*vars)["Method"] = method->name();
(*vars)["Request"] = method->input_type_name();
@ -1248,6 +1315,7 @@ void PrintHeaderServerMethodGeneric(grpc_generator::Printer* printer,
"~WithGenericMethod_$Method$() override {\n"
" BaseClassMustBeDerivedFromService(this);\n"
"}\n");
if (params.allow_sync_server_api) {
if (method->NoStreaming()) {
printer->Print(
*vars,
@ -1292,12 +1360,14 @@ void PrintHeaderServerMethodGeneric(grpc_generator::Printer* printer,
" return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
"}\n");
}
}
printer->Outdent();
printer->Print(*vars, "};\n");
}
void PrintHeaderServerMethodRaw(grpc_generator::Printer* printer,
const grpc_generator::Method* method,
const Parameters& params,
std::map<std::string, std::string>* vars) {
(*vars)["Method"] = method->name();
// These will be disabled
@ -1322,13 +1392,14 @@ void PrintHeaderServerMethodRaw(grpc_generator::Printer* printer,
"~WithRawMethod_$Method$() override {\n"
" BaseClassMustBeDerivedFromService(this);\n"
"}\n");
PrintHeaderServerAsyncMethodsHelper(printer, method, vars);
PrintHeaderServerAsyncMethodsHelper(printer, method, params, vars);
printer->Outdent();
printer->Print(*vars, "};\n");
}
void PrintHeaderService(grpc_generator::Printer* printer,
const grpc_generator::Service* service,
const Parameters& params,
std::map<std::string, std::string>* vars) {
(*vars)["Service"] = service->name();
@ -1352,8 +1423,8 @@ void PrintHeaderService(grpc_generator::Printer* printer,
printer->Print("virtual ~StubInterface() {}\n");
for (int i = 0; i < service->method_count(); ++i) {
printer->Print(service->method(i)->GetLeadingComments("//").c_str());
PrintHeaderClientMethodInterfaces(printer, service->method(i).get(), vars,
true);
PrintHeaderClientMethodInterfaces(printer, service->method(i).get(), params,
vars, true);
printer->Print(service->method(i)->GetTrailingComments("//").c_str());
}
PrintHeaderClientMethodCallbackInterfacesStart(printer, vars);
@ -1368,8 +1439,8 @@ void PrintHeaderService(grpc_generator::Printer* printer,
printer->Print(" private:\n");
printer->Indent();
for (int i = 0; i < service->method_count(); ++i) {
PrintHeaderClientMethodInterfaces(printer, service->method(i).get(), vars,
false);
PrintHeaderClientMethodInterfaces(printer, service->method(i).get(), params,
vars, false);
}
printer->Outdent();
printer->Print("};\n");
@ -1382,7 +1453,8 @@ void PrintHeaderService(grpc_generator::Printer* printer,
"channel, const ::grpc::StubOptions& options = "
"::grpc::StubOptions());\n");
for (int i = 0; i < service->method_count(); ++i) {
PrintHeaderClientMethod(printer, service->method(i).get(), vars, true);
PrintHeaderClientMethod(printer, service->method(i).get(), params, vars,
true);
}
PrintHeaderClientMethodCallbackStart(printer, vars);
for (int i = 0; i < service->method_count(); ++i) {
@ -1395,7 +1467,8 @@ void PrintHeaderService(grpc_generator::Printer* printer,
printer->Print("std::shared_ptr< ::grpc::ChannelInterface> channel_;\n");
printer->Print("class async async_stub_{this};\n");
for (int i = 0; i < service->method_count(); ++i) {
PrintHeaderClientMethod(printer, service->method(i).get(), vars, false);
PrintHeaderClientMethod(printer, service->method(i).get(), params, vars,
false);
}
for (int i = 0; i < service->method_count(); ++i) {
PrintHeaderClientMethodData(printer, service->method(i).get(), vars);
@ -1417,15 +1490,18 @@ void PrintHeaderService(grpc_generator::Printer* printer,
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);
PrintHeaderServerMethodSync(printer, service->method(i).get(), params,
vars);
}
printer->Outdent();
printer->Print("};\n");
if (params.allow_cq_api) {
// Server side - Asynchronous
for (int i = 0; i < service->method_count(); ++i) {
(*vars)["Idx"] = as_string(i);
PrintHeaderServerMethodAsync(printer, service->method(i).get(), vars);
PrintHeaderServerMethodAsync(printer, service->method(i).get(), params,
vars);
}
printer->Print("typedef ");
@ -1439,11 +1515,13 @@ void PrintHeaderService(grpc_generator::Printer* printer,
printer->Print(" >");
}
printer->Print(" AsyncService;\n");
}
// Server side - Callback
for (int i = 0; i < service->method_count(); ++i) {
(*vars)["Idx"] = as_string(i);
PrintHeaderServerMethodCallback(printer, service->method(i).get(), vars);
PrintHeaderServerMethodCallback(printer, service->method(i).get(), params,
vars);
}
printer->Print("typedef ");
@ -1465,21 +1543,27 @@ void PrintHeaderService(grpc_generator::Printer* printer,
// Server side - Generic
for (int i = 0; i < service->method_count(); ++i) {
(*vars)["Idx"] = as_string(i);
PrintHeaderServerMethodGeneric(printer, service->method(i).get(), vars);
PrintHeaderServerMethodGeneric(printer, service->method(i).get(), params,
vars);
}
// Server side - Raw
// Server side - Raw Async
if (params.allow_cq_api) {
for (int i = 0; i < service->method_count(); ++i) {
(*vars)["Idx"] = as_string(i);
PrintHeaderServerMethodRaw(printer, service->method(i).get(), vars);
PrintHeaderServerMethodRaw(printer, service->method(i).get(), params,
vars);
}
}
// Server side - Raw Callback
for (int i = 0; i < service->method_count(); ++i) {
(*vars)["Idx"] = as_string(i);
PrintHeaderServerMethodRawCallback(printer, service->method(i).get(), vars);
PrintHeaderServerMethodRawCallback(printer, service->method(i).get(),
params, vars);
}
if (params.allow_sync_server_api) {
// Server side - Streamed Unary
for (int i = 0; i < service->method_count(); ++i) {
(*vars)["Idx"] = as_string(i);
@ -1547,6 +1631,7 @@ void PrintHeaderService(grpc_generator::Printer* printer,
}
}
printer->Print(" StreamedService;\n");
}
printer->Outdent();
printer->Print("};\n");
@ -1573,7 +1658,7 @@ std::string GetHeaderServices(grpc_generator::File* file,
}
for (int i = 0; i < file->service_count(); ++i) {
PrintHeaderService(printer.get(), file->service(i).get(), &vars);
PrintHeaderService(printer.get(), file->service(i).get(), params, &vars);
printer->Print("\n");
}
@ -1682,6 +1767,7 @@ std::string GetSourceIncludes(grpc_generator::File* file,
void PrintSourceClientMethod(grpc_generator::Printer* printer,
const grpc_generator::Method* method,
const Parameters& params,
std::map<std::string, std::string>* vars) {
(*vars)["Method"] = method->name();
(*vars)["Request"] = method->input_type_name();
@ -1729,6 +1815,7 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer,
"(stub_->channel_.get(), stub_->rpcmethod_$Method$_, "
"context, request, response, reactor);\n}\n\n");
if (params.allow_cq_api) {
printer->Print(*vars,
"::grpc::ClientAsyncResponseReader< $Response$>* "
"$ns$$Service$::Stub::PrepareAsync$Method$Raw(::grpc::"
@ -1749,12 +1836,14 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer,
"ClientContext* context, "
"const $Request$& request, "
"::grpc::CompletionQueue* cq) {\n");
printer->Print(*vars,
printer->Print(
*vars,
" auto* result =\n"
" this->PrepareAsync$Method$Raw(context, request, cq);\n"
" result->StartCall();\n"
" return result;\n"
"}\n\n");
}
} else if (ClientOnlyStreaming(method)) {
printer->Print(*vars,
"::grpc::ClientWriter< $Request$>* "
@ -1781,7 +1870,8 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer,
"context, response, reactor);\n"
"}\n\n");
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncStart"] = async_prefix.start;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
@ -1799,6 +1889,7 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer,
"context, response, $AsyncStart$$AsyncCreateArgs$);\n"
"}\n\n");
}
}
} else if (ServerOnlyStreaming(method)) {
printer->Print(
*vars,
@ -1826,7 +1917,8 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer,
"context, request, reactor);\n"
"}\n\n");
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncStart"] = async_prefix.start;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
@ -1845,6 +1937,7 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer,
"context, request, $AsyncStart$$AsyncCreateArgs$);\n"
"}\n\n");
}
}
} else if (method->BidiStreaming()) {
printer->Print(
*vars,
@ -1871,12 +1964,14 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer,
"context, reactor);\n"
"}\n\n");
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncStart"] = async_prefix.start;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["AsyncCreateArgs"] = async_prefix.create_args;
printer->Print(*vars,
printer->Print(
*vars,
"::grpc::ClientAsyncReaderWriter< $Request$, $Response$>* "
"$ns$$Service$::Stub::$AsyncPrefix$$Method$Raw(::grpc::"
"ClientContext* context, "
@ -1891,10 +1986,12 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer,
"}\n\n");
}
}
}
}
void PrintSourceServerMethod(grpc_generator::Printer* printer,
const grpc_generator::Method* method,
const Parameters& params,
std::map<std::string, std::string>* vars) {
(*vars)["Method"] = method->name();
(*vars)["Request"] = method->input_type_name();
@ -1904,6 +2001,9 @@ void PrintSourceServerMethod(grpc_generator::Printer* printer,
"::grpc::Status $ns$$Service$::Service::$Method$("
"::grpc::ServerContext* context, "
"const $Request$* request, $Response$* response) {\n");
if (!params.allow_sync_server_api) {
printer->Print(" abort();\n");
}
printer->Print(" (void) context;\n");
printer->Print(" (void) request;\n");
printer->Print(" (void) response;\n");
@ -1917,6 +2017,9 @@ void PrintSourceServerMethod(grpc_generator::Printer* printer,
"::grpc::ServerContext* context, "
"::grpc::ServerReader< $Request$>* reader, "
"$Response$* response) {\n");
if (!params.allow_sync_server_api) {
printer->Print(" abort();\n");
}
printer->Print(" (void) context;\n");
printer->Print(" (void) reader;\n");
printer->Print(" (void) response;\n");
@ -1930,6 +2033,9 @@ void PrintSourceServerMethod(grpc_generator::Printer* printer,
"::grpc::ServerContext* context, "
"const $Request$* request, "
"::grpc::ServerWriter< $Response$>* writer) {\n");
if (!params.allow_sync_server_api) {
printer->Print(" abort();\n");
}
printer->Print(" (void) context;\n");
printer->Print(" (void) request;\n");
printer->Print(" (void) writer;\n");
@ -1943,6 +2049,9 @@ void PrintSourceServerMethod(grpc_generator::Printer* printer,
"::grpc::ServerContext* context, "
"::grpc::ServerReaderWriter< $Response$, $Request$>* "
"stream) {\n");
if (!params.allow_sync_server_api) {
printer->Print(" abort();\n");
}
printer->Print(" (void) context;\n");
printer->Print(" (void) stream;\n");
printer->Print(
@ -1954,6 +2063,7 @@ void PrintSourceServerMethod(grpc_generator::Printer* printer,
void PrintSourceService(grpc_generator::Printer* printer,
const grpc_generator::Service* service,
const Parameters& params,
std::map<std::string, std::string>* vars) {
(*vars)["Service"] = service->name();
@ -2011,7 +2121,7 @@ void PrintSourceService(grpc_generator::Printer* printer,
for (int i = 0; i < service->method_count(); ++i) {
(*vars)["Idx"] = as_string(i);
PrintSourceClientMethod(printer, service->method(i).get(), vars);
PrintSourceClientMethod(printer, service->method(i).get(), params, vars);
}
printer->Print(*vars, "$ns$$Service$::Service::Service() {\n");
@ -2087,7 +2197,7 @@ void PrintSourceService(grpc_generator::Printer* printer,
"}\n\n");
for (int i = 0; i < service->method_count(); ++i) {
(*vars)["Idx"] = as_string(i);
PrintSourceServerMethod(printer, service->method(i).get(), vars);
PrintSourceServerMethod(printer, service->method(i).get(), params, vars);
}
}
@ -2113,7 +2223,7 @@ std::string GetSourceServices(grpc_generator::File* file,
}
for (int i = 0; i < file->service_count(); ++i) {
PrintSourceService(printer.get(), file->service(i).get(), &vars);
PrintSourceService(printer.get(), file->service(i).get(), params, &vars);
printer->Print("\n");
}
}
@ -2224,6 +2334,7 @@ std::string GetMockIncludes(grpc_generator::File* file,
void PrintMockClientMethods(grpc_generator::Printer* printer,
const grpc_generator::Method* method,
const Parameters& params,
std::map<std::string, std::string>* vars) {
(*vars)["Method"] = method->name();
(*vars)["Request"] = method->input_type_name();
@ -2240,7 +2351,8 @@ void PrintMockClientMethods(grpc_generator::Printer* printer,
*vars,
"MOCK_METHOD3($Method$, ::grpc::Status(::grpc::ClientContext* context, "
"const $Request$& request, $Response$* response));\n");
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
printer->Print(
*vars,
@ -2249,13 +2361,15 @@ void PrintMockClientMethods(grpc_generator::Printer* printer,
"(::grpc::ClientContext* context, const $Request$& request, "
"::grpc::CompletionQueue* cq));\n");
}
}
} else if (ClientOnlyStreaming(method)) {
printer->Print(
*vars,
"MOCK_METHOD2($Method$Raw, "
"::grpc::ClientWriterInterface< $Request$>*"
"(::grpc::ClientContext* context, $Response$* response));\n");
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["MockArgs"] =
@ -2266,13 +2380,15 @@ void PrintMockClientMethods(grpc_generator::Printer* printer,
"(::grpc::ClientContext* context, $Response$* response, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$));\n");
}
}
} else if (ServerOnlyStreaming(method)) {
printer->Print(
*vars,
"MOCK_METHOD2($Method$Raw, "
"::grpc::ClientReaderInterface< $Response$>*"
"(::grpc::ClientContext* context, const $Request$& request));\n");
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["MockArgs"] =
@ -2284,13 +2400,15 @@ void PrintMockClientMethods(grpc_generator::Printer* printer,
"(::grpc::ClientContext* context, const $Request$& request, "
"::grpc::CompletionQueue* cq$AsyncMethodParams$));\n");
}
}
} else if (method->BidiStreaming()) {
printer->Print(
*vars,
"MOCK_METHOD1($Method$Raw, "
"::grpc::ClientReaderWriterInterface< $Request$, $Response$>*"
"(::grpc::ClientContext* context));\n");
for (auto async_prefix : async_prefixes) {
if (params.allow_cq_api) {
for (const auto& async_prefix : async_prefixes) {
(*vars)["AsyncPrefix"] = async_prefix.prefix;
(*vars)["AsyncMethodParams"] = async_prefix.method_params;
(*vars)["MockArgs"] =
@ -2304,10 +2422,12 @@ void PrintMockClientMethods(grpc_generator::Printer* printer,
"$AsyncMethodParams$));\n");
}
}
}
}
void PrintMockService(grpc_generator::Printer* printer,
const grpc_generator::Service* service,
const Parameters& params,
std::map<std::string, std::string>* vars) {
(*vars)["Service"] = service->name();
@ -2316,7 +2436,7 @@ void PrintMockService(grpc_generator::Printer* printer,
" public:\n");
printer->Indent();
for (int i = 0; i < service->method_count(); ++i) {
PrintMockClientMethods(printer, service->method(i).get(), vars);
PrintMockClientMethods(printer, service->method(i).get(), params, vars);
}
printer->Outdent();
printer->Print("};\n");
@ -2342,7 +2462,7 @@ std::string GetMockServices(grpc_generator::File* file,
}
for (int i = 0; i < file->service_count(); i++) {
PrintMockService(printer.get(), file->service(i).get(), &vars);
PrintMockService(printer.get(), file->service(i).get(), params, &vars);
printer->Print("\n");
}

@ -64,6 +64,10 @@ struct Parameters {
std::string message_header_extension;
// Whether to include headers corresponding to imports in source file.
bool include_import_headers;
// Whether to expose synchronous server API.
bool allow_sync_server_api;
// Whether to generate completion queue API.
bool allow_cq_api;
};
// Return the prologue of the generated header file.

@ -65,6 +65,8 @@ class CppGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
generator_parameters.use_system_headers = true;
generator_parameters.generate_mock_code = false;
generator_parameters.include_import_headers = false;
generator_parameters.allow_sync_server_api = true;
generator_parameters.allow_cq_api = true;
ProtoBufFile pbfile(file);
@ -95,6 +97,24 @@ class CppGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
*error = std::string("Invalid parameter: ") + *parameter_string;
return false;
}
} else if (param[0] == "allow_sync_server_api") {
if (param[1] == "true") {
generator_parameters.allow_sync_server_api = true;
} else if (param[1] == "false") {
generator_parameters.allow_sync_server_api = false;
} else {
*error = std::string("Invalid parameter: ") + *parameter_string;
return false;
}
} else if (param[0] == "allow_cq_api") {
if (param[1] == "true") {
generator_parameters.allow_cq_api = true;
} else if (param[1] == "false") {
generator_parameters.allow_cq_api = false;
} else {
*error = std::string("Invalid parameter: ") + *parameter_string;
return false;
}
} else if (param[0] == "gmock_search_path") {
generator_parameters.gmock_search_path = param[1];
} else if (param[0] == "additional_header_includes") {

Loading…
Cancel
Save