blob: bcb9d56ec0b8b99174ccd81311cd970955762cbe [file] [log] [blame]
Jon Skeetad748532009-06-25 16:55:58 +01001// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
3// http://github.com/jskeet/dotnet-protobufs/
4// Original C++/Java/Python code:
5// http://code.google.com/p/protobuf/
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions are
9// met:
10//
11// * Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// * Redistributions in binary form must reproduce the above
14// copyright notice, this list of conditions and the following disclaimer
15// in the documentation and/or other materials provided with the
16// distribution.
17// * Neither the name of Google Inc. nor the names of its
18// contributors may be used to endorse or promote products derived from
19// this software without specific prior written permission.
20//
21// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Jon Skeet68036862008-10-22 13:30:34 +010032using Google.ProtocolBuffers.Descriptors;
33
34namespace Google.ProtocolBuffers.ProtoGen {
35 // TODO(jonskeet): Refactor this. There's loads of common code here.
36 internal class PrimitiveFieldGenerator : FieldGeneratorBase, IFieldSourceGenerator {
37
38 internal PrimitiveFieldGenerator(FieldDescriptor descriptor)
39 : base(descriptor) {
40 }
41
42 public void GenerateMembers(TextGenerator writer) {
Jon Skeet4cf9e3c2008-11-24 11:11:28 +000043 writer.WriteLine("private bool has{0};", PropertyName);
Jon Skeet68036862008-10-22 13:30:34 +010044 writer.WriteLine("private {0} {1}_ = {2};", TypeName, Name, DefaultValue);
Jon Skeet4cf9e3c2008-11-24 11:11:28 +000045 writer.WriteLine("public bool Has{0} {{", PropertyName);
46 writer.WriteLine(" get {{ return has{0}; }}", PropertyName);
Jon Skeet68036862008-10-22 13:30:34 +010047 writer.WriteLine("}");
Jon Skeetd6dd0a42009-06-05 22:00:05 +010048 AddClsComplianceCheck(writer);
Jon Skeet68036862008-10-22 13:30:34 +010049 writer.WriteLine("public {0} {1} {{", TypeName, PropertyName);
50 writer.WriteLine(" get {{ return {0}_; }}", Name);
51 writer.WriteLine("}");
52 }
53
54 public void GenerateBuilderMembers(TextGenerator writer) {
Jon Skeet4cf9e3c2008-11-24 11:11:28 +000055 writer.WriteLine("public bool Has{0} {{", PropertyName);
56 writer.WriteLine(" get {{ return result.Has{0}; }}", PropertyName);
Jon Skeet68036862008-10-22 13:30:34 +010057 writer.WriteLine("}");
Jon Skeetd6dd0a42009-06-05 22:00:05 +010058 AddClsComplianceCheck(writer);
Jon Skeet68036862008-10-22 13:30:34 +010059 writer.WriteLine("public {0} {1} {{", TypeName, PropertyName);
60 writer.WriteLine(" get {{ return result.{0}; }}", PropertyName);
Jon Skeet4cf9e3c2008-11-24 11:11:28 +000061 writer.WriteLine(" set {{ Set{0}(value); }}", PropertyName);
Jon Skeet68036862008-10-22 13:30:34 +010062 writer.WriteLine("}");
Jon Skeetd6dd0a42009-06-05 22:00:05 +010063 AddClsComplianceCheck(writer);
Jon Skeet4cf9e3c2008-11-24 11:11:28 +000064 writer.WriteLine("public Builder Set{0}({1} value) {{", PropertyName, TypeName);
Jon Skeet642a8142009-01-27 12:25:21 +000065 AddNullCheck(writer);
Jon Skeet4cf9e3c2008-11-24 11:11:28 +000066 writer.WriteLine(" result.has{0} = true;", PropertyName);
Jon Skeet68036862008-10-22 13:30:34 +010067 writer.WriteLine(" result.{0}_ = value;", Name);
68 writer.WriteLine(" return this;");
69 writer.WriteLine("}");
Jon Skeet4cf9e3c2008-11-24 11:11:28 +000070 writer.WriteLine("public Builder Clear{0}() {{", PropertyName);
71 writer.WriteLine(" result.has{0} = false;", PropertyName);
Jon Skeet68036862008-10-22 13:30:34 +010072 writer.WriteLine(" result.{0}_ = {1};", Name, DefaultValue);
73 writer.WriteLine(" return this;");
74 writer.WriteLine("}");
75 }
76
77 public void GenerateMergingCode(TextGenerator writer) {
Jon Skeet4cf9e3c2008-11-24 11:11:28 +000078 writer.WriteLine("if (other.Has{0}) {{", PropertyName);
Jon Skeet68036862008-10-22 13:30:34 +010079 writer.WriteLine(" {0} = other.{0};", PropertyName);
80 writer.WriteLine("}");
81 }
82
83 public void GenerateBuildingCode(TextGenerator writer) {
84 // Nothing to do here for primitive types
85 }
86
87 public void GenerateParsingCode(TextGenerator writer) {
88 writer.WriteLine("{0} = input.Read{1}();", PropertyName, CapitalizedTypeName);
89 }
90
91 public void GenerateSerializationCode(TextGenerator writer) {
Jon Skeet4cf9e3c2008-11-24 11:11:28 +000092 writer.WriteLine("if (Has{0}) {{", PropertyName);
Jon Skeet68036862008-10-22 13:30:34 +010093 writer.WriteLine(" output.Write{0}({1}, {2});", CapitalizedTypeName, Number, PropertyName);
94 writer.WriteLine("}");
95 }
96
97 public void GenerateSerializedSizeCode(TextGenerator writer) {
Jon Skeet4cf9e3c2008-11-24 11:11:28 +000098 writer.WriteLine("if (Has{0}) {{", PropertyName);
Jon Skeet68036862008-10-22 13:30:34 +010099 writer.WriteLine(" size += pb::CodedOutputStream.Compute{0}Size({1}, {2});",
100 CapitalizedTypeName, Number, PropertyName);
101 writer.WriteLine("}");
102 }
103 }
104}