Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtoGen/RepeatedEnumFieldGenerator.cs @ 13325

Last change on this file since 13325 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.8 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 RepeatedEnumFieldGenerator : FieldGeneratorBase, IFieldSourceGenerator
42    {
43        internal RepeatedEnumFieldGenerator(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            AddDeprecatedFlag(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            AddDeprecatedFlag(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            AddDeprecatedFlag(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            AddDeprecatedFlag(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            AddDeprecatedFlag(writer);
89            writer.WriteLine("public Builder Set{0}(int index, {1} value) {{", PropertyName, TypeName);
90            writer.WriteLine("  PrepareBuilder();");
91            writer.WriteLine("  result.{0}_[index] = value;", Name);
92            writer.WriteLine("  return this;");
93            writer.WriteLine("}");
94            AddDeprecatedFlag(writer);
95            writer.WriteLine("public Builder Add{0}({1} value) {{", PropertyName, TypeName);
96            writer.WriteLine("  PrepareBuilder();");
97            writer.WriteLine("  result.{0}_.Add(value);", Name, TypeName);
98            writer.WriteLine("  return this;");
99            writer.WriteLine("}");
100            AddDeprecatedFlag(writer);
101            writer.WriteLine("public Builder AddRange{0}(scg::IEnumerable<{1}> values) {{", PropertyName, TypeName);
102            writer.WriteLine("  PrepareBuilder();");
103            writer.WriteLine("  result.{0}_.Add(values);", Name);
104            writer.WriteLine("  return this;");
105            writer.WriteLine("}");
106            AddDeprecatedFlag(writer);
107            writer.WriteLine("public Builder Clear{0}() {{", PropertyName);
108            writer.WriteLine("  PrepareBuilder();");
109            writer.WriteLine("  result.{0}_.Clear();", Name);
110            writer.WriteLine("  return this;");
111            writer.WriteLine("}");
112        }
113
114        public void GenerateMergingCode(TextGenerator writer)
115        {
116            writer.WriteLine("if (other.{0}_.Count != 0) {{", Name);
117            writer.WriteLine("  result.{0}_.Add(other.{0}_);", Name);
118            writer.WriteLine("}");
119        }
120
121        public void GenerateBuildingCode(TextGenerator writer)
122        {
123            writer.WriteLine("{0}_.MakeReadOnly();", Name);
124        }
125
126        public void GenerateParsingCode(TextGenerator writer)
127        {
128            writer.WriteLine("scg::ICollection<object> unknownItems;");
129            writer.WriteLine("input.ReadEnumArray<{0}>(tag, field_name, result.{1}_, out unknownItems);", TypeName, Name);
130            if (!UseLiteRuntime)
131            {
132                writer.WriteLine("if (unknownItems != null) {");
133                writer.WriteLine("  if (unknownFields == null) {");
134                writer.WriteLine("    unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields);");
135                writer.WriteLine("  }");
136                writer.WriteLine("  foreach (object rawValue in unknownItems)");
137                writer.WriteLine("    if (rawValue is int)");
138                writer.WriteLine("      unknownFields.MergeVarintField({0}, (ulong)(int)rawValue);", Number);
139                writer.WriteLine("}");
140            }
141        }
142
143        public void GenerateSerializationCode(TextGenerator writer)
144        {
145            writer.WriteLine("if ({0}_.Count > 0) {{", Name);
146            writer.Indent();
147            if (Descriptor.IsPacked)
148            {
149                writer.WriteLine(
150                    "output.WritePackedEnumArray({0}, field_names[{2}], {1}MemoizedSerializedSize, {1}_);", Number, Name,
151                    FieldOrdinal, Descriptor.FieldType);
152            }
153            else
154            {
155                writer.WriteLine("output.WriteEnumArray({0}, field_names[{2}], {1}_);", Number, Name, FieldOrdinal,
156                                 Descriptor.FieldType);
157            }
158            writer.Outdent();
159            writer.WriteLine("}");
160        }
161
162        public void GenerateSerializedSizeCode(TextGenerator writer)
163        {
164            writer.WriteLine("{");
165            writer.Indent();
166            writer.WriteLine("int dataSize = 0;");
167            writer.WriteLine("if ({0}_.Count > 0) {{", Name);
168            writer.Indent();
169            writer.WriteLine("foreach ({0} element in {1}_) {{", TypeName, Name);
170            writer.WriteLine("  dataSize += pb::CodedOutputStream.ComputeEnumSizeNoTag((int) element);");
171            writer.WriteLine("}");
172            writer.WriteLine("size += dataSize;");
173            int tagSize = CodedOutputStream.ComputeTagSize(Descriptor.FieldNumber);
174            if (Descriptor.IsPacked)
175            {
176                writer.WriteLine("size += {0};", tagSize);
177                writer.WriteLine("size += pb::CodedOutputStream.ComputeRawVarint32Size((uint) dataSize);");
178            }
179            else
180            {
181                writer.WriteLine("size += {0} * {1}_.Count;", tagSize, Name);
182            }
183            writer.Outdent();
184            writer.WriteLine("}");
185            // cache the data size for packed fields.
186            if (Descriptor.IsPacked)
187            {
188                writer.WriteLine("{0}MemoizedSerializedSize = dataSize;", Name);
189            }
190            writer.Outdent();
191            writer.WriteLine("}");
192        }
193
194        public override void WriteHash(TextGenerator writer)
195        {
196            writer.WriteLine("foreach({0} i in {1}_)", TypeName, Name);
197            writer.WriteLine("  hash ^= i.GetHashCode();");
198        }
199
200        public override void WriteEquals(TextGenerator writer)
201        {
202            writer.WriteLine("if({0}_.Count != other.{0}_.Count) return false;", Name);
203            writer.WriteLine("for(int ix=0; ix < {0}_.Count; ix++)", Name);
204            writer.WriteLine("  if(!{0}_[ix].Equals(other.{0}_[ix])) return false;", Name);
205        }
206
207        public override void WriteToString(TextGenerator writer)
208        {
209            writer.WriteLine("PrintField(\"{0}\", {1}_, writer);", Descriptor.Name, Name);
210        }
211    }
212}
Note: See TracBrowser for help on using the repository browser.