Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtoGen/EnumFieldGenerator.cs @ 12007

Last change on this file since 12007 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.7 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 EnumFieldGenerator : FieldGeneratorBase, IFieldSourceGenerator
42    {
43        internal EnumFieldGenerator(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}_ = {2};", TypeName, Name, DefaultValue);
52            AddDeprecatedFlag(writer);
53            writer.WriteLine("public bool Has{0} {{", PropertyName);
54            writer.WriteLine("  get {{ return has{0}; }}", PropertyName);
55            writer.WriteLine("}");
56            AddPublicMemberAttributes(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            AddDeprecatedFlag(writer);
65            writer.WriteLine("public bool Has{0} {{", PropertyName);
66            writer.WriteLine(" get {{ return result.has{0}; }}", PropertyName);
67            writer.WriteLine("}");
68            AddPublicMemberAttributes(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            AddPublicMemberAttributes(writer);
74            writer.WriteLine("public Builder Set{0}({1} value) {{", PropertyName, TypeName);
75            writer.WriteLine("  PrepareBuilder();");
76            writer.WriteLine("  result.has{0} = true;", PropertyName);
77            writer.WriteLine("  result.{0}_ = value;", Name);
78            writer.WriteLine("  return this;");
79            writer.WriteLine("}");
80            AddDeprecatedFlag(writer);
81            writer.WriteLine("public Builder Clear{0}() {{", PropertyName);
82            writer.WriteLine("  PrepareBuilder();");
83            writer.WriteLine("  result.has{0} = false;", PropertyName);
84            writer.WriteLine("  result.{0}_ = {1};", Name, DefaultValue);
85            writer.WriteLine("  return this;");
86            writer.WriteLine("}");
87        }
88
89        public void GenerateMergingCode(TextGenerator writer)
90        {
91            writer.WriteLine("if (other.Has{0}) {{", PropertyName);
92            writer.WriteLine("  {0} = other.{0};", PropertyName);
93            writer.WriteLine("}");
94        }
95
96        public void GenerateBuildingCode(TextGenerator writer)
97        {
98            // Nothing to do here for enum types
99        }
100
101        public void GenerateParsingCode(TextGenerator writer)
102        {
103            writer.WriteLine("object unknown;");
104            writer.WriteLine("if(input.ReadEnum(ref result.{0}_, out unknown)) {{", Name);
105            writer.WriteLine("  result.has{0} = true;", PropertyName);
106            writer.WriteLine("} else if(unknown is int) {");
107            if (!UseLiteRuntime)
108            {
109                writer.WriteLine("  if (unknownFields == null) {"); // First unknown field - create builder now
110                writer.WriteLine("    unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields);");
111                writer.WriteLine("  }");
112                writer.WriteLine("  unknownFields.MergeVarintField({0}, (ulong)(int)unknown);", Number);
113            }
114            writer.WriteLine("}");
115        }
116
117        public void GenerateSerializationCode(TextGenerator writer)
118        {
119            writer.WriteLine("if (has{0}) {{", PropertyName);
120            writer.WriteLine("  output.WriteEnum({0}, field_names[{2}], (int) {1}, {1});", Number, PropertyName,
121                             FieldOrdinal);
122            writer.WriteLine("}");
123        }
124
125        public void GenerateSerializedSizeCode(TextGenerator writer)
126        {
127            writer.WriteLine("if (has{0}) {{", PropertyName);
128            writer.WriteLine("  size += pb::CodedOutputStream.ComputeEnumSize({0}, (int) {1});", Number, PropertyName);
129            writer.WriteLine("}");
130        }
131
132        public override void WriteHash(TextGenerator writer)
133        {
134            writer.WriteLine("if (has{0}) hash ^= {1}_.GetHashCode();", PropertyName, Name);
135        }
136
137        public override void WriteEquals(TextGenerator writer)
138        {
139            writer.WriteLine("if (has{0} != other.has{0} || (has{0} && !{1}_.Equals(other.{1}_))) return false;",
140                             PropertyName, Name);
141        }
142
143        public override void WriteToString(TextGenerator writer)
144        {
145            writer.WriteLine("PrintField(\"{0}\", has{1}, {2}_, writer);", Descriptor.Name, PropertyName, Name);
146        }
147    }
148}
Note: See TracBrowser for help on using the repository browser.