RepeatedEnumFieldGenerator.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // http://github.com/jskeet/dotnet-protobufs/
  5. // Original C++/Java/Python code:
  6. // http://code.google.com/p/protobuf/
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. //
  12. // * Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. // * Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following disclaimer
  16. // in the documentation and/or other materials provided with the
  17. // distribution.
  18. // * Neither the name of Google Inc. nor the names of its
  19. // contributors may be used to endorse or promote products derived from
  20. // this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. #endregion
  34. using Google.ProtocolBuffers.Descriptors;
  35. namespace Google.ProtocolBuffers.ProtoGen
  36. {
  37. internal class RepeatedEnumFieldGenerator : FieldGeneratorBase, IFieldSourceGenerator
  38. {
  39. internal RepeatedEnumFieldGenerator(FieldDescriptor descriptor, int fieldOrdinal)
  40. : base(descriptor, fieldOrdinal)
  41. {
  42. }
  43. public void GenerateMembers(TextGenerator writer)
  44. {
  45. if (Descriptor.IsPacked && OptimizeSpeed)
  46. {
  47. writer.WriteLine("private int {0}MemoizedSerializedSize;", Name);
  48. }
  49. writer.WriteLine("private pbc::PopsicleList<{0}> {1}_ = new pbc::PopsicleList<{0}>();", TypeName, Name);
  50. AddDeprecatedFlag(writer);
  51. writer.WriteLine("public scg::IList<{0}> {1}List {{", TypeName, PropertyName);
  52. writer.WriteLine(" get {{ return pbc::Lists.AsReadOnly({0}_); }}", Name);
  53. writer.WriteLine("}");
  54. // TODO(jonskeet): Redundant API calls? Possibly - include for portability though. Maybe create an option.
  55. AddDeprecatedFlag(writer);
  56. writer.WriteLine("public int {0}Count {{", PropertyName);
  57. writer.WriteLine(" get {{ return {0}_.Count; }}", Name);
  58. writer.WriteLine("}");
  59. AddDeprecatedFlag(writer);
  60. writer.WriteLine("public {0} Get{1}(int index) {{", TypeName, PropertyName);
  61. writer.WriteLine(" return {0}_[index];", Name);
  62. writer.WriteLine("}");
  63. }
  64. public void GenerateBuilderMembers(TextGenerator writer)
  65. {
  66. // Note: We can return the original list here, because we make it unmodifiable when we build
  67. // We return it via IPopsicleList so that collection initializers work more pleasantly.
  68. AddDeprecatedFlag(writer);
  69. writer.WriteLine("public pbc::IPopsicleList<{0}> {1}List {{", TypeName, PropertyName);
  70. writer.WriteLine(" get {{ return PrepareBuilder().{0}_; }}", Name);
  71. writer.WriteLine("}");
  72. AddDeprecatedFlag(writer);
  73. writer.WriteLine("public int {0}Count {{", PropertyName);
  74. writer.WriteLine(" get {{ return result.{0}Count; }}", PropertyName);
  75. writer.WriteLine("}");
  76. AddDeprecatedFlag(writer);
  77. writer.WriteLine("public {0} Get{1}(int index) {{", TypeName, PropertyName);
  78. writer.WriteLine(" return result.Get{0}(index);", PropertyName);
  79. writer.WriteLine("}");
  80. AddDeprecatedFlag(writer);
  81. writer.WriteLine("public Builder Set{0}(int index, {1} value) {{", PropertyName, TypeName);
  82. writer.WriteLine(" PrepareBuilder();");
  83. writer.WriteLine(" result.{0}_[index] = value;", Name);
  84. writer.WriteLine(" return this;");
  85. writer.WriteLine("}");
  86. AddDeprecatedFlag(writer);
  87. writer.WriteLine("public Builder Add{0}({1} value) {{", PropertyName, TypeName);
  88. writer.WriteLine(" PrepareBuilder();");
  89. writer.WriteLine(" result.{0}_.Add(value);", Name, TypeName);
  90. writer.WriteLine(" return this;");
  91. writer.WriteLine("}");
  92. AddDeprecatedFlag(writer);
  93. writer.WriteLine("public Builder AddRange{0}(scg::IEnumerable<{1}> values) {{", PropertyName, TypeName);
  94. writer.WriteLine(" PrepareBuilder();");
  95. writer.WriteLine(" result.{0}_.Add(values);", Name);
  96. writer.WriteLine(" return this;");
  97. writer.WriteLine("}");
  98. AddDeprecatedFlag(writer);
  99. writer.WriteLine("public Builder Clear{0}() {{", PropertyName);
  100. writer.WriteLine(" PrepareBuilder();");
  101. writer.WriteLine(" result.{0}_.Clear();", Name);
  102. writer.WriteLine(" return this;");
  103. writer.WriteLine("}");
  104. }
  105. public void GenerateMergingCode(TextGenerator writer)
  106. {
  107. writer.WriteLine("if (other.{0}_.Count != 0) {{", Name);
  108. writer.WriteLine(" result.{0}_.Add(other.{0}_);", Name);
  109. writer.WriteLine("}");
  110. }
  111. public void GenerateBuildingCode(TextGenerator writer)
  112. {
  113. writer.WriteLine("{0}_.MakeReadOnly();", Name);
  114. }
  115. public void GenerateParsingCode(TextGenerator writer)
  116. {
  117. writer.WriteLine("scg::ICollection<object> unknownItems;");
  118. writer.WriteLine("input.ReadEnumArray<{0}>(tag, field_name, result.{1}_, out unknownItems);", TypeName, Name);
  119. if (!UseLiteRuntime)
  120. {
  121. writer.WriteLine("if (unknownItems != null) {");
  122. writer.WriteLine(" if (unknownFields == null) {");
  123. writer.WriteLine(" unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields);");
  124. writer.WriteLine(" }");
  125. writer.WriteLine(" foreach (object rawValue in unknownItems)");
  126. writer.WriteLine(" if (rawValue is int)");
  127. writer.WriteLine(" unknownFields.MergeVarintField({0}, (ulong)(int)rawValue);", Number);
  128. writer.WriteLine("}");
  129. }
  130. }
  131. public void GenerateSerializationCode(TextGenerator writer)
  132. {
  133. writer.WriteLine("if ({0}_.Count > 0) {{", Name);
  134. writer.Indent();
  135. if (Descriptor.IsPacked)
  136. {
  137. writer.WriteLine(
  138. "output.WritePackedEnumArray({0}, field_names[{2}], {1}MemoizedSerializedSize, {1}_);", Number, Name,
  139. FieldOrdinal, Descriptor.FieldType);
  140. }
  141. else
  142. {
  143. writer.WriteLine("output.WriteEnumArray({0}, field_names[{2}], {1}_);", Number, Name, FieldOrdinal,
  144. Descriptor.FieldType);
  145. }
  146. writer.Outdent();
  147. writer.WriteLine("}");
  148. }
  149. public void GenerateSerializedSizeCode(TextGenerator writer)
  150. {
  151. writer.WriteLine("{");
  152. writer.Indent();
  153. writer.WriteLine("int dataSize = 0;");
  154. writer.WriteLine("if ({0}_.Count > 0) {{", Name);
  155. writer.Indent();
  156. writer.WriteLine("foreach ({0} element in {1}_) {{", TypeName, Name);
  157. writer.WriteLine(" dataSize += pb::CodedOutputStream.ComputeEnumSizeNoTag((int) element);");
  158. writer.WriteLine("}");
  159. writer.WriteLine("size += dataSize;");
  160. int tagSize = CodedOutputStream.ComputeTagSize(Descriptor.FieldNumber);
  161. if (Descriptor.IsPacked)
  162. {
  163. writer.WriteLine("size += {0};", tagSize);
  164. writer.WriteLine("size += pb::CodedOutputStream.ComputeRawVarint32Size((uint) dataSize);");
  165. }
  166. else
  167. {
  168. writer.WriteLine("size += {0} * {1}_.Count;", tagSize, Name);
  169. }
  170. writer.Outdent();
  171. writer.WriteLine("}");
  172. // cache the data size for packed fields.
  173. if (Descriptor.IsPacked)
  174. {
  175. writer.WriteLine("{0}MemoizedSerializedSize = dataSize;", Name);
  176. }
  177. writer.Outdent();
  178. writer.WriteLine("}");
  179. }
  180. public override void WriteHash(TextGenerator writer)
  181. {
  182. writer.WriteLine("foreach({0} i in {1}_)", TypeName, Name);
  183. writer.WriteLine(" hash ^= i.GetHashCode();");
  184. }
  185. public override void WriteEquals(TextGenerator writer)
  186. {
  187. writer.WriteLine("if({0}_.Count != other.{0}_.Count) return false;", Name);
  188. writer.WriteLine("for(int ix=0; ix < {0}_.Count; ix++)", Name);
  189. writer.WriteLine(" if(!{0}_[ix].Equals(other.{0}_[ix])) return false;", Name);
  190. }
  191. public override void WriteToString(TextGenerator writer)
  192. {
  193. writer.WriteLine("PrintField(\"{0}\", {1}_, writer);", Descriptor.Name, Name);
  194. }
  195. }
  196. }