PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/compiler/as3/as3_service.cc

http://protobuf-actionscript3.googlecode.com/
C++ | 225 lines | 25 code | 25 blank | 175 comment | 0 complexity | 85dabece69569e59a408bd62b058f1a6 MD5 | raw file
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. // Author: Robert Blackwood (ported from Kenton's)
  17. // Based on original Protocol Buffers design by
  18. // Sanjay Ghemawat, Jeff Dean, and others.
  19. #include <google/protobuf/compiler/as3/as3_service.h>
  20. #include <google/protobuf/compiler/as3/as3_helpers.h>
  21. #include <google/protobuf/io/printer.h>
  22. #include <google/protobuf/descriptor.pb.h>
  23. #include <google/protobuf/stubs/strutil.h>
  24. namespace google {
  25. namespace protobuf {
  26. namespace compiler {
  27. namespace as3 {
  28. ServiceGenerator::ServiceGenerator(const ServiceDescriptor* descriptor)
  29. : descriptor_(descriptor) {}
  30. ServiceGenerator::~ServiceGenerator() {}
  31. void ServiceGenerator::Generate(io::Printer* printer) {
  32. //bool is_own_file = descriptor_->file()->options().java_multiple_files();
  33. //printer->Print(
  34. // "public $static$ abstract class $classname$\n"
  35. // " implements com.google.protobuf.Service {\n",
  36. // "static", is_own_file ? "" : "static",
  37. // "classname", descriptor_->name());
  38. //printer->Indent();
  39. //// Generate abstract method declarations.
  40. //for (int i = 0; i < descriptor_->method_count(); i++) {
  41. // const MethodDescriptor* method = descriptor_->method(i);
  42. // map<string, string> vars;
  43. // vars["name"] = UnderscoresToCamelCase(method);
  44. // vars["input"] = ClassName(method->input_type());
  45. // vars["output"] = ClassName(method->output_type());
  46. // printer->Print(vars,
  47. // "public abstract void $name$(\n"
  48. // " com.google.protobuf.RpcController controller,\n"
  49. // " $input$ request,\n"
  50. // " com.google.protobuf.RpcCallback<$output$> done);\n");
  51. //}
  52. //// Generate getDescriptor() and getDescriptorForType().
  53. //printer->Print(
  54. // "\n"
  55. // "public static final\n"
  56. // " com.google.protobuf.Descriptors.ServiceDescriptor\n"
  57. // " getDescriptor() {\n"
  58. // " return $file$.getDescriptor().getServices().get($index$);\n"
  59. // "}\n"
  60. // "public final com.google.protobuf.Descriptors.ServiceDescriptor\n"
  61. // " getDescriptorForType() {\n"
  62. // " return getDescriptor();\n"
  63. // "}\n",
  64. // "file", ClassName(descriptor_->file()),
  65. // "index", SimpleItoa(descriptor_->index()));
  66. //// Generate more stuff.
  67. //GenerateCallMethod(printer);
  68. //GenerateGetPrototype(REQUEST, printer);
  69. //GenerateGetPrototype(RESPONSE, printer);
  70. //GenerateStub(printer);
  71. //printer->Outdent();
  72. //printer->Print("}\n\n");
  73. }
  74. void ServiceGenerator::GenerateCallMethod(io::Printer* printer) {
  75. //printer->Print(
  76. // "\n"
  77. // "public final void callMethod(\n"
  78. // " com.google.protobuf.Descriptors.MethodDescriptor method,\n"
  79. // " com.google.protobuf.RpcController controller,\n"
  80. // " com.google.protobuf.Message request,\n"
  81. // " com.google.protobuf.RpcCallback<\n"
  82. // " com.google.protobuf.Message> done) {\n"
  83. // " if (method.getService() != getDescriptor()) {\n"
  84. // " throw new as3.lang.IllegalArgumentException(\n"
  85. // " \"Service.callMethod() given method descriptor for wrong \" +\n"
  86. // " \"service type.\");\n"
  87. // " }\n"
  88. // " switch(method.getIndex()) {\n");
  89. //printer->Indent();
  90. //printer->Indent();
  91. //for (int i = 0; i < descriptor_->method_count(); i++) {
  92. // const MethodDescriptor* method = descriptor_->method(i);
  93. // map<string, string> vars;
  94. // vars["index"] = SimpleItoa(i);
  95. // vars["method"] = UnderscoresToCamelCase(method);
  96. // vars["input"] = ClassName(method->input_type());
  97. // vars["output"] = ClassName(method->output_type());
  98. // printer->Print(vars,
  99. // "case $index$:\n"
  100. // " this.$method$(controller, ($input$)request,\n"
  101. // " com.google.protobuf.RpcUtil.<$output$>specializeCallback(\n"
  102. // " done));\n"
  103. // " return;\n");
  104. //}
  105. //printer->Print(
  106. // "default:\n"
  107. // " throw new as3.lang.RuntimeException(\"Can't get here.\");\n");
  108. //printer->Outdent();
  109. //printer->Outdent();
  110. //printer->Print(
  111. // " }\n"
  112. // "}\n"
  113. // "\n");
  114. }
  115. void ServiceGenerator::GenerateGetPrototype(RequestOrResponse which,
  116. io::Printer* printer) {
  117. //printer->Print(
  118. // "public final com.google.protobuf.Message\n"
  119. // " get$request_or_response$Prototype(\n"
  120. // " com.google.protobuf.Descriptors.MethodDescriptor method) {\n"
  121. // " if (method.getService() != getDescriptor()) {\n"
  122. // " throw new as3.lang.IllegalArgumentException(\n"
  123. // " \"Service.get$request_or_response$Prototype() given method \" +\n"
  124. // " \"descriptor for wrong service type.\");\n"
  125. // " }\n"
  126. // " switch(method.getIndex()) {\n",
  127. // "request_or_response", (which == REQUEST) ? "Request" : "Response");
  128. //printer->Indent();
  129. //printer->Indent();
  130. //for (int i = 0; i < descriptor_->method_count(); i++) {
  131. // const MethodDescriptor* method = descriptor_->method(i);
  132. // map<string, string> vars;
  133. // vars["index"] = SimpleItoa(i);
  134. // vars["type"] = ClassName(
  135. // (which == REQUEST) ? method->input_type() : method->output_type());
  136. // printer->Print(vars,
  137. // "case $index$:\n"
  138. // " return $type$.getDefaultInstance();\n");
  139. //}
  140. //printer->Print(
  141. // "default:\n"
  142. // " throw new as3.lang.RuntimeException(\"Can't get here.\");\n");
  143. //printer->Outdent();
  144. //printer->Outdent();
  145. //printer->Print(
  146. // " }\n"
  147. // "}\n"
  148. // "\n");
  149. }
  150. void ServiceGenerator::GenerateStub(io::Printer* printer) {
  151. //printer->Print(
  152. // "public static Stub newStub(\n"
  153. // " com.google.protobuf.RpcChannel channel) {\n"
  154. // " return new Stub(channel);\n"
  155. // "}\n"
  156. // "\n"
  157. // "public static final class Stub extends $classname$ {\n",
  158. // "classname", ClassName(descriptor_));
  159. //printer->Indent();
  160. //printer->Print(
  161. // "private Stub(com.google.protobuf.RpcChannel channel) {\n"
  162. // " this.channel = channel;\n"
  163. // "}\n"
  164. // "\n"
  165. // "private final com.google.protobuf.RpcChannel channel;\n"
  166. // "\n"
  167. // "public com.google.protobuf.RpcChannel getChannel() {\n"
  168. // " return channel;\n"
  169. // "}\n");
  170. //for (int i = 0; i < descriptor_->method_count(); i++) {
  171. // const MethodDescriptor* method = descriptor_->method(i);
  172. // map<string, string> vars;
  173. // vars["index"] = SimpleItoa(i);
  174. // vars["method"] = UnderscoresToCamelCase(method);
  175. // vars["input"] = ClassName(method->input_type());
  176. // vars["output"] = ClassName(method->output_type());
  177. // printer->Print(vars,
  178. // "\n"
  179. // "public void $method$(\n"
  180. // " com.google.protobuf.RpcController controller,\n"
  181. // " $input$ request,\n"
  182. // " com.google.protobuf.RpcCallback<$output$> done) {\n"
  183. // " channel.callMethod(\n"
  184. // " getDescriptor().getMethods().get($index$),\n"
  185. // " controller,\n"
  186. // " request,\n"
  187. // " $output$.getDefaultInstance(),\n"
  188. // " com.google.protobuf.RpcUtil.generalizeCallback(\n"
  189. // " done,\n"
  190. // " $output$.class,\n"
  191. // " $output$.getDefaultInstance()));\n"
  192. // "}\n");
  193. //}
  194. //printer->Outdent();
  195. //printer->Print("}\n");
  196. }
  197. } // namespace as3
  198. } // namespace compiler
  199. } // namespace protobuf
  200. } // namespace google