Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtoGen/RepeatedMessageFieldGenerator.cs @ 10864

Last change on this file since 10864 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: 8.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 RepeatedMessageFieldGenerator : FieldGeneratorBase, IFieldSourceGenerator
42    {
43        internal RepeatedMessageFieldGenerator(FieldDescriptor descriptor, int fieldOrdinal)
44            : base(descriptor, fieldOrdinal)
45        {
46        }
47
48        public void GenerateMembers(TextGenerator writer)
49        {
50            writer.WriteLine("private pbc::PopsicleList<{0}> {1}_ = new pbc::PopsicleList<{0}>();", TypeName, Name);
51            AddDeprecatedFlag(writer);
52            writer.WriteLine("public scg::IList<{0}> {1}List {{", TypeName, PropertyName);
53            writer.WriteLine("  get {{ return {0}_; }}", Name);
54            writer.WriteLine("}");
55
56            // TODO(jonskeet): Redundant API calls? Possibly - include for portability though. Maybe create an option.
57            AddDeprecatedFlag(writer);
58            writer.WriteLine("public int {0}Count {{", PropertyName);
59            writer.WriteLine("  get {{ return {0}_.Count; }}", Name);
60            writer.WriteLine("}");
61
62            AddDeprecatedFlag(writer);
63            writer.WriteLine("public {0} Get{1}(int index) {{", TypeName, PropertyName);
64            writer.WriteLine("  return {0}_[index];", Name);
65            writer.WriteLine("}");
66        }
67
68        public void GenerateBuilderMembers(TextGenerator writer)
69        {
70            // Note:  We can return the original list here, because we make it unmodifiable when we build
71            // We return it via IPopsicleList so that collection initializers work more pleasantly.
72            AddDeprecatedFlag(writer);
73            writer.WriteLine("public pbc::IPopsicleList<{0}> {1}List {{", TypeName, PropertyName);
74            writer.WriteLine("  get {{ return PrepareBuilder().{0}_; }}", Name);
75            writer.WriteLine("}");
76            AddDeprecatedFlag(writer);
77            writer.WriteLine("public int {0}Count {{", PropertyName);
78            writer.WriteLine("  get {{ return result.{0}Count; }}", PropertyName);
79            writer.WriteLine("}");
80            AddDeprecatedFlag(writer);
81            writer.WriteLine("public {0} Get{1}(int index) {{", TypeName, PropertyName);
82            writer.WriteLine("  return result.Get{0}(index);", PropertyName);
83            writer.WriteLine("}");
84            AddDeprecatedFlag(writer);
85            writer.WriteLine("public Builder Set{0}(int index, {1} value) {{", PropertyName, TypeName);
86            AddNullCheck(writer);
87            writer.WriteLine("  PrepareBuilder();");
88            writer.WriteLine("  result.{0}_[index] = value;", Name);
89            writer.WriteLine("  return this;");
90            writer.WriteLine("}");
91            // Extra overload for builder (just on messages)
92            AddDeprecatedFlag(writer);
93            writer.WriteLine("public Builder Set{0}(int index, {1}.Builder builderForValue) {{", PropertyName, TypeName);
94            AddNullCheck(writer, "builderForValue");
95            writer.WriteLine("  PrepareBuilder();");
96            writer.WriteLine("  result.{0}_[index] = builderForValue.Build();", Name);
97            writer.WriteLine("  return this;");
98            writer.WriteLine("}");
99            AddDeprecatedFlag(writer);
100            writer.WriteLine("public Builder Add{0}({1} value) {{", PropertyName, TypeName);
101            AddNullCheck(writer);
102            writer.WriteLine("  PrepareBuilder();");
103            writer.WriteLine("  result.{0}_.Add(value);", Name, TypeName);
104            writer.WriteLine("  return this;");
105            writer.WriteLine("}");
106            // Extra overload for builder (just on messages)
107            AddDeprecatedFlag(writer);
108            writer.WriteLine("public Builder Add{0}({1}.Builder builderForValue) {{", PropertyName, TypeName);
109            AddNullCheck(writer, "builderForValue");
110            writer.WriteLine("  PrepareBuilder();");
111            writer.WriteLine("  result.{0}_.Add(builderForValue.Build());", Name);
112            writer.WriteLine("  return this;");
113            writer.WriteLine("}");
114            AddDeprecatedFlag(writer);
115            writer.WriteLine("public Builder AddRange{0}(scg::IEnumerable<{1}> values) {{", PropertyName, TypeName);
116            writer.WriteLine("  PrepareBuilder();");
117            writer.WriteLine("  result.{0}_.Add(values);", Name);
118            writer.WriteLine("  return this;");
119            writer.WriteLine("}");
120            AddDeprecatedFlag(writer);
121            writer.WriteLine("public Builder Clear{0}() {{", PropertyName);
122            writer.WriteLine("  PrepareBuilder();");
123            writer.WriteLine("  result.{0}_.Clear();", Name);
124            writer.WriteLine("  return this;");
125            writer.WriteLine("}");
126        }
127
128        public void GenerateMergingCode(TextGenerator writer)
129        {
130            writer.WriteLine("if (other.{0}_.Count != 0) {{", Name);
131            writer.WriteLine("  result.{0}_.Add(other.{0}_);", Name);
132            writer.WriteLine("}");
133        }
134
135        public void GenerateBuildingCode(TextGenerator writer)
136        {
137            writer.WriteLine("{0}_.MakeReadOnly();", Name);
138        }
139
140        public void GenerateParsingCode(TextGenerator writer)
141        {
142            writer.WriteLine(
143                "input.Read{0}Array(tag, field_name, result.{1}_, {2}.DefaultInstance, extensionRegistry);",
144                MessageOrGroup, Name, TypeName);
145        }
146
147        public void GenerateSerializationCode(TextGenerator writer)
148        {
149            writer.WriteLine("if ({0}_.Count > 0) {{", Name);
150            writer.Indent();
151            writer.WriteLine("output.Write{0}Array({1}, field_names[{3}], {2}_);", MessageOrGroup, Number, Name,
152                             FieldOrdinal, Descriptor.FieldType);
153            writer.Outdent();
154            writer.WriteLine("}");
155        }
156
157        public void GenerateSerializedSizeCode(TextGenerator writer)
158        {
159            writer.WriteLine("foreach ({0} element in {1}List) {{", TypeName, PropertyName);
160            writer.WriteLine("  size += pb::CodedOutputStream.Compute{0}Size({1}, element);", MessageOrGroup, Number);
161            writer.WriteLine("}");
162        }
163
164        public override void WriteHash(TextGenerator writer)
165        {
166            writer.WriteLine("foreach({0} i in {1}_)", TypeName, Name);
167            writer.WriteLine("  hash ^= i.GetHashCode();");
168        }
169
170        public override void WriteEquals(TextGenerator writer)
171        {
172            writer.WriteLine("if({0}_.Count != other.{0}_.Count) return false;", Name);
173            writer.WriteLine("for(int ix=0; ix < {0}_.Count; ix++)", Name);
174            writer.WriteLine("  if(!{0}_[ix].Equals(other.{0}_[ix])) return false;", Name);
175        }
176
177        public override void WriteToString(TextGenerator writer)
178        {
179            writer.WriteLine("PrintField(\"{0}\", {1}_, writer);",
180                             Descriptor.FieldType == FieldType.Group ? Descriptor.MessageType.Name : Descriptor.Name,
181                             Name);
182        }
183    }
184}
Note: See TracBrowser for help on using the repository browser.