| 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 map<string, string> sub_vars; | 146 std::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 map<string, string> vars; | 164 std::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 printer->Print(vars_, | 175 vars_["index"] = SimpleItoa(index_in_metadata_); |
| 176 "$classname$::~$classname$() {}\n" | 176 printer->Print( |
| 177 "\n" | 177 vars_, |
| 178 "const ::google::protobuf::ServiceDescriptor* $classname$::descriptor() {\n" | 178 "$classname$::~$classname$() {}\n" |
| 179 " protobuf_AssignDescriptorsOnce();\n" | 179 "\n" |
| 180 " return $classname$_descriptor_;\n" | 180 "const ::google::protobuf::ServiceDescriptor* $classname$::descriptor() {\
n" |
| 181 "}\n" | 181 " protobuf_AssignDescriptorsOnce();\n" |
| 182 "\n" | 182 " return file_level_service_descriptors[$index$];\n" |
| 183 "const ::google::protobuf::ServiceDescriptor* $classname$::GetDescriptor() {
\n" | 183 "}\n" |
| 184 " protobuf_AssignDescriptorsOnce();\n" | 184 "\n" |
| 185 " return $classname$_descriptor_;\n" | 185 "const ::google::protobuf::ServiceDescriptor* $classname$::GetDescriptor()
{\n" |
| 186 "}\n" | 186 " return descriptor();\n" |
| 187 "\n"); | 187 "}\n" |
| 188 "\n"); |
| 188 | 189 |
| 189 // Generate methods of the interface. | 190 // Generate methods of the interface. |
| 190 GenerateNotImplementedMethods(printer); | 191 GenerateNotImplementedMethods(printer); |
| 191 GenerateCallMethod(printer); | 192 GenerateCallMethod(printer); |
| 192 GenerateGetPrototype(REQUEST, printer); | 193 GenerateGetPrototype(REQUEST, printer); |
| 193 GenerateGetPrototype(RESPONSE, printer); | 194 GenerateGetPrototype(RESPONSE, printer); |
| 194 | 195 |
| 195 // Generate stub implementation. | 196 // Generate stub implementation. |
| 196 printer->Print(vars_, | 197 printer->Print(vars_, |
| 197 "$classname$_Stub::$classname$_Stub(::google::protobuf::RpcChannel* channel)
\n" | 198 "$classname$_Stub::$classname$_Stub(::google::protobuf::RpcChannel* channel)
\n" |
| 198 " : channel_(channel), owns_channel_(false) {}\n" | 199 " : channel_(channel), owns_channel_(false) {}\n" |
| 199 "$classname$_Stub::$classname$_Stub(\n" | 200 "$classname$_Stub::$classname$_Stub(\n" |
| 200 " ::google::protobuf::RpcChannel* channel,\n" | 201 " ::google::protobuf::RpcChannel* channel,\n" |
| 201 " ::google::protobuf::Service::ChannelOwnership ownership)\n" | 202 " ::google::protobuf::Service::ChannelOwnership ownership)\n" |
| 202 " : channel_(channel),\n" | 203 " : channel_(channel),\n" |
| 203 " owns_channel_(ownership == ::google::protobuf::Service::STUB_OWNS_CHANN
EL) {}\n" | 204 " owns_channel_(ownership == ::google::protobuf::Service::STUB_OWNS_CHANN
EL) {}\n" |
| 204 "$classname$_Stub::~$classname$_Stub() {\n" | 205 "$classname$_Stub::~$classname$_Stub() {\n" |
| 205 " if (owns_channel_) delete channel_;\n" | 206 " if (owns_channel_) delete channel_;\n" |
| 206 "}\n" | 207 "}\n" |
| 207 "\n"); | 208 "\n"); |
| 208 | 209 |
| 209 GenerateStubMethods(printer); | 210 GenerateStubMethods(printer); |
| 210 } | 211 } |
| 211 | 212 |
| 212 void ServiceGenerator::GenerateNotImplementedMethods(io::Printer* printer) { | 213 void ServiceGenerator::GenerateNotImplementedMethods(io::Printer* printer) { |
| 213 for (int i = 0; i < descriptor_->method_count(); i++) { | 214 for (int i = 0; i < descriptor_->method_count(); i++) { |
| 214 const MethodDescriptor* method = descriptor_->method(i); | 215 const MethodDescriptor* method = descriptor_->method(i); |
| 215 map<string, string> sub_vars; | 216 std::map<string, string> sub_vars; |
| 216 sub_vars["classname"] = descriptor_->name(); | 217 sub_vars["classname"] = descriptor_->name(); |
| 217 sub_vars["name"] = method->name(); | 218 sub_vars["name"] = method->name(); |
| 218 sub_vars["index"] = SimpleItoa(i); | 219 sub_vars["index"] = SimpleItoa(i); |
| 219 sub_vars["input_type"] = ClassName(method->input_type(), true); | 220 sub_vars["input_type"] = ClassName(method->input_type(), true); |
| 220 sub_vars["output_type"] = ClassName(method->output_type(), true); | 221 sub_vars["output_type"] = ClassName(method->output_type(), true); |
| 221 | 222 |
| 222 printer->Print(sub_vars, | 223 printer->Print(sub_vars, |
| 223 "void $classname$::$name$(::google::protobuf::RpcController* controller,\n
" | 224 "void $classname$::$name$(::google::protobuf::RpcController* controller,\n
" |
| 224 " const $input_type$*,\n" | 225 " const $input_type$*,\n" |
| 225 " $output_type$*,\n" | 226 " $output_type$*,\n" |
| 226 " ::google::protobuf::Closure* done) {\n" | 227 " ::google::protobuf::Closure* done) {\n" |
| 227 " controller->SetFailed(\"Method $name$() not implemented.\");\n" | 228 " controller->SetFailed(\"Method $name$() not implemented.\");\n" |
| 228 " done->Run();\n" | 229 " done->Run();\n" |
| 229 "}\n" | 230 "}\n" |
| 230 "\n"); | 231 "\n"); |
| 231 } | 232 } |
| 232 } | 233 } |
| 233 | 234 |
| 234 void ServiceGenerator::GenerateCallMethod(io::Printer* printer) { | 235 void ServiceGenerator::GenerateCallMethod(io::Printer* printer) { |
| 235 printer->Print(vars_, | 236 printer->Print( |
| 236 "void $classname$::CallMethod(const ::google::protobuf::MethodDescriptor* me
thod,\n" | 237 vars_, |
| 237 " ::google::protobuf::RpcController* controller,
\n" | 238 "void $classname$::CallMethod(const ::google::protobuf::MethodDescriptor*
method,\n" |
| 238 " const ::google::protobuf::Message* request,\n" | 239 " ::google::protobuf::RpcController* controlle
r,\n" |
| 239 " ::google::protobuf::Message* response,\n" | 240 " const ::google::protobuf::Message* request,\
n" |
| 240 " ::google::protobuf::Closure* done) {\n" | 241 " ::google::protobuf::Message* response,\n" |
| 241 " GOOGLE_DCHECK_EQ(method->service(), $classname$_descriptor_);\n" | 242 " ::google::protobuf::Closure* done) {\n" |
| 242 " switch(method->index()) {\n"); | 243 " GOOGLE_DCHECK_EQ(method->service(), " |
| 244 "file_level_service_descriptors[$index$]);\n" |
| 245 " switch(method->index()) {\n"); |
| 243 | 246 |
| 244 for (int i = 0; i < descriptor_->method_count(); i++) { | 247 for (int i = 0; i < descriptor_->method_count(); i++) { |
| 245 const MethodDescriptor* method = descriptor_->method(i); | 248 const MethodDescriptor* method = descriptor_->method(i); |
| 246 map<string, string> sub_vars; | 249 std::map<string, string> sub_vars; |
| 247 sub_vars["name"] = method->name(); | 250 sub_vars["name"] = method->name(); |
| 248 sub_vars["index"] = SimpleItoa(i); | 251 sub_vars["index"] = SimpleItoa(i); |
| 249 sub_vars["input_type"] = ClassName(method->input_type(), true); | 252 sub_vars["input_type"] = ClassName(method->input_type(), true); |
| 250 sub_vars["output_type"] = ClassName(method->output_type(), true); | 253 sub_vars["output_type"] = ClassName(method->output_type(), true); |
| 251 | 254 |
| 252 // Note: down_cast does not work here because it only works on pointers, | 255 // Note: down_cast does not work here because it only works on pointers, |
| 253 // not references. | 256 // not references. |
| 254 printer->Print(sub_vars, | 257 printer->Print(sub_vars, |
| 255 " case $index$:\n" | 258 " case $index$:\n" |
| 256 " $name$(controller,\n" | 259 " $name$(controller,\n" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 282 printer->Print(vars_, | 285 printer->Print(vars_, |
| 283 " const ::google::protobuf::MethodDescriptor* method) const {\n" | 286 " const ::google::protobuf::MethodDescriptor* method) const {\n" |
| 284 " GOOGLE_DCHECK_EQ(method->service(), descriptor());\n" | 287 " GOOGLE_DCHECK_EQ(method->service(), descriptor());\n" |
| 285 " switch(method->index()) {\n"); | 288 " switch(method->index()) {\n"); |
| 286 | 289 |
| 287 for (int i = 0; i < descriptor_->method_count(); i++) { | 290 for (int i = 0; i < descriptor_->method_count(); i++) { |
| 288 const MethodDescriptor* method = descriptor_->method(i); | 291 const MethodDescriptor* method = descriptor_->method(i); |
| 289 const Descriptor* type = | 292 const Descriptor* type = |
| 290 (which == REQUEST) ? method->input_type() : method->output_type(); | 293 (which == REQUEST) ? method->input_type() : method->output_type(); |
| 291 | 294 |
| 292 map<string, string> sub_vars; | 295 std::map<string, string> sub_vars; |
| 293 sub_vars["index"] = SimpleItoa(i); | 296 sub_vars["index"] = SimpleItoa(i); |
| 294 sub_vars["type"] = ClassName(type, true); | 297 sub_vars["type"] = ClassName(type, true); |
| 295 | 298 |
| 296 printer->Print(sub_vars, | 299 printer->Print(sub_vars, |
| 297 " case $index$:\n" | 300 " case $index$:\n" |
| 298 " return $type$::default_instance();\n"); | 301 " return $type$::default_instance();\n"); |
| 299 } | 302 } |
| 300 | 303 |
| 301 printer->Print(vars_, | 304 printer->Print( |
| 302 " default:\n" | 305 " default:\n" |
| 303 " GOOGLE_LOG(FATAL) << \"Bad method index; this should never happen.\";
\n" | 306 " GOOGLE_LOG(FATAL) << \"Bad method index; this should never happen.\";
\n" |
| 304 " return *static_cast< ::google::protobuf::Message*>(NULL);\n" | 307 " return *::google::protobuf::MessageFactory::generated_factory()\n" |
| 308 " ->GetPrototype(method->$input_or_output$_type());\n" |
| 305 " }\n" | 309 " }\n" |
| 306 "}\n" | 310 "}\n" |
| 307 "\n"); | 311 "\n", |
| 312 "input_or_output", which == REQUEST ? "input" : "output"); |
| 308 } | 313 } |
| 309 | 314 |
| 310 void ServiceGenerator::GenerateStubMethods(io::Printer* printer) { | 315 void ServiceGenerator::GenerateStubMethods(io::Printer* printer) { |
| 311 for (int i = 0; i < descriptor_->method_count(); i++) { | 316 for (int i = 0; i < descriptor_->method_count(); i++) { |
| 312 const MethodDescriptor* method = descriptor_->method(i); | 317 const MethodDescriptor* method = descriptor_->method(i); |
| 313 map<string, string> sub_vars; | 318 std::map<string, string> sub_vars; |
| 314 sub_vars["classname"] = descriptor_->name(); | 319 sub_vars["classname"] = descriptor_->name(); |
| 315 sub_vars["name"] = method->name(); | 320 sub_vars["name"] = method->name(); |
| 316 sub_vars["index"] = SimpleItoa(i); | 321 sub_vars["index"] = SimpleItoa(i); |
| 317 sub_vars["input_type"] = ClassName(method->input_type(), true); | 322 sub_vars["input_type"] = ClassName(method->input_type(), true); |
| 318 sub_vars["output_type"] = ClassName(method->output_type(), true); | 323 sub_vars["output_type"] = ClassName(method->output_type(), true); |
| 319 | 324 |
| 320 printer->Print(sub_vars, | 325 printer->Print(sub_vars, |
| 321 "void $classname$_Stub::$name$(::google::protobuf::RpcController* controll
er,\n" | 326 "void $classname$_Stub::$name$(::google::protobuf::RpcController* controll
er,\n" |
| 322 " const $input_type$* request,\n" | 327 " const $input_type$* request,\n" |
| 323 " $output_type$* response,\n" | 328 " $output_type$* response,\n" |
| 324 " ::google::protobuf::Closure* done) {\n" | 329 " ::google::protobuf::Closure* done) {\n" |
| 325 " channel_->CallMethod(descriptor()->method($index$),\n" | 330 " channel_->CallMethod(descriptor()->method($index$),\n" |
| 326 " controller, request, response, done);\n" | 331 " controller, request, response, done);\n" |
| 327 "}\n"); | 332 "}\n"); |
| 328 } | 333 } |
| 329 } | 334 } |
| 330 | 335 |
| 331 } // namespace cpp | 336 } // namespace cpp |
| 332 } // namespace compiler | 337 } // namespace compiler |
| 333 } // namespace protobuf | 338 } // namespace protobuf |
| 334 } // namespace google | 339 } // namespace google |
| OLD | NEW |