Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtoGen/RepeatedPrimitiveFieldGenerator.cs @ 11920

Last change on this file since 11920 was 8295, checked in by abeham, 12 years ago

#1897:

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