blob: dbc203d08706af6abc8826216632bf9cb671fd46 [file] [log] [blame]
Jon Skeet0aac0e42009-09-09 18:48:02 +01001#region Copyright notice and license
Jon Skeetad748532009-06-25 16:55:58 +01002// 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.
Jon Skeet0aac0e42009-09-09 18:48:02 +010033#endregion
34
Jon Skeet68036862008-10-22 13:30:34 +010035using Google.ProtocolBuffers.Descriptors;
36
37namespace Google.ProtocolBuffers.ProtoGen {
38 // TODO(jonskeet): Refactor this. There's loads of common code here.
39 internal class PrimitiveFieldGenerator : FieldGeneratorBase, IFieldSourceGenerator {
40
41 internal PrimitiveFieldGenerator(FieldDescriptor descriptor)
42 : base(descriptor) {
43 }
44
45 public void GenerateMembers(TextGenerator writer) {
Jon Skeet4cf9e3c2008-11-24 11:11:28 +000046 writer.WriteLine("private bool has{0};", PropertyName);
Jon Skeet68036862008-10-22 13:30:34 +010047 writer.WriteLine("private {0} {1}_ = {2};", TypeName, Name, DefaultValue);
Jon Skeet4cf9e3c2008-11-24 11:11:28 +000048 writer.WriteLine("public bool Has{0} {{", PropertyName);
49 writer.WriteLine(" get {{ return has{0}; }}", PropertyName);
Jon Skeet68036862008-10-22 13:30:34 +010050 writer.WriteLine("}");
Jon Skeetd6dd0a42009-06-05 22:00:05 +010051 AddClsComplianceCheck(writer);
Jon Skeet68036862008-10-22 13:30:34 +010052 writer.WriteLine("public {0} {1} {{", TypeName, PropertyName);
53 writer.WriteLine(" get {{ return {0}_; }}", Name);
54 writer.WriteLine("}");
55 }
56
57 public void GenerateBuilderMembers(TextGenerator writer) {
Jon Skeet4cf9e3c2008-11-24 11:11:28 +000058 writer.WriteLine("public bool Has{0} {{", PropertyName);
59 writer.WriteLine(" get {{ return result.Has{0}; }}", PropertyName);
Jon Skeet68036862008-10-22 13:30:34 +010060 writer.WriteLine("}");
Jon Skeetd6dd0a42009-06-05 22:00:05 +010061 AddClsComplianceCheck(writer);
Jon Skeet68036862008-10-22 13:30:34 +010062 writer.WriteLine("public {0} {1} {{", TypeName, PropertyName);
63 writer.WriteLine(" get {{ return result.{0}; }}", PropertyName);
Jon Skeet4cf9e3c2008-11-24 11:11:28 +000064 writer.WriteLine(" set {{ Set{0}(value); }}", PropertyName);
Jon Skeet68036862008-10-22 13:30:34 +010065 writer.WriteLine("}");
Jon Skeetd6dd0a42009-06-05 22:00:05 +010066 AddClsComplianceCheck(writer);
Jon Skeet4cf9e3c2008-11-24 11:11:28 +000067 writer.WriteLine("public Builder Set{0}({1} value) {{", PropertyName, TypeName);
Jon Skeet642a8142009-01-27 12:25:21 +000068 AddNullCheck(writer);
Jon Skeet4cf9e3c2008-11-24 11:11:28 +000069 writer.WriteLine(" result.has{0} = true;", PropertyName);
Jon Skeet68036862008-10-22 13:30:34 +010070 writer.WriteLine(" result.{0}_ = value;", Name);
71 writer.WriteLine(" return this;");
72 writer.WriteLine("}");
Jon Skeet4cf9e3c2008-11-24 11:11:28 +000073 writer.WriteLine("public Builder Clear{0}() {{", PropertyName);
74 writer.WriteLine(" result.has{0} = false;", PropertyName);
Jon Skeet68036862008-10-22 13:30:34 +010075 writer.WriteLine(" result.{0}_ = {1};", Name, DefaultValue);
76 writer.WriteLine(" return this;");
77 writer.WriteLine("}");
78 }
79
80 public void GenerateMergingCode(TextGenerator writer) {
Jon Skeet4cf9e3c2008-11-24 11:11:28 +000081 writer.WriteLine("if (other.Has{0}) {{", PropertyName);
Jon Skeet68036862008-10-22 13:30:34 +010082 writer.WriteLine(" {0} = other.{0};", PropertyName);
83 writer.WriteLine("}");
84 }
85
86 public void GenerateBuildingCode(TextGenerator writer) {
87 // Nothing to do here for primitive types
88 }
89
90 public void GenerateParsingCode(TextGenerator writer) {
91 writer.WriteLine("{0} = input.Read{1}();", PropertyName, CapitalizedTypeName);
92 }
93
94 public void GenerateSerializationCode(TextGenerator writer) {
Jon Skeet4cf9e3c2008-11-24 11:11:28 +000095 writer.WriteLine("if (Has{0}) {{", PropertyName);
Jon Skeet68036862008-10-22 13:30:34 +010096 writer.WriteLine(" output.Write{0}({1}, {2});", CapitalizedTypeName, Number, PropertyName);
97 writer.WriteLine("}");
98 }
99
100 public void GenerateSerializedSizeCode(TextGenerator writer) {
Jon Skeet4cf9e3c2008-11-24 11:11:28 +0000101 writer.WriteLine("if (Has{0}) {{", PropertyName);
Jon Skeet68036862008-10-22 13:30:34 +0100102 writer.WriteLine(" size += pb::CodedOutputStream.Compute{0}Size({1}, {2});",
103 CapitalizedTypeName, Number, PropertyName);
104 writer.WriteLine("}");
105 }
csharptest272cb8a2010-11-09 20:49:12 -0600106
107 public override void WriteHash(TextGenerator writer) {
108 writer.WriteLine("if (has{0}) hash ^= {1}_.GetHashCode();", PropertyName, Name);
109 }
110
111 public override void WriteEquals(TextGenerator writer) {
112 writer.WriteLine("if (has{0} != other.has{0} || (has{0} && !{1}_.Equals(other.{1}_))) return false;", PropertyName, Name);
113 }
114
115 public override void WriteToString(TextGenerator writer) {
116 writer.WriteLine("PrintField(\"{0}\", has{1}, {2}_, writer);", Descriptor.Name, PropertyName, Name);
117 }
Jon Skeet68036862008-10-22 13:30:34 +0100118 }
119}