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