| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 " bool owns_channel_;\n" | 136 " bool owns_channel_;\n" |
| 137 " GOOGLE_DISALLOW_EVIL_CONSTRUCTORS($classname$_Stub);\n" | 137 " GOOGLE_DISALLOW_EVIL_CONSTRUCTORS($classname$_Stub);\n" |
| 138 "};\n" | 138 "};\n" |
| 139 "\n"); | 139 "\n"); |
| 140 } | 140 } |
| 141 | 141 |
| 142 void ServiceGenerator::GenerateMethodSignatures( | 142 void ServiceGenerator::GenerateMethodSignatures( |
| 143 VirtualOrNon virtual_or_non, io::Printer* printer) { | 143 VirtualOrNon virtual_or_non, io::Printer* printer) { |
| 144 for (int i = 0; i < descriptor_->method_count(); i++) { | 144 for (int i = 0; i < descriptor_->method_count(); i++) { |
| 145 const MethodDescriptor* method = descriptor_->method(i); | 145 const MethodDescriptor* method = descriptor_->method(i); |
| 146 std::map<string, string> sub_vars; | 146 map<string, string> sub_vars; |
| 147 sub_vars["name"] = method->name(); | 147 sub_vars["name"] = method->name(); |
| 148 sub_vars["input_type"] = ClassName(method->input_type(), true); | 148 sub_vars["input_type"] = ClassName(method->input_type(), true); |
| 149 sub_vars["output_type"] = ClassName(method->output_type(), true); | 149 sub_vars["output_type"] = ClassName(method->output_type(), true); |
| 150 sub_vars["virtual"] = virtual_or_non == VIRTUAL ? "virtual " : ""; | 150 sub_vars["virtual"] = virtual_or_non == VIRTUAL ? "virtual " : ""; |
| 151 | 151 |
| 152 printer->Print(sub_vars, | 152 printer->Print(sub_vars, |
| 153 "$virtual$void $name$(::google::protobuf::RpcController* controller,\n" | 153 "$virtual$void $name$(::google::protobuf::RpcController* controller,\n" |
| 154 " const $input_type$* request,\n" | 154 " const $input_type$* request,\n" |
| 155 " $output_type$* response,\n" | 155 " $output_type$* response,\n" |
| 156 " ::google::protobuf::Closure* done);\n"); | 156 " ::google::protobuf::Closure* done);\n"); |
| 157 } | 157 } |
| 158 } | 158 } |
| 159 | 159 |
| 160 // =================================================================== | 160 // =================================================================== |
| 161 | 161 |
| 162 void ServiceGenerator::GenerateDescriptorInitializer( | 162 void ServiceGenerator::GenerateDescriptorInitializer( |
| 163 io::Printer* printer, int index) { | 163 io::Printer* printer, int index) { |
| 164 std::map<string, string> vars; | 164 map<string, string> vars; |
| 165 vars["classname"] = descriptor_->name(); | 165 vars["classname"] = descriptor_->name(); |
| 166 vars["index"] = SimpleItoa(index); | 166 vars["index"] = SimpleItoa(index); |
| 167 | 167 |
| 168 printer->Print(vars, | 168 printer->Print(vars, |
| 169 "$classname$_descriptor_ = file->service($index$);\n"); | 169 "$classname$_descriptor_ = file->service($index$);\n"); |
| 170 } | 170 } |
| 171 | 171 |
| 172 // =================================================================== | 172 // =================================================================== |
| 173 | 173 |
| 174 void ServiceGenerator::GenerateImplementation(io::Printer* printer) { | 174 void ServiceGenerator::GenerateImplementation(io::Printer* printer) { |
| 175 vars_["index"] = SimpleItoa(index_in_metadata_); | 175 printer->Print(vars_, |
| 176 printer->Print( | 176 "$classname$::~$classname$() {}\n" |
| 177 vars_, | 177 "\n" |
| 178 "$classname$::~$classname$() {}\n" | 178 "const ::google::protobuf::ServiceDescriptor* $classname$::descriptor() {\n" |
| 179 "\n" | 179 " protobuf_AssignDescriptorsOnce();\n" |
| 180 "const ::google::protobuf::ServiceDescriptor* $classname$::descriptor() {\
n" | 180 " return $classname$_descriptor_;\n" |
| 181 " protobuf_AssignDescriptorsOnce();\n" | 181 "}\n" |
| 182 " return file_level_service_descriptors[$index$];\n" | 182 "\n" |
| 183 "}\n" | 183 "const ::google::protobuf::ServiceDescriptor* $classname$::GetDescriptor() {
\n" |
| 184 "\n" | 184 " protobuf_AssignDescriptorsOnce();\n" |
| 185 "const ::google::protobuf::ServiceDescriptor* $classname$::GetDescriptor()
{\n" | 185 " return $classname$_descriptor_;\n" |
| 186 " return descriptor();\n" | 186 "}\n" |
| 187 "}\n" | 187 "\n"); |
| 188 "\n"); | |
| 189 | 188 |
| 190 // Generate methods of the interface. | 189 // Generate methods of the interface. |
| 191 GenerateNotImplementedMethods(printer); | 190 GenerateNotImplementedMethods(printer); |
| 192 GenerateCallMethod(printer); | 191 GenerateCallMethod(printer); |
| 193 GenerateGetPrototype(REQUEST, printer); | 192 GenerateGetPrototype(REQUEST, printer); |
| 194 GenerateGetPrototype(RESPONSE, printer); | 193 GenerateGetPrototype(RESPONSE, printer); |
| 195 | 194 |
| 196 // Generate stub implementation. | 195 // Generate stub implementation. |
| 197 printer->Print(vars_, | 196 printer->Print(vars_, |
| 198 "$classname$_Stub::$classname$_Stub(::google::protobuf::RpcChannel* channel)
\n" | 197 "$classname$_Stub::$classname$_Stub(::google::protobuf::RpcChannel* channel)
\n" |
| 199 " : channel_(channel), owns_channel_(false) {}\n" | 198 " : channel_(channel), owns_channel_(false) {}\n" |
| 200 "$classname$_Stub::$classname$_Stub(\n" | 199 "$classname$_Stub::$classname$_Stub(\n" |
| 201 " ::google::protobuf::RpcChannel* channel,\n" | 200 " ::google::protobuf::RpcChannel* channel,\n" |
| 202 " ::google::protobuf::Service::ChannelOwnership ownership)\n" | 201 " ::google::protobuf::Service::ChannelOwnership ownership)\n" |
| 203 " : channel_(channel),\n" | 202 " : channel_(channel),\n" |
| 204 " owns_channel_(ownership == ::google::protobuf::Service::STUB_OWNS_CHANN
EL) {}\n" | 203 " owns_channel_(ownership == ::google::protobuf::Service::STUB_OWNS_CHANN
EL) {}\n" |
| 205 "$classname$_Stub::~$classname$_Stub() {\n" | 204 "$classname$_Stub::~$classname$_Stub() {\n" |
| 206 " if (owns_channel_) delete channel_;\n" | 205 " if (owns_channel_) delete channel_;\n" |
| 207 "}\n" | 206 "}\n" |
| 208 "\n"); | 207 "\n"); |
| 209 | 208 |
| 210 GenerateStubMethods(printer); | 209 GenerateStubMethods(printer); |
| 211 } | 210 } |
| 212 | 211 |
| 213 void ServiceGenerator::GenerateNotImplementedMethods(io::Printer* printer) { | 212 void ServiceGenerator::GenerateNotImplementedMethods(io::Printer* printer) { |
| 214 for (int i = 0; i < descriptor_->method_count(); i++) { | 213 for (int i = 0; i < descriptor_->method_count(); i++) { |
| 215 const MethodDescriptor* method = descriptor_->method(i); | 214 const MethodDescriptor* method = descriptor_->method(i); |
| 216 std::map<string, string> sub_vars; | 215 map<string, string> sub_vars; |
| 217 sub_vars["classname"] = descriptor_->name(); | 216 sub_vars["classname"] = descriptor_->name(); |
| 218 sub_vars["name"] = method->name(); | 217 sub_vars["name"] = method->name(); |
| 219 sub_vars["index"] = SimpleItoa(i); | 218 sub_vars["index"] = SimpleItoa(i); |
| 220 sub_vars["input_type"] = ClassName(method->input_type(), true); | 219 sub_vars["input_type"] = ClassName(method->input_type(), true); |
| 221 sub_vars["output_type"] = ClassName(method->output_type(), true); | 220 sub_vars["output_type"] = ClassName(method->output_type(), true); |
| 222 | 221 |
| 223 printer->Print(sub_vars, | 222 printer->Print(sub_vars, |
| 224 "void $classname$::$name$(::google::protobuf::RpcController* controller,\n
" | 223 "void $classname$::$name$(::google::protobuf::RpcController* controller,\n
" |
| 225 " const $input_type$*,\n" | 224 " const $input_type$*,\n" |
| 226 " $output_type$*,\n" | 225 " $output_type$*,\n" |
| 227 " ::google::protobuf::Closure* done) {\n" | 226 " ::google::protobuf::Closure* done) {\n" |
| 228 " controller->SetFailed(\"Method $name$() not implemented.\");\n" | 227 " controller->SetFailed(\"Method $name$() not implemented.\");\n" |
| 229 " done->Run();\n" | 228 " done->Run();\n" |
| 230 "}\n" | 229 "}\n" |
| 231 "\n"); | 230 "\n"); |
| 232 } | 231 } |
| 233 } | 232 } |
| 234 | 233 |
| 235 void ServiceGenerator::GenerateCallMethod(io::Printer* printer) { | 234 void ServiceGenerator::GenerateCallMethod(io::Printer* printer) { |
| 236 printer->Print( | 235 printer->Print(vars_, |
| 237 vars_, | 236 "void $classname$::CallMethod(const ::google::protobuf::MethodDescriptor* me
thod,\n" |
| 238 "void $classname$::CallMethod(const ::google::protobuf::MethodDescriptor*
method,\n" | 237 " ::google::protobuf::RpcController* controller,
\n" |
| 239 " ::google::protobuf::RpcController* controlle
r,\n" | 238 " const ::google::protobuf::Message* request,\n" |
| 240 " const ::google::protobuf::Message* request,\
n" | 239 " ::google::protobuf::Message* response,\n" |
| 241 " ::google::protobuf::Message* response,\n" | 240 " ::google::protobuf::Closure* done) {\n" |
| 242 " ::google::protobuf::Closure* done) {\n" | 241 " GOOGLE_DCHECK_EQ(method->service(), $classname$_descriptor_);\n" |
| 243 " GOOGLE_DCHECK_EQ(method->service(), " | 242 " switch(method->index()) {\n"); |
| 244 "file_level_service_descriptors[$index$]);\n" | |
| 245 " switch(method->index()) {\n"); | |
| 246 | 243 |
| 247 for (int i = 0; i < descriptor_->method_count(); i++) { | 244 for (int i = 0; i < descriptor_->method_count(); i++) { |
| 248 const MethodDescriptor* method = descriptor_->method(i); | 245 const MethodDescriptor* method = descriptor_->method(i); |
| 249 std::map<string, string> sub_vars; | 246 map<string, string> sub_vars; |
| 250 sub_vars["name"] = method->name(); | 247 sub_vars["name"] = method->name(); |
| 251 sub_vars["index"] = SimpleItoa(i); | 248 sub_vars["index"] = SimpleItoa(i); |
| 252 sub_vars["input_type"] = ClassName(method->input_type(), true); | 249 sub_vars["input_type"] = ClassName(method->input_type(), true); |
| 253 sub_vars["output_type"] = ClassName(method->output_type(), true); | 250 sub_vars["output_type"] = ClassName(method->output_type(), true); |
| 254 | 251 |
| 255 // Note: down_cast does not work here because it only works on pointers, | 252 // Note: down_cast does not work here because it only works on pointers, |
| 256 // not references. | 253 // not references. |
| 257 printer->Print(sub_vars, | 254 printer->Print(sub_vars, |
| 258 " case $index$:\n" | 255 " case $index$:\n" |
| 259 " $name$(controller,\n" | 256 " $name$(controller,\n" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 285 printer->Print(vars_, | 282 printer->Print(vars_, |
| 286 " const ::google::protobuf::MethodDescriptor* method) const {\n" | 283 " const ::google::protobuf::MethodDescriptor* method) const {\n" |
| 287 " GOOGLE_DCHECK_EQ(method->service(), descriptor());\n" | 284 " GOOGLE_DCHECK_EQ(method->service(), descriptor());\n" |
| 288 " switch(method->index()) {\n"); | 285 " switch(method->index()) {\n"); |
| 289 | 286 |
| 290 for (int i = 0; i < descriptor_->method_count(); i++) { | 287 for (int i = 0; i < descriptor_->method_count(); i++) { |
| 291 const MethodDescriptor* method = descriptor_->method(i); | 288 const MethodDescriptor* method = descriptor_->method(i); |
| 292 const Descriptor* type = | 289 const Descriptor* type = |
| 293 (which == REQUEST) ? method->input_type() : method->output_type(); | 290 (which == REQUEST) ? method->input_type() : method->output_type(); |
| 294 | 291 |
| 295 std::map<string, string> sub_vars; | 292 map<string, string> sub_vars; |
| 296 sub_vars["index"] = SimpleItoa(i); | 293 sub_vars["index"] = SimpleItoa(i); |
| 297 sub_vars["type"] = ClassName(type, true); | 294 sub_vars["type"] = ClassName(type, true); |
| 298 | 295 |
| 299 printer->Print(sub_vars, | 296 printer->Print(sub_vars, |
| 300 " case $index$:\n" | 297 " case $index$:\n" |
| 301 " return $type$::default_instance();\n"); | 298 " return $type$::default_instance();\n"); |
| 302 } | 299 } |
| 303 | 300 |
| 304 printer->Print( | 301 printer->Print(vars_, |
| 305 " default:\n" | 302 " default:\n" |
| 306 " GOOGLE_LOG(FATAL) << \"Bad method index; this should never happen.\";
\n" | 303 " GOOGLE_LOG(FATAL) << \"Bad method index; this should never happen.\";
\n" |
| 307 " return *::google::protobuf::MessageFactory::generated_factory()\n" | 304 " return *static_cast< ::google::protobuf::Message*>(NULL);\n" |
| 308 " ->GetPrototype(method->$input_or_output$_type());\n" | |
| 309 " }\n" | 305 " }\n" |
| 310 "}\n" | 306 "}\n" |
| 311 "\n", | 307 "\n"); |
| 312 "input_or_output", which == REQUEST ? "input" : "output"); | |
| 313 } | 308 } |
| 314 | 309 |
| 315 void ServiceGenerator::GenerateStubMethods(io::Printer* printer) { | 310 void ServiceGenerator::GenerateStubMethods(io::Printer* printer) { |
| 316 for (int i = 0; i < descriptor_->method_count(); i++) { | 311 for (int i = 0; i < descriptor_->method_count(); i++) { |
| 317 const MethodDescriptor* method = descriptor_->method(i); | 312 const MethodDescriptor* method = descriptor_->method(i); |
| 318 std::map<string, string> sub_vars; | 313 map<string, string> sub_vars; |
| 319 sub_vars["classname"] = descriptor_->name(); | 314 sub_vars["classname"] = descriptor_->name(); |
| 320 sub_vars["name"] = method->name(); | 315 sub_vars["name"] = method->name(); |
| 321 sub_vars["index"] = SimpleItoa(i); | 316 sub_vars["index"] = SimpleItoa(i); |
| 322 sub_vars["input_type"] = ClassName(method->input_type(), true); | 317 sub_vars["input_type"] = ClassName(method->input_type(), true); |
| 323 sub_vars["output_type"] = ClassName(method->output_type(), true); | 318 sub_vars["output_type"] = ClassName(method->output_type(), true); |
| 324 | 319 |
| 325 printer->Print(sub_vars, | 320 printer->Print(sub_vars, |
| 326 "void $classname$_Stub::$name$(::google::protobuf::RpcController* controll
er,\n" | 321 "void $classname$_Stub::$name$(::google::protobuf::RpcController* controll
er,\n" |
| 327 " const $input_type$* request,\n" | 322 " const $input_type$* request,\n" |
| 328 " $output_type$* response,\n" | 323 " $output_type$* response,\n" |
| 329 " ::google::protobuf::Closure* done) {\n" | 324 " ::google::protobuf::Closure* done) {\n" |
| 330 " channel_->CallMethod(descriptor()->method($index$),\n" | 325 " channel_->CallMethod(descriptor()->method($index$),\n" |
| 331 " controller, request, response, done);\n" | 326 " controller, request, response, done);\n" |
| 332 "}\n"); | 327 "}\n"); |
| 333 } | 328 } |
| 334 } | 329 } |
| 335 | 330 |
| 336 } // namespace cpp | 331 } // namespace cpp |
| 337 } // namespace compiler | 332 } // namespace compiler |
| 338 } // namespace protobuf | 333 } // namespace protobuf |
| 339 } // namespace google | 334 } // namespace google |
| OLD | NEW |