PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/external/protobuf/src/google/protobuf/compiler/cpp/cpp_service.cc

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk
C++ | 334 lines | 253 code | 42 blank | 39 comment | 12 complexity | 4d7565c92f4de323cd2d885c777945d0 MD5 | raw file
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <google/protobuf/compiler/cpp/cpp_service.h>
  34. #include <google/protobuf/compiler/cpp/cpp_helpers.h>
  35. #include <google/protobuf/io/printer.h>
  36. #include <google/protobuf/stubs/strutil.h>
  37. namespace google {
  38. namespace protobuf {
  39. namespace compiler {
  40. namespace cpp {
  41. ServiceGenerator::ServiceGenerator(const ServiceDescriptor* descriptor,
  42. const string& dllexport_decl)
  43. : descriptor_(descriptor) {
  44. vars_["classname"] = descriptor_->name();
  45. vars_["full_name"] = descriptor_->full_name();
  46. if (dllexport_decl.empty()) {
  47. vars_["dllexport"] = "";
  48. } else {
  49. vars_["dllexport"] = dllexport_decl + " ";
  50. }
  51. }
  52. ServiceGenerator::~ServiceGenerator() {}
  53. void ServiceGenerator::GenerateDeclarations(io::Printer* printer) {
  54. // Forward-declare the stub type.
  55. printer->Print(vars_,
  56. "class $classname$_Stub;\n"
  57. "\n");
  58. GenerateInterface(printer);
  59. GenerateStubDefinition(printer);
  60. }
  61. void ServiceGenerator::GenerateInterface(io::Printer* printer) {
  62. printer->Print(vars_,
  63. "class $dllexport$$classname$ : public ::google::protobuf::Service {\n"
  64. " protected:\n"
  65. " // This class should be treated as an abstract interface.\n"
  66. " inline $classname$() {};\n"
  67. " public:\n"
  68. " virtual ~$classname$();\n");
  69. printer->Indent();
  70. printer->Print(vars_,
  71. "\n"
  72. "typedef $classname$_Stub Stub;\n"
  73. "\n"
  74. "static const ::google::protobuf::ServiceDescriptor* descriptor();\n"
  75. "\n");
  76. GenerateMethodSignatures(VIRTUAL, printer);
  77. printer->Print(
  78. "\n"
  79. "// implements Service ----------------------------------------------\n"
  80. "\n"
  81. "const ::google::protobuf::ServiceDescriptor* GetDescriptor();\n"
  82. "void CallMethod(const ::google::protobuf::MethodDescriptor* method,\n"
  83. " ::google::protobuf::RpcController* controller,\n"
  84. " const ::google::protobuf::Message* request,\n"
  85. " ::google::protobuf::Message* response,\n"
  86. " ::google::protobuf::Closure* done);\n"
  87. "const ::google::protobuf::Message& GetRequestPrototype(\n"
  88. " const ::google::protobuf::MethodDescriptor* method) const;\n"
  89. "const ::google::protobuf::Message& GetResponsePrototype(\n"
  90. " const ::google::protobuf::MethodDescriptor* method) const;\n");
  91. printer->Outdent();
  92. printer->Print(vars_,
  93. "\n"
  94. " private:\n"
  95. " GOOGLE_DISALLOW_EVIL_CONSTRUCTORS($classname$);\n"
  96. "};\n"
  97. "\n");
  98. }
  99. void ServiceGenerator::GenerateStubDefinition(io::Printer* printer) {
  100. printer->Print(vars_,
  101. "class $dllexport$$classname$_Stub : public $classname$ {\n"
  102. " public:\n");
  103. printer->Indent();
  104. printer->Print(vars_,
  105. "$classname$_Stub(::google::protobuf::RpcChannel* channel);\n"
  106. "$classname$_Stub(::google::protobuf::RpcChannel* channel,\n"
  107. " ::google::protobuf::Service::ChannelOwnership ownership);\n"
  108. "~$classname$_Stub();\n"
  109. "\n"
  110. "inline ::google::protobuf::RpcChannel* channel() { return channel_; }\n"
  111. "\n"
  112. "// implements $classname$ ------------------------------------------\n"
  113. "\n");
  114. GenerateMethodSignatures(NON_VIRTUAL, printer);
  115. printer->Outdent();
  116. printer->Print(vars_,
  117. " private:\n"
  118. " ::google::protobuf::RpcChannel* channel_;\n"
  119. " bool owns_channel_;\n"
  120. " GOOGLE_DISALLOW_EVIL_CONSTRUCTORS($classname$_Stub);\n"
  121. "};\n"
  122. "\n");
  123. }
  124. void ServiceGenerator::GenerateMethodSignatures(
  125. VirtualOrNon virtual_or_non, io::Printer* printer) {
  126. for (int i = 0; i < descriptor_->method_count(); i++) {
  127. const MethodDescriptor* method = descriptor_->method(i);
  128. map<string, string> sub_vars;
  129. sub_vars["name"] = method->name();
  130. sub_vars["input_type"] = ClassName(method->input_type(), true);
  131. sub_vars["output_type"] = ClassName(method->output_type(), true);
  132. sub_vars["virtual"] = virtual_or_non == VIRTUAL ? "virtual " : "";
  133. printer->Print(sub_vars,
  134. "$virtual$void $name$(::google::protobuf::RpcController* controller,\n"
  135. " const $input_type$* request,\n"
  136. " $output_type$* response,\n"
  137. " ::google::protobuf::Closure* done);\n");
  138. }
  139. }
  140. // ===================================================================
  141. void ServiceGenerator::GenerateDescriptorInitializer(
  142. io::Printer* printer, int index) {
  143. map<string, string> vars;
  144. vars["classname"] = descriptor_->name();
  145. vars["index"] = SimpleItoa(index);
  146. printer->Print(vars,
  147. "$classname$_descriptor_ = file->service($index$);\n");
  148. }
  149. // ===================================================================
  150. void ServiceGenerator::GenerateImplementation(io::Printer* printer) {
  151. printer->Print(vars_,
  152. "$classname$::~$classname$() {}\n"
  153. "\n"
  154. "const ::google::protobuf::ServiceDescriptor* $classname$::descriptor() {\n"
  155. " protobuf_AssignDescriptorsOnce();\n"
  156. " return $classname$_descriptor_;\n"
  157. "}\n"
  158. "\n"
  159. "const ::google::protobuf::ServiceDescriptor* $classname$::GetDescriptor() {\n"
  160. " protobuf_AssignDescriptorsOnce();\n"
  161. " return $classname$_descriptor_;\n"
  162. "}\n"
  163. "\n");
  164. // Generate methods of the interface.
  165. GenerateNotImplementedMethods(printer);
  166. GenerateCallMethod(printer);
  167. GenerateGetPrototype(REQUEST, printer);
  168. GenerateGetPrototype(RESPONSE, printer);
  169. // Generate stub implementation.
  170. printer->Print(vars_,
  171. "$classname$_Stub::$classname$_Stub(::google::protobuf::RpcChannel* channel)\n"
  172. " : channel_(channel), owns_channel_(false) {}\n"
  173. "$classname$_Stub::$classname$_Stub(\n"
  174. " ::google::protobuf::RpcChannel* channel,\n"
  175. " ::google::protobuf::Service::ChannelOwnership ownership)\n"
  176. " : channel_(channel),\n"
  177. " owns_channel_(ownership == ::google::protobuf::Service::STUB_OWNS_CHANNEL) {}\n"
  178. "$classname$_Stub::~$classname$_Stub() {\n"
  179. " if (owns_channel_) delete channel_;\n"
  180. "}\n"
  181. "\n");
  182. GenerateStubMethods(printer);
  183. }
  184. void ServiceGenerator::GenerateNotImplementedMethods(io::Printer* printer) {
  185. for (int i = 0; i < descriptor_->method_count(); i++) {
  186. const MethodDescriptor* method = descriptor_->method(i);
  187. map<string, string> sub_vars;
  188. sub_vars["classname"] = descriptor_->name();
  189. sub_vars["name"] = method->name();
  190. sub_vars["index"] = SimpleItoa(i);
  191. sub_vars["input_type"] = ClassName(method->input_type(), true);
  192. sub_vars["output_type"] = ClassName(method->output_type(), true);
  193. printer->Print(sub_vars,
  194. "void $classname$::$name$(::google::protobuf::RpcController* controller,\n"
  195. " const $input_type$*,\n"
  196. " $output_type$*,\n"
  197. " ::google::protobuf::Closure* done) {\n"
  198. " controller->SetFailed(\"Method $name$() not implemented.\");\n"
  199. " done->Run();\n"
  200. "}\n"
  201. "\n");
  202. }
  203. }
  204. void ServiceGenerator::GenerateCallMethod(io::Printer* printer) {
  205. printer->Print(vars_,
  206. "void $classname$::CallMethod(const ::google::protobuf::MethodDescriptor* method,\n"
  207. " ::google::protobuf::RpcController* controller,\n"
  208. " const ::google::protobuf::Message* request,\n"
  209. " ::google::protobuf::Message* response,\n"
  210. " ::google::protobuf::Closure* done) {\n"
  211. " GOOGLE_DCHECK_EQ(method->service(), $classname$_descriptor_);\n"
  212. " switch(method->index()) {\n");
  213. for (int i = 0; i < descriptor_->method_count(); i++) {
  214. const MethodDescriptor* method = descriptor_->method(i);
  215. map<string, string> sub_vars;
  216. sub_vars["name"] = method->name();
  217. sub_vars["index"] = SimpleItoa(i);
  218. sub_vars["input_type"] = ClassName(method->input_type(), true);
  219. sub_vars["output_type"] = ClassName(method->output_type(), true);
  220. // Note: down_cast does not work here because it only works on pointers,
  221. // not references.
  222. printer->Print(sub_vars,
  223. " case $index$:\n"
  224. " $name$(controller,\n"
  225. " ::google::protobuf::down_cast<const $input_type$*>(request),\n"
  226. " ::google::protobuf::down_cast< $output_type$*>(response),\n"
  227. " done);\n"
  228. " break;\n");
  229. }
  230. printer->Print(vars_,
  231. " default:\n"
  232. " GOOGLE_LOG(FATAL) << \"Bad method index; this should never happen.\";\n"
  233. " break;\n"
  234. " }\n"
  235. "}\n"
  236. "\n");
  237. }
  238. void ServiceGenerator::GenerateGetPrototype(RequestOrResponse which,
  239. io::Printer* printer) {
  240. if (which == REQUEST) {
  241. printer->Print(vars_,
  242. "const ::google::protobuf::Message& $classname$::GetRequestPrototype(\n");
  243. } else {
  244. printer->Print(vars_,
  245. "const ::google::protobuf::Message& $classname$::GetResponsePrototype(\n");
  246. }
  247. printer->Print(vars_,
  248. " const ::google::protobuf::MethodDescriptor* method) const {\n"
  249. " GOOGLE_DCHECK_EQ(method->service(), descriptor());\n"
  250. " switch(method->index()) {\n");
  251. for (int i = 0; i < descriptor_->method_count(); i++) {
  252. const MethodDescriptor* method = descriptor_->method(i);
  253. const Descriptor* type =
  254. (which == REQUEST) ? method->input_type() : method->output_type();
  255. map<string, string> sub_vars;
  256. sub_vars["index"] = SimpleItoa(i);
  257. sub_vars["type"] = ClassName(type, true);
  258. printer->Print(sub_vars,
  259. " case $index$:\n"
  260. " return $type$::default_instance();\n");
  261. }
  262. printer->Print(vars_,
  263. " default:\n"
  264. " GOOGLE_LOG(FATAL) << \"Bad method index; this should never happen.\";\n"
  265. " return *reinterpret_cast< ::google::protobuf::Message*>(NULL);\n"
  266. " }\n"
  267. "}\n"
  268. "\n");
  269. }
  270. void ServiceGenerator::GenerateStubMethods(io::Printer* printer) {
  271. for (int i = 0; i < descriptor_->method_count(); i++) {
  272. const MethodDescriptor* method = descriptor_->method(i);
  273. map<string, string> sub_vars;
  274. sub_vars["classname"] = descriptor_->name();
  275. sub_vars["name"] = method->name();
  276. sub_vars["index"] = SimpleItoa(i);
  277. sub_vars["input_type"] = ClassName(method->input_type(), true);
  278. sub_vars["output_type"] = ClassName(method->output_type(), true);
  279. printer->Print(sub_vars,
  280. "void $classname$_Stub::$name$(::google::protobuf::RpcController* controller,\n"
  281. " const $input_type$* request,\n"
  282. " $output_type$* response,\n"
  283. " ::google::protobuf::Closure* done) {\n"
  284. " channel_->CallMethod(descriptor()->method($index$),\n"
  285. " controller, request, response, done);\n"
  286. "}\n");
  287. }
  288. }
  289. } // namespace cpp
  290. } // namespace compiler
  291. } // namespace protobuf
  292. } // namespace google