blob: ea487bcd89a1ed2e82a0e909856a694b33635797 [file] [log] [blame]
nnobleebebb7e2014-12-10 16:31:01 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
nnobleebebb7e2014-12-10 16:31:01 -08004 * 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
Nicolas "Pixel" Noble36f53232015-01-16 06:39:58 +010034#include <map>
35
nnobleebebb7e2014-12-10 16:31:01 -080036#include "src/compiler/cpp_generator.h"
nnobleebebb7e2014-12-10 16:31:01 -080037#include "src/compiler/cpp_generator_helpers.h"
Nicolas Nobled446eb82015-03-12 17:22:33 -070038
39#include "src/compiler/config.h"
40
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080041#include <sstream>
nnobleebebb7e2014-12-10 16:31:01 -080042
43namespace grpc_cpp_generator {
44namespace {
45
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080046template <class T>
Nicolas Nobled446eb82015-03-12 17:22:33 -070047grpc::string as_string(T x) {
Craig Tiller8c8d0aa2015-02-12 11:38:36 -080048 std::ostringstream out;
49 out << x;
50 return out.str();
51}
52
Nicolas Nobled446eb82015-03-12 17:22:33 -070053bool NoStreaming(const grpc::protobuf::MethodDescriptor *method) {
Craig Tillerb5dcec52015-01-13 11:13:42 -080054 return !method->client_streaming() && !method->server_streaming();
nnobleebebb7e2014-12-10 16:31:01 -080055}
56
Nicolas Nobled446eb82015-03-12 17:22:33 -070057bool ClientOnlyStreaming(const grpc::protobuf::MethodDescriptor *method) {
Craig Tillerb5dcec52015-01-13 11:13:42 -080058 return method->client_streaming() && !method->server_streaming();
nnobleebebb7e2014-12-10 16:31:01 -080059}
60
Nicolas Nobled446eb82015-03-12 17:22:33 -070061bool ServerOnlyStreaming(const grpc::protobuf::MethodDescriptor *method) {
yangg1b151092015-01-09 15:31:05 -080062 return !method->client_streaming() && method->server_streaming();
nnobleebebb7e2014-12-10 16:31:01 -080063}
64
Nicolas Nobled446eb82015-03-12 17:22:33 -070065bool BidiStreaming(const grpc::protobuf::MethodDescriptor *method) {
Craig Tillerb5dcec52015-01-13 11:13:42 -080066 return method->client_streaming() && method->server_streaming();
nnobleebebb7e2014-12-10 16:31:01 -080067}
68
Craig Tiller277d3cf2015-04-14 14:04:51 -070069grpc::string FilenameIdentifier(const grpc::string &filename) {
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +020070 grpc::string result;
71 for (unsigned i = 0; i < filename.size(); i++) {
72 char c = filename[i];
73 if (isalnum(c)) {
74 result.push_back(c);
75 } else {
76 static char hex[] = "0123456789abcdef";
77 result.push_back('_');
78 result.push_back(hex[(c >> 4) & 0xf]);
79 result.push_back(hex[c & 0xf]);
80 }
81 }
82 return result;
83}
nnobleebebb7e2014-12-10 16:31:01 -080084} // namespace
85
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +020086grpc::string GetHeaderPrologue(const grpc::protobuf::FileDescriptor *file,
87 const Parameters &params) {
88 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -070089 {
90 // Scope the output stream so it closes and finalizes output to the string.
91 grpc::protobuf::io::StringOutputStream output_stream(&output);
92 grpc::protobuf::io::Printer printer(&output_stream, '$');
93 std::map<grpc::string, grpc::string> vars;
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +020094
Jan Tattermusch5dcebd92015-05-27 15:30:59 -070095 vars["filename"] = file->name();
96 vars["filename_identifier"] = FilenameIdentifier(file->name());
97 vars["filename_base"] = grpc_generator::StripProto(file->name());
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +020098
Jan Tattermusch5dcebd92015-05-27 15:30:59 -070099 printer.Print(vars, "// Generated by the gRPC protobuf plugin.\n");
Craig Tillerce40de52015-06-05 07:14:58 -0700100 printer.Print(vars,
101 "// If you make any local change, they will be lost.\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700102 printer.Print(vars, "// source: $filename$\n");
103 printer.Print(vars, "#ifndef GRPC_$filename_identifier$__INCLUDED\n");
104 printer.Print(vars, "#define GRPC_$filename_identifier$__INCLUDED\n");
105 printer.Print(vars, "\n");
106 printer.Print(vars, "#include \"$filename_base$.pb.h\"\n");
107 printer.Print(vars, "\n");
108 }
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200109 return output;
110}
111
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100112grpc::string GetHeaderIncludes(const grpc::protobuf::FileDescriptor *file,
113 const Parameters &params) {
Nicolas Nobled446eb82015-03-12 17:22:33 -0700114 grpc::string temp =
Craig Tiller14a65f92015-02-09 13:13:14 -0800115 "#include <grpc++/impl/internal_stub.h>\n"
Craig Tillerbd6c6182015-04-10 17:08:15 -0700116 "#include <grpc++/impl/rpc_method.h>\n"
Craig Tiller50a7a682015-06-04 12:53:40 -0700117 "#include <grpc++/impl/proto_utils.h>\n"
Craig Tiller14a65f92015-02-09 13:13:14 -0800118 "#include <grpc++/impl/service_type.h>\n"
Yang Gaoc6924c82015-05-05 10:42:51 -0700119 "#include <grpc++/async_unary_call.h>\n"
Craig Tiller14a65f92015-02-09 13:13:14 -0800120 "#include <grpc++/status.h>\n"
Yang Gaoc6924c82015-05-05 10:42:51 -0700121 "#include <grpc++/stream.h>\n"
yang-g297a25b2015-08-03 16:43:46 -0700122 "#include <grpc++/stub_options.h>\n"
nnobleebebb7e2014-12-10 16:31:01 -0800123 "\n"
124 "namespace grpc {\n"
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800125 "class CompletionQueue;\n"
nnobleebebb7e2014-12-10 16:31:01 -0800126 "class ChannelInterface;\n"
yangga4b6f5d2014-12-17 15:53:12 -0800127 "class RpcService;\n"
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700128 "class ServerCompletionQueue;\n"
Yang Gaoc6924c82015-05-05 10:42:51 -0700129 "class ServerContext;\n"
130 "} // namespace grpc\n\n";
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200131
Yang Gao1dc1a432015-04-10 13:53:11 -0700132 if (!file->package().empty()) {
133 std::vector<grpc::string> parts =
134 grpc_generator::tokenize(file->package(), ".");
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200135
Yang Gao1dc1a432015-04-10 13:53:11 -0700136 for (auto part = parts.begin(); part != parts.end(); part++) {
137 temp.append("namespace ");
138 temp.append(*part);
139 temp.append(" {\n");
140 }
141 temp.append("\n");
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200142 }
143
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200144 return temp;
nnobleebebb7e2014-12-10 16:31:01 -0800145}
146
Craig Tillerce40de52015-06-05 07:14:58 -0700147void PrintHeaderClientMethodInterfaces(
148 grpc::protobuf::io::Printer *printer,
149 const grpc::protobuf::MethodDescriptor *method,
150 std::map<grpc::string, grpc::string> *vars, bool is_public) {
nnobleebebb7e2014-12-10 16:31:01 -0800151 (*vars)["Method"] = method->name();
152 (*vars)["Request"] =
153 grpc_cpp_generator::ClassName(method->input_type(), true);
154 (*vars)["Response"] =
155 grpc_cpp_generator::ClassName(method->output_type(), true);
Yang Gaoc6924c82015-05-05 10:42:51 -0700156
157 if (is_public) {
158 if (NoStreaming(method)) {
159 printer->Print(
160 *vars,
161 "virtual ::grpc::Status $Method$(::grpc::ClientContext* context, "
162 "const $Request$& request, $Response$* response) = 0;\n");
Craig Tillerce40de52015-06-05 07:14:58 -0700163 printer->Print(*vars,
164 "std::unique_ptr< "
165 "::grpc::ClientAsyncResponseReaderInterface< $Response$>> "
166 "Async$Method$(::grpc::ClientContext* context, "
167 "const $Request$& request, "
168 "::grpc::CompletionQueue* cq) {\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700169 printer->Indent();
Craig Tillerce40de52015-06-05 07:14:58 -0700170 printer->Print(*vars,
171 "return std::unique_ptr< "
172 "::grpc::ClientAsyncResponseReaderInterface< $Response$>>("
173 "Async$Method$Raw(context, request, cq));\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700174 printer->Outdent();
175 printer->Print("}\n");
176 } else if (ClientOnlyStreaming(method)) {
177 printer->Print(
178 *vars,
179 "std::unique_ptr< ::grpc::ClientWriterInterface< $Request$>>"
180 " $Method$("
181 "::grpc::ClientContext* context, $Response$* response) {\n");
182 printer->Indent();
183 printer->Print(
184 *vars,
185 "return std::unique_ptr< ::grpc::ClientWriterInterface< $Request$>>"
186 "($Method$Raw(context, response));\n");
187 printer->Outdent();
188 printer->Print("}\n");
189 printer->Print(
190 *vars,
191 "std::unique_ptr< ::grpc::ClientAsyncWriterInterface< $Request$>>"
Craig Tillerce40de52015-06-05 07:14:58 -0700192 " Async$Method$(::grpc::ClientContext* context, $Response$* "
193 "response, "
Yang Gaoc6924c82015-05-05 10:42:51 -0700194 "::grpc::CompletionQueue* cq, void* tag) {\n");
195 printer->Indent();
Craig Tillerce40de52015-06-05 07:14:58 -0700196 printer->Print(*vars,
197 "return std::unique_ptr< "
198 "::grpc::ClientAsyncWriterInterface< $Request$>>("
199 "Async$Method$Raw(context, response, cq, tag));\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700200 printer->Outdent();
201 printer->Print("}\n");
202 } else if (ServerOnlyStreaming(method)) {
203 printer->Print(
204 *vars,
205 "std::unique_ptr< ::grpc::ClientReaderInterface< $Response$>>"
206 " $Method$(::grpc::ClientContext* context, const $Request$& request)"
207 " {\n");
208 printer->Indent();
209 printer->Print(
210 *vars,
211 "return std::unique_ptr< ::grpc::ClientReaderInterface< $Response$>>"
212 "($Method$Raw(context, request));\n");
213 printer->Outdent();
214 printer->Print("}\n");
215 printer->Print(
216 *vars,
217 "std::unique_ptr< ::grpc::ClientAsyncReaderInterface< $Response$>> "
218 "Async$Method$("
219 "::grpc::ClientContext* context, const $Request$& request, "
220 "::grpc::CompletionQueue* cq, void* tag) {\n");
221 printer->Indent();
Craig Tillerce40de52015-06-05 07:14:58 -0700222 printer->Print(*vars,
223 "return std::unique_ptr< "
224 "::grpc::ClientAsyncReaderInterface< $Response$>>("
225 "Async$Method$Raw(context, request, cq, tag));\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700226 printer->Outdent();
227 printer->Print("}\n");
228 } else if (BidiStreaming(method)) {
Craig Tillerce40de52015-06-05 07:14:58 -0700229 printer->Print(*vars,
230 "std::unique_ptr< ::grpc::ClientReaderWriterInterface< "
231 "$Request$, $Response$>> "
232 "$Method$(::grpc::ClientContext* context) {\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700233 printer->Indent();
234 printer->Print(
235 *vars,
236 "return std::unique_ptr< "
237 "::grpc::ClientReaderWriterInterface< $Request$, $Response$>>("
238 "$Method$Raw(context));\n");
239 printer->Outdent();
240 printer->Print("}\n");
241 printer->Print(
242 *vars,
243 "std::unique_ptr< "
244 "::grpc::ClientAsyncReaderWriterInterface< $Request$, $Response$>> "
245 "Async$Method$(::grpc::ClientContext* context, "
246 "::grpc::CompletionQueue* cq, void* tag) {\n");
247 printer->Indent();
248 printer->Print(
249 *vars,
250 "return std::unique_ptr< "
251 "::grpc::ClientAsyncReaderWriterInterface< $Request$, $Response$>>("
252 "Async$Method$Raw(context, cq, tag));\n");
253 printer->Outdent();
254 printer->Print("}\n");
255 }
256 } else {
257 if (NoStreaming(method)) {
258 printer->Print(
259 *vars,
260 "virtual ::grpc::ClientAsyncResponseReaderInterface< $Response$>* "
261 "Async$Method$Raw(::grpc::ClientContext* context, "
262 "const $Request$& request, "
Craig Tiller5f871ac2015-05-08 13:05:51 -0700263 "::grpc::CompletionQueue* cq) = 0;\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700264 } else if (ClientOnlyStreaming(method)) {
265 printer->Print(
266 *vars,
267 "virtual ::grpc::ClientWriterInterface< $Request$>*"
268 " $Method$Raw("
269 "::grpc::ClientContext* context, $Response$* response) = 0;\n");
Craig Tillerce40de52015-06-05 07:14:58 -0700270 printer->Print(*vars,
271 "virtual ::grpc::ClientAsyncWriterInterface< $Request$>*"
272 " Async$Method$Raw(::grpc::ClientContext* context, "
273 "$Response$* response, "
274 "::grpc::CompletionQueue* cq, void* tag) = 0;\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700275 } else if (ServerOnlyStreaming(method)) {
276 printer->Print(
277 *vars,
278 "virtual ::grpc::ClientReaderInterface< $Response$>* $Method$Raw("
279 "::grpc::ClientContext* context, const $Request$& request) = 0;\n");
280 printer->Print(
281 *vars,
282 "virtual ::grpc::ClientAsyncReaderInterface< $Response$>* "
283 "Async$Method$Raw("
284 "::grpc::ClientContext* context, const $Request$& request, "
285 "::grpc::CompletionQueue* cq, void* tag) = 0;\n");
286 } else if (BidiStreaming(method)) {
Craig Tillerce40de52015-06-05 07:14:58 -0700287 printer->Print(*vars,
288 "virtual ::grpc::ClientReaderWriterInterface< $Request$, "
289 "$Response$>* "
290 "$Method$Raw(::grpc::ClientContext* context) = 0;\n");
291 printer->Print(*vars,
292 "virtual ::grpc::ClientAsyncReaderWriterInterface< "
293 "$Request$, $Response$>* "
294 "Async$Method$Raw(::grpc::ClientContext* context, "
295 "::grpc::CompletionQueue* cq, void* tag) = 0;\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700296 }
297 }
298}
299
300void PrintHeaderClientMethod(grpc::protobuf::io::Printer *printer,
301 const grpc::protobuf::MethodDescriptor *method,
302 std::map<grpc::string, grpc::string> *vars,
303 bool is_public) {
304 (*vars)["Method"] = method->name();
305 (*vars)["Request"] =
306 grpc_cpp_generator::ClassName(method->input_type(), true);
307 (*vars)["Response"] =
308 grpc_cpp_generator::ClassName(method->output_type(), true);
Yang Gaoc6924c82015-05-05 10:42:51 -0700309 if (is_public) {
310 if (NoStreaming(method)) {
311 printer->Print(
312 *vars,
313 "::grpc::Status $Method$(::grpc::ClientContext* context, "
314 "const $Request$& request, $Response$* response) GRPC_OVERRIDE;\n");
315 printer->Print(
316 *vars,
317 "std::unique_ptr< ::grpc::ClientAsyncResponseReader< $Response$>> "
318 "Async$Method$(::grpc::ClientContext* context, "
319 "const $Request$& request, "
Craig Tiller5f871ac2015-05-08 13:05:51 -0700320 "::grpc::CompletionQueue* cq) {\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700321 printer->Indent();
Craig Tillerce40de52015-06-05 07:14:58 -0700322 printer->Print(*vars,
323 "return std::unique_ptr< "
324 "::grpc::ClientAsyncResponseReader< $Response$>>("
325 "Async$Method$Raw(context, request, cq));\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700326 printer->Outdent();
327 printer->Print("}\n");
328 } else if (ClientOnlyStreaming(method)) {
329 printer->Print(
330 *vars,
331 "std::unique_ptr< ::grpc::ClientWriter< $Request$>>"
332 " $Method$("
333 "::grpc::ClientContext* context, $Response$* response) {\n");
334 printer->Indent();
Craig Tillerce40de52015-06-05 07:14:58 -0700335 printer->Print(*vars,
336 "return std::unique_ptr< ::grpc::ClientWriter< $Request$>>"
337 "($Method$Raw(context, response));\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700338 printer->Outdent();
339 printer->Print("}\n");
Craig Tillerce40de52015-06-05 07:14:58 -0700340 printer->Print(*vars,
341 "std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>>"
342 " Async$Method$(::grpc::ClientContext* context, "
343 "$Response$* response, "
344 "::grpc::CompletionQueue* cq, void* tag) {\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700345 printer->Indent();
346 printer->Print(
347 *vars,
348 "return std::unique_ptr< ::grpc::ClientAsyncWriter< $Request$>>("
349 "Async$Method$Raw(context, response, cq, tag));\n");
350 printer->Outdent();
351 printer->Print("}\n");
352 } else if (ServerOnlyStreaming(method)) {
353 printer->Print(
354 *vars,
355 "std::unique_ptr< ::grpc::ClientReader< $Response$>>"
356 " $Method$(::grpc::ClientContext* context, const $Request$& request)"
357 " {\n");
358 printer->Indent();
359 printer->Print(
360 *vars,
361 "return std::unique_ptr< ::grpc::ClientReader< $Response$>>"
362 "($Method$Raw(context, request));\n");
363 printer->Outdent();
364 printer->Print("}\n");
365 printer->Print(
366 *vars,
367 "std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>> "
368 "Async$Method$("
369 "::grpc::ClientContext* context, const $Request$& request, "
370 "::grpc::CompletionQueue* cq, void* tag) {\n");
371 printer->Indent();
372 printer->Print(
373 *vars,
374 "return std::unique_ptr< ::grpc::ClientAsyncReader< $Response$>>("
375 "Async$Method$Raw(context, request, cq, tag));\n");
376 printer->Outdent();
377 printer->Print("}\n");
378 } else if (BidiStreaming(method)) {
379 printer->Print(
380 *vars,
381 "std::unique_ptr< ::grpc::ClientReaderWriter< $Request$, $Response$>>"
382 " $Method$(::grpc::ClientContext* context) {\n");
383 printer->Indent();
Craig Tillerce40de52015-06-05 07:14:58 -0700384 printer->Print(*vars,
385 "return std::unique_ptr< "
386 "::grpc::ClientReaderWriter< $Request$, $Response$>>("
387 "$Method$Raw(context));\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700388 printer->Outdent();
389 printer->Print("}\n");
Craig Tillerce40de52015-06-05 07:14:58 -0700390 printer->Print(*vars,
391 "std::unique_ptr< ::grpc::ClientAsyncReaderWriter< "
392 "$Request$, $Response$>> "
393 "Async$Method$(::grpc::ClientContext* context, "
394 "::grpc::CompletionQueue* cq, void* tag) {\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700395 printer->Indent();
Craig Tillerce40de52015-06-05 07:14:58 -0700396 printer->Print(*vars,
397 "return std::unique_ptr< "
398 "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>>("
399 "Async$Method$Raw(context, cq, tag));\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700400 printer->Outdent();
401 printer->Print("}\n");
402 }
403 } else {
404 if (NoStreaming(method)) {
Craig Tillerce40de52015-06-05 07:14:58 -0700405 printer->Print(*vars,
406 "::grpc::ClientAsyncResponseReader< $Response$>* "
407 "Async$Method$Raw(::grpc::ClientContext* context, "
408 "const $Request$& request, "
409 "::grpc::CompletionQueue* cq) GRPC_OVERRIDE;\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700410 } else if (ClientOnlyStreaming(method)) {
Craig Tillerce40de52015-06-05 07:14:58 -0700411 printer->Print(*vars,
412 "::grpc::ClientWriter< $Request$>* $Method$Raw("
413 "::grpc::ClientContext* context, $Response$* response) "
414 "GRPC_OVERRIDE;\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700415 printer->Print(
416 *vars,
417 "::grpc::ClientAsyncWriter< $Request$>* Async$Method$Raw("
418 "::grpc::ClientContext* context, $Response$* response, "
419 "::grpc::CompletionQueue* cq, void* tag) GRPC_OVERRIDE;\n");
420 } else if (ServerOnlyStreaming(method)) {
Craig Tillerce40de52015-06-05 07:14:58 -0700421 printer->Print(*vars,
422 "::grpc::ClientReader< $Response$>* $Method$Raw("
423 "::grpc::ClientContext* context, const $Request$& request)"
424 " GRPC_OVERRIDE;\n");
Yang Gaoc6924c82015-05-05 10:42:51 -0700425 printer->Print(
426 *vars,
427 "::grpc::ClientAsyncReader< $Response$>* Async$Method$Raw("
428 "::grpc::ClientContext* context, const $Request$& request, "
429 "::grpc::CompletionQueue* cq, void* tag) GRPC_OVERRIDE;\n");
430 } else if (BidiStreaming(method)) {
431 printer->Print(
432 *vars,
433 "::grpc::ClientReaderWriter< $Request$, $Response$>* "
434 "$Method$Raw(::grpc::ClientContext* context) GRPC_OVERRIDE;\n");
435 printer->Print(
436 *vars,
437 "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>* "
438 "Async$Method$Raw(::grpc::ClientContext* context, "
439 "::grpc::CompletionQueue* cq, void* tag) GRPC_OVERRIDE;\n");
440 }
nnobleebebb7e2014-12-10 16:31:01 -0800441 }
442}
443
Craig Tillerbd6c6182015-04-10 17:08:15 -0700444void PrintHeaderClientMethodData(grpc::protobuf::io::Printer *printer,
445 const grpc::protobuf::MethodDescriptor *method,
446 std::map<grpc::string, grpc::string> *vars) {
447 (*vars)["Method"] = method->name();
448 printer->Print(*vars, "const ::grpc::RpcMethod rpcmethod_$Method$_;\n");
449}
450
Craig Tiller277d3cf2015-04-14 14:04:51 -0700451void PrintHeaderServerMethodSync(grpc::protobuf::io::Printer *printer,
452 const grpc::protobuf::MethodDescriptor *method,
453 std::map<grpc::string, grpc::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800454 (*vars)["Method"] = method->name();
455 (*vars)["Request"] =
456 grpc_cpp_generator::ClassName(method->input_type(), true);
457 (*vars)["Response"] =
458 grpc_cpp_generator::ClassName(method->output_type(), true);
459 if (NoStreaming(method)) {
460 printer->Print(*vars,
yangga4b6f5d2014-12-17 15:53:12 -0800461 "virtual ::grpc::Status $Method$("
462 "::grpc::ServerContext* context, const $Request$* request, "
nnobleebebb7e2014-12-10 16:31:01 -0800463 "$Response$* response);\n");
464 } else if (ClientOnlyStreaming(method)) {
465 printer->Print(*vars,
466 "virtual ::grpc::Status $Method$("
yangga4b6f5d2014-12-17 15:53:12 -0800467 "::grpc::ServerContext* context, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800468 "::grpc::ServerReader< $Request$>* reader, "
nnobleebebb7e2014-12-10 16:31:01 -0800469 "$Response$* response);\n");
470 } else if (ServerOnlyStreaming(method)) {
471 printer->Print(*vars,
yangga4b6f5d2014-12-17 15:53:12 -0800472 "virtual ::grpc::Status $Method$("
473 "::grpc::ServerContext* context, const $Request$* request, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800474 "::grpc::ServerWriter< $Response$>* writer);\n");
nnobleebebb7e2014-12-10 16:31:01 -0800475 } else if (BidiStreaming(method)) {
Yang Gao5680ff42015-01-14 12:14:21 -0800476 printer->Print(
477 *vars,
478 "virtual ::grpc::Status $Method$("
479 "::grpc::ServerContext* context, "
480 "::grpc::ServerReaderWriter< $Response$, $Request$>* stream);"
481 "\n");
nnobleebebb7e2014-12-10 16:31:01 -0800482 }
483}
484
Craig Tiller5ef5db12015-02-09 12:47:21 -0800485void PrintHeaderServerMethodAsync(
Nicolas Nobled446eb82015-03-12 17:22:33 -0700486 grpc::protobuf::io::Printer *printer,
487 const grpc::protobuf::MethodDescriptor *method,
488 std::map<grpc::string, grpc::string> *vars) {
Craig Tiller2dff17d2015-02-09 12:42:23 -0800489 (*vars)["Method"] = method->name();
490 (*vars)["Request"] =
491 grpc_cpp_generator::ClassName(method->input_type(), true);
492 (*vars)["Response"] =
493 grpc_cpp_generator::ClassName(method->output_type(), true);
494 if (NoStreaming(method)) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700495 printer->Print(
496 *vars,
497 "void Request$Method$("
498 "::grpc::ServerContext* context, $Request$* request, "
499 "::grpc::ServerAsyncResponseWriter< $Response$>* response, "
500 "::grpc::CompletionQueue* new_call_cq, "
501 "::grpc::ServerCompletionQueue* notification_cq, void *tag);\n");
Craig Tiller2dff17d2015-02-09 12:42:23 -0800502 } else if (ClientOnlyStreaming(method)) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700503 printer->Print(
504 *vars,
505 "void Request$Method$("
506 "::grpc::ServerContext* context, "
507 "::grpc::ServerAsyncReader< $Response$, $Request$>* reader, "
508 "::grpc::CompletionQueue* new_call_cq, "
509 "::grpc::ServerCompletionQueue* notification_cq, void *tag);\n");
Craig Tiller2dff17d2015-02-09 12:42:23 -0800510 } else if (ServerOnlyStreaming(method)) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700511 printer->Print(
512 *vars,
513 "void Request$Method$("
514 "::grpc::ServerContext* context, $Request$* request, "
515 "::grpc::ServerAsyncWriter< $Response$>* writer, "
516 "::grpc::CompletionQueue* new_call_cq, "
517 "::grpc::ServerCompletionQueue* notification_cq, void *tag);\n");
Craig Tiller2dff17d2015-02-09 12:42:23 -0800518 } else if (BidiStreaming(method)) {
519 printer->Print(
520 *vars,
Yang Gaoca3cb3e2015-02-12 00:05:11 -0800521 "void Request$Method$("
Craig Tiller2dff17d2015-02-09 12:42:23 -0800522 "::grpc::ServerContext* context, "
Craig Tiller225f7be2015-02-09 22:32:37 -0800523 "::grpc::ServerAsyncReaderWriter< $Response$, $Request$>* stream, "
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700524 "::grpc::CompletionQueue* new_call_cq, "
525 "::grpc::ServerCompletionQueue* notification_cq, void *tag);\n");
Craig Tiller2dff17d2015-02-09 12:42:23 -0800526 }
527}
528
Nicolas Nobled446eb82015-03-12 17:22:33 -0700529void PrintHeaderService(grpc::protobuf::io::Printer *printer,
530 const grpc::protobuf::ServiceDescriptor *service,
531 std::map<grpc::string, grpc::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800532 (*vars)["Service"] = service->name();
533
534 printer->Print(*vars,
Craig Tillercf133f42015-02-26 14:05:56 -0800535 "class $Service$ GRPC_FINAL {\n"
nnobleebebb7e2014-12-10 16:31:01 -0800536 " public:\n");
537 printer->Indent();
538
539 // Client side
Craig Tillerb5dcec52015-01-13 11:13:42 -0800540 printer->Print(
Yang Gao72e0fb82015-05-01 16:24:07 -0700541 "class StubInterface {\n"
Craig Tillerb5dcec52015-01-13 11:13:42 -0800542 " public:\n");
nnobleebebb7e2014-12-10 16:31:01 -0800543 printer->Indent();
Yang Gao72e0fb82015-05-01 16:24:07 -0700544 printer->Print("virtual ~StubInterface() {}\n");
545 for (int i = 0; i < service->method_count(); ++i) {
Yang Gaoc6924c82015-05-05 10:42:51 -0700546 PrintHeaderClientMethodInterfaces(printer, service->method(i), vars, true);
547 }
548 printer->Outdent();
549 printer->Print("private:\n");
550 printer->Indent();
551 for (int i = 0; i < service->method_count(); ++i) {
552 PrintHeaderClientMethodInterfaces(printer, service->method(i), vars, false);
Yang Gao72e0fb82015-05-01 16:24:07 -0700553 }
554 printer->Outdent();
555 printer->Print("};\n");
556 printer->Print(
557 "class Stub GRPC_FINAL : public StubInterface,"
558 " public ::grpc::InternalStub {\n public:\n");
559 printer->Indent();
Craig Tiller277d3cf2015-04-14 14:04:51 -0700560 printer->Print(
561 "Stub(const std::shared_ptr< ::grpc::ChannelInterface>& channel);\n");
nnobleebebb7e2014-12-10 16:31:01 -0800562 for (int i = 0; i < service->method_count(); ++i) {
Yang Gaoc6924c82015-05-05 10:42:51 -0700563 PrintHeaderClientMethod(printer, service->method(i), vars, true);
nnobleebebb7e2014-12-10 16:31:01 -0800564 }
565 printer->Outdent();
Yang Gaoc6924c82015-05-05 10:42:51 -0700566 printer->Print("\n private:\n");
Craig Tillerbd6c6182015-04-10 17:08:15 -0700567 printer->Indent();
568 for (int i = 0; i < service->method_count(); ++i) {
Yang Gaoc6924c82015-05-05 10:42:51 -0700569 PrintHeaderClientMethod(printer, service->method(i), vars, false);
570 }
571 for (int i = 0; i < service->method_count(); ++i) {
Craig Tillerbd6c6182015-04-10 17:08:15 -0700572 PrintHeaderClientMethodData(printer, service->method(i), vars);
573 }
574 printer->Outdent();
nnobleebebb7e2014-12-10 16:31:01 -0800575 printer->Print("};\n");
576 printer->Print(
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800577 "static std::unique_ptr<Stub> NewStub(const std::shared_ptr< "
yang-g297a25b2015-08-03 16:43:46 -0700578 "::grpc::ChannelInterface>& channel, "
579 "const ::grpc::StubOptions& options = ::grpc::StubOptions());\n");
nnobleebebb7e2014-12-10 16:31:01 -0800580
581 printer->Print("\n");
582
Craig Tiller2dff17d2015-02-09 12:42:23 -0800583 // Server side - Synchronous
Craig Tillerb5dcec52015-01-13 11:13:42 -0800584 printer->Print(
Craig Tiller14a65f92015-02-09 13:13:14 -0800585 "class Service : public ::grpc::SynchronousService {\n"
Craig Tillerb5dcec52015-01-13 11:13:42 -0800586 " public:\n");
nnobleebebb7e2014-12-10 16:31:01 -0800587 printer->Indent();
588 printer->Print("Service() : service_(nullptr) {}\n");
589 printer->Print("virtual ~Service();\n");
590 for (int i = 0; i < service->method_count(); ++i) {
Craig Tiller2dff17d2015-02-09 12:42:23 -0800591 PrintHeaderServerMethodSync(printer, service->method(i), vars);
592 }
Craig Tillercf133f42015-02-26 14:05:56 -0800593 printer->Print("::grpc::RpcService* service() GRPC_OVERRIDE GRPC_FINAL;\n");
Craig Tiller2dff17d2015-02-09 12:42:23 -0800594 printer->Outdent();
595 printer->Print(
596 " private:\n"
597 " ::grpc::RpcService* service_;\n");
598 printer->Print("};\n");
599
600 // Server side - Asynchronous
601 printer->Print(
Craig Tillercf133f42015-02-26 14:05:56 -0800602 "class AsyncService GRPC_FINAL : public ::grpc::AsynchronousService {\n"
Craig Tiller2dff17d2015-02-09 12:42:23 -0800603 " public:\n");
604 printer->Indent();
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800605 (*vars)["MethodCount"] = as_string(service->method_count());
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700606 printer->Print("explicit AsyncService();\n");
Craig Tiller0220cf12015-02-12 17:39:26 -0800607 printer->Print("~AsyncService() {};\n");
Craig Tiller2dff17d2015-02-09 12:42:23 -0800608 for (int i = 0; i < service->method_count(); ++i) {
609 PrintHeaderServerMethodAsync(printer, service->method(i), vars);
nnobleebebb7e2014-12-10 16:31:01 -0800610 }
nnobleebebb7e2014-12-10 16:31:01 -0800611 printer->Outdent();
nnobleebebb7e2014-12-10 16:31:01 -0800612 printer->Print("};\n");
613
614 printer->Outdent();
615 printer->Print("};\n");
616}
617
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100618grpc::string GetHeaderServices(const grpc::protobuf::FileDescriptor *file,
619 const Parameters &params) {
Nicolas Nobled446eb82015-03-12 17:22:33 -0700620 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700621 {
Craig Tillerce40de52015-06-05 07:14:58 -0700622 // Scope the output stream so it closes and finalizes output to the string.
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700623 grpc::protobuf::io::StringOutputStream output_stream(&output);
624 grpc::protobuf::io::Printer printer(&output_stream, '$');
625 std::map<grpc::string, grpc::string> vars;
nnobleebebb7e2014-12-10 16:31:01 -0800626
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700627 if (!params.services_namespace.empty()) {
628 vars["services_namespace"] = params.services_namespace;
629 printer.Print(vars, "\nnamespace $services_namespace$ {\n\n");
630 }
631
632 for (int i = 0; i < file->service_count(); ++i) {
633 PrintHeaderService(&printer, file->service(i), &vars);
634 printer.Print("\n");
635 }
636
637 if (!params.services_namespace.empty()) {
638 printer.Print(vars, "} // namespace $services_namespace$\n\n");
639 }
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100640 }
nnobleebebb7e2014-12-10 16:31:01 -0800641 return output;
642}
643
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200644grpc::string GetHeaderEpilogue(const grpc::protobuf::FileDescriptor *file,
645 const Parameters &params) {
646 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700647 {
648 // Scope the output stream so it closes and finalizes output to the string.
649 grpc::protobuf::io::StringOutputStream output_stream(&output);
650 grpc::protobuf::io::Printer printer(&output_stream, '$');
651 std::map<grpc::string, grpc::string> vars;
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200652
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700653 vars["filename"] = file->name();
654 vars["filename_identifier"] = FilenameIdentifier(file->name());
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200655
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700656 if (!file->package().empty()) {
657 std::vector<grpc::string> parts =
658 grpc_generator::tokenize(file->package(), ".");
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200659
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700660 for (auto part = parts.rbegin(); part != parts.rend(); part++) {
661 vars["part"] = *part;
662 printer.Print(vars, "} // namespace $part$\n");
663 }
664 printer.Print(vars, "\n");
Yang Gao1dc1a432015-04-10 13:53:11 -0700665 }
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700666
Yang Gao1dc1a432015-04-10 13:53:11 -0700667 printer.Print(vars, "\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700668 printer.Print(vars, "#endif // GRPC_$filename_identifier$__INCLUDED\n");
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200669 }
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200670 return output;
671}
672
673grpc::string GetSourcePrologue(const grpc::protobuf::FileDescriptor *file,
674 const Parameters &params) {
675 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700676 {
677 // Scope the output stream so it closes and finalizes output to the string.
678 grpc::protobuf::io::StringOutputStream output_stream(&output);
679 grpc::protobuf::io::Printer printer(&output_stream, '$');
680 std::map<grpc::string, grpc::string> vars;
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200681
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700682 vars["filename"] = file->name();
683 vars["filename_base"] = grpc_generator::StripProto(file->name());
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200684
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700685 printer.Print(vars, "// Generated by the gRPC protobuf plugin.\n");
Craig Tillerce40de52015-06-05 07:14:58 -0700686 printer.Print(vars,
687 "// If you make any local change, they will be lost.\n");
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700688 printer.Print(vars, "// source: $filename$\n\n");
689 printer.Print(vars, "#include \"$filename_base$.pb.h\"\n");
690 printer.Print(vars, "#include \"$filename_base$.grpc.pb.h\"\n");
691 printer.Print(vars, "\n");
692 }
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200693 return output;
694}
695
696grpc::string GetSourceIncludes(const grpc::protobuf::FileDescriptor *file,
697 const Parameters &param) {
698 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700699 {
700 // Scope the output stream so it closes and finalizes output to the string.
701 grpc::protobuf::io::StringOutputStream output_stream(&output);
702 grpc::protobuf::io::Printer printer(&output_stream, '$');
703 std::map<grpc::string, grpc::string> vars;
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200704
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700705 printer.Print(vars, "#include <grpc++/async_unary_call.h>\n");
706 printer.Print(vars, "#include <grpc++/channel_interface.h>\n");
707 printer.Print(vars, "#include <grpc++/impl/client_unary_call.h>\n");
708 printer.Print(vars, "#include <grpc++/impl/rpc_service_method.h>\n");
709 printer.Print(vars, "#include <grpc++/impl/service_type.h>\n");
710 printer.Print(vars, "#include <grpc++/stream.h>\n");
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200711
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700712 if (!file->package().empty()) {
713 std::vector<grpc::string> parts =
714 grpc_generator::tokenize(file->package(), ".");
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200715
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700716 for (auto part = parts.begin(); part != parts.end(); part++) {
717 vars["part"] = *part;
718 printer.Print(vars, "namespace $part$ {\n");
719 }
Yang Gao1dc1a432015-04-10 13:53:11 -0700720 }
Jan Tattermusch5dcebd92015-05-27 15:30:59 -0700721
722 printer.Print(vars, "\n");
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200723 }
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +0200724 return output;
725}
726
Nicolas Nobled446eb82015-03-12 17:22:33 -0700727void PrintSourceClientMethod(grpc::protobuf::io::Printer *printer,
728 const grpc::protobuf::MethodDescriptor *method,
729 std::map<grpc::string, grpc::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800730 (*vars)["Method"] = method->name();
731 (*vars)["Request"] =
732 grpc_cpp_generator::ClassName(method->input_type(), true);
733 (*vars)["Response"] =
734 grpc_cpp_generator::ClassName(method->output_type(), true);
735 if (NoStreaming(method)) {
736 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100737 "::grpc::Status $ns$$Service$::Stub::$Method$("
nnobleebebb7e2014-12-10 16:31:01 -0800738 "::grpc::ClientContext* context, "
739 "const $Request$& request, $Response$* response) {\n");
740 printer->Print(*vars,
Craig Tillerbd6c6182015-04-10 17:08:15 -0700741 " return ::grpc::BlockingUnaryCall(channel(), "
742 "rpcmethod_$Method$_, "
nnobleebebb7e2014-12-10 16:31:01 -0800743 "context, request, response);\n"
744 "}\n\n");
Yang Gao5680ff42015-01-14 12:14:21 -0800745 printer->Print(
746 *vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700747 "::grpc::ClientAsyncResponseReader< $Response$>* "
748 "$ns$$Service$::Stub::Async$Method$Raw(::grpc::ClientContext* context, "
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800749 "const $Request$& request, "
Craig Tiller3676b382015-05-06 13:01:05 -0700750 "::grpc::CompletionQueue* cq) {\n");
yangg5bcea0d2015-01-06 10:35:03 -0800751 printer->Print(*vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700752 " return new "
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800753 "::grpc::ClientAsyncResponseReader< $Response$>("
754 "channel(), cq, "
Craig Tillerbd6c6182015-04-10 17:08:15 -0700755 "rpcmethod_$Method$_, "
Craig Tiller5f871ac2015-05-08 13:05:51 -0700756 "context, request);\n"
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800757 "}\n\n");
758 } else if (ClientOnlyStreaming(method)) {
759 printer->Print(*vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700760 "::grpc::ClientWriter< $Request$>* "
761 "$ns$$Service$::Stub::$Method$Raw("
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800762 "::grpc::ClientContext* context, $Response$* response) {\n");
763 printer->Print(*vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700764 " return new ::grpc::ClientWriter< $Request$>("
765 "channel(), "
Craig Tillerbd6c6182015-04-10 17:08:15 -0700766 "rpcmethod_$Method$_, "
Yang Gaoc6924c82015-05-05 10:42:51 -0700767 "context, response);\n"
yangg5bcea0d2015-01-06 10:35:03 -0800768 "}\n\n");
Yang Gao068c85b2015-02-12 15:21:24 -0800769 printer->Print(*vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700770 "::grpc::ClientAsyncWriter< $Request$>* "
771 "$ns$$Service$::Stub::Async$Method$Raw("
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800772 "::grpc::ClientContext* context, $Response$* response, "
773 "::grpc::CompletionQueue* cq, void* tag) {\n");
774 printer->Print(*vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700775 " return new ::grpc::ClientAsyncWriter< $Request$>("
Yang Gao068c85b2015-02-12 15:21:24 -0800776 "channel(), cq, "
Craig Tillerbd6c6182015-04-10 17:08:15 -0700777 "rpcmethod_$Method$_, "
Yang Gaoc6924c82015-05-05 10:42:51 -0700778 "context, response, tag);\n"
Yang Gao068c85b2015-02-12 15:21:24 -0800779 "}\n\n");
nnobleebebb7e2014-12-10 16:31:01 -0800780 } else if (ServerOnlyStreaming(method)) {
781 printer->Print(
782 *vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700783 "::grpc::ClientReader< $Response$>* "
784 "$ns$$Service$::Stub::$Method$Raw("
Yang Gao07d83042015-02-13 14:11:31 -0800785 "::grpc::ClientContext* context, const $Request$& request) {\n");
yangg5bcea0d2015-01-06 10:35:03 -0800786 printer->Print(*vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700787 " return new ::grpc::ClientReader< $Response$>("
788 "channel(), "
Craig Tillerbd6c6182015-04-10 17:08:15 -0700789 "rpcmethod_$Method$_, "
Yang Gaoc6924c82015-05-05 10:42:51 -0700790 "context, request);\n"
yangg5bcea0d2015-01-06 10:35:03 -0800791 "}\n\n");
Yang Gao068c85b2015-02-12 15:21:24 -0800792 printer->Print(*vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700793 "::grpc::ClientAsyncReader< $Response$>* "
794 "$ns$$Service$::Stub::Async$Method$Raw("
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800795 "::grpc::ClientContext* context, const $Request$& request, "
796 "::grpc::CompletionQueue* cq, void* tag) {\n");
797 printer->Print(*vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700798 " return new ::grpc::ClientAsyncReader< $Response$>("
Yang Gao068c85b2015-02-12 15:21:24 -0800799 "channel(), cq, "
Craig Tillerbd6c6182015-04-10 17:08:15 -0700800 "rpcmethod_$Method$_, "
Yang Gaoc6924c82015-05-05 10:42:51 -0700801 "context, request, tag);\n"
Yang Gao068c85b2015-02-12 15:21:24 -0800802 "}\n\n");
nnobleebebb7e2014-12-10 16:31:01 -0800803 } else if (BidiStreaming(method)) {
804 printer->Print(
805 *vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700806 "::grpc::ClientReaderWriter< $Request$, $Response$>* "
807 "$ns$$Service$::Stub::$Method$Raw(::grpc::ClientContext* context) {\n");
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800808 printer->Print(*vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700809 " return new ::grpc::ClientReaderWriter< "
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800810 "$Request$, $Response$>("
Yang Gaoc6924c82015-05-05 10:42:51 -0700811 "channel(), "
Craig Tillerbd6c6182015-04-10 17:08:15 -0700812 "rpcmethod_$Method$_, "
Yang Gaoc6924c82015-05-05 10:42:51 -0700813 "context);\n"
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800814 "}\n\n");
Craig Tiller277d3cf2015-04-14 14:04:51 -0700815 printer->Print(
816 *vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700817 "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>* "
818 "$ns$$Service$::Stub::Async$Method$Raw(::grpc::ClientContext* context, "
Craig Tiller277d3cf2015-04-14 14:04:51 -0700819 "::grpc::CompletionQueue* cq, void* tag) {\n");
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800820 printer->Print(*vars,
Yang Gaoc6924c82015-05-05 10:42:51 -0700821 " return new "
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800822 "::grpc::ClientAsyncReaderWriter< $Request$, $Response$>("
823 "channel(), cq, "
Craig Tillerbd6c6182015-04-10 17:08:15 -0700824 "rpcmethod_$Method$_, "
Yang Gaoc6924c82015-05-05 10:42:51 -0700825 "context, tag);\n"
Craig Tillerfd1b49b2015-02-23 12:53:39 -0800826 "}\n\n");
nnobleebebb7e2014-12-10 16:31:01 -0800827 }
828}
829
Nicolas Nobled446eb82015-03-12 17:22:33 -0700830void PrintSourceServerMethod(grpc::protobuf::io::Printer *printer,
831 const grpc::protobuf::MethodDescriptor *method,
832 std::map<grpc::string, grpc::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800833 (*vars)["Method"] = method->name();
834 (*vars)["Request"] =
835 grpc_cpp_generator::ClassName(method->input_type(), true);
836 (*vars)["Response"] =
837 grpc_cpp_generator::ClassName(method->output_type(), true);
838 if (NoStreaming(method)) {
839 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100840 "::grpc::Status $ns$$Service$::Service::$Method$("
yangga4b6f5d2014-12-17 15:53:12 -0800841 "::grpc::ServerContext* context, "
nnobleebebb7e2014-12-10 16:31:01 -0800842 "const $Request$* request, $Response$* response) {\n");
Nicolas "Pixel" Nobleb14fbf72015-06-10 23:49:23 +0200843 printer->Print(" (void) context;\n");
844 printer->Print(" (void) request;\n");
845 printer->Print(" (void) response;\n");
nnobleebebb7e2014-12-10 16:31:01 -0800846 printer->Print(
847 " return ::grpc::Status("
Yang Gaoc1a2c312015-06-16 10:59:46 -0700848 "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n");
nnobleebebb7e2014-12-10 16:31:01 -0800849 printer->Print("}\n\n");
850 } else if (ClientOnlyStreaming(method)) {
851 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100852 "::grpc::Status $ns$$Service$::Service::$Method$("
yangga4b6f5d2014-12-17 15:53:12 -0800853 "::grpc::ServerContext* context, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800854 "::grpc::ServerReader< $Request$>* reader, "
nnobleebebb7e2014-12-10 16:31:01 -0800855 "$Response$* response) {\n");
Nicolas "Pixel" Nobleb14fbf72015-06-10 23:49:23 +0200856 printer->Print(" (void) context;\n");
857 printer->Print(" (void) reader;\n");
858 printer->Print(" (void) response;\n");
nnobleebebb7e2014-12-10 16:31:01 -0800859 printer->Print(
860 " return ::grpc::Status("
Yang Gaoc1a2c312015-06-16 10:59:46 -0700861 "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n");
nnobleebebb7e2014-12-10 16:31:01 -0800862 printer->Print("}\n\n");
863 } else if (ServerOnlyStreaming(method)) {
864 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100865 "::grpc::Status $ns$$Service$::Service::$Method$("
yangga4b6f5d2014-12-17 15:53:12 -0800866 "::grpc::ServerContext* context, "
nnobleebebb7e2014-12-10 16:31:01 -0800867 "const $Request$* request, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800868 "::grpc::ServerWriter< $Response$>* writer) {\n");
Nicolas "Pixel" Nobleb14fbf72015-06-10 23:49:23 +0200869 printer->Print(" (void) context;\n");
870 printer->Print(" (void) request;\n");
871 printer->Print(" (void) writer;\n");
nnobleebebb7e2014-12-10 16:31:01 -0800872 printer->Print(
873 " return ::grpc::Status("
Yang Gaoc1a2c312015-06-16 10:59:46 -0700874 "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n");
nnobleebebb7e2014-12-10 16:31:01 -0800875 printer->Print("}\n\n");
876 } else if (BidiStreaming(method)) {
877 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100878 "::grpc::Status $ns$$Service$::Service::$Method$("
yangga4b6f5d2014-12-17 15:53:12 -0800879 "::grpc::ServerContext* context, "
Yang Gao1ff11f62015-01-14 11:45:32 -0800880 "::grpc::ServerReaderWriter< $Response$, $Request$>* "
nnobleebebb7e2014-12-10 16:31:01 -0800881 "stream) {\n");
Nicolas "Pixel" Nobleb14fbf72015-06-10 23:49:23 +0200882 printer->Print(" (void) context;\n");
883 printer->Print(" (void) stream;\n");
nnobleebebb7e2014-12-10 16:31:01 -0800884 printer->Print(
885 " return ::grpc::Status("
Yang Gaoc1a2c312015-06-16 10:59:46 -0700886 "::grpc::StatusCode::UNIMPLEMENTED, \"\");\n");
nnobleebebb7e2014-12-10 16:31:01 -0800887 printer->Print("}\n\n");
888 }
889}
890
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800891void PrintSourceServerAsyncMethod(
Nicolas Nobled446eb82015-03-12 17:22:33 -0700892 grpc::protobuf::io::Printer *printer,
893 const grpc::protobuf::MethodDescriptor *method,
894 std::map<grpc::string, grpc::string> *vars) {
Craig Tiller225f7be2015-02-09 22:32:37 -0800895 (*vars)["Method"] = method->name();
896 (*vars)["Request"] =
897 grpc_cpp_generator::ClassName(method->input_type(), true);
898 (*vars)["Response"] =
899 grpc_cpp_generator::ClassName(method->output_type(), true);
900 if (NoStreaming(method)) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700901 printer->Print(
902 *vars,
903 "void $ns$$Service$::AsyncService::Request$Method$("
904 "::grpc::ServerContext* context, "
905 "$Request$* request, "
906 "::grpc::ServerAsyncResponseWriter< $Response$>* response, "
907 "::grpc::CompletionQueue* new_call_cq, "
908 "::grpc::ServerCompletionQueue* notification_cq, void *tag) {\n");
Craig Tiller277d3cf2015-04-14 14:04:51 -0700909 printer->Print(*vars,
910 " AsynchronousService::RequestAsyncUnary($Idx$, context, "
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700911 "request, response, new_call_cq, notification_cq, tag);\n");
Craig Tiller225f7be2015-02-09 22:32:37 -0800912 printer->Print("}\n\n");
913 } else if (ClientOnlyStreaming(method)) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700914 printer->Print(
915 *vars,
916 "void $ns$$Service$::AsyncService::Request$Method$("
917 "::grpc::ServerContext* context, "
918 "::grpc::ServerAsyncReader< $Response$, $Request$>* reader, "
919 "::grpc::CompletionQueue* new_call_cq, "
920 "::grpc::ServerCompletionQueue* notification_cq, void *tag) {\n");
Craig Tiller277d3cf2015-04-14 14:04:51 -0700921 printer->Print(*vars,
922 " AsynchronousService::RequestClientStreaming($Idx$, "
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700923 "context, reader, new_call_cq, notification_cq, tag);\n");
Craig Tiller225f7be2015-02-09 22:32:37 -0800924 printer->Print("}\n\n");
925 } else if (ServerOnlyStreaming(method)) {
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700926 printer->Print(
927 *vars,
928 "void $ns$$Service$::AsyncService::Request$Method$("
929 "::grpc::ServerContext* context, "
930 "$Request$* request, "
931 "::grpc::ServerAsyncWriter< $Response$>* writer, "
932 "::grpc::CompletionQueue* new_call_cq, "
933 "::grpc::ServerCompletionQueue* notification_cq, void *tag) {\n");
934 printer->Print(
935 *vars,
936 " AsynchronousService::RequestServerStreaming($Idx$, "
937 "context, request, writer, new_call_cq, notification_cq, tag);\n");
Craig Tiller225f7be2015-02-09 22:32:37 -0800938 printer->Print("}\n\n");
939 } else if (BidiStreaming(method)) {
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800940 printer->Print(
941 *vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100942 "void $ns$$Service$::AsyncService::Request$Method$("
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800943 "::grpc::ServerContext* context, "
944 "::grpc::ServerAsyncReaderWriter< $Response$, $Request$>* stream, "
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700945 "::grpc::CompletionQueue* new_call_cq, "
946 "::grpc::ServerCompletionQueue* notification_cq, void *tag) {\n");
Craig Tiller277d3cf2015-04-14 14:04:51 -0700947 printer->Print(*vars,
948 " AsynchronousService::RequestBidiStreaming($Idx$, "
Craig Tillerf9e6adf2015-05-06 11:45:59 -0700949 "context, stream, new_call_cq, notification_cq, tag);\n");
Craig Tiller225f7be2015-02-09 22:32:37 -0800950 printer->Print("}\n\n");
951 }
952}
953
Nicolas Nobled446eb82015-03-12 17:22:33 -0700954void PrintSourceService(grpc::protobuf::io::Printer *printer,
955 const grpc::protobuf::ServiceDescriptor *service,
956 std::map<grpc::string, grpc::string> *vars) {
nnobleebebb7e2014-12-10 16:31:01 -0800957 (*vars)["Service"] = service->name();
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800958
Craig Tiller277d3cf2015-04-14 14:04:51 -0700959 printer->Print(*vars,
960 "static const char* $prefix$$Service$_method_names[] = {\n");
Craig Tiller8c8d0aa2015-02-12 11:38:36 -0800961 for (int i = 0; i < service->method_count(); ++i) {
962 (*vars)["Method"] = service->method(i)->name();
963 printer->Print(*vars, " \"/$Package$$Service$/$Method$\",\n");
964 }
965 printer->Print(*vars, "};\n\n");
966
Yang Gao5680ff42015-01-14 12:14:21 -0800967 printer->Print(
968 *vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +0100969 "std::unique_ptr< $ns$$Service$::Stub> $ns$$Service$::NewStub("
yang-g297a25b2015-08-03 16:43:46 -0700970 "const std::shared_ptr< ::grpc::ChannelInterface>& channel, "
971 "const ::grpc::StubOptions& options) {\n"
Craig Tiller277d3cf2015-04-14 14:04:51 -0700972 " std::unique_ptr< $ns$$Service$::Stub> stub(new "
973 "$ns$$Service$::Stub(channel));\n"
Yang Gao5680ff42015-01-14 12:14:21 -0800974 " return stub;\n"
Todd Poynor2a6fd262015-02-26 20:04:26 -0800975 "}\n\n");
Craig Tiller277d3cf2015-04-14 14:04:51 -0700976 printer->Print(*vars,
977 "$ns$$Service$::Stub::Stub(const std::shared_ptr< "
978 "::grpc::ChannelInterface>& channel)\n");
Craig Tillerbd6c6182015-04-10 17:08:15 -0700979 printer->Indent();
Craig Tiller3beef682015-04-14 13:55:03 -0700980 printer->Print(": ::grpc::InternalStub(channel)");
Craig Tillerbd6c6182015-04-10 17:08:15 -0700981 for (int i = 0; i < service->method_count(); ++i) {
982 const grpc::protobuf::MethodDescriptor *method = service->method(i);
Craig Tillerbd6c6182015-04-10 17:08:15 -0700983 (*vars)["Method"] = method->name();
984 (*vars)["Idx"] = as_string(i);
985 if (NoStreaming(method)) {
986 (*vars)["StreamingType"] = "NORMAL_RPC";
987 } else if (ClientOnlyStreaming(method)) {
988 (*vars)["StreamingType"] = "CLIENT_STREAMING";
989 } else if (ServerOnlyStreaming(method)) {
990 (*vars)["StreamingType"] = "SERVER_STREAMING";
991 } else {
992 (*vars)["StreamingType"] = "BIDI_STREAMING";
993 }
Craig Tiller277d3cf2015-04-14 14:04:51 -0700994 printer->Print(
995 *vars,
996 ", rpcmethod_$Method$_("
997 "$prefix$$Service$_method_names[$Idx$], "
998 "::grpc::RpcMethod::$StreamingType$, "
999 "channel->RegisterMethod($prefix$$Service$_method_names[$Idx$])"
1000 ")\n");
Craig Tillerbd6c6182015-04-10 17:08:15 -07001001 }
Craig Tiller3beef682015-04-14 13:55:03 -07001002 printer->Print("{}\n\n");
Craig Tillerbd6c6182015-04-10 17:08:15 -07001003 printer->Outdent();
1004
nnobleebebb7e2014-12-10 16:31:01 -08001005 for (int i = 0; i < service->method_count(); ++i) {
Craig Tiller8c8d0aa2015-02-12 11:38:36 -08001006 (*vars)["Idx"] = as_string(i);
nnobleebebb7e2014-12-10 16:31:01 -08001007 PrintSourceClientMethod(printer, service->method(i), vars);
1008 }
1009
Craig Tiller8c8d0aa2015-02-12 11:38:36 -08001010 (*vars)["MethodCount"] = as_string(service->method_count());
Craig Tiller277d3cf2015-04-14 14:04:51 -07001011 printer->Print(*vars,
Craig Tillerf9e6adf2015-05-06 11:45:59 -07001012 "$ns$$Service$::AsyncService::AsyncService() : "
1013 "::grpc::AsynchronousService("
Craig Tiller277d3cf2015-04-14 14:04:51 -07001014 "$prefix$$Service$_method_names, $MethodCount$) "
1015 "{}\n\n");
Craig Tiller8c8d0aa2015-02-12 11:38:36 -08001016
nnobleebebb7e2014-12-10 16:31:01 -08001017 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +01001018 "$ns$$Service$::Service::~Service() {\n"
nnobleebebb7e2014-12-10 16:31:01 -08001019 " delete service_;\n"
1020 "}\n\n");
1021 for (int i = 0; i < service->method_count(); ++i) {
Craig Tiller1c9a2a92015-02-12 14:10:25 -08001022 (*vars)["Idx"] = as_string(i);
nnobleebebb7e2014-12-10 16:31:01 -08001023 PrintSourceServerMethod(printer, service->method(i), vars);
Craig Tiller225f7be2015-02-09 22:32:37 -08001024 PrintSourceServerAsyncMethod(printer, service->method(i), vars);
nnobleebebb7e2014-12-10 16:31:01 -08001025 }
1026 printer->Print(*vars,
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +01001027 "::grpc::RpcService* $ns$$Service$::Service::service() {\n");
nnobleebebb7e2014-12-10 16:31:01 -08001028 printer->Indent();
Craig Tillerb5dcec52015-01-13 11:13:42 -08001029 printer->Print(
1030 "if (service_ != nullptr) {\n"
1031 " return service_;\n"
1032 "}\n");
nnobleebebb7e2014-12-10 16:31:01 -08001033 printer->Print("service_ = new ::grpc::RpcService();\n");
1034 for (int i = 0; i < service->method_count(); ++i) {
Nicolas Nobled446eb82015-03-12 17:22:33 -07001035 const grpc::protobuf::MethodDescriptor *method = service->method(i);
Craig Tiller8c8d0aa2015-02-12 11:38:36 -08001036 (*vars)["Idx"] = as_string(i);
nnobleebebb7e2014-12-10 16:31:01 -08001037 (*vars)["Method"] = method->name();
1038 (*vars)["Request"] =
1039 grpc_cpp_generator::ClassName(method->input_type(), true);
1040 (*vars)["Response"] =
1041 grpc_cpp_generator::ClassName(method->output_type(), true);
1042 if (NoStreaming(method)) {
1043 printer->Print(
1044 *vars,
1045 "service_->AddMethod(new ::grpc::RpcServiceMethod(\n"
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +01001046 " $prefix$$Service$_method_names[$Idx$],\n"
yangg5bcea0d2015-01-06 10:35:03 -08001047 " ::grpc::RpcMethod::NORMAL_RPC,\n"
Craig Tiller277d3cf2015-04-14 14:04:51 -07001048 " new ::grpc::RpcMethodHandler< $ns$$Service$::Service, "
1049 "$Request$, "
nnobleebebb7e2014-12-10 16:31:01 -08001050 "$Response$>(\n"
Craig Tiller50a7a682015-06-04 12:53:40 -07001051 " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n");
nnobleebebb7e2014-12-10 16:31:01 -08001052 } else if (ClientOnlyStreaming(method)) {
1053 printer->Print(
1054 *vars,
1055 "service_->AddMethod(new ::grpc::RpcServiceMethod(\n"
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +01001056 " $prefix$$Service$_method_names[$Idx$],\n"
yangg5bcea0d2015-01-06 10:35:03 -08001057 " ::grpc::RpcMethod::CLIENT_STREAMING,\n"
Yang Gao1ff11f62015-01-14 11:45:32 -08001058 " new ::grpc::ClientStreamingHandler< "
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +01001059 "$ns$$Service$::Service, $Request$, $Response$>(\n"
Craig Tiller50a7a682015-06-04 12:53:40 -07001060 " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n");
nnobleebebb7e2014-12-10 16:31:01 -08001061 } else if (ServerOnlyStreaming(method)) {
1062 printer->Print(
1063 *vars,
1064 "service_->AddMethod(new ::grpc::RpcServiceMethod(\n"
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +01001065 " $prefix$$Service$_method_names[$Idx$],\n"
yangg5bcea0d2015-01-06 10:35:03 -08001066 " ::grpc::RpcMethod::SERVER_STREAMING,\n"
Yang Gao1ff11f62015-01-14 11:45:32 -08001067 " new ::grpc::ServerStreamingHandler< "
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +01001068 "$ns$$Service$::Service, $Request$, $Response$>(\n"
Craig Tiller50a7a682015-06-04 12:53:40 -07001069 " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n");
nnobleebebb7e2014-12-10 16:31:01 -08001070 } else if (BidiStreaming(method)) {
1071 printer->Print(
1072 *vars,
1073 "service_->AddMethod(new ::grpc::RpcServiceMethod(\n"
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +01001074 " $prefix$$Service$_method_names[$Idx$],\n"
yangg5bcea0d2015-01-06 10:35:03 -08001075 " ::grpc::RpcMethod::BIDI_STREAMING,\n"
Yang Gao1ff11f62015-01-14 11:45:32 -08001076 " new ::grpc::BidiStreamingHandler< "
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +01001077 "$ns$$Service$::Service, $Request$, $Response$>(\n"
Craig Tiller50a7a682015-06-04 12:53:40 -07001078 " std::mem_fn(&$ns$$Service$::Service::$Method$), this)));\n");
nnobleebebb7e2014-12-10 16:31:01 -08001079 }
1080 }
1081 printer->Print("return service_;\n");
1082 printer->Outdent();
1083 printer->Print("}\n\n");
1084}
1085
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +01001086grpc::string GetSourceServices(const grpc::protobuf::FileDescriptor *file,
1087 const Parameters &params) {
Nicolas Nobled446eb82015-03-12 17:22:33 -07001088 grpc::string output;
Jan Tattermusch5dcebd92015-05-27 15:30:59 -07001089 {
1090 // Scope the output stream so it closes and finalizes output to the string.
1091 grpc::protobuf::io::StringOutputStream output_stream(&output);
1092 grpc::protobuf::io::Printer printer(&output_stream, '$');
1093 std::map<grpc::string, grpc::string> vars;
1094 // Package string is empty or ends with a dot. It is used to fully qualify
1095 // method names.
1096 vars["Package"] = file->package();
1097 if (!file->package().empty()) {
1098 vars["Package"].append(".");
1099 }
1100 if (!params.services_namespace.empty()) {
1101 vars["ns"] = params.services_namespace + "::";
1102 vars["prefix"] = params.services_namespace;
1103 } else {
1104 vars["ns"] = "";
1105 vars["prefix"] = "";
1106 }
nnobleebebb7e2014-12-10 16:31:01 -08001107
Jan Tattermusch5dcebd92015-05-27 15:30:59 -07001108 for (int i = 0; i < file->service_count(); ++i) {
1109 PrintSourceService(&printer, file->service(i), &vars);
1110 printer.Print("\n");
1111 }
nnobleebebb7e2014-12-10 16:31:01 -08001112 }
1113 return output;
1114}
1115
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +02001116grpc::string GetSourceEpilogue(const grpc::protobuf::FileDescriptor *file,
1117 const Parameters &params) {
1118 grpc::string temp;
1119
Yang Gao1dc1a432015-04-10 13:53:11 -07001120 if (!file->package().empty()) {
1121 std::vector<grpc::string> parts =
1122 grpc_generator::tokenize(file->package(), ".");
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +02001123
Yang Gao1dc1a432015-04-10 13:53:11 -07001124 for (auto part = parts.begin(); part != parts.end(); part++) {
1125 temp.append("} // namespace ");
1126 temp.append(*part);
1127 temp.append("\n");
1128 }
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +02001129 temp.append("\n");
1130 }
1131
Nicolas "Pixel" Noble0caebbf2015-04-09 23:08:51 +02001132 return temp;
1133}
1134
Craig Tiller190d3602015-02-18 09:23:38 -08001135} // namespace grpc_cpp_generator