PageRenderTime 572ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/compiler/as3/as3_message_field.cc

http://protobuf-actionscript3.googlecode.com/
C++ | 270 lines | 157 code | 33 blank | 80 comment | 3 complexity | 49794948d06b747ca67db81fe9588204 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 <map>
  20. #include <string>
  21. #include <google/protobuf/compiler/as3/as3_message_field.h>
  22. #include <google/protobuf/compiler/as3/as3_helpers.h>
  23. #include <google/protobuf/io/printer.h>
  24. #include <google/protobuf/wire_format.h>
  25. #include <google/protobuf/stubs/strutil.h>
  26. namespace google {
  27. namespace protobuf {
  28. namespace compiler {
  29. namespace as3 {
  30. namespace {
  31. // TODO(kenton): Factor out a "SetCommonFieldVariables()" to get rid of
  32. // repeat code between this and the other field types.
  33. void SetMessageVariables(const FieldDescriptor* descriptor,
  34. map<string, string>* variables) {
  35. string package = FileAs3Package(descriptor->message_type()->file());
  36. (*variables)["name"] =
  37. UnderscoresToCamelCase(descriptor);
  38. (*variables)["capitalized_name"] =
  39. UnderscoresToCapitalizedCamelCase(descriptor);
  40. (*variables)["number"] = SimpleItoa(descriptor->number());
  41. if(!package.empty()) {
  42. (*variables)["java_package"] = package.append(".");
  43. } else {
  44. (*variables)["java_package"] = package;
  45. }
  46. (*variables)["type"] = descriptor->message_type()->name();
  47. (*variables)["label"] = SimpleItoa(descriptor->label());
  48. (*variables)["parent"] = descriptor->containing_type()->name();
  49. (*variables)["group_or_message"] =
  50. (descriptor->type() == FieldDescriptor::TYPE_GROUP) ?
  51. "Group" : "Message";
  52. }
  53. } // namespace
  54. // ===================================================================
  55. MessageFieldGenerator::
  56. MessageFieldGenerator(const FieldDescriptor* descriptor)
  57. : descriptor_(descriptor) {
  58. SetMessageVariables(descriptor, &variables_);
  59. }
  60. MessageFieldGenerator::~MessageFieldGenerator() {}
  61. void MessageFieldGenerator::
  62. GenerateMembers(io::Printer* printer) const {
  63. printer->Print(variables_,
  64. "public var $name$:$java_package$$type$ = null;\n");}
  65. void MessageFieldGenerator::
  66. GenerateBuilderMembers(io::Printer* printer) const {
  67. printer->Print(variables_,
  68. "public function get$capitalized_name$():$type$ {\n"
  69. " return $name$;\n"
  70. "}\n"
  71. "public function set$capitalized_name$(value:$type$):$parent$ {\n"
  72. " $name$ = value;\n"
  73. " return this;\n"
  74. "}\n"
  75. //"public function set$capitalized_name$(builderForValue:$type$.Builder):Builder {\n"
  76. // " result.has$capitalized_name$ = true;\n"
  77. // " result.$name$_ = builderForValue.build();\n"
  78. // " return this;\n"
  79. // "}\n"
  80. "public function clear$capitalized_name$():$parent$ {\n"
  81. " $name$ = $type$.getDefaultInstance();\n"
  82. " return this;\n"
  83. "}\n");
  84. }
  85. void MessageFieldGenerator::
  86. GenerateMergingCode(io::Printer* printer) const {
  87. printer->Print(variables_,
  88. "if (other.has$capitalized_name$()) {\n"
  89. " merge$capitalized_name$(other.get$capitalized_name$());\n"
  90. "}\n");
  91. }
  92. void MessageFieldGenerator::
  93. GenerateBuildingCode(io::Printer* printer) const {
  94. // Nothing to do for singular fields.
  95. }
  96. void MessageFieldGenerator::
  97. GenerateParsingCode(io::Printer* printer) const {
  98. //printer->Print(variables_,
  99. // "$type$.Builder subBuilder = $type$.newBuilder();\n"
  100. // "if (has$capitalized_name$()) {\n"
  101. // " subBuilder.mergeFrom(get$capitalized_name$());\n"
  102. // "}\n");
  103. //if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
  104. // printer->Print(variables_,
  105. // "input.readGroup($number$, subBuilder, extensionRegistry);\n");
  106. //} else {
  107. // printer->Print(variables_,
  108. // "input.readMessage(subBuilder, extensionRegistry);\n");
  109. //}
  110. //printer->Print(variables_,
  111. // "set$capitalized_name$(subBuilder.buildPartial());\n");
  112. }
  113. void MessageFieldGenerator::
  114. GenerateSerializationCode(io::Printer* printer) const {
  115. printer->Print(variables_,
  116. "if (has$capitalized_name$()) {\n"
  117. " output.write$group_or_message$($number$, get$capitalized_name$());\n"
  118. "}\n");
  119. }
  120. void MessageFieldGenerator::
  121. GenerateSerializedSizeCode(io::Printer* printer) const {
  122. printer->Print(variables_,
  123. "if (has$capitalized_name$()) {\n"
  124. " size += com.google.protobuf.CodedOutputStream\n"
  125. " .compute$group_or_message$Size($number$, get$capitalized_name$());\n"
  126. "}\n");
  127. }
  128. string MessageFieldGenerator::GetBoxedType() const {
  129. return ClassName(descriptor_->message_type());
  130. }
  131. // ===================================================================
  132. RepeatedMessageFieldGenerator::
  133. RepeatedMessageFieldGenerator(const FieldDescriptor* descriptor)
  134. : descriptor_(descriptor) {
  135. SetMessageVariables(descriptor, &variables_);
  136. }
  137. RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() {}
  138. void RepeatedMessageFieldGenerator::
  139. GenerateMembers(io::Printer* printer) const {
  140. printer->Print(variables_,
  141. "public var $name$:Array = new Array();\n\n"
  142. "//fix bug 1 protobuf-actionscript3\n"
  143. "//dummy var using $java_package$ necessary to avoid following exception\n"
  144. "//ReferenceError: Error #1065: Variable NetworkInfo is not defined.\n"
  145. "//at global/flash.utils::getDefinitionByName()\n"
  146. "//at com.google.protobuf::Message/readFromCodedStream()\n"
  147. "private var $name$Dummy:$java_package$$type$ = null;\n");
  148. }
  149. void RepeatedMessageFieldGenerator::
  150. GenerateBuilderMembers(io::Printer* printer) const {
  151. printer->Print(variables_,
  152. "public function get$capitalized_name$List():Array {\n"
  153. " return $name$;\n"
  154. "}\n"
  155. "public function get$capitalized_name$Count():int {\n"
  156. " return $name$.length;\n"
  157. "}\n"
  158. "public function get$capitalized_name$(index:int):$type$ {\n"
  159. " return $name$[index];\n"
  160. "}\n"
  161. "public function set$capitalized_name$(index:int, value:$type$):$parent$ {\n"
  162. " $name$[index] = value;\n"
  163. " return this;\n"
  164. "}\n"
  165. "public function add$capitalized_name$(value:$type$):$parent$ {\n"
  166. //" if ($name$_.isEmpty()) {\n"
  167. //" $name$_ = new ArrayList<$boxed_type$>();\n"
  168. //" }\n"
  169. " $name$.push(value);\n"
  170. " return this;\n"
  171. "}\n"
  172. "public function addAll$capitalized_name$(values:Array):$parent$ {\n"
  173. //" if ($name$_.isEmpty()) {\n"
  174. //" $name$_ = new ArrayList<$boxed_type$>();\n"
  175. //" }\n"
  176. " $name$.concat(values);\n"
  177. " return this;\n"
  178. "}\n"
  179. "public function clear$capitalized_name$():$parent$ {\n"
  180. " $name$ = new Array();\n"
  181. " return this;\n"
  182. "}\n");
  183. }
  184. void RepeatedMessageFieldGenerator::
  185. GenerateMergingCode(io::Printer* printer) const {
  186. //printer->Print(variables_,
  187. // "if (!other.$name$_.isEmpty()) {\n"
  188. // " if (result.$name$_.isEmpty()) {\n"
  189. // " result.$name$_ = new as3.util.ArrayList<$type$>();\n"
  190. // " }\n"
  191. // " result.$name$_.addAll(other.$name$_);\n"
  192. // "}\n");
  193. }
  194. void RepeatedMessageFieldGenerator::
  195. GenerateBuildingCode(io::Printer* printer) const {
  196. //printer->Print(variables_,
  197. // "if (result.$name$_ != as3.util.Collections.EMPTY_LIST) {\n"
  198. // " result.$name$_ =\n"
  199. // " as3.util.Collections.unmodifiableList(result.$name$_);\n"
  200. // "}\n");
  201. }
  202. void RepeatedMessageFieldGenerator::
  203. GenerateParsingCode(io::Printer* printer) const {
  204. //printer->Print(variables_,
  205. // "var subBuilder;$type$.Builder = $type$.newBuilder();\n");
  206. //if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
  207. // printer->Print(variables_,
  208. // "input.readGroup($number$, subBuilder, extensionRegistry);\n");
  209. //} else {
  210. // printer->Print(variables_,
  211. // "input.readMessage(subBuilder, extensionRegistry);\n");
  212. //}
  213. //printer->Print(variables_,
  214. // "add$capitalized_name$(subBuilder.buildPartial());\n");
  215. }
  216. void RepeatedMessageFieldGenerator::
  217. GenerateSerializationCode(io::Printer* printer) const {
  218. // printer->Print(variables_,
  219. //"for (var element:$type$ in get$capitalized_name$List()) {\n"
  220. // " output.write$group_or_message$($number$, element);\n"
  221. // "}\n");
  222. }
  223. void RepeatedMessageFieldGenerator::
  224. GenerateSerializedSizeCode(io::Printer* printer) const {
  225. // printer->Print(variables_,
  226. //"for (var element:$type$ in get$capitalized_name$List()) {\n"
  227. // " size += com.google.protobuf.CodedOutputStream\n"
  228. // " .compute$group_or_message$Size($number$, element);\n"
  229. // "}\n");
  230. }
  231. string RepeatedMessageFieldGenerator::GetBoxedType() const {
  232. return ClassName(descriptor_->message_type());
  233. }
  234. } // namespace as3
  235. } // namespace compiler
  236. } // namespace protobuf
  237. } // namespace google