Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.1/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/0.9.1/ProtobufCS/src/ProtoGen/RepeatedMessageFieldGenerator.cs

Last change on this file was 3857, checked in by abeham, 14 years ago

#866

  • Added protobuf-csharp-port project source to ExtLibs
File size: 6.7 KB
Line 
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
35using Google.ProtocolBuffers.Descriptors;
36
37namespace Google.ProtocolBuffers.ProtoGen {
38  internal class RepeatedMessageFieldGenerator : FieldGeneratorBase, IFieldSourceGenerator {
39
40    internal RepeatedMessageFieldGenerator(FieldDescriptor descriptor)
41      : base(descriptor) {
42    }
43   
44    public void GenerateMembers(TextGenerator writer) {
45      writer.WriteLine("private pbc::PopsicleList<{0}> {1}_ = new pbc::PopsicleList<{0}>();", TypeName, Name);
46      writer.WriteLine("public scg::IList<{0}> {1}List {{", TypeName, PropertyName);
47      writer.WriteLine("  get {{ return {0}_; }}", Name);
48      writer.WriteLine("}");
49
50      // TODO(jonskeet): Redundant API calls? Possibly - include for portability though. Maybe create an option.
51      writer.WriteLine("public int {0}Count {{", PropertyName);
52      writer.WriteLine("  get {{ return {0}_.Count; }}", Name);
53      writer.WriteLine("}");
54
55      writer.WriteLine("public {0} Get{1}(int index) {{", TypeName, PropertyName);
56      writer.WriteLine("  return {0}_[index];", Name);
57      writer.WriteLine("}");
58    }   
59
60    public void GenerateBuilderMembers(TextGenerator writer) {
61      // Note:  We can return the original list here, because we make it unmodifiable when we build
62      // We return it via IPopsicleList so that collection initializers work more pleasantly.
63      writer.WriteLine("public pbc::IPopsicleList<{0}> {1}List {{", TypeName, PropertyName);
64      writer.WriteLine("  get {{ return result.{0}_; }}", Name);
65      writer.WriteLine("}");
66      writer.WriteLine("public int {0}Count {{", PropertyName);
67      writer.WriteLine("  get {{ return result.{0}Count; }}", PropertyName);
68      writer.WriteLine("}");
69      writer.WriteLine("public {0} Get{1}(int index) {{", TypeName, PropertyName);
70      writer.WriteLine("  return result.Get{0}(index);", PropertyName);
71      writer.WriteLine("}");
72      writer.WriteLine("public Builder Set{0}(int index, {1} value) {{", PropertyName, TypeName);
73      AddNullCheck(writer);
74      writer.WriteLine("  result.{0}_[index] = value;", Name);
75      writer.WriteLine("  return this;");
76      writer.WriteLine("}");
77      // Extra overload for builder (just on messages)
78      writer.WriteLine("public Builder Set{0}(int index, {1}.Builder builderForValue) {{", PropertyName, TypeName);
79      AddNullCheck(writer, "builderForValue");
80      writer.WriteLine("  result.{0}_[index] = builderForValue.Build();", Name);
81      writer.WriteLine("  return this;");
82      writer.WriteLine("}");
83      writer.WriteLine("public Builder Add{0}({1} value) {{", PropertyName, TypeName);
84      AddNullCheck(writer);
85      writer.WriteLine("  result.{0}_.Add(value);", Name, TypeName);
86      writer.WriteLine("  return this;");
87      writer.WriteLine("}");
88      // Extra overload for builder (just on messages)
89      writer.WriteLine("public Builder Add{0}({1}.Builder builderForValue) {{", PropertyName, TypeName);
90      AddNullCheck(writer, "builderForValue");
91      writer.WriteLine("  result.{0}_.Add(builderForValue.Build());", Name);
92      writer.WriteLine("  return this;");
93      writer.WriteLine("}");
94      writer.WriteLine("public Builder AddRange{0}(scg::IEnumerable<{1}> values) {{", PropertyName, TypeName);
95      writer.WriteLine("  base.AddRange(values, result.{0}_);", Name);
96      writer.WriteLine("  return this;");
97      writer.WriteLine("}");
98      writer.WriteLine("public Builder Clear{0}() {{", PropertyName);
99      writer.WriteLine("  result.{0}_.Clear();", Name);
100      writer.WriteLine("  return this;");
101      writer.WriteLine("}");
102    }   
103
104    public void GenerateMergingCode(TextGenerator writer) {
105      writer.WriteLine("if (other.{0}_.Count != 0) {{", Name);
106      writer.WriteLine("  base.AddRange(other.{0}_, result.{0}_);", Name);
107      writer.WriteLine("}");
108    }
109
110    public void GenerateBuildingCode(TextGenerator writer) {
111      writer.WriteLine("result.{0}_.MakeReadOnly();", Name);
112    }
113
114    public void GenerateParsingCode(TextGenerator writer) {
115      writer.WriteLine("{0}.Builder subBuilder = {0}.CreateBuilder();", TypeName);
116      if (Descriptor.FieldType == FieldType.Group) {
117        writer.WriteLine("input.ReadGroup({0}, subBuilder, extensionRegistry);", Number);
118      } else {
119        writer.WriteLine("input.ReadMessage(subBuilder, extensionRegistry);");
120      }
121      writer.WriteLine("Add{0}(subBuilder.BuildPartial());", PropertyName);
122    }
123
124    public void GenerateSerializationCode(TextGenerator writer) {
125      writer.WriteLine("foreach ({0} element in {1}List) {{", TypeName, PropertyName);
126      writer.WriteLine("  output.Write{0}({1}, element);", MessageOrGroup, Number);
127      writer.WriteLine("}");
128    }
129
130    public void GenerateSerializedSizeCode(TextGenerator writer) {
131      writer.WriteLine("foreach ({0} element in {1}List) {{", TypeName, PropertyName);
132      writer.WriteLine("  size += pb::CodedOutputStream.Compute{0}Size({1}, element);", MessageOrGroup, Number);
133      writer.WriteLine("}");
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.