protobuf/src/google/protobuf/compiler/cpp/cpp_service.cc
Go to the documentation of this file.
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 
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 
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 
66 
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 
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 
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 
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 
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 
153 void ServiceGenerator::GenerateMethodSignatures(VirtualOrNon virtual_or_non,
154  io::Printer* printer) {
155  for (int i = 0; i < descriptor_->method_count(); 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 
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",
186 
187  // Generate methods of the interface.
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 
213  for (int i = 0; i < descriptor_->method_count(); 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 
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",
241 
242  for (int i = 0; i < descriptor_->method_count(); i++) {
244  Formatter format_method(printer, vars_);
245  InitMethodVariables(method, options_, &format_method);
246 
247  // Note: down_cast does not work here because it only works on pointers,
248  // not references.
249  format_method(
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 
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++) {
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",
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 
307  for (int i = 0; i < descriptor_->method_count(); 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
google::protobuf::compiler::cpp::ServiceGenerator::NON_VIRTUAL
@ NON_VIRTUAL
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_service.h:78
descriptor_
string_view descriptor_
Definition: elf.cc:154
http2_test_server.format
format
Definition: http2_test_server.py:118
google::protobuf::compiler::cpp::ServiceGenerator::GenerateGetPrototype
void GenerateGetPrototype(RequestOrResponse which, io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_service.cc:270
options
double_dict options[]
Definition: capstone_test.c:55
google::protobuf::ServiceDescriptor::full_name
const std::string & full_name() const
grpc::protobuf::io::Printer
GRPC_CUSTOM_PRINTER Printer
Definition: src/compiler/config.h:54
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
google::protobuf::compiler::cpp::QualifiedClassName
std::string QualifiedClassName(const Descriptor *d, const Options &options)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_helpers.cc:320
google::protobuf::compiler::cpp::ServiceGenerator::GenerateDeclarations
void GenerateDeclarations(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_service.cc:67
google::protobuf::ServiceDescriptor::name
const std::string & name() const
Descriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:121
google::protobuf::compiler::cpp::ServiceGenerator::GenerateInterface
void GenerateInterface(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_service.cc:78
google::protobuf::compiler::cpp::ServiceGenerator::ServiceGenerator
ServiceGenerator(const ServiceDescriptor *descriptor, const std::map< std::string, std::string > &vars, const Options &options)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_service.cc:57
google::protobuf::EnumValueDescriptor::name
const std::string & name() const
google::protobuf::compiler::cpp::ServiceGenerator::VIRTUAL
@ VIRTUAL
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_service.h:78
google::protobuf::compiler::cpp::ServiceGenerator::RESPONSE
@ RESPONSE
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_service.h:77
grpc::protobuf::MethodDescriptor
GRPC_CUSTOM_METHODDESCRIPTOR MethodDescriptor
Definition: include/grpcpp/impl/codegen/config_protobuf.h:87
google::protobuf::compiler::cpp::ServiceGenerator::GenerateCallMethod
void GenerateCallMethod(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_service.cc:229
python_utils.jobset.which
def which(filename)
Definition: jobset.py:157
google::protobuf::compiler::cpp::ServiceGenerator::GenerateMethodSignatures
void GenerateMethodSignatures(VirtualOrNon virtual_or_non, io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_service.cc:153
google::protobuf::compiler::cpp::ServiceGenerator::GenerateStubDefinition
void GenerateStubDefinition(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_service.cc:122
cpp
Definition: third_party/bloaty/third_party/googletest/googlemock/scripts/generator/cpp/__init__.py:1
google::protobuf::compiler::cpp::ServiceGenerator::index_in_metadata_
int index_in_metadata_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_service.h:111
google::protobuf::compiler::cpp::ServiceGenerator::vars_
std::map< std::string, std::string > vars_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_service.h:108
options_
DebugStringOptions options_
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.cc:2390
google::protobuf::compiler::cpp::ServiceGenerator::~ServiceGenerator
~ServiceGenerator()
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_service.cc:65
google::protobuf::ServiceDescriptor::method_count
int method_count() const
google::protobuf::compiler::cpp::ServiceGenerator::REQUEST
@ REQUEST
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_service.h:77
google::protobuf::compiler::cpp::ServiceGenerator::options_
const Options & options_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_service.h:109
google::protobuf::compiler::cpp::ServiceGenerator::GenerateImplementation
void GenerateImplementation(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_service.cc:170
google::protobuf::compiler::cpp::ServiceGenerator::GenerateNotImplementedMethods
void GenerateNotImplementedMethods(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_service.cc:212
google::protobuf::ServiceDescriptor::method
const MethodDescriptor * method(int index) const
google::protobuf::compiler::cpp::ServiceGenerator::descriptor_
const ServiceDescriptor * descriptor_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_service.h:107
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
google::protobuf::compiler::cpp::ServiceGenerator::GenerateStubMethods
void GenerateStubMethods(io::Printer *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/cpp/cpp_service.cc:306
descriptor
static const char descriptor[1336]
Definition: certs.upbdefs.c:16
grpc::protobuf::ServiceDescriptor
GRPC_CUSTOM_SERVICEDESCRIPTOR ServiceDescriptor
Definition: include/grpcpp/impl/codegen/config_protobuf.h:88
compiler
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/plugin.pb.cc:21
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
google::protobuf::method
const Descriptor::ReservedRange const EnumValueDescriptor method
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1973
framework.helpers.highlighter.Formatter
Formatter
Definition: highlighter.py:53


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:06