| 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/service.h> |
| 36 | |
| 37 | #include <google/protobuf/io/printer.h> |
| 38 | #include <google/protobuf/stubs/strutil.h> |
| 39 | #include <google/protobuf/compiler/cpp/helpers.h> |
| 40 | |
| 41 | namespace google { |
| 42 | namespace protobuf { |
| 43 | namespace compiler { |
| 44 | namespace cpp { |
| 45 | |
| 46 | namespace { |
| 47 | |
| 48 | void InitMethodVariables(const MethodDescriptor* method, const Options& options, |
| 49 | Formatter* format) { |
| 50 | format->Set(key: "name" , value: method->name()); |
| 51 | format->Set(key: "input_type" , value: QualifiedClassName(d: method->input_type(), options)); |
| 52 | format->Set(key: "output_type" , |
| 53 | value: QualifiedClassName(d: method->output_type(), options)); |
| 54 | } |
| 55 | |
| 56 | } // namespace |
| 57 | |
| 58 | ServiceGenerator::ServiceGenerator( |
| 59 | const ServiceDescriptor* descriptor, |
| 60 | const std::map<std::string, std::string>& vars, const Options& options) |
| 61 | : descriptor_(descriptor), vars_(vars), options_(options) { |
| 62 | vars_["classname" ] = descriptor_->name(); |
| 63 | vars_["full_name" ] = descriptor_->full_name(); |
| 64 | } |
| 65 | |
| 66 | ServiceGenerator::~ServiceGenerator() {} |
| 67 | |
| 68 | void ServiceGenerator::GenerateDeclarations(io::Printer* printer) { |
| 69 | Formatter format(printer, vars_); |
| 70 | // Forward-declare the stub type. |
| 71 | format( |
| 72 | "class $classname$_Stub;\n" |
| 73 | "\n" ); |
| 74 | |
| 75 | GenerateInterface(printer); |
| 76 | GenerateStubDefinition(printer); |
| 77 | } |
| 78 | |
| 79 | void ServiceGenerator::GenerateInterface(io::Printer* printer) { |
| 80 | Formatter format(printer, vars_); |
| 81 | format( |
| 82 | "class $dllexport_decl $$classname$ : public ::$proto_ns$::Service {\n" |
| 83 | " protected:\n" |
| 84 | " // This class should be treated as an abstract interface.\n" |
| 85 | " inline $classname$() {};\n" |
| 86 | " public:\n" |
| 87 | " virtual ~$classname$();\n" ); |
| 88 | printer->Indent(); |
| 89 | |
| 90 | format( |
| 91 | "\n" |
| 92 | "typedef $classname$_Stub Stub;\n" |
| 93 | "\n" |
| 94 | "static const ::$proto_ns$::ServiceDescriptor* descriptor();\n" |
| 95 | "\n" ); |
| 96 | |
| 97 | GenerateMethodSignatures(virtual_or_non: VIRTUAL, printer); |
| 98 | |
| 99 | format( |
| 100 | "\n" |
| 101 | "// implements Service ----------------------------------------------\n" |
| 102 | "\n" |
| 103 | "const ::$proto_ns$::ServiceDescriptor* GetDescriptor();\n" |
| 104 | "void CallMethod(const ::$proto_ns$::MethodDescriptor* method,\n" |
| 105 | " ::$proto_ns$::RpcController* controller,\n" |
| 106 | " const ::$proto_ns$::Message* request,\n" |
| 107 | " ::$proto_ns$::Message* response,\n" |
| 108 | " ::google::protobuf::Closure* done);\n" |
| 109 | "const ::$proto_ns$::Message& GetRequestPrototype(\n" |
| 110 | " const ::$proto_ns$::MethodDescriptor* method) const;\n" |
| 111 | "const ::$proto_ns$::Message& GetResponsePrototype(\n" |
| 112 | " const ::$proto_ns$::MethodDescriptor* method) const;\n" ); |
| 113 | |
| 114 | printer->Outdent(); |
| 115 | format( |
| 116 | "\n" |
| 117 | " private:\n" |
| 118 | " GOOGLE_DISALLOW_EVIL_CONSTRUCTORS($classname$);\n" |
| 119 | "};\n" |
| 120 | "\n" ); |
| 121 | } |
| 122 | |
| 123 | void ServiceGenerator::GenerateStubDefinition(io::Printer* printer) { |
| 124 | Formatter format(printer, vars_); |
| 125 | format( |
| 126 | "class $dllexport_decl $$classname$_Stub : public $classname$ {\n" |
| 127 | " public:\n" ); |
| 128 | |
| 129 | printer->Indent(); |
| 130 | |
| 131 | format( |
| 132 | "$classname$_Stub(::$proto_ns$::RpcChannel* channel);\n" |
| 133 | "$classname$_Stub(::$proto_ns$::RpcChannel* channel,\n" |
| 134 | " ::$proto_ns$::Service::ChannelOwnership ownership);\n" |
| 135 | "~$classname$_Stub();\n" |
| 136 | "\n" |
| 137 | "inline ::$proto_ns$::RpcChannel* channel() { return channel_; }\n" |
| 138 | "\n" |
| 139 | "// implements $classname$ ------------------------------------------\n" |
| 140 | "\n" ); |
| 141 | |
| 142 | GenerateMethodSignatures(virtual_or_non: NON_VIRTUAL, printer); |
| 143 | |
| 144 | printer->Outdent(); |
| 145 | format( |
| 146 | " private:\n" |
| 147 | " ::$proto_ns$::RpcChannel* channel_;\n" |
| 148 | " bool owns_channel_;\n" |
| 149 | " GOOGLE_DISALLOW_EVIL_CONSTRUCTORS($classname$_Stub);\n" |
| 150 | "};\n" |
| 151 | "\n" ); |
| 152 | } |
| 153 | |
| 154 | void ServiceGenerator::GenerateMethodSignatures(VirtualOrNon virtual_or_non, |
| 155 | io::Printer* printer) { |
| 156 | for (int i = 0; i < descriptor_->method_count(); i++) { |
| 157 | const MethodDescriptor* method = descriptor_->method(index: i); |
| 158 | Formatter format(printer, vars_); |
| 159 | InitMethodVariables(method, options: options_, format: &format); |
| 160 | format.Set(key: "virtual" , value: virtual_or_non == VIRTUAL ? "virtual " : "" ); |
| 161 | format( |
| 162 | "$virtual$void $name$(::$proto_ns$::RpcController* controller,\n" |
| 163 | " const $input_type$* request,\n" |
| 164 | " $output_type$* response,\n" |
| 165 | " ::google::protobuf::Closure* done);\n" ); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // =================================================================== |
| 170 | |
| 171 | void ServiceGenerator::GenerateImplementation(io::Printer* printer) { |
| 172 | Formatter format(printer, vars_); |
| 173 | format( |
| 174 | "$classname$::~$classname$() {}\n" |
| 175 | "\n" |
| 176 | "const ::$proto_ns$::ServiceDescriptor* $classname$::descriptor() {\n" |
| 177 | " " |
| 178 | "::$proto_ns$::internal::AssignDescriptors(&$desc_table$);\n" |
| 179 | " return $file_level_service_descriptors$[$1$];\n" |
| 180 | "}\n" |
| 181 | "\n" |
| 182 | "const ::$proto_ns$::ServiceDescriptor* $classname$::GetDescriptor() {\n" |
| 183 | " return descriptor();\n" |
| 184 | "}\n" |
| 185 | "\n" , |
| 186 | index_in_metadata_); |
| 187 | |
| 188 | // Generate methods of the interface. |
| 189 | GenerateNotImplementedMethods(printer); |
| 190 | GenerateCallMethod(printer); |
| 191 | GenerateGetPrototype(which: REQUEST, printer); |
| 192 | GenerateGetPrototype(which: RESPONSE, printer); |
| 193 | |
| 194 | // Generate stub implementation. |
| 195 | format( |
| 196 | "$classname$_Stub::$classname$_Stub(::$proto_ns$::RpcChannel* channel)\n" |
| 197 | " : channel_(channel), owns_channel_(false) {}\n" |
| 198 | "$classname$_Stub::$classname$_Stub(\n" |
| 199 | " ::$proto_ns$::RpcChannel* channel,\n" |
| 200 | " ::$proto_ns$::Service::ChannelOwnership ownership)\n" |
| 201 | " : channel_(channel),\n" |
| 202 | " owns_channel_(ownership == " |
| 203 | "::$proto_ns$::Service::STUB_OWNS_CHANNEL) " |
| 204 | "{}\n" |
| 205 | "$classname$_Stub::~$classname$_Stub() {\n" |
| 206 | " if (owns_channel_) delete channel_;\n" |
| 207 | "}\n" |
| 208 | "\n" ); |
| 209 | |
| 210 | GenerateStubMethods(printer); |
| 211 | } |
| 212 | |
| 213 | void ServiceGenerator::GenerateNotImplementedMethods(io::Printer* printer) { |
| 214 | for (int i = 0; i < descriptor_->method_count(); i++) { |
| 215 | const MethodDescriptor* method = descriptor_->method(index: i); |
| 216 | Formatter format(printer, vars_); |
| 217 | InitMethodVariables(method, options: options_, format: &format); |
| 218 | format( |
| 219 | "void $classname$::$name$(::$proto_ns$::RpcController* controller,\n" |
| 220 | " const $input_type$*,\n" |
| 221 | " $output_type$*,\n" |
| 222 | " ::google::protobuf::Closure* done) {\n" |
| 223 | " controller->SetFailed(\"Method $name$() not implemented.\");\n" |
| 224 | " done->Run();\n" |
| 225 | "}\n" |
| 226 | "\n" ); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | void ServiceGenerator::GenerateCallMethod(io::Printer* printer) { |
| 231 | Formatter format(printer, vars_); |
| 232 | format( |
| 233 | "void $classname$::CallMethod(const ::$proto_ns$::MethodDescriptor* " |
| 234 | "method,\n" |
| 235 | " ::$proto_ns$::RpcController* controller,\n" |
| 236 | " const ::$proto_ns$::Message* request,\n" |
| 237 | " ::$proto_ns$::Message* response,\n" |
| 238 | " ::google::protobuf::Closure* done) {\n" |
| 239 | " GOOGLE_DCHECK_EQ(method->service(), $file_level_service_descriptors$[$1$]);\n" |
| 240 | " switch(method->index()) {\n" , |
| 241 | index_in_metadata_); |
| 242 | |
| 243 | for (int i = 0; i < descriptor_->method_count(); i++) { |
| 244 | const MethodDescriptor* method = descriptor_->method(index: i); |
| 245 | Formatter format_method(printer, vars_); |
| 246 | InitMethodVariables(method, options: options_, format: &format_method); |
| 247 | |
| 248 | // Note: down_cast does not work here because it only works on pointers, |
| 249 | // not references. |
| 250 | format_method( |
| 251 | " case $1$:\n" |
| 252 | " $name$(controller,\n" |
| 253 | " ::$proto_ns$::internal::DownCast<const $input_type$*>(\n" |
| 254 | " request),\n" |
| 255 | " ::$proto_ns$::internal::DownCast<$output_type$*>(\n" |
| 256 | " response),\n" |
| 257 | " done);\n" |
| 258 | " break;\n" , |
| 259 | i); |
| 260 | } |
| 261 | |
| 262 | format( |
| 263 | " default:\n" |
| 264 | " GOOGLE_LOG(FATAL) << \"Bad method index; this should never happen.\";\n" |
| 265 | " break;\n" |
| 266 | " }\n" |
| 267 | "}\n" |
| 268 | "\n" ); |
| 269 | } |
| 270 | |
| 271 | void ServiceGenerator::GenerateGetPrototype(RequestOrResponse which, |
| 272 | io::Printer* printer) { |
| 273 | Formatter format(printer, vars_); |
| 274 | if (which == REQUEST) { |
| 275 | format("const ::$proto_ns$::Message& $classname$::GetRequestPrototype(\n" ); |
| 276 | } else { |
| 277 | format("const ::$proto_ns$::Message& $classname$::GetResponsePrototype(\n" ); |
| 278 | } |
| 279 | |
| 280 | format( |
| 281 | " const ::$proto_ns$::MethodDescriptor* method) const {\n" |
| 282 | " GOOGLE_DCHECK_EQ(method->service(), descriptor());\n" |
| 283 | " switch(method->index()) {\n" ); |
| 284 | |
| 285 | for (int i = 0; i < descriptor_->method_count(); i++) { |
| 286 | const MethodDescriptor* method = descriptor_->method(index: i); |
| 287 | const Descriptor* type = |
| 288 | (which == REQUEST) ? method->input_type() : method->output_type(); |
| 289 | |
| 290 | format( |
| 291 | " case $1$:\n" |
| 292 | " return $2$::default_instance();\n" , |
| 293 | i, QualifiedClassName(d: type, options: options_)); |
| 294 | } |
| 295 | |
| 296 | format( |
| 297 | " default:\n" |
| 298 | " GOOGLE_LOG(FATAL) << \"Bad method index; this should never happen.\";\n" |
| 299 | " return *::$proto_ns$::MessageFactory::generated_factory()\n" |
| 300 | " ->GetPrototype(method->$1$_type());\n" |
| 301 | " }\n" |
| 302 | "}\n" |
| 303 | "\n" , |
| 304 | which == REQUEST ? "input" : "output" ); |
| 305 | } |
| 306 | |
| 307 | void ServiceGenerator::GenerateStubMethods(io::Printer* printer) { |
| 308 | for (int i = 0; i < descriptor_->method_count(); i++) { |
| 309 | const MethodDescriptor* method = descriptor_->method(index: i); |
| 310 | Formatter format(printer, vars_); |
| 311 | InitMethodVariables(method, options: options_, format: &format); |
| 312 | format( |
| 313 | "void $classname$_Stub::$name$(::$proto_ns$::RpcController* " |
| 314 | "controller,\n" |
| 315 | " const $input_type$* request,\n" |
| 316 | " $output_type$* response,\n" |
| 317 | " ::google::protobuf::Closure* done) {\n" |
| 318 | " channel_->CallMethod(descriptor()->method($1$),\n" |
| 319 | " controller, request, response, done);\n" |
| 320 | "}\n" , |
| 321 | i); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | } // namespace cpp |
| 326 | } // namespace compiler |
| 327 | } // namespace protobuf |
| 328 | } // namespace google |
| 329 | |