PrimitiveFieldGenerator.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // 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.
  32. using Google.ProtocolBuffers.Descriptors;
  33. namespace Google.ProtocolBuffers.ProtoGen {
  34. // TODO(jonskeet): Refactor this. There's loads of common code here.
  35. internal class PrimitiveFieldGenerator : FieldGeneratorBase, IFieldSourceGenerator {
  36. internal PrimitiveFieldGenerator(FieldDescriptor descriptor)
  37. : base(descriptor) {
  38. }
  39. public void GenerateMembers(TextGenerator writer) {
  40. writer.WriteLine("private bool has{0};", PropertyName);
  41. writer.WriteLine("private {0} {1}_ = {2};", TypeName, Name, DefaultValue);
  42. writer.WriteLine("public bool Has{0} {{", PropertyName);
  43. writer.WriteLine(" get {{ return has{0}; }}", PropertyName);
  44. writer.WriteLine("}");
  45. AddClsComplianceCheck(writer);
  46. writer.WriteLine("public {0} {1} {{", TypeName, PropertyName);
  47. writer.WriteLine(" get {{ return {0}_; }}", Name);
  48. writer.WriteLine("}");
  49. }
  50. public void GenerateBuilderMembers(TextGenerator writer) {
  51. writer.WriteLine("public bool Has{0} {{", PropertyName);
  52. writer.WriteLine(" get {{ return result.Has{0}; }}", PropertyName);
  53. writer.WriteLine("}");
  54. AddClsComplianceCheck(writer);
  55. writer.WriteLine("public {0} {1} {{", TypeName, PropertyName);
  56. writer.WriteLine(" get {{ return result.{0}; }}", PropertyName);
  57. writer.WriteLine(" set {{ Set{0}(value); }}", PropertyName);
  58. writer.WriteLine("}");
  59. AddClsComplianceCheck(writer);
  60. writer.WriteLine("public Builder Set{0}({1} value) {{", PropertyName, TypeName);
  61. AddNullCheck(writer);
  62. writer.WriteLine(" result.has{0} = true;", PropertyName);
  63. writer.WriteLine(" result.{0}_ = value;", Name);
  64. writer.WriteLine(" return this;");
  65. writer.WriteLine("}");
  66. writer.WriteLine("public Builder Clear{0}() {{", PropertyName);
  67. writer.WriteLine(" result.has{0} = false;", PropertyName);
  68. writer.WriteLine(" result.{0}_ = {1};", Name, DefaultValue);
  69. writer.WriteLine(" return this;");
  70. writer.WriteLine("}");
  71. }
  72. public void GenerateMergingCode(TextGenerator writer) {
  73. writer.WriteLine("if (other.Has{0}) {{", PropertyName);
  74. writer.WriteLine(" {0} = other.{0};", PropertyName);
  75. writer.WriteLine("}");
  76. }
  77. public void GenerateBuildingCode(TextGenerator writer) {
  78. // Nothing to do here for primitive types
  79. }
  80. public void GenerateParsingCode(TextGenerator writer) {
  81. writer.WriteLine("{0} = input.Read{1}();", PropertyName, CapitalizedTypeName);
  82. }
  83. public void GenerateSerializationCode(TextGenerator writer) {
  84. writer.WriteLine("if (Has{0}) {{", PropertyName);
  85. writer.WriteLine(" output.Write{0}({1}, {2});", CapitalizedTypeName, Number, PropertyName);
  86. writer.WriteLine("}");
  87. }
  88. public void GenerateSerializedSizeCode(TextGenerator writer) {
  89. writer.WriteLine("if (Has{0}) {{", PropertyName);
  90. writer.WriteLine(" size += pb::CodedOutputStream.Compute{0}Size({1}, {2});",
  91. CapitalizedTypeName, Number, PropertyName);
  92. writer.WriteLine("}");
  93. }
  94. }
  95. }