EnumFieldGenerator.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. using Google.ProtocolBuffers.Descriptors;
  35. namespace Google.ProtocolBuffers.ProtoGen
  36. {
  37. internal class EnumFieldGenerator : FieldGeneratorBase, IFieldSourceGenerator
  38. {
  39. internal EnumFieldGenerator(FieldDescriptor descriptor, int fieldOrdinal)
  40. : base(descriptor, fieldOrdinal)
  41. {
  42. }
  43. public void GenerateMembers(TextGenerator writer)
  44. {
  45. writer.WriteLine("private bool has{0};", PropertyName);
  46. writer.WriteLine("private {0} {1}_ = {2};", TypeName, Name, DefaultValue);
  47. AddDeprecatedFlag(writer);
  48. writer.WriteLine("public bool Has{0} {{", PropertyName);
  49. writer.WriteLine(" get {{ return has{0}; }}", PropertyName);
  50. writer.WriteLine("}");
  51. AddPublicMemberAttributes(writer);
  52. writer.WriteLine("public {0} {1} {{", TypeName, PropertyName);
  53. writer.WriteLine(" get {{ return {0}_; }}", Name);
  54. writer.WriteLine("}");
  55. }
  56. public void GenerateBuilderMembers(TextGenerator writer)
  57. {
  58. AddDeprecatedFlag(writer);
  59. writer.WriteLine("public bool Has{0} {{", PropertyName);
  60. writer.WriteLine(" get {{ return result.has{0}; }}", PropertyName);
  61. writer.WriteLine("}");
  62. AddPublicMemberAttributes(writer);
  63. writer.WriteLine("public {0} {1} {{", TypeName, PropertyName);
  64. writer.WriteLine(" get {{ return result.{0}; }}", PropertyName);
  65. writer.WriteLine(" set {{ Set{0}(value); }}", PropertyName);
  66. writer.WriteLine("}");
  67. AddPublicMemberAttributes(writer);
  68. writer.WriteLine("public Builder Set{0}({1} value) {{", PropertyName, TypeName);
  69. writer.WriteLine(" PrepareBuilder();");
  70. writer.WriteLine(" result.has{0} = true;", PropertyName);
  71. writer.WriteLine(" result.{0}_ = value;", Name);
  72. writer.WriteLine(" return this;");
  73. writer.WriteLine("}");
  74. AddDeprecatedFlag(writer);
  75. writer.WriteLine("public Builder Clear{0}() {{", PropertyName);
  76. writer.WriteLine(" PrepareBuilder();");
  77. writer.WriteLine(" result.has{0} = false;", PropertyName);
  78. writer.WriteLine(" result.{0}_ = {1};", Name, DefaultValue);
  79. writer.WriteLine(" return this;");
  80. writer.WriteLine("}");
  81. }
  82. public void GenerateMergingCode(TextGenerator writer)
  83. {
  84. writer.WriteLine("if (other.Has{0}) {{", PropertyName);
  85. writer.WriteLine(" {0} = other.{0};", PropertyName);
  86. writer.WriteLine("}");
  87. }
  88. public void GenerateBuildingCode(TextGenerator writer)
  89. {
  90. // Nothing to do here for enum types
  91. }
  92. public void GenerateParsingCode(TextGenerator writer)
  93. {
  94. writer.WriteLine("object unknown;");
  95. writer.WriteLine("if(input.ReadEnum(ref result.{0}_, out unknown)) {{", Name);
  96. writer.WriteLine(" result.has{0} = true;", PropertyName);
  97. writer.WriteLine("} else if(unknown is int) {");
  98. if (!UseLiteRuntime)
  99. {
  100. writer.WriteLine(" if (unknownFields == null) {"); // First unknown field - create builder now
  101. writer.WriteLine(" unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields);");
  102. writer.WriteLine(" }");
  103. writer.WriteLine(" unknownFields.MergeVarintField({0}, (ulong)(int)unknown);", Number);
  104. }
  105. writer.WriteLine("}");
  106. }
  107. public void GenerateSerializationCode(TextGenerator writer)
  108. {
  109. writer.WriteLine("if (has{0}) {{", PropertyName);
  110. writer.WriteLine(" output.WriteEnum({0}, field_names[{2}], (int) {1}, {1});", Number, PropertyName,
  111. FieldOrdinal);
  112. writer.WriteLine("}");
  113. }
  114. public void GenerateSerializedSizeCode(TextGenerator writer)
  115. {
  116. writer.WriteLine("if (has{0}) {{", PropertyName);
  117. writer.WriteLine(" size += pb::CodedOutputStream.ComputeEnumSize({0}, (int) {1});", Number, PropertyName);
  118. writer.WriteLine("}");
  119. }
  120. public override void WriteHash(TextGenerator writer)
  121. {
  122. writer.WriteLine("if (has{0}) hash ^= {1}_.GetHashCode();", PropertyName, Name);
  123. }
  124. public override void WriteEquals(TextGenerator writer)
  125. {
  126. writer.WriteLine("if (has{0} != other.has{0} || (has{0} && !{1}_.Equals(other.{1}_))) return false;",
  127. PropertyName, Name);
  128. }
  129. public override void WriteToString(TextGenerator writer)
  130. {
  131. writer.WriteLine("PrintField(\"{0}\", has{1}, {2}_, writer);", Descriptor.Name, PropertyName, Name);
  132. }
  133. }
  134. }