• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // Author: kenton@google.com (Kenton Varda)
32 //  Based on original Protocol Buffers design by
33 //  Sanjay Ghemawat, Jeff Dean, and others.
34 
35 #include <google/protobuf/compiler/cpp/cpp_service.h>
36 #include <google/protobuf/compiler/cpp/cpp_helpers.h>
37 #include <google/protobuf/io/printer.h>
38 #include <google/protobuf/stubs/strutil.h>
39 
40 namespace google {
41 namespace protobuf {
42 namespace compiler {
43 namespace cpp {
44 
45 namespace {
46 
InitMethodVariables(const MethodDescriptor * method,const Options & options,Formatter * format)47 void InitMethodVariables(const MethodDescriptor* method, const Options& options,
48                          Formatter* format) {
49   format->Set("name", method->name());
50   format->Set("input_type", QualifiedClassName(method->input_type(), options));
51   format->Set("output_type",
52               QualifiedClassName(method->output_type(), options));
53 }
54 
55 }  // namespace
56 
ServiceGenerator(const ServiceDescriptor * descriptor,const std::map<std::string,std::string> & vars,const Options & options)57 ServiceGenerator::ServiceGenerator(
58     const ServiceDescriptor* descriptor,
59     const std::map<std::string, std::string>& vars, const Options& options)
60     : descriptor_(descriptor), vars_(vars), options_(options) {
61   vars_["classname"] = descriptor_->name();
62   vars_["full_name"] = descriptor_->full_name();
63 }
64 
~ServiceGenerator()65 ServiceGenerator::~ServiceGenerator() {}
66 
GenerateDeclarations(io::Printer * printer)67 void ServiceGenerator::GenerateDeclarations(io::Printer* printer) {
68   Formatter format(printer, vars_);
69   // Forward-declare the stub type.
70   format(
71       "class $classname$_Stub;\n"
72       "\n");
73 
74   GenerateInterface(printer);
75   GenerateStubDefinition(printer);
76 }
77 
GenerateInterface(io::Printer * printer)78 void ServiceGenerator::GenerateInterface(io::Printer* printer) {
79   Formatter format(printer, vars_);
80   format(
81       "class $dllexport_decl $$classname$ : public ::$proto_ns$::Service {\n"
82       " protected:\n"
83       "  // This class should be treated as an abstract interface.\n"
84       "  inline $classname$() {};\n"
85       " public:\n"
86       "  virtual ~$classname$();\n");
87   printer->Indent();
88 
89   format(
90       "\n"
91       "typedef $classname$_Stub Stub;\n"
92       "\n"
93       "static const ::$proto_ns$::ServiceDescriptor* descriptor();\n"
94       "\n");
95 
96   GenerateMethodSignatures(VIRTUAL, printer);
97 
98   format(
99       "\n"
100       "// implements Service ----------------------------------------------\n"
101       "\n"
102       "const ::$proto_ns$::ServiceDescriptor* GetDescriptor();\n"
103       "void CallMethod(const ::$proto_ns$::MethodDescriptor* method,\n"
104       "                ::$proto_ns$::RpcController* controller,\n"
105       "                const ::$proto_ns$::Message* request,\n"
106       "                ::$proto_ns$::Message* response,\n"
107       "                ::google::protobuf::Closure* done);\n"
108       "const ::$proto_ns$::Message& GetRequestPrototype(\n"
109       "  const ::$proto_ns$::MethodDescriptor* method) const;\n"
110       "const ::$proto_ns$::Message& GetResponsePrototype(\n"
111       "  const ::$proto_ns$::MethodDescriptor* method) const;\n");
112 
113   printer->Outdent();
114   format(
115       "\n"
116       " private:\n"
117       "  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS($classname$);\n"
118       "};\n"
119       "\n");
120 }
121 
GenerateStubDefinition(io::Printer * printer)122 void ServiceGenerator::GenerateStubDefinition(io::Printer* printer) {
123   Formatter format(printer, vars_);
124   format(
125       "class $dllexport_decl $$classname$_Stub : public $classname$ {\n"
126       " public:\n");
127 
128   printer->Indent();
129 
130   format(
131       "$classname$_Stub(::$proto_ns$::RpcChannel* channel);\n"
132       "$classname$_Stub(::$proto_ns$::RpcChannel* channel,\n"
133       "                 ::$proto_ns$::Service::ChannelOwnership ownership);\n"
134       "~$classname$_Stub();\n"
135       "\n"
136       "inline ::$proto_ns$::RpcChannel* channel() { return channel_; }\n"
137       "\n"
138       "// implements $classname$ ------------------------------------------\n"
139       "\n");
140 
141   GenerateMethodSignatures(NON_VIRTUAL, printer);
142 
143   printer->Outdent();
144   format(
145       " private:\n"
146       "  ::$proto_ns$::RpcChannel* channel_;\n"
147       "  bool owns_channel_;\n"
148       "  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS($classname$_Stub);\n"
149       "};\n"
150       "\n");
151 }
152 
GenerateMethodSignatures(VirtualOrNon virtual_or_non,io::Printer * printer)153 void ServiceGenerator::GenerateMethodSignatures(VirtualOrNon virtual_or_non,
154                                                 io::Printer* printer) {
155   for (int i = 0; i < descriptor_->method_count(); i++) {
156     const MethodDescriptor* method = descriptor_->method(i);
157     Formatter format(printer, vars_);
158     InitMethodVariables(method, options_, &format);
159     format.Set("virtual", virtual_or_non == VIRTUAL ? "virtual " : "");
160     format(
161         "$virtual$void $name$(::$proto_ns$::RpcController* controller,\n"
162         "                     const $input_type$* request,\n"
163         "                     $output_type$* response,\n"
164         "                     ::google::protobuf::Closure* done);\n");
165   }
166 }
167 
168 // ===================================================================
169 
GenerateImplementation(io::Printer * printer)170 void ServiceGenerator::GenerateImplementation(io::Printer* printer) {
171   Formatter format(printer, vars_);
172   format(
173       "$classname$::~$classname$() {}\n"
174       "\n"
175       "const ::$proto_ns$::ServiceDescriptor* $classname$::descriptor() {\n"
176       "  "
177       "::$proto_ns$::internal::AssignDescriptors(&$desc_table$);\n"
178       "  return $file_level_service_descriptors$[$1$];\n"
179       "}\n"
180       "\n"
181       "const ::$proto_ns$::ServiceDescriptor* $classname$::GetDescriptor() {\n"
182       "  return descriptor();\n"
183       "}\n"
184       "\n",
185       index_in_metadata_);
186 
187   // Generate methods of the interface.
188   GenerateNotImplementedMethods(printer);
189   GenerateCallMethod(printer);
190   GenerateGetPrototype(REQUEST, printer);
191   GenerateGetPrototype(RESPONSE, printer);
192 
193   // Generate stub implementation.
194   format(
195       "$classname$_Stub::$classname$_Stub(::$proto_ns$::RpcChannel* channel)\n"
196       "  : channel_(channel), owns_channel_(false) {}\n"
197       "$classname$_Stub::$classname$_Stub(\n"
198       "    ::$proto_ns$::RpcChannel* channel,\n"
199       "    ::$proto_ns$::Service::ChannelOwnership ownership)\n"
200       "  : channel_(channel),\n"
201       "    owns_channel_(ownership == "
202       "::$proto_ns$::Service::STUB_OWNS_CHANNEL) "
203       "{}\n"
204       "$classname$_Stub::~$classname$_Stub() {\n"
205       "  if (owns_channel_) delete channel_;\n"
206       "}\n"
207       "\n");
208 
209   GenerateStubMethods(printer);
210 }
211 
GenerateNotImplementedMethods(io::Printer * printer)212 void ServiceGenerator::GenerateNotImplementedMethods(io::Printer* printer) {
213   for (int i = 0; i < descriptor_->method_count(); i++) {
214     const MethodDescriptor* method = descriptor_->method(i);
215     Formatter format(printer, vars_);
216     InitMethodVariables(method, options_, &format);
217     format(
218         "void $classname$::$name$(::$proto_ns$::RpcController* controller,\n"
219         "                         const $input_type$*,\n"
220         "                         $output_type$*,\n"
221         "                         ::google::protobuf::Closure* done) {\n"
222         "  controller->SetFailed(\"Method $name$() not implemented.\");\n"
223         "  done->Run();\n"
224         "}\n"
225         "\n");
226   }
227 }
228 
GenerateCallMethod(io::Printer * printer)229 void ServiceGenerator::GenerateCallMethod(io::Printer* printer) {
230   Formatter format(printer, vars_);
231   format(
232       "void $classname$::CallMethod(const ::$proto_ns$::MethodDescriptor* "
233       "method,\n"
234       "                             ::$proto_ns$::RpcController* controller,\n"
235       "                             const ::$proto_ns$::Message* request,\n"
236       "                             ::$proto_ns$::Message* response,\n"
237       "                             ::google::protobuf::Closure* done) {\n"
238       "  GOOGLE_DCHECK_EQ(method->service(), $file_level_service_descriptors$[$1$]);\n"
239       "  switch(method->index()) {\n",
240       index_in_metadata_);
241 
242   for (int i = 0; i < descriptor_->method_count(); i++) {
243     const MethodDescriptor* method = descriptor_->method(i);
244     Formatter format(printer, vars_);
245     InitMethodVariables(method, options_, &format);
246 
247     // Note:  down_cast does not work here because it only works on pointers,
248     //   not references.
249     format(
250         "    case $1$:\n"
251         "      $name$(controller,\n"
252         "             ::$proto_ns$::internal::DownCast<const $input_type$*>(\n"
253         "                 request),\n"
254         "             ::$proto_ns$::internal::DownCast<$output_type$*>(\n"
255         "                 response),\n"
256         "             done);\n"
257         "      break;\n",
258         i);
259   }
260 
261   format(
262       "    default:\n"
263       "      GOOGLE_LOG(FATAL) << \"Bad method index; this should never happen.\";\n"
264       "      break;\n"
265       "  }\n"
266       "}\n"
267       "\n");
268 }
269 
GenerateGetPrototype(RequestOrResponse which,io::Printer * printer)270 void ServiceGenerator::GenerateGetPrototype(RequestOrResponse which,
271                                             io::Printer* printer) {
272   Formatter format(printer, vars_);
273   if (which == REQUEST) {
274     format("const ::$proto_ns$::Message& $classname$::GetRequestPrototype(\n");
275   } else {
276     format("const ::$proto_ns$::Message& $classname$::GetResponsePrototype(\n");
277   }
278 
279   format(
280       "    const ::$proto_ns$::MethodDescriptor* method) const {\n"
281       "  GOOGLE_DCHECK_EQ(method->service(), descriptor());\n"
282       "  switch(method->index()) {\n");
283 
284   for (int i = 0; i < descriptor_->method_count(); i++) {
285     const MethodDescriptor* method = descriptor_->method(i);
286     const Descriptor* type =
287         (which == REQUEST) ? method->input_type() : method->output_type();
288 
289     format(
290         "    case $1$:\n"
291         "      return $2$::default_instance();\n",
292         i, QualifiedClassName(type, options_));
293   }
294 
295   format(
296       "    default:\n"
297       "      GOOGLE_LOG(FATAL) << \"Bad method index; this should never happen.\";\n"
298       "      return *::$proto_ns$::MessageFactory::generated_factory()\n"
299       "          ->GetPrototype(method->$1$_type());\n"
300       "  }\n"
301       "}\n"
302       "\n",
303       which == REQUEST ? "input" : "output");
304 }
305 
GenerateStubMethods(io::Printer * printer)306 void ServiceGenerator::GenerateStubMethods(io::Printer* printer) {
307   for (int i = 0; i < descriptor_->method_count(); i++) {
308     const MethodDescriptor* method = descriptor_->method(i);
309     Formatter format(printer, vars_);
310     InitMethodVariables(method, options_, &format);
311     format(
312         "void $classname$_Stub::$name$(::$proto_ns$::RpcController* "
313         "controller,\n"
314         "                              const $input_type$* request,\n"
315         "                              $output_type$* response,\n"
316         "                              ::google::protobuf::Closure* done) {\n"
317         "  channel_->CallMethod(descriptor()->method($1$),\n"
318         "                       controller, request, response, done);\n"
319         "}\n",
320         i);
321   }
322 }
323 
324 }  // namespace cpp
325 }  // namespace compiler
326 }  // namespace protobuf
327 }  // namespace google
328