Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtoGen/MessageFieldGenerator.cs @ 11623

Last change on this file since 11623 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.1 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 MessageFieldGenerator : FieldGeneratorBase, IFieldSourceGenerator
42    {
43        internal MessageFieldGenerator(FieldDescriptor descriptor, int fieldOrdinal)
44            : base(descriptor, fieldOrdinal)
45        {
46        }
47
48        public void GenerateMembers(TextGenerator writer)
49        {
50            writer.WriteLine("private bool has{0};", PropertyName);
51            writer.WriteLine("private {0} {1}_;", TypeName, Name);
52            AddDeprecatedFlag(writer);
53            writer.WriteLine("public bool Has{0} {{", PropertyName);
54            writer.WriteLine("  get {{ return has{0}; }}", PropertyName);
55            writer.WriteLine("}");
56            AddDeprecatedFlag(writer);
57            writer.WriteLine("public {0} {1} {{", TypeName, PropertyName);
58            writer.WriteLine("  get {{ return {0}_ ?? {1}; }}", Name, DefaultValue);
59            writer.WriteLine("}");
60        }
61
62        public void GenerateBuilderMembers(TextGenerator writer)
63        {
64            AddDeprecatedFlag(writer);
65            writer.WriteLine("public bool Has{0} {{", PropertyName);
66            writer.WriteLine(" get {{ return result.has{0}; }}", PropertyName);
67            writer.WriteLine("}");
68            AddDeprecatedFlag(writer);
69            writer.WriteLine("public {0} {1} {{", TypeName, PropertyName);
70            writer.WriteLine("  get {{ return result.{0}; }}", PropertyName);
71            writer.WriteLine("  set {{ Set{0}(value); }}", PropertyName);
72            writer.WriteLine("}");
73            AddDeprecatedFlag(writer);
74            writer.WriteLine("public Builder Set{0}({1} value) {{", PropertyName, TypeName);
75            AddNullCheck(writer);
76            writer.WriteLine("  PrepareBuilder();");
77            writer.WriteLine("  result.has{0} = true;", PropertyName);
78            writer.WriteLine("  result.{0}_ = value;", Name);
79            writer.WriteLine("  return this;");
80            writer.WriteLine("}");
81            AddDeprecatedFlag(writer);
82            writer.WriteLine("public Builder Set{0}({1}.Builder builderForValue) {{", PropertyName, TypeName);
83            AddNullCheck(writer, "builderForValue");
84            writer.WriteLine("  PrepareBuilder();");
85            writer.WriteLine("  result.has{0} = true;", PropertyName);
86            writer.WriteLine("  result.{0}_ = builderForValue.Build();", Name);
87            writer.WriteLine("  return this;");
88            writer.WriteLine("}");
89            AddDeprecatedFlag(writer);
90            writer.WriteLine("public Builder Merge{0}({1} value) {{", PropertyName, TypeName);
91            AddNullCheck(writer);
92            writer.WriteLine("  PrepareBuilder();");
93            writer.WriteLine("  if (result.has{0} &&", PropertyName);
94            writer.WriteLine("      result.{0}_ != {1}) {{", Name, DefaultValue);
95            writer.WriteLine("      result.{0}_ = {1}.CreateBuilder(result.{0}_).MergeFrom(value).BuildPartial();", Name,
96                             TypeName);
97            writer.WriteLine("  } else {");
98            writer.WriteLine("    result.{0}_ = value;", Name);
99            writer.WriteLine("  }");
100            writer.WriteLine("  result.has{0} = true;", PropertyName);
101            writer.WriteLine("  return this;");
102            writer.WriteLine("}");
103            AddDeprecatedFlag(writer);
104            writer.WriteLine("public Builder Clear{0}() {{", PropertyName);
105            writer.WriteLine("  PrepareBuilder();");
106            writer.WriteLine("  result.has{0} = false;", PropertyName);
107            writer.WriteLine("  result.{0}_ = null;", Name);
108            writer.WriteLine("  return this;");
109            writer.WriteLine("}");
110        }
111
112        public void GenerateMergingCode(TextGenerator writer)
113        {
114            writer.WriteLine("if (other.Has{0}) {{", PropertyName);
115            writer.WriteLine("  Merge{0}(other.{0});", PropertyName);
116            writer.WriteLine("}");
117        }
118
119        public void GenerateBuildingCode(TextGenerator writer)
120        {
121            // Nothing to do for singular fields
122        }
123
124        public void GenerateParsingCode(TextGenerator writer)
125        {
126            writer.WriteLine("{0}.Builder subBuilder = {0}.CreateBuilder();", TypeName);
127            writer.WriteLine("if (result.has{0}) {{", PropertyName);
128            writer.WriteLine("  subBuilder.MergeFrom({0});", PropertyName);
129            writer.WriteLine("}");
130            if (Descriptor.FieldType == FieldType.Group)
131            {
132                writer.WriteLine("input.ReadGroup({0}, subBuilder, extensionRegistry);", Number);
133            }
134            else
135            {
136                writer.WriteLine("input.ReadMessage(subBuilder, extensionRegistry);");
137            }
138            writer.WriteLine("{0} = subBuilder.BuildPartial();", PropertyName);
139        }
140
141        public void GenerateSerializationCode(TextGenerator writer)
142        {
143            writer.WriteLine("if (has{0}) {{", PropertyName);
144            writer.WriteLine("  output.Write{0}({1}, field_names[{3}], {2});", MessageOrGroup, Number, PropertyName,
145                             FieldOrdinal);
146            writer.WriteLine("}");
147        }
148
149        public void GenerateSerializedSizeCode(TextGenerator writer)
150        {
151            writer.WriteLine("if (has{0}) {{", PropertyName);
152            writer.WriteLine("  size += pb::CodedOutputStream.Compute{0}Size({1}, {2});",
153                             MessageOrGroup, Number, PropertyName);
154            writer.WriteLine("}");
155        }
156
157        public override void WriteHash(TextGenerator writer)
158        {
159            writer.WriteLine("if (has{0}) hash ^= {1}_.GetHashCode();", PropertyName, Name);
160        }
161
162        public override void WriteEquals(TextGenerator writer)
163        {
164            writer.WriteLine("if (has{0} != other.has{0} || (has{0} && !{1}_.Equals(other.{1}_))) return false;",
165                             PropertyName, Name);
166        }
167
168        public override void WriteToString(TextGenerator writer)
169        {
170            writer.WriteLine("PrintField(\"{2}\", has{0}, {1}_, writer);", PropertyName, Name,
171                             Descriptor.FieldType == FieldType.Group ? Descriptor.MessageType.Name : Descriptor.Name);
172        }
173    }
174}
Note: See TracBrowser for help on using the repository browser.