RepeatedEnumFieldGenerator.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.DescriptorProtos;
  35. using Google.ProtocolBuffers.Descriptors;
  36. namespace Google.ProtocolBuffers.ProtoGen {
  37. internal class RepeatedEnumFieldGenerator : FieldGeneratorBase, IFieldSourceGenerator {
  38. internal RepeatedEnumFieldGenerator(FieldDescriptor descriptor)
  39. : base(descriptor) {
  40. }
  41. public void GenerateMembers(TextGenerator writer) {
  42. if (Descriptor.IsPacked && Descriptor.File.Options.OptimizeFor == FileOptions.Types.OptimizeMode.SPEED) {
  43. writer.WriteLine("private int {0}MemoizedSerializedSize;", Name);
  44. }
  45. writer.WriteLine("private pbc::PopsicleList<{0}> {1}_ = new pbc::PopsicleList<{0}>();", TypeName, Name);
  46. writer.WriteLine("public scg::IList<{0}> {1}List {{", TypeName, PropertyName);
  47. writer.WriteLine(" get {{ return pbc::Lists.AsReadOnly({0}_); }}", Name);
  48. writer.WriteLine("}");
  49. // TODO(jonskeet): Redundant API calls? Possibly - include for portability though. Maybe create an option.
  50. writer.WriteLine("public int {0}Count {{", PropertyName);
  51. writer.WriteLine(" get {{ return {0}_.Count; }}", Name);
  52. writer.WriteLine("}");
  53. writer.WriteLine("public {0} Get{1}(int index) {{", TypeName, PropertyName);
  54. writer.WriteLine(" return {0}_[index];", Name);
  55. writer.WriteLine("}");
  56. }
  57. public void GenerateBuilderMembers(TextGenerator writer) {
  58. // Note: We can return the original list here, because we make it unmodifiable when we build
  59. // We return it via IPopsicleList so that collection initializers work more pleasantly.
  60. writer.WriteLine("public pbc::IPopsicleList<{0}> {1}List {{", TypeName, PropertyName);
  61. writer.WriteLine(" get {{ return result.{0}_; }}", Name);
  62. writer.WriteLine("}");
  63. writer.WriteLine("public int {0}Count {{", PropertyName);
  64. writer.WriteLine(" get {{ return result.{0}Count; }}", PropertyName);
  65. writer.WriteLine("}");
  66. writer.WriteLine("public {0} Get{1}(int index) {{", TypeName, PropertyName);
  67. writer.WriteLine(" return result.Get{0}(index);", PropertyName);
  68. writer.WriteLine("}");
  69. writer.WriteLine("public Builder Set{0}(int index, {1} value) {{", PropertyName, TypeName);
  70. writer.WriteLine(" result.{0}_[index] = value;", Name);
  71. writer.WriteLine(" return this;");
  72. writer.WriteLine("}");
  73. writer.WriteLine("public Builder Add{0}({1} value) {{", PropertyName, TypeName);
  74. writer.WriteLine(" result.{0}_.Add(value);", Name, TypeName);
  75. writer.WriteLine(" return this;");
  76. writer.WriteLine("}");
  77. writer.WriteLine("public Builder AddRange{0}(scg::IEnumerable<{1}> values) {{", PropertyName, TypeName);
  78. writer.WriteLine(" base.AddRange(values, result.{0}_);", Name);
  79. writer.WriteLine(" return this;");
  80. writer.WriteLine("}");
  81. writer.WriteLine("public Builder Clear{0}() {{", PropertyName);
  82. writer.WriteLine(" result.{0}_.Clear();", Name);
  83. writer.WriteLine(" return this;");
  84. writer.WriteLine("}");
  85. }
  86. public void GenerateMergingCode(TextGenerator writer) {
  87. writer.WriteLine("if (other.{0}_.Count != 0) {{", Name);
  88. writer.WriteLine(" base.AddRange(other.{0}_, result.{0}_);", Name);
  89. writer.WriteLine("}");
  90. }
  91. public void GenerateBuildingCode(TextGenerator writer) {
  92. writer.WriteLine("result.{0}_.MakeReadOnly();", Name);
  93. }
  94. public void GenerateParsingCode(TextGenerator writer) {
  95. // If packed, set up the while loop
  96. if (Descriptor.IsPacked) {
  97. writer.WriteLine("int length = input.ReadInt32();");
  98. writer.WriteLine("int oldLimit = input.PushLimit(length);");
  99. writer.WriteLine("while (!input.ReachedLimit) {");
  100. writer.Indent();
  101. }
  102. // Read and store the enum
  103. // TODO(jonskeet): Make a more efficient way of doing this
  104. writer.WriteLine("int rawValue = input.ReadEnum();");
  105. writer.WriteLine("if (!global::System.Enum.IsDefined(typeof({0}), rawValue)) {{", TypeName);
  106. writer.WriteLine(" if (unknownFields == null) {"); // First unknown field - create builder now
  107. writer.WriteLine(" unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields);");
  108. writer.WriteLine(" }");
  109. writer.WriteLine(" unknownFields.MergeVarintField({0}, (ulong) rawValue);", Number);
  110. writer.WriteLine("} else {");
  111. writer.WriteLine(" Add{0}(({1}) rawValue);", PropertyName, TypeName);
  112. writer.WriteLine("}");
  113. if (Descriptor.IsPacked) {
  114. writer.Outdent();
  115. writer.WriteLine("}");
  116. writer.WriteLine("input.PopLimit(oldLimit);");
  117. }
  118. }
  119. public void GenerateSerializationCode(TextGenerator writer) {
  120. writer.WriteLine("if ({0}_.Count > 0) {{", Name);
  121. writer.Indent();
  122. if (Descriptor.IsPacked) {
  123. writer.WriteLine("output.WriteRawVarint32({0});", WireFormat.MakeTag(Descriptor));
  124. writer.WriteLine("output.WriteRawVarint32((uint) {0}MemoizedSerializedSize);", Name);
  125. writer.WriteLine("foreach (int element in {0}_) {{", Name);
  126. writer.WriteLine(" output.WriteEnumNoTag(element);");
  127. writer.WriteLine("}");
  128. } else {
  129. writer.WriteLine("foreach (int element in {0}_) {{", Name);
  130. writer.WriteLine(" output.WriteEnum({0}, element);", Number);
  131. writer.WriteLine("}");
  132. }
  133. writer.Outdent();
  134. writer.WriteLine("}");
  135. }
  136. public void GenerateSerializedSizeCode(TextGenerator writer) {
  137. writer.WriteLine("{");
  138. writer.Indent();
  139. writer.WriteLine("int dataSize = 0;");
  140. writer.WriteLine("if ({0}_.Count > 0) {{", Name);
  141. writer.Indent();
  142. writer.WriteLine("foreach ({0} element in {1}_) {{", TypeName, Name);
  143. writer.WriteLine(" dataSize += pb::CodedOutputStream.ComputeEnumSizeNoTag((int) element);");
  144. writer.WriteLine("}");
  145. writer.WriteLine("size += dataSize;");
  146. int tagSize = CodedOutputStream.ComputeTagSize(Descriptor.FieldNumber);
  147. if (Descriptor.IsPacked) {
  148. writer.WriteLine("size += {0};", tagSize);
  149. writer.WriteLine("size += pb::CodedOutputStream.ComputeRawVarint32Size((uint) dataSize);");
  150. } else {
  151. writer.WriteLine("size += {0} * {1}_.Count;", tagSize, Name);
  152. }
  153. writer.Outdent();
  154. writer.WriteLine("}");
  155. // cache the data size for packed fields.
  156. if (Descriptor.IsPacked) {
  157. writer.WriteLine("{0}MemoizedSerializedSize = dataSize;", Name);
  158. }
  159. writer.Outdent();
  160. writer.WriteLine("}");
  161. }
  162. }
  163. }