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 RepeatedPrimitiveFieldGenerator : FieldGeneratorBase, IFieldSourceGenerator {
|
---|
40 |
|
---|
41 | internal RepeatedPrimitiveFieldGenerator(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 | AddClsComplianceCheck(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 |
|
---|
55 | // TODO(jonskeet): Redundant API calls? Possibly - include for portability though. Maybe create an option.
|
---|
56 | writer.WriteLine("public int {0}Count {{", PropertyName);
|
---|
57 | writer.WriteLine(" get {{ return {0}_.Count; }}", Name);
|
---|
58 | writer.WriteLine("}");
|
---|
59 |
|
---|
60 | AddClsComplianceCheck(writer);
|
---|
61 | writer.WriteLine("public {0} Get{1}(int index) {{", TypeName, PropertyName);
|
---|
62 | writer.WriteLine(" return {0}_[index];", Name);
|
---|
63 | writer.WriteLine("}");
|
---|
64 | }
|
---|
65 |
|
---|
66 | public void GenerateBuilderMembers(TextGenerator writer) {
|
---|
67 | // Note: We can return the original list here, because we make it unmodifiable when we build
|
---|
68 | // We return it via IPopsicleList so that collection initializers work more pleasantly.
|
---|
69 | AddClsComplianceCheck(writer);
|
---|
70 | writer.WriteLine("public pbc::IPopsicleList<{0}> {1}List {{", TypeName, PropertyName);
|
---|
71 | writer.WriteLine(" get {{ return result.{0}_; }}", Name);
|
---|
72 | writer.WriteLine("}");
|
---|
73 | writer.WriteLine("public int {0}Count {{", PropertyName);
|
---|
74 | writer.WriteLine(" get {{ return result.{0}Count; }}", PropertyName);
|
---|
75 | writer.WriteLine("}");
|
---|
76 | AddClsComplianceCheck(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 | AddClsComplianceCheck(writer);
|
---|
81 | writer.WriteLine("public Builder Set{0}(int index, {1} value) {{", PropertyName, TypeName);
|
---|
82 | AddNullCheck(writer);
|
---|
83 | writer.WriteLine(" result.{0}_[index] = value;", Name);
|
---|
84 | writer.WriteLine(" return this;");
|
---|
85 | writer.WriteLine("}");
|
---|
86 | AddClsComplianceCheck(writer);
|
---|
87 | writer.WriteLine("public Builder Add{0}({1} value) {{", PropertyName, TypeName);
|
---|
88 | AddNullCheck(writer);
|
---|
89 | writer.WriteLine(" result.{0}_.Add(value);", Name, TypeName);
|
---|
90 | writer.WriteLine(" return this;");
|
---|
91 | writer.WriteLine("}");
|
---|
92 | AddClsComplianceCheck(writer);
|
---|
93 | writer.WriteLine("public Builder AddRange{0}(scg::IEnumerable<{1}> values) {{", PropertyName, TypeName);
|
---|
94 | writer.WriteLine(" base.AddRange(values, result.{0}_);", Name);
|
---|
95 | writer.WriteLine(" return this;");
|
---|
96 | writer.WriteLine("}");
|
---|
97 | writer.WriteLine("public Builder Clear{0}() {{", PropertyName);
|
---|
98 | writer.WriteLine(" result.{0}_.Clear();", Name);
|
---|
99 | writer.WriteLine(" return this;");
|
---|
100 | writer.WriteLine("}");
|
---|
101 | }
|
---|
102 |
|
---|
103 | public void GenerateMergingCode(TextGenerator writer) {
|
---|
104 | writer.WriteLine("if (other.{0}_.Count != 0) {{", Name);
|
---|
105 | writer.WriteLine(" base.AddRange(other.{0}_, result.{0}_);", Name);
|
---|
106 | writer.WriteLine("}");
|
---|
107 | }
|
---|
108 |
|
---|
109 | public void GenerateBuildingCode(TextGenerator writer) {
|
---|
110 | writer.WriteLine("result.{0}_.MakeReadOnly();", Name);
|
---|
111 | }
|
---|
112 |
|
---|
113 | public void GenerateParsingCode(TextGenerator writer) {
|
---|
114 | if (Descriptor.IsPacked) {
|
---|
115 | writer.WriteLine("int length = input.ReadInt32();");
|
---|
116 | writer.WriteLine("int limit = input.PushLimit(length);");
|
---|
117 | writer.WriteLine("while (!input.ReachedLimit) {");
|
---|
118 | writer.WriteLine(" Add{0}(input.Read{1}());", PropertyName, CapitalizedTypeName);
|
---|
119 | writer.WriteLine("}");
|
---|
120 | writer.WriteLine("input.PopLimit(limit);");
|
---|
121 | } else {
|
---|
122 | writer.WriteLine("Add{0}(input.Read{1}());", PropertyName, CapitalizedTypeName);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | public void GenerateSerializationCode(TextGenerator writer) {
|
---|
127 | writer.WriteLine("if ({0}_.Count > 0) {{", Name);
|
---|
128 | writer.Indent();
|
---|
129 | if (Descriptor.IsPacked) {
|
---|
130 | writer.WriteLine("output.WriteRawVarint32({0});", WireFormat.MakeTag(Descriptor));
|
---|
131 | writer.WriteLine("output.WriteRawVarint32((uint) {0}MemoizedSerializedSize);", Name);
|
---|
132 | writer.WriteLine("foreach ({0} element in {1}_) {{", TypeName, Name);
|
---|
133 | writer.WriteLine(" output.Write{0}NoTag(element);", CapitalizedTypeName);
|
---|
134 | writer.WriteLine("}");
|
---|
135 | } else {
|
---|
136 | writer.WriteLine("foreach ({0} element in {1}_) {{", TypeName, Name);
|
---|
137 | writer.WriteLine(" output.Write{0}({1}, element);", CapitalizedTypeName, Number);
|
---|
138 | writer.WriteLine("}");
|
---|
139 | }
|
---|
140 | writer.Outdent();
|
---|
141 | writer.WriteLine("}");
|
---|
142 | }
|
---|
143 |
|
---|
144 | public void GenerateSerializedSizeCode(TextGenerator writer) {
|
---|
145 | writer.WriteLine("{");
|
---|
146 | writer.Indent();
|
---|
147 | writer.WriteLine("int dataSize = 0;");
|
---|
148 | if (FixedSize == -1) {
|
---|
149 | writer.WriteLine("foreach ({0} element in {1}List) {{", TypeName, PropertyName);
|
---|
150 | writer.WriteLine(" dataSize += pb::CodedOutputStream.Compute{0}SizeNoTag(element);", CapitalizedTypeName, Number);
|
---|
151 | writer.WriteLine("}");
|
---|
152 | } else {
|
---|
153 | writer.WriteLine("dataSize = {0} * {1}_.Count;", FixedSize, Name);
|
---|
154 | }
|
---|
155 | writer.WriteLine("size += dataSize;");
|
---|
156 | int tagSize = CodedOutputStream.ComputeTagSize(Descriptor.FieldNumber);
|
---|
157 | if (Descriptor.IsPacked) {
|
---|
158 | writer.WriteLine("if ({0}_.Count != 0) {{", Name);
|
---|
159 | writer.WriteLine(" size += {0} + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);", tagSize);
|
---|
160 | writer.WriteLine("}");
|
---|
161 | } else {
|
---|
162 | writer.WriteLine("size += {0} * {1}_.Count;", tagSize, Name);
|
---|
163 | }
|
---|
164 | // cache the data size for packed fields.
|
---|
165 | if (Descriptor.IsPacked) {
|
---|
166 | writer.WriteLine("{0}MemoizedSerializedSize = dataSize;", Name);
|
---|
167 | }
|
---|
168 | writer.Outdent();
|
---|
169 | writer.WriteLine("}");
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|