blob: 0002c2a0ea616ffcbb470dfe6244df6ec63fb437 [file] [log] [blame]
csharptest71f662c2011-05-20 15:15:34 -05001#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 // TODO(jonskeet): Refactor this. There's loads of common code here.
42 internal class PrimitiveFieldGenerator : FieldGeneratorBase, IFieldSourceGenerator
43 {
csharptest920b09a2011-06-08 20:13:29 -050044 internal PrimitiveFieldGenerator(FieldDescriptor descriptor, int fieldOrdinal)
45 : base(descriptor, fieldOrdinal)
csharptest71f662c2011-05-20 15:15:34 -050046 {
47 }
48
49 public void GenerateMembers(TextGenerator writer)
50 {
51 writer.WriteLine("private bool has{0};", PropertyName);
csharptestced18e12011-06-09 19:47:56 -050052 writer.WriteLine("private {0} {1}_{2};", TypeName, Name, HasDefaultValue ? " = " + DefaultValue : "");
csharptest71f662c2011-05-20 15:15:34 -050053 writer.WriteLine("public bool Has{0} {{", PropertyName);
54 writer.WriteLine(" get {{ return has{0}; }}", PropertyName);
55 writer.WriteLine("}");
56 AddClsComplianceCheck(writer);
57 writer.WriteLine("public {0} {1} {{", TypeName, PropertyName);
58 writer.WriteLine(" get {{ return {0}_; }}", Name);
59 writer.WriteLine("}");
60 }
61
62 public void GenerateBuilderMembers(TextGenerator writer)
63 {
64 writer.WriteLine("public bool Has{0} {{", PropertyName);
csharptestc671a4b2011-06-08 11:51:24 -050065 writer.WriteLine(" get {{ return result.has{0}; }}", PropertyName);
csharptest71f662c2011-05-20 15:15:34 -050066 writer.WriteLine("}");
67 AddClsComplianceCheck(writer);
68 writer.WriteLine("public {0} {1} {{", TypeName, PropertyName);
69 writer.WriteLine(" get {{ return result.{0}; }}", PropertyName);
70 writer.WriteLine(" set {{ Set{0}(value); }}", PropertyName);
71 writer.WriteLine("}");
72 AddClsComplianceCheck(writer);
73 writer.WriteLine("public Builder Set{0}({1} value) {{", PropertyName, TypeName);
74 AddNullCheck(writer);
75 writer.WriteLine(" result.has{0} = true;", PropertyName);
76 writer.WriteLine(" result.{0}_ = value;", Name);
77 writer.WriteLine(" return this;");
78 writer.WriteLine("}");
79 writer.WriteLine("public Builder Clear{0}() {{", PropertyName);
80 writer.WriteLine(" result.has{0} = false;", PropertyName);
81 writer.WriteLine(" result.{0}_ = {1};", Name, DefaultValue);
82 writer.WriteLine(" return this;");
83 writer.WriteLine("}");
84 }
85
86 public void GenerateMergingCode(TextGenerator writer)
87 {
88 writer.WriteLine("if (other.Has{0}) {{", PropertyName);
89 writer.WriteLine(" {0} = other.{0};", PropertyName);
90 writer.WriteLine("}");
91 }
92
93 public void GenerateBuildingCode(TextGenerator writer)
94 {
95 // Nothing to do here for primitive types
96 }
97
98 public void GenerateParsingCode(TextGenerator writer)
99 {
csharptest920b09a2011-06-08 20:13:29 -0500100 writer.WriteLine("result.has{0} = input.Read{1}(ref result.{2}_);", PropertyName, CapitalizedTypeName, Name);
csharptest71f662c2011-05-20 15:15:34 -0500101 }
102
103 public void GenerateSerializationCode(TextGenerator writer)
104 {
csharptestc671a4b2011-06-08 11:51:24 -0500105 writer.WriteLine("if (has{0}) {{", PropertyName);
csharptest920b09a2011-06-08 20:13:29 -0500106 writer.WriteLine(" output.Write{0}({1}, field_names[{3}], {2});", CapitalizedTypeName, Number, PropertyName, FieldOrdinal);
csharptest71f662c2011-05-20 15:15:34 -0500107 writer.WriteLine("}");
108 }
109
110 public void GenerateSerializedSizeCode(TextGenerator writer)
111 {
csharptestc671a4b2011-06-08 11:51:24 -0500112 writer.WriteLine("if (has{0}) {{", PropertyName);
csharptest71f662c2011-05-20 15:15:34 -0500113 writer.WriteLine(" size += pb::CodedOutputStream.Compute{0}Size({1}, {2});",
114 CapitalizedTypeName, Number, PropertyName);
115 writer.WriteLine("}");
116 }
117
118 public override void WriteHash(TextGenerator writer)
119 {
120 writer.WriteLine("if (has{0}) hash ^= {1}_.GetHashCode();", PropertyName, Name);
121 }
122
123 public override void WriteEquals(TextGenerator writer)
124 {
125 writer.WriteLine("if (has{0} != other.has{0} || (has{0} && !{1}_.Equals(other.{1}_))) return false;",
126 PropertyName, Name);
127 }
128
129 public override void WriteToString(TextGenerator writer)
130 {
131 writer.WriteLine("PrintField(\"{0}\", has{1}, {2}_, writer);", Descriptor.Name, PropertyName, Name);
132 }
133 }
134}