Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtoGen/PrimitiveFieldGenerator.cs @ 8295

Last change on this file since 8295 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: 6.3 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    // TODO(jonskeet): Refactor this. There's loads of common code here.
42    internal class PrimitiveFieldGenerator : FieldGeneratorBase, IFieldSourceGenerator
43    {
44        internal PrimitiveFieldGenerator(FieldDescriptor descriptor, int fieldOrdinal)
45            : base(descriptor, fieldOrdinal)
46        {
47        }
48
49        public void GenerateMembers(TextGenerator writer)
50        {
51            writer.WriteLine("private bool has{0};", PropertyName);
52            writer.WriteLine("private {0} {1}_{2};", TypeName, Name, HasDefaultValue ? " = " + DefaultValue : "");
53            AddDeprecatedFlag(writer);
54            writer.WriteLine("public bool Has{0} {{", PropertyName);
55            writer.WriteLine("  get {{ return has{0}; }}", PropertyName);
56            writer.WriteLine("}");
57            AddPublicMemberAttributes(writer);
58            writer.WriteLine("public {0} {1} {{", TypeName, PropertyName);
59            writer.WriteLine("  get {{ return {0}_; }}", Name);
60            writer.WriteLine("}");
61        }
62
63        public void GenerateBuilderMembers(TextGenerator writer)
64        {
65            AddDeprecatedFlag(writer);
66            writer.WriteLine("public bool Has{0} {{", PropertyName);
67            writer.WriteLine("  get {{ return result.has{0}; }}", PropertyName);
68            writer.WriteLine("}");
69            AddPublicMemberAttributes(writer);
70            writer.WriteLine("public {0} {1} {{", TypeName, PropertyName);
71            writer.WriteLine("  get {{ return result.{0}; }}", PropertyName);
72            writer.WriteLine("  set {{ Set{0}(value); }}", PropertyName);
73            writer.WriteLine("}");
74            AddPublicMemberAttributes(writer);
75            writer.WriteLine("public Builder Set{0}({1} value) {{", PropertyName, TypeName);
76            AddNullCheck(writer);
77            writer.WriteLine("  PrepareBuilder();");
78            writer.WriteLine("  result.has{0} = true;", PropertyName);
79            writer.WriteLine("  result.{0}_ = value;", Name);
80            writer.WriteLine("  return this;");
81            writer.WriteLine("}");
82            AddDeprecatedFlag(writer);
83            writer.WriteLine("public Builder Clear{0}() {{", PropertyName);
84            writer.WriteLine("  PrepareBuilder();");
85            writer.WriteLine("  result.has{0} = false;", PropertyName);
86            writer.WriteLine("  result.{0}_ = {1};", Name, DefaultValue);
87            writer.WriteLine("  return this;");
88            writer.WriteLine("}");
89        }
90
91        public void GenerateMergingCode(TextGenerator writer)
92        {
93            writer.WriteLine("if (other.Has{0}) {{", PropertyName);
94            writer.WriteLine("  {0} = other.{0};", PropertyName);
95            writer.WriteLine("}");
96        }
97
98        public void GenerateBuildingCode(TextGenerator writer)
99        {
100            // Nothing to do here for primitive types
101        }
102
103        public void GenerateParsingCode(TextGenerator writer)
104        {
105            writer.WriteLine("result.has{0} = input.Read{1}(ref result.{2}_);", PropertyName, CapitalizedTypeName, Name);
106        }
107
108        public void GenerateSerializationCode(TextGenerator writer)
109        {
110            writer.WriteLine("if (has{0}) {{", PropertyName);
111            writer.WriteLine("  output.Write{0}({1}, field_names[{3}], {2});", CapitalizedTypeName, Number, PropertyName,
112                             FieldOrdinal);
113            writer.WriteLine("}");
114        }
115
116        public void GenerateSerializedSizeCode(TextGenerator writer)
117        {
118            writer.WriteLine("if (has{0}) {{", PropertyName);
119            writer.WriteLine("  size += pb::CodedOutputStream.Compute{0}Size({1}, {2});",
120                             CapitalizedTypeName, Number, PropertyName);
121            writer.WriteLine("}");
122        }
123
124        public override void WriteHash(TextGenerator writer)
125        {
126            writer.WriteLine("if (has{0}) hash ^= {1}_.GetHashCode();", PropertyName, Name);
127        }
128
129        public override void WriteEquals(TextGenerator writer)
130        {
131            writer.WriteLine("if (has{0} != other.has{0} || (has{0} && !{1}_.Equals(other.{1}_))) return false;",
132                             PropertyName, Name);
133        }
134
135        public override void WriteToString(TextGenerator writer)
136        {
137            writer.WriteLine("PrintField(\"{0}\", has{1}, {2}_, writer);", Descriptor.Name, PropertyName, Name);
138        }
139    }
140}
Note: See TracBrowser for help on using the repository browser.