1 | #region Copyright notice and license
|
---|
2 | // 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.
|
---|
33 | #endregion
|
---|
34 |
|
---|
35 | using Google.ProtocolBuffers.Descriptors;
|
---|
36 |
|
---|
37 | namespace Google.ProtocolBuffers.ProtoGen {
|
---|
38 | internal class EnumFieldGenerator : FieldGeneratorBase, IFieldSourceGenerator {
|
---|
39 | internal EnumFieldGenerator(FieldDescriptor descriptor)
|
---|
40 | : base(descriptor) {
|
---|
41 | }
|
---|
42 |
|
---|
43 | public void GenerateMembers(TextGenerator writer) {
|
---|
44 | writer.WriteLine("private bool has{0};", PropertyName);
|
---|
45 | writer.WriteLine("private {0} {1}_ = {2};", TypeName, Name, DefaultValue);
|
---|
46 | writer.WriteLine("public bool Has{0} {{", PropertyName);
|
---|
47 | writer.WriteLine(" get {{ return has{0}; }}", PropertyName);
|
---|
48 | writer.WriteLine("}");
|
---|
49 | AddClsComplianceCheck(writer);
|
---|
50 | writer.WriteLine("public {0} {1} {{", TypeName, PropertyName);
|
---|
51 | writer.WriteLine(" get {{ return {0}_; }}", Name);
|
---|
52 | writer.WriteLine("}");
|
---|
53 | }
|
---|
54 |
|
---|
55 | public void GenerateBuilderMembers(TextGenerator writer) {
|
---|
56 | writer.WriteLine("public bool Has{0} {{", PropertyName);
|
---|
57 | writer.WriteLine(" get {{ return result.Has{0}; }}", PropertyName);
|
---|
58 | writer.WriteLine("}");
|
---|
59 | AddClsComplianceCheck(writer);
|
---|
60 | writer.WriteLine("public {0} {1} {{", TypeName, PropertyName);
|
---|
61 | writer.WriteLine(" get {{ return result.{0}; }}", PropertyName);
|
---|
62 | writer.WriteLine(" set {{ Set{0}(value); }}", PropertyName);
|
---|
63 | writer.WriteLine("}");
|
---|
64 | AddClsComplianceCheck(writer);
|
---|
65 | writer.WriteLine("public Builder Set{0}({1} value) {{", PropertyName, TypeName);
|
---|
66 | writer.WriteLine(" result.has{0} = true;", PropertyName);
|
---|
67 | writer.WriteLine(" result.{0}_ = value;", Name);
|
---|
68 | writer.WriteLine(" return this;");
|
---|
69 | writer.WriteLine("}");
|
---|
70 | writer.WriteLine("public Builder Clear{0}() {{", PropertyName);
|
---|
71 | writer.WriteLine(" result.has{0} = false;", PropertyName);
|
---|
72 | writer.WriteLine(" result.{0}_ = {1};", Name, DefaultValue);
|
---|
73 | writer.WriteLine(" return this;");
|
---|
74 | writer.WriteLine("}");
|
---|
75 | }
|
---|
76 |
|
---|
77 | public void GenerateMergingCode(TextGenerator writer) {
|
---|
78 | writer.WriteLine("if (other.Has{0}) {{", PropertyName);
|
---|
79 | writer.WriteLine(" {0} = other.{0};", PropertyName);
|
---|
80 | writer.WriteLine("}");
|
---|
81 | }
|
---|
82 |
|
---|
83 | public void GenerateBuildingCode(TextGenerator writer) {
|
---|
84 | // Nothing to do here for enum types
|
---|
85 | }
|
---|
86 |
|
---|
87 | public void GenerateParsingCode(TextGenerator writer) {
|
---|
88 | // TODO(jonskeet): Make a more efficient way of doing this
|
---|
89 | writer.WriteLine("int rawValue = input.ReadEnum();");
|
---|
90 | writer.WriteLine("if (!global::System.Enum.IsDefined(typeof({0}), rawValue)) {{", TypeName);
|
---|
91 | writer.WriteLine(" if (unknownFields == null) {"); // First unknown field - create builder now
|
---|
92 | writer.WriteLine(" unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields);");
|
---|
93 | writer.WriteLine(" }");
|
---|
94 | writer.WriteLine(" unknownFields.MergeVarintField({0}, (ulong) rawValue);", Number);
|
---|
95 | writer.WriteLine("} else {");
|
---|
96 | writer.WriteLine(" {0} = ({1}) rawValue;", PropertyName, TypeName);
|
---|
97 | writer.WriteLine("}");
|
---|
98 | }
|
---|
99 |
|
---|
100 | public void GenerateSerializationCode(TextGenerator writer) {
|
---|
101 | writer.WriteLine("if (Has{0}) {{", PropertyName);
|
---|
102 | writer.WriteLine(" output.WriteEnum({0}, (int) {1});", Number, PropertyName);
|
---|
103 | writer.WriteLine("}");
|
---|
104 | }
|
---|
105 |
|
---|
106 | public void GenerateSerializedSizeCode(TextGenerator writer) {
|
---|
107 | writer.WriteLine("if (Has{0}) {{", PropertyName);
|
---|
108 | writer.WriteLine(" size += pb::CodedOutputStream.ComputeEnumSize({0}, (int) {1});", Number, PropertyName);
|
---|
109 | writer.WriteLine("}");
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|