cpp_generator.cc 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <map>
  34. #include "src/compiler/cpp_generator.h"
  35. #include <sstream>
  36. namespace grpc_cpp_generator {
  37. namespace {
  38. template <class T>
  39. grpc::string as_string(T x) {
  40. std::ostringstream out;
  41. out << x;
  42. return out.str();
  43. }
  44. grpc::string FilenameIdentifier(const grpc::string &filename) {
  45. grpc::string result;
  46. for (unsigned i = 0; i < filename.size(); i++) {
  47. char c = filename[i];
  48. if (isalnum(c)) {
  49. result.push_back(c);
  50. } else {
  51. static char hex[] = "0123456789abcdef";
  52. result.push_back('_');
  53. result.push_back(hex[(c >> 4) & 0xf]);
  54. result.push_back(hex[c & 0xf]);
  55. }
  56. }
  57. return result;
  58. }
  59. } // namespace
  60. template <class T, size_t N>
  61. T *array_end(T (&array)[N]) {
  62. return array + N;
  63. }
  64. void PrintIncludes(Printer *printer, const std::vector<grpc::string> &headers,
  65. const Parameters &params) {
  66. std::map<grpc::string, grpc::string> vars;
  67. vars["l"] = params.use_system_headers ? '<' : '"';
  68. vars["r"] = params.use_system_headers ? '>' : '"';
  69. auto &s = params.grpc_search_path;
  70. if (!s.empty()) {
  71. vars["l"] += s;
  72. if (s[s.size() - 1] != '/') {
  73. vars["l"] += '/';
  74. }
  75. }
  76. for (auto i = headers.begin(); i != headers.end(); i++) {
  77. vars["h"] = *i;
  78. printer->Print(vars, "#include $l$$h$$r$\n");
  79. }
  80. }
  81. grpc::string GetHeaderPrologue(File *file, const Parameters & /*params*/) {
  82. grpc::string output;
  83. {
  84. // Scope the output stream so it closes and finalizes output to the string.
  85. auto printer = file->CreatePrinter(&output);
  86. std::map<grpc::string, grpc::string> vars;
  87. vars["filename"] = file->filename();
  88. vars["filename_identifier"] = FilenameIdentifier(file->filename());
  89. vars["filename_base"] = file->filename_without_ext();
  90. vars["message_header_ext"] = file->message_header_ext();
  91. printer->Print(vars, "// Generated by the gRPC protobuf plugin.\n");
  92. printer->Print(vars,
  93. "// If you make any local change, they will be lost.\n");
  94. printer->Print(vars, "// source: $filename$\n");
  95. grpc::string leading_comments = file->GetLeadingComments();
  96. if (!leading_comments.empty()) {
  97. printer->Print(vars, "// Original file comments:\n");
  98. printer->Print(leading_comments.c_str());
  99. }
  100. printer->Print(vars, "#ifndef GRPC_$filename_identifier$__INCLUDED\n");
  101. printer->Print(vars, "#define GRPC_$filename_identifier$__INCLUDED\n");
  102. printer->Print(vars, "\n");
  103. printer->Print(vars, "#include \"$filename_base$$message_header_ext$\"\n");
  104. printer->Print(vars, "\n");
  105. }
  106. return output;
  107. }
  108. grpc::string GetHeaderIncludes(File *file, const Parameters &params) {
  109. grpc::string output;
  110. {
  111. // Scope the output stream so it closes and finalizes output to the string.
  112. auto printer = file->CreatePrinter(&output);
  113. std::map<grpc::string, grpc::string> vars;
  114. static const char *headers_strs[] = {
  115. "grpc++/impl/codegen/async_stream.h",
  116. "grpc++/impl/codegen/async_unary_call.h",
  117. "grpc++/impl/codegen/proto_utils.h",
  118. "grpc++/impl/codegen/rpc_method.h",
  119. "grpc++/impl/codegen/service_type.h",
  120. "grpc++/impl/codegen/status.h",
  121. "grpc++/impl/codegen/stub_options.h",
  122. "grpc++/impl/codegen/sync_stream.h"};
  123. std::vector<grpc::string> headers(headers_strs, array_end(headers_strs));
  124. PrintIncludes(printer.get(), headers, params);
  125. printer->Print(vars, "\n");
  126. printer->Print(vars, "namespace grpc {\n");
  127. printer->Print(vars, "class CompletionQueue;\n");
  128. printer->Print(vars, "class Channel;\n");
  129. printer->Print(vars, "class RpcService;\n");
  130. printer->Print(vars, "class ServerCompletionQueue;\n");
  131. printer->Print(vars, "class ServerContext;\n");
  132. printer->Print(vars, "} // namespace grpc\n\n");
  133. if (!file->package().empty()) {
  134. std::vector<grpc::string> parts = file->package_parts();
  135. for (auto part = parts.begin(); part != parts.end(); part++) {
  136. vars["part"] = *part;
  137. printer->Print(vars, "namespace $part$ {\n");
  138. }
  139. printer->Print(vars, "\n");
  140. }
  141. }
  142. return output;
  143. }
  144. void PrintHeaderClientMethodInterfaces(
  145. Printer *printer, const Method *method,
  146. std::map<grpc::string, grpc::string> *vars, bool is_public) {
  147. (*vars)["Method"] = method->name();
  148. (*vars)["Request"] = method->input_type_name();
  149. (*vars)["Response"] = method->output_type_name();
  150. if (is_public) {
  151. if (method->NoStreaming()) {
  152. printer->Print(
  153. *vars,
  154. "virtual ::grpc::Status $Method$(::grpc::ClientContext* context, "
  155. "const $Request$& request, $Response$* response) = 0;\n");
  156. printer->Print(*vars,
  157. "std::unique_ptr< "
  158. "::grpc::ClientAsyncResponseReaderInterface< $Response$>> "
  159. "Async$Method$(::grpc::ClientContext* context, "
  160. "const $Request$& request, "
  161. "::grpc::CompletionQueue* cq) {\n");
  162. printer->Indent();
  163. printer->Print(*vars,
  164. "return std::unique_ptr< "
  165. "::grpc::ClientAsyncResponseReaderInterface< $Response$>>("
  166. "Async$Method$Raw(context, request, cq));\n");
  167. printer->Outdent();
  168. printer->Print("}\n");
  169. } else if (method->ClientOnlyStreaming()) {
  170. printer->Print(
  171. *vars,
  172. "std::unique_ptr< ::grpc::ClientWriterInterface< $Request$>>"
  173. " $Method$("
  174. "::grpc::ClientContext* context, $Response$* response) {\n");
  175. printer->Indent();
  176. printer->Print(
  177. *vars,
  178. "return std::unique_ptr< ::grpc::ClientWriterInterface< $Request$>>"
  179. "($Method$Raw(context, response));\n");
  180. printer->Outdent();
  181. printer->Print("}\n");
  182. printer->Print(
  183. *vars,
  184. "std::unique_ptr< ::grpc::ClientAsyncWriterInterface< $Request$>>"
  185. " Async$Method$(::grpc::ClientContext* context, $Response$* "
  186. "response, "
  187. "::grpc::CompletionQueue* cq, void* tag) {\n");
  188. printer->Indent();
  189. printer->Print(*vars,
  190. "return std::unique_ptr< "
  191. "::grpc::ClientAsyncWriterInterface< $Request$>>("
  192. "Async$Method$Raw(context, response, cq, tag));\n");
  193. printer->Outdent();
  194. printer->Print("}\n");
  195. } else if (method->ServerOnlyStreaming()) {
  196. printer->Print(
  197. *vars,
  198. "std::unique_ptr< ::grpc::ClientReaderInterface< $Response$>>"
  199. " $Method$(::grpc::ClientContext* context, const $Request$& request)"
  200. " {\n");
  201. printer->Indent();
  202. printer->Print(
  203. *vars,
  204. "return std::unique_ptr< ::grpc::ClientReaderInterface< $Response$>>"
  205. "($Method$Raw(context, request));\n");
  206. printer->Outdent();
  207. printer->Print("}\n");
  208. printer->Print(
  209. *vars,
  210. "std::unique_ptr< ::grpc::ClientAsyncReaderInterface< $Response$>> "
  211. "Async$Method$("
  212. "::grpc::ClientContext* context, const $Request$& request, "
  213. "::grpc::CompletionQueue* cq, void* tag) {\n");
  214. printer->Indent();
  215. printer->Print(*vars,
  216. "return std::unique_ptr< "
  217. "::grpc::ClientAsyncReaderInterface< $Response$>>("
  218. "Async$Method$Raw(context, request, cq, tag));\n");
  219. printer->Outdent();
  220. printer->Print("}\n");
  221. } else if (method->BidiStreaming()) {
  222. printer->Print(*vars,
  223. "std::unique_ptr< ::grpc::ClientReaderWriterInterface< "
  224. "$Request$, $Response$>> "
  225. "$Method$(::grpc::ClientContext* context) {\n");
  226. printer->Indent();
  227. printer->Print(
  228. *vars,
  229. "return std::unique_ptr< "
  230. "::grpc::ClientReaderWriterInterface< $Request$, $Response$>>("
  231. "$Method$Raw(context));\n");
  232. printer->Outdent();
  233. printer->Print("}\n");
  234. printer->Print(
  235. *vars,
  236. "std::unique_ptr< "
  237. "::grpc::ClientAsyncReaderWriterInterface< $Request$, $Response$>> "
  238. "Async$Method$(::grpc::ClientContext* context, "
  239. "::grpc::CompletionQueue* cq, void* tag) {\n");
  240. printer->Indent();
  241. printer->Print(
  242. *vars,
  243. "return std::unique_ptr< "
  244. "::grpc::ClientAsyncReaderWriterInterface< $Request$, $Response$>>("
  245. "Async$Method$Raw(context, cq, tag));\n");
  246. printer->Outdent();
  247. printer->Print("}\n");
  248. }
  249. } else {
  250. if (method->NoStreaming()) {
  251. printer->Print(
  252. *vars,
  253. "virtual ::grpc::ClientAsyncResponseReaderInterface< $Response$>* "
  254. "Async$Method$Raw(::grpc::ClientContext* context, "
  255. "const $Request$& request, "
  256. "::grpc::CompletionQueue* cq) = 0;\n");
  257. } else if (method->ClientOnlyStreaming()) {
  258. printer->Print(
  259. *vars,
  260. "virtual ::grpc::ClientWriterInterface< $Request$>*"
  261. " $Method$Raw("
  262. "::grpc::ClientContext* context, $Response$* response) = 0;\n");
  263. printer->Print(*vars,
  264. "virtual ::grpc::ClientAsyncWriterInterface< $Request$>*"
  265. " Async$Method$Raw(::grpc::ClientContext* context, "
  266. "$Response$* response, "
  267. "::grpc::CompletionQueue* cq, void* tag) = 0;\n");
  268. } else if (method->ServerOnlyStreaming()) {
  269. printer->Print(
  270. *vars,
  271. "virtual ::grpc::ClientReaderInterface< $Response$>* $Method$Raw("
  272. "::grpc::ClientContext* context, const $Request$& request) = 0;\n");
  273. printer->Print(
  274. *vars,
  275. "virtual ::grpc::ClientAsyncReaderInterface< $Response$>* "
  276. "Async$Method$Raw("
  277. "::grpc::ClientContext* context, const $Request$& request, "
  278. "::grpc::CompletionQueue* cq, void* tag) = 0;\n");
  279. } else if (method->BidiStreaming()) {
  280. printer->Print(*vars,
  281. "virtual ::grpc::ClientReaderWriterInterface< $Request$, "
  282. "$Response$>* "
  283. "$Method$Raw(::grpc::ClientContext* context) = 0;\n");
  284. printer->Print(*vars,
  285. "virtual ::grpc::ClientAsyncReaderWriterInterface< "
  286. "$Request$, $Response$>* "
  287. "Async$Method$Raw(::grpc::ClientContext* context, "
  288. "::grpc::CompletionQueue* cq, void* tag) = 0;\n");
  289. }
  290. }
  291. }
  292. void PrintHeaderClientMethod(Printer *printer, const Method *method,
  293. std::map<grpc::string, grpc::string> *vars,
  294. bool is_public) {
  295. (*vars)["Method"] = method->name();
  296. (*vars)["Request"] = method->input_type_name();
  297. (*vars)["Response"] = method->output_type_name();
  298. if (is_public) {
  299. if (method->NoStreaming()) {
  300. printer->Print(
  301. *vars,
  302. "::grpc::Status $Method$(::grpc::ClientContext* context, "
  303. "const $Request$& request, $Response$* response) GRPC_OVERRIDE;\n");
  304. printer->Print(
  305. *vars,
  306. "std::unique_ptr< ::grpc::ClientAsyncResponseReader< $Response$>> "
  307. "Async$Method$(::grpc::ClientContext* context, "
  308. "const $Request$& request, "
  309. "::grpc::CompletionQueue* cq) {\n");
  310. printer->Indent();
  311. printer->Print(*vars,
  312. "return std::unique_ptr< "
  313. "::grpc::ClientAsyncResponseReader< $Response$>>("
  314. "Async$Method$Raw(context, request, cq));\n");
  315. printer->Outdent();
  316. printer->Print("}\n");
  317. } else if (method->ClientOnlyStreaming()) {
  318. printer->Print(
  319. *vars,
  320. "std::unique_ptr< ::grpc::ClientWriter< $Request$>>"
  321. " $Method$("
  322. "::grpc::ClientContext* context, $Response$* response) {\n");
  323. printer->Indent();
  324. printer->Print(*vars,
  325. "return std::unique_ptr< ::grpc::ClientWriter< $Request$>>"
  326. "($Method$Raw(context, response));\n");
  327. printer->Outdent();
  328. printer->Print("}\n");
  329. printer->Print(*vars,
  330. "std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>>"
  331. " Async$Method$(::grpc::ClientContext* context, "
  332. "$Response$* response, "
  333. "::grpc::CompletionQueue* cq, void* tag) {\n");
  334. printer->Indent();
  335. printer->Print(
  336. *vars,
  337. "return std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>>("
  338. "Async$Method$Raw(context, response, cq, tag));\n");
  339. printer->Outdent();
  340. printer->Print("}\n");
  341. } else if (method->ServerOnlyStreaming()) {
  342. printer->Print(
  343. *vars,
  344. "std::unique_ptr< ::grpc::ClientReader< $Response$>>"
  345. " $Method$(::grpc::ClientContext* context, const $Request$& request)"
  346. " {\n");
  347. printer->Indent();
  348. printer->Print(
  349. *vars,
  350. "return std::unique_ptr< ::grpc::ClientReader< $Response$>>"
  351. "($Method$Raw(context, request));\n");
  352. printer->Outdent();
  353. printer->Print("}\n");
  354. printer->Print(
  355. *vars,
  356. "std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>> "
  357. "Async$Method$("
  358. "::grpc::ClientContext* context, const $Request$& request, "
  359. "::grpc::CompletionQueue* cq, void* tag) {\n");
  360. printer->Indent();
  361. printer->Print(
  362. *vars,
  363. "return std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>>("
  364. "Async$Method$Raw(context, request, cq, tag));\n");
  365. printer->Outdent();
  366. printer->Print("}\n");
  367. } else if (method->BidiStreaming()) {
  368. printer->Print(
  369. *vars,
  370. "std::unique_ptr< ::grpc::ClientReaderWriter< $Request$, $Response$>>"
  371. " $Method$(::grpc::ClientContext* context) {\n");
  372. printer->Indent();
  373. printer->Print(*vars,
  374. "return std::unique_ptr< "
  375. "::grpc::ClientReaderWriter< $Request$, $Response$>>("
  376. "$Method$Raw(context));\n");
  377. printer->Outdent();
  378. printer->Print("}\n");
  379. printer->Print(*vars,
  380. "std::unique_ptr< ::grpc::ClientAsyncReaderWriter< "
  381. "$Request$, $Response$>> "
  382. "Async$Method$(::grpc::ClientContext* context, "
  383. "::grpc::CompletionQueue* cq, void* tag) {\n");
  384. printer->Indent();
  385. printer->Print(*vars,
  386. "return std::unique_ptr< "
  387. "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>>("
  388. "Async$Method$Raw(context, cq, tag));\n");
  389. printer->Outdent();
  390. printer->Print("}\n");
  391. }
  392. } else {
  393. if (method->NoStreaming()) {
  394. printer->Print(*vars,
  395. "::grpc::ClientAsyncResponseReader< $Response$>* "
  396. "Async$Method$Raw(::grpc::ClientContext* context, "
  397. "const $Request$& request, "
  398. "::grpc::CompletionQueue* cq) GRPC_OVERRIDE;\n");
  399. } else if (method->ClientOnlyStreaming()) {
  400. printer->Print(*vars,
  401. "::grpc::ClientWriter< $Request$>* $Method$Raw("
  402. "::grpc::ClientContext* context, $Response$* response) "
  403. "GRPC_OVERRIDE;\n");
  404. printer->Print(
  405. *vars,
  406. "::grpc::ClientAsyncWriter< $Request$>* Async$Method$Raw("
  407. "::grpc::ClientContext* context, $Response$* response, "
  408. "::grpc::CompletionQueue* cq, void* tag) GRPC_OVERRIDE;\n");
  409. } else if (method->ServerOnlyStreaming()) {
  410. printer->Print(*vars,
  411. "::grpc::ClientReader< $Response$>* $Method$Raw("
  412. "::grpc::ClientContext* context, const $Request$& request)"
  413. " GRPC_OVERRIDE;\n");
  414. printer->Print(
  415. *vars,
  416. "::grpc::ClientAsyncReader< $Response$>* Async$Method$Raw("
  417. "::grpc::ClientContext* context, const $Request$& request, "
  418. "::grpc::CompletionQueue* cq, void* tag) GRPC_OVERRIDE;\n");
  419. } else if (method->BidiStreaming()) {
  420. printer->Print(
  421. *vars,
  422. "::grpc::ClientReaderWriter< $Request$, $Response$>* "
  423. "$Method$Raw(::grpc::ClientContext* context) GRPC_OVERRIDE;\n");
  424. printer->Print(
  425. *vars,
  426. "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>* "
  427. "Async$Method$Raw(::grpc::ClientContext* context, "
  428. "::grpc::CompletionQueue* cq, void* tag) GRPC_OVERRIDE;\n");
  429. }
  430. }
  431. }
  432. void PrintHeaderClientMethodData(Printer *printer, const Method *method,
  433. std::map<grpc::string, grpc::string> *vars) {
  434. (*vars)["Method"] = method->name();
  435. printer->Print(*vars, "const ::grpc::RpcMethod rpcmethod_$Method$_;\n");
  436. }
  437. void PrintHeaderServerMethodSync(Printer *printer, const Method *method,
  438. std::map<grpc::string, grpc::string> *vars) {
  439. (*vars)["Method"] = method->name();
  440. (*vars)["Request"] = method->input_type_name();
  441. (*vars)["Response"] = method->output_type_name();
  442. printer->Print(method->GetLeadingComments().c_str());
  443. if (method->NoStreaming()) {
  444. printer->Print(*vars,
  445. "virtual ::grpc::Status $Method$("
  446. "::grpc::ServerContext* context, const $Request$* request, "
  447. "$Response$* response);\n");
  448. } else if (method->ClientOnlyStreaming()) {
  449. printer->Print(*vars,
  450. "virtual ::grpc::Status $Method$("
  451. "::grpc::ServerContext* context, "
  452. "::grpc::ServerReader< $Request$>* reader, "
  453. "$Response$* response);\n");
  454. } else if (method->ServerOnlyStreaming()) {
  455. printer->Print(*vars,
  456. "virtual ::grpc::Status $Method$("
  457. "::grpc::ServerContext* context, const $Request$* request, "
  458. "::grpc::ServerWriter< $Response$>* writer);\n");
  459. } else if (method->BidiStreaming()) {
  460. printer->Print(
  461. *vars,
  462. "virtual ::grpc::Status $Method$("
  463. "::grpc::ServerContext* context, "
  464. "::grpc::ServerReaderWriter< $Response$, $Request$>* stream);"
  465. "\n");
  466. }
  467. printer->Print(method->GetTrailingComments().c_str());
  468. }
  469. void PrintHeaderServerMethodAsync(Printer *printer, const Method *method,
  470. std::map<grpc::string, grpc::string> *vars) {
  471. (*vars)["Method"] = method->name();
  472. (*vars)["Request"] = method->input_type_name();
  473. (*vars)["Response"] = method->output_type_name();
  474. printer->Print(*vars, "template <class BaseClass>\n");
  475. printer->Print(*vars,
  476. "class WithAsyncMethod_$Method$ : public BaseClass {\n");
  477. printer->Print(
  478. " private:\n"
  479. " void BaseClassMustBeDerivedFromService(const Service *service) {}\n");
  480. printer->Print(" public:\n");
  481. printer->Indent();
  482. printer->Print(*vars,
  483. "WithAsyncMethod_$Method$() {\n"
  484. " ::grpc::Service::MarkMethodAsync($Idx$);\n"
  485. "}\n");
  486. printer->Print(*vars,
  487. "~WithAsyncMethod_$Method$() GRPC_OVERRIDE {\n"
  488. " BaseClassMustBeDerivedFromService(this);\n"
  489. "}\n");
  490. if (method->NoStreaming()) {
  491. printer->Print(
  492. *vars,
  493. "// disable synchronous version of this method\n"
  494. "::grpc::Status $Method$("
  495. "::grpc::ServerContext* context, const $Request$* request, "
  496. "$Response$* response) GRPC_FINAL GRPC_OVERRIDE {\n"
  497. " abort();\n"
  498. " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
  499. "}\n");
  500. printer->Print(
  501. *vars,
  502. "void Request$Method$("
  503. "::grpc::ServerContext* context, $Request$* request, "
  504. "::grpc::ServerAsyncResponseWriter< $Response$>* response, "
  505. "::grpc::CompletionQueue* new_call_cq, "
  506. "::grpc::ServerCompletionQueue* notification_cq, void *tag) {\n");
  507. printer->Print(*vars,
  508. " ::grpc::Service::RequestAsyncUnary($Idx$, context, "
  509. "request, response, new_call_cq, notification_cq, tag);\n");
  510. printer->Print("}\n");
  511. } else if (method->ClientOnlyStreaming()) {
  512. printer->Print(
  513. *vars,
  514. "// disable synchronous version of this method\n"
  515. "::grpc::Status $Method$("
  516. "::grpc::ServerContext* context, "
  517. "::grpc::ServerReader< $Request$>* reader, "
  518. "$Response$* response) GRPC_FINAL GRPC_OVERRIDE {\n"
  519. " abort();\n"
  520. " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
  521. "}\n");
  522. printer->Print(
  523. *vars,
  524. "void Request$Method$("
  525. "::grpc::ServerContext* context, "
  526. "::grpc::ServerAsyncReader< $Response$, $Request$>* reader, "
  527. "::grpc::CompletionQueue* new_call_cq, "
  528. "::grpc::ServerCompletionQueue* notification_cq, void *tag) {\n");
  529. printer->Print(*vars,
  530. " ::grpc::Service::RequestAsyncClientStreaming($Idx$, "
  531. "context, reader, new_call_cq, notification_cq, tag);\n");
  532. printer->Print("}\n");
  533. } else if (method->ServerOnlyStreaming()) {
  534. printer->Print(
  535. *vars,
  536. "// disable synchronous version of this method\n"
  537. "::grpc::Status $Method$("
  538. "::grpc::ServerContext* context, const $Request$* request, "
  539. "::grpc::ServerWriter< $Response$>* writer) GRPC_FINAL GRPC_OVERRIDE "
  540. "{\n"
  541. " abort();\n"
  542. " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
  543. "}\n");
  544. printer->Print(
  545. *vars,
  546. "void Request$Method$("
  547. "::grpc::ServerContext* context, $Request$* request, "
  548. "::grpc::ServerAsyncWriter< $Response$>* writer, "
  549. "::grpc::CompletionQueue* new_call_cq, "
  550. "::grpc::ServerCompletionQueue* notification_cq, void *tag) {\n");
  551. printer->Print(
  552. *vars,
  553. " ::grpc::Service::RequestAsyncServerStreaming($Idx$, "
  554. "context, request, writer, new_call_cq, notification_cq, tag);\n");
  555. printer->Print("}\n");
  556. } else if (method->BidiStreaming()) {
  557. printer->Print(
  558. *vars,
  559. "// disable synchronous version of this method\n"
  560. "::grpc::Status $Method$("
  561. "::grpc::ServerContext* context, "
  562. "::grpc::ServerReaderWriter< $Response$, $Request$>* stream) "
  563. "GRPC_FINAL GRPC_OVERRIDE {\n"
  564. " abort();\n"
  565. " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
  566. "}\n");
  567. printer->Print(
  568. *vars,
  569. "void Request$Method$("
  570. "::grpc::ServerContext* context, "
  571. "::grpc::ServerAsyncReaderWriter< $Response$, $Request$>* stream, "
  572. "::grpc::CompletionQueue* new_call_cq, "
  573. "::grpc::ServerCompletionQueue* notification_cq, void *tag) {\n");
  574. printer->Print(*vars,
  575. " ::grpc::Service::RequestAsyncBidiStreaming($Idx$, "
  576. "context, stream, new_call_cq, notification_cq, tag);\n");
  577. printer->Print("}\n");
  578. }
  579. printer->Outdent();
  580. printer->Print(*vars, "};\n");
  581. }
  582. void PrintHeaderServerMethodGeneric(
  583. Printer *printer, const Method *method,
  584. std::map<grpc::string, grpc::string> *vars) {
  585. (*vars)["Method"] = method->name();
  586. (*vars)["Request"] = method->input_type_name();
  587. (*vars)["Response"] = method->output_type_name();
  588. printer->Print(*vars, "template <class BaseClass>\n");
  589. printer->Print(*vars,
  590. "class WithGenericMethod_$Method$ : public BaseClass {\n");
  591. printer->Print(
  592. " private:\n"
  593. " void BaseClassMustBeDerivedFromService(const Service *service) {}\n");
  594. printer->Print(" public:\n");
  595. printer->Indent();
  596. printer->Print(*vars,
  597. "WithGenericMethod_$Method$() {\n"
  598. " ::grpc::Service::MarkMethodGeneric($Idx$);\n"
  599. "}\n");
  600. printer->Print(*vars,
  601. "~WithGenericMethod_$Method$() GRPC_OVERRIDE {\n"
  602. " BaseClassMustBeDerivedFromService(this);\n"
  603. "}\n");
  604. if (method->NoStreaming()) {
  605. printer->Print(
  606. *vars,
  607. "// disable synchronous version of this method\n"
  608. "::grpc::Status $Method$("
  609. "::grpc::ServerContext* context, const $Request$* request, "
  610. "$Response$* response) GRPC_FINAL GRPC_OVERRIDE {\n"
  611. " abort();\n"
  612. " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
  613. "}\n");
  614. } else if (method->ClientOnlyStreaming()) {
  615. printer->Print(
  616. *vars,
  617. "// disable synchronous version of this method\n"
  618. "::grpc::Status $Method$("
  619. "::grpc::ServerContext* context, "
  620. "::grpc::ServerReader< $Request$>* reader, "
  621. "$Response$* response) GRPC_FINAL GRPC_OVERRIDE {\n"
  622. " abort();\n"
  623. " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
  624. "}\n");
  625. } else if (method->ServerOnlyStreaming()) {
  626. printer->Print(
  627. *vars,
  628. "// disable synchronous version of this method\n"
  629. "::grpc::Status $Method$("
  630. "::grpc::ServerContext* context, const $Request$* request, "
  631. "::grpc::ServerWriter< $Response$>* writer) GRPC_FINAL GRPC_OVERRIDE "
  632. "{\n"
  633. " abort();\n"
  634. " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
  635. "}\n");
  636. } else if (method->BidiStreaming()) {
  637. printer->Print(
  638. *vars,
  639. "// disable synchronous version of this method\n"
  640. "::grpc::Status $Method$("
  641. "::grpc::ServerContext* context, "
  642. "::grpc::ServerReaderWriter< $Response$, $Request$>* stream) "
  643. "GRPC_FINAL GRPC_OVERRIDE {\n"
  644. " abort();\n"
  645. " return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, \"\");\n"
  646. "}\n");
  647. }
  648. printer->Outdent();
  649. printer->Print(*vars, "};\n");
  650. }
  651. void PrintHeaderService(Printer *printer, const Service *service,
  652. std::map<grpc::string, grpc::string> *vars) {
  653. (*vars)["Service"] = service->name();
  654. printer->Print(service->GetLeadingComments().c_str());
  655. printer->Print(*vars,
  656. "class $Service$ GRPC_FINAL {\n"
  657. " public:\n");
  658. printer->Indent();
  659. // Client side
  660. printer->Print(
  661. "class StubInterface {\n"
  662. " public:\n");
  663. printer->Indent();
  664. printer->Print("virtual ~StubInterface() {}\n");
  665. for (int i = 0; i < service->method_count(); ++i) {
  666. printer->Print(service->method(i)->GetLeadingComments().c_str());
  667. PrintHeaderClientMethodInterfaces(printer, service->method(i).get(), vars,
  668. true);
  669. printer->Print(service->method(i)->GetTrailingComments().c_str());
  670. }
  671. printer->Outdent();
  672. printer->Print("private:\n");
  673. printer->Indent();
  674. for (int i = 0; i < service->method_count(); ++i) {
  675. PrintHeaderClientMethodInterfaces(printer, service->method(i).get(), vars,
  676. false);
  677. }
  678. printer->Outdent();
  679. printer->Print("};\n");
  680. printer->Print(
  681. "class Stub GRPC_FINAL : public StubInterface"
  682. " {\n public:\n");
  683. printer->Indent();
  684. printer->Print(
  685. "Stub(const std::shared_ptr< ::grpc::ChannelInterface>& channel);\n");
  686. for (int i = 0; i < service->method_count(); ++i) {
  687. PrintHeaderClientMethod(printer, service->method(i).get(), vars, true);
  688. }
  689. printer->Outdent();
  690. printer->Print("\n private:\n");
  691. printer->Indent();
  692. printer->Print("std::shared_ptr< ::grpc::ChannelInterface> channel_;\n");
  693. for (int i = 0; i < service->method_count(); ++i) {
  694. PrintHeaderClientMethod(printer, service->method(i).get(), vars, false);
  695. }
  696. for (int i = 0; i < service->method_count(); ++i) {
  697. PrintHeaderClientMethodData(printer, service->method(i).get(), vars);
  698. }
  699. printer->Outdent();
  700. printer->Print("};\n");
  701. printer->Print(
  702. "static std::unique_ptr<Stub> NewStub(const std::shared_ptr< "
  703. "::grpc::ChannelInterface>& channel, "
  704. "const ::grpc::StubOptions& options = ::grpc::StubOptions());\n");
  705. printer->Print("\n");
  706. // Server side - base
  707. printer->Print(
  708. "class Service : public ::grpc::Service {\n"
  709. " public:\n");
  710. printer->Indent();
  711. printer->Print("Service();\n");
  712. printer->Print("virtual ~Service();\n");
  713. for (int i = 0; i < service->method_count(); ++i) {
  714. PrintHeaderServerMethodSync(printer, service->method(i).get(), vars);
  715. }
  716. printer->Outdent();
  717. printer->Print("};\n");
  718. // Server side - Asynchronous
  719. for (int i = 0; i < service->method_count(); ++i) {
  720. (*vars)["Idx"] = as_string(i);
  721. PrintHeaderServerMethodAsync(printer, service->method(i).get(), vars);
  722. }
  723. printer->Print("typedef ");
  724. for (int i = 0; i < service->method_count(); ++i) {
  725. (*vars)["method_name"] = service->method(i).get()->name();
  726. printer->Print(*vars, "WithAsyncMethod_$method_name$<");
  727. }
  728. printer->Print("Service");
  729. for (int i = 0; i < service->method_count(); ++i) {
  730. printer->Print(" >");
  731. }
  732. printer->Print(" AsyncService;\n");
  733. // Server side - Generic
  734. for (int i = 0; i < service->method_count(); ++i) {
  735. (*vars)["Idx"] = as_string(i);
  736. PrintHeaderServerMethodGeneric(printer, service->method(i).get(), vars);
  737. }
  738. printer->Outdent();
  739. printer->Print("};\n");
  740. printer->Print(service->GetTrailingComments().c_str());
  741. }
  742. grpc::string GetHeaderServices(File *file, const Parameters &params) {
  743. grpc::string output;
  744. {
  745. // Scope the output stream so it closes and finalizes output to the string.
  746. auto printer = file->CreatePrinter(&output);
  747. std::map<grpc::string, grpc::string> vars;
  748. // Package string is empty or ends with a dot. It is used to fully qualify
  749. // method names.
  750. vars["Package"] = file->package();
  751. if (!file->package().empty()) {
  752. vars["Package"].append(".");
  753. }
  754. if (!params.services_namespace.empty()) {
  755. vars["services_namespace"] = params.services_namespace;
  756. printer->Print(vars, "\nnamespace $services_namespace$ {\n\n");
  757. }
  758. for (int i = 0; i < file->service_count(); ++i) {
  759. PrintHeaderService(printer.get(), file->service(i).get(), &vars);
  760. printer->Print("\n");
  761. }
  762. if (!params.services_namespace.empty()) {
  763. printer->Print(vars, "} // namespace $services_namespace$\n\n");
  764. }
  765. }
  766. return output;
  767. }
  768. grpc::string GetHeaderEpilogue(File *file, const Parameters & /*params*/) {
  769. grpc::string output;
  770. {
  771. // Scope the output stream so it closes and finalizes output to the string.
  772. auto printer = file->CreatePrinter(&output);
  773. std::map<grpc::string, grpc::string> vars;
  774. vars["filename"] = file->filename();
  775. vars["filename_identifier"] = FilenameIdentifier(file->filename());
  776. if (!file->package().empty()) {
  777. std::vector<grpc::string> parts = file->package_parts();
  778. for (auto part = parts.rbegin(); part != parts.rend(); part++) {
  779. vars["part"] = *part;
  780. printer->Print(vars, "} // namespace $part$\n");
  781. }
  782. printer->Print(vars, "\n");
  783. }
  784. printer->Print(vars, "\n");
  785. printer->Print(vars, "#endif // GRPC_$filename_identifier$__INCLUDED\n");
  786. printer->Print(file->GetTrailingComments().c_str());
  787. }
  788. return output;
  789. }
  790. grpc::string GetSourcePrologue(File *file, const Parameters & /*params*/) {
  791. grpc::string output;
  792. {
  793. // Scope the output stream so it closes and finalizes output to the string.
  794. auto printer = file->CreatePrinter(&output);
  795. std::map<grpc::string, grpc::string> vars;
  796. vars["filename"] = file->filename();
  797. vars["filename_base"] = file->filename_without_ext();
  798. vars["message_header_ext"] = file->message_header_ext();
  799. vars["service_header_ext"] = file->service_header_ext();
  800. printer->Print(vars, "// Generated by the gRPC protobuf plugin.\n");
  801. printer->Print(vars,
  802. "// If you make any local change, they will be lost.\n");
  803. printer->Print(vars, "// source: $filename$\n\n");
  804. printer->Print(vars, "#include \"$filename_base$$message_header_ext$\"\n");
  805. printer->Print(vars, "#include \"$filename_base$$service_header_ext$\"\n");
  806. printer->Print(vars, file->additional_headers().c_str());
  807. printer->Print(vars, "\n");
  808. }
  809. return output;
  810. }
  811. grpc::string GetSourceIncludes(File *file, const Parameters &params) {
  812. grpc::string output;
  813. {
  814. // Scope the output stream so it closes and finalizes output to the string.
  815. auto printer = file->CreatePrinter(&output);
  816. std::map<grpc::string, grpc::string> vars;
  817. static const char *headers_strs[] = {
  818. "grpc++/impl/codegen/async_stream.h",
  819. "grpc++/impl/codegen/async_unary_call.h",
  820. "grpc++/impl/codegen/channel_interface.h",
  821. "grpc++/impl/codegen/client_unary_call.h",
  822. "grpc++/impl/codegen/method_handler_impl.h",
  823. "grpc++/impl/codegen/rpc_service_method.h",
  824. "grpc++/impl/codegen/service_type.h",
  825. "grpc++/impl/codegen/sync_stream.h"};
  826. std::vector<grpc::string> headers(headers_strs, array_end(headers_strs));
  827. PrintIncludes(printer.get(), headers, params);
  828. if (!file->package().empty()) {
  829. std::vector<grpc::string> parts = file->package_parts();
  830. for (auto part = parts.begin(); part != parts.end(); part++) {
  831. vars["part"] = *part;
  832. printer->Print(vars, "namespace $part$ {\n");
  833. }
  834. }
  835. printer->Print(vars, "\n");
  836. }
  837. return output;
  838. }
  839. void PrintSourceClientMethod(Printer *printer, const Method *method,
  840. std::map<grpc::string, grpc::string> *vars) {
  841. (*vars)["Method"] = method->name();
  842. (*vars)["Request"] = method->input_type_name();
  843. (*vars)["Response"] = method->output_type_name();
  844. if (method->NoStreaming()) {
  845. printer->Print(*vars,
  846. "::grpc::Status $ns$$Service$::Stub::$Method$("
  847. "::grpc::ClientContext* context, "
  848. "const $Request$& request, $Response$* response) {\n");
  849. printer->Print(*vars,
  850. " return ::grpc::BlockingUnaryCall(channel_.get(), "
  851. "rpcmethod_$Method$_, "
  852. "context, request, response);\n"
  853. "}\n\n");
  854. printer->Print(
  855. *vars,
  856. "::grpc::ClientAsyncResponseReader< $Response$>* "
  857. "$ns$$Service$::Stub::Async$Method$Raw(::grpc::ClientContext* context, "
  858. "const $Request$& request, "
  859. "::grpc::CompletionQueue* cq) {\n");
  860. printer->Print(*vars,
  861. " return new "
  862. "::grpc::ClientAsyncResponseReader< $Response$>("
  863. "channel_.get(), cq, "
  864. "rpcmethod_$Method$_, "
  865. "context, request);\n"
  866. "}\n\n");
  867. } else if (method->ClientOnlyStreaming()) {
  868. printer->Print(*vars,
  869. "::grpc::ClientWriter< $Request$>* "
  870. "$ns$$Service$::Stub::$Method$Raw("
  871. "::grpc::ClientContext* context, $Response$* response) {\n");
  872. printer->Print(*vars,
  873. " return new ::grpc::ClientWriter< $Request$>("
  874. "channel_.get(), "
  875. "rpcmethod_$Method$_, "
  876. "context, response);\n"
  877. "}\n\n");
  878. printer->Print(*vars,
  879. "::grpc::ClientAsyncWriter< $Request$>* "
  880. "$ns$$Service$::Stub::Async$Method$Raw("
  881. "::grpc::ClientContext* context, $Response$* response, "
  882. "::grpc::CompletionQueue* cq, void* tag) {\n");
  883. printer->Print(*vars,
  884. " return new ::grpc::ClientAsyncWriter< $Request$>("
  885. "channel_.get(), cq, "
  886. "rpcmethod_$Method$_, "
  887. "context, response, tag);\n"
  888. "}\n\n");
  889. } else if (method->ServerOnlyStreaming()) {
  890. printer->Print(
  891. *vars,
  892. "::grpc::ClientReader< $Response$>* "
  893. "$ns$$Service$::Stub::$Method$Raw("
  894. "::grpc::ClientContext* context, const $Request$& request) {\n");
  895. printer->Print(*vars,
  896. " return new ::grpc::ClientReader< $Response$>("
  897. "channel_.get(), "
  898. "rpcmethod_$Method$_, "
  899. "context, request);\n"
  900. "}\n\n");
  901. printer->Print(*vars,
  902. "::grpc::ClientAsyncReader< $Response$>* "
  903. "$ns$$Service$::Stub::Async$Method$Raw("
  904. "::grpc::ClientContext* context, const $Request$& request, "
  905. "::grpc::CompletionQueue* cq, void* tag) {\n");
  906. printer->Print(*vars,
  907. " return new ::grpc::ClientAsyncReader< $Response$>("
  908. "channel_.get(), cq, "
  909. "rpcmethod_$Method$_, "
  910. "context, request, tag);\n"
  911. "}\n\n");
  912. } else if (method->BidiStreaming()) {
  913. printer->Print(
  914. *vars,
  915. "::grpc::ClientReaderWriter< $Request$, $Response$>* "
  916. "$ns$$Service$::Stub::$Method$Raw(::grpc::ClientContext* context) {\n");
  917. printer->Print(*vars,
  918. " return new ::grpc::ClientReaderWriter< "
  919. "$Request$, $Response$>("
  920. "channel_.get(), "
  921. "rpcmethod_$Method$_, "
  922. "context);\n"
  923. "}\n\n");
  924. printer->Print(
  925. *vars,
  926. "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>* "
  927. "$ns$$Service$::Stub::Async$Method$Raw(::grpc::ClientContext* context, "
  928. "::grpc::CompletionQueue* cq, void* tag) {\n");
  929. printer->Print(*vars,
  930. " return new "
  931. "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>("
  932. "channel_.get(), cq, "
  933. "rpcmethod_$Method$_, "
  934. "context, tag);\n"
  935. "}\n\n");
  936. }
  937. }
  938. void PrintSourceServerMethod(Printer *printer, const Method *method,
  939. std::map<grpc::string, grpc::string> *vars) {
  940. (*vars)["Method"] = method->name();
  941. (*vars)["Request"] = method->input_type_name();
  942. (*vars)["Response"] = method->output_type_name();
  943. if (method->NoStreaming()) {
  944. printer->Print(*vars,
  945. "::grpc::Status $ns$$Service$::Service::$Method$("
  946. "::grpc::ServerContext* context, "
  947. "const $Request$* request, $Response$* response) {\n");
  948. printer->Print(" (void) context;\n");
  949. printer->Print(" (void) request;\n");
  950. printer->Print(" (void) response;\n");
  951. printer->Print(
  952. " return ::grpc::Status("
  953. "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n");
  954. printer->Print("}\n\n");
  955. } else if (method->ClientOnlyStreaming()) {
  956. printer->Print(*vars,
  957. "::grpc::Status $ns$$Service$::Service::$Method$("
  958. "::grpc::ServerContext* context, "
  959. "::grpc::ServerReader< $Request$>* reader, "
  960. "$Response$* response) {\n");
  961. printer->Print(" (void) context;\n");
  962. printer->Print(" (void) reader;\n");
  963. printer->Print(" (void) response;\n");
  964. printer->Print(
  965. " return ::grpc::Status("
  966. "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n");
  967. printer->Print("}\n\n");
  968. } else if (method->ServerOnlyStreaming()) {
  969. printer->Print(*vars,
  970. "::grpc::Status $ns$$Service$::Service::$Method$("
  971. "::grpc::ServerContext* context, "
  972. "const $Request$* request, "
  973. "::grpc::ServerWriter< $Response$>* writer) {\n");
  974. printer->Print(" (void) context;\n");
  975. printer->Print(" (void) request;\n");
  976. printer->Print(" (void) writer;\n");
  977. printer->Print(
  978. " return ::grpc::Status("
  979. "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n");
  980. printer->Print("}\n\n");
  981. } else if (method->BidiStreaming()) {
  982. printer->Print(*vars,
  983. "::grpc::Status $ns$$Service$::Service::$Method$("
  984. "::grpc::ServerContext* context, "
  985. "::grpc::ServerReaderWriter< $Response$, $Request$>* "
  986. "stream) {\n");
  987. printer->Print(" (void) context;\n");
  988. printer->Print(" (void) stream;\n");
  989. printer->Print(
  990. " return ::grpc::Status("
  991. "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n");
  992. printer->Print("}\n\n");
  993. }
  994. }
  995. void PrintSourceService(Printer *printer, const Service *service,
  996. std::map<grpc::string, grpc::string> *vars) {
  997. (*vars)["Service"] = service->name();
  998. printer->Print(*vars,
  999. "static const char* $prefix$$Service$_method_names[] = {\n");
  1000. for (int i = 0; i < service->method_count(); ++i) {
  1001. (*vars)["Method"] = service->method(i).get()->name();
  1002. printer->Print(*vars, " \"/$Package$$Service$/$Method$\",\n");
  1003. }
  1004. printer->Print(*vars, "};\n\n");
  1005. printer->Print(*vars,
  1006. "std::unique_ptr< $ns$$Service$::Stub> $ns$$Service$::NewStub("
  1007. "const std::shared_ptr< ::grpc::ChannelInterface>& channel, "
  1008. "const ::grpc::StubOptions& options) {\n"
  1009. " std::unique_ptr< $ns$$Service$::Stub> stub(new "
  1010. "$ns$$Service$::Stub(channel));\n"
  1011. " return stub;\n"
  1012. "}\n\n");
  1013. printer->Print(*vars,
  1014. "$ns$$Service$::Stub::Stub(const std::shared_ptr< "
  1015. "::grpc::ChannelInterface>& channel)\n");
  1016. printer->Indent();
  1017. printer->Print(": channel_(channel)");
  1018. for (int i = 0; i < service->method_count(); ++i) {
  1019. auto method = service->method(i);
  1020. (*vars)["Method"] = method->name();
  1021. (*vars)["Idx"] = as_string(i);
  1022. if (method->NoStreaming()) {
  1023. (*vars)["StreamingType"] = "NORMAL_RPC";
  1024. } else if (method->ClientOnlyStreaming()) {
  1025. (*vars)["StreamingType"] = "CLIENT_STREAMING";
  1026. } else if (method->ServerOnlyStreaming()) {
  1027. (*vars)["StreamingType"] = "SERVER_STREAMING";
  1028. } else {
  1029. (*vars)["StreamingType"] = "BIDI_STREAMING";
  1030. }
  1031. printer->Print(*vars,
  1032. ", rpcmethod_$Method$_("
  1033. "$prefix$$Service$_method_names[$Idx$], "
  1034. "::grpc::RpcMethod::$StreamingType$, "
  1035. "channel"
  1036. ")\n");
  1037. }
  1038. printer->Print("{}\n\n");
  1039. printer->Outdent();
  1040. for (int i = 0; i < service->method_count(); ++i) {
  1041. (*vars)["Idx"] = as_string(i);
  1042. PrintSourceClientMethod(printer, service->method(i).get(), vars);
  1043. }
  1044. printer->Print(*vars, "$ns$$Service$::Service::Service() {\n");
  1045. printer->Indent();
  1046. printer->Print(*vars, "(void)$prefix$$Service$_method_names;\n");
  1047. for (int i = 0; i < service->method_count(); ++i) {
  1048. auto method = service->method(i);
  1049. (*vars)["Idx"] = as_string(i);
  1050. (*vars)["Method"] = method->name();
  1051. (*vars)["Request"] = method->input_type_name();
  1052. (*vars)["Response"] = method->output_type_name();
  1053. if (method->NoStreaming()) {
  1054. printer->Print(
  1055. *vars,
  1056. "AddMethod(new ::grpc::RpcServiceMethod(\n"
  1057. " $prefix$$Service$_method_names[$Idx$],\n"
  1058. " ::grpc::RpcMethod::NORMAL_RPC,\n"
  1059. " new ::grpc::RpcMethodHandler< $ns$$Service$::Service, "
  1060. "$Request$, "
  1061. "$Response$>(\n"
  1062. " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n");
  1063. } else if (method->ClientOnlyStreaming()) {
  1064. printer->Print(
  1065. *vars,
  1066. "AddMethod(new ::grpc::RpcServiceMethod(\n"
  1067. " $prefix$$Service$_method_names[$Idx$],\n"
  1068. " ::grpc::RpcMethod::CLIENT_STREAMING,\n"
  1069. " new ::grpc::ClientStreamingHandler< "
  1070. "$ns$$Service$::Service, $Request$, $Response$>(\n"
  1071. " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n");
  1072. } else if (method->ServerOnlyStreaming()) {
  1073. printer->Print(
  1074. *vars,
  1075. "AddMethod(new ::grpc::RpcServiceMethod(\n"
  1076. " $prefix$$Service$_method_names[$Idx$],\n"
  1077. " ::grpc::RpcMethod::SERVER_STREAMING,\n"
  1078. " new ::grpc::ServerStreamingHandler< "
  1079. "$ns$$Service$::Service, $Request$, $Response$>(\n"
  1080. " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n");
  1081. } else if (method->BidiStreaming()) {
  1082. printer->Print(
  1083. *vars,
  1084. "AddMethod(new ::grpc::RpcServiceMethod(\n"
  1085. " $prefix$$Service$_method_names[$Idx$],\n"
  1086. " ::grpc::RpcMethod::BIDI_STREAMING,\n"
  1087. " new ::grpc::BidiStreamingHandler< "
  1088. "$ns$$Service$::Service, $Request$, $Response$>(\n"
  1089. " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n");
  1090. }
  1091. }
  1092. printer->Outdent();
  1093. printer->Print(*vars, "}\n\n");
  1094. printer->Print(*vars,
  1095. "$ns$$Service$::Service::~Service() {\n"
  1096. "}\n\n");
  1097. for (int i = 0; i < service->method_count(); ++i) {
  1098. (*vars)["Idx"] = as_string(i);
  1099. PrintSourceServerMethod(printer, service->method(i).get(), vars);
  1100. }
  1101. }
  1102. grpc::string GetSourceServices(File *file, const Parameters &params) {
  1103. grpc::string output;
  1104. {
  1105. // Scope the output stream so it closes and finalizes output to the string.
  1106. auto printer = file->CreatePrinter(&output);
  1107. std::map<grpc::string, grpc::string> vars;
  1108. // Package string is empty or ends with a dot. It is used to fully qualify
  1109. // method names.
  1110. vars["Package"] = file->package();
  1111. if (!file->package().empty()) {
  1112. vars["Package"].append(".");
  1113. }
  1114. if (!params.services_namespace.empty()) {
  1115. vars["ns"] = params.services_namespace + "::";
  1116. vars["prefix"] = params.services_namespace;
  1117. } else {
  1118. vars["ns"] = "";
  1119. vars["prefix"] = "";
  1120. }
  1121. for (int i = 0; i < file->service_count(); ++i) {
  1122. PrintSourceService(printer.get(), file->service(i).get(), &vars);
  1123. printer->Print("\n");
  1124. }
  1125. }
  1126. return output;
  1127. }
  1128. grpc::string GetSourceEpilogue(File *file, const Parameters & /*params*/) {
  1129. grpc::string temp;
  1130. if (!file->package().empty()) {
  1131. std::vector<grpc::string> parts = file->package_parts();
  1132. for (auto part = parts.begin(); part != parts.end(); part++) {
  1133. temp.append("} // namespace ");
  1134. temp.append(*part);
  1135. temp.append("\n");
  1136. }
  1137. temp.append("\n");
  1138. }
  1139. return temp;
  1140. }
  1141. } // namespace grpc_cpp_generator