ExtensionValue.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using Google.Protobuf.Collections;
  33. using System;
  34. namespace Google.Protobuf
  35. {
  36. internal interface IExtensionValue : IEquatable<IExtensionValue>, IDeepCloneable<IExtensionValue>
  37. {
  38. void MergeFrom(CodedInputStream input);
  39. void MergeFrom(IExtensionValue value);
  40. void WriteTo(CodedOutputStream output);
  41. int CalculateSize();
  42. }
  43. internal sealed class ExtensionValue<T> : IExtensionValue
  44. {
  45. private bool hasValue;
  46. private T field;
  47. private FieldCodec<T> codec;
  48. internal ExtensionValue(FieldCodec<T> codec)
  49. {
  50. this.codec = codec;
  51. field = codec.DefaultValue;
  52. }
  53. public int CalculateSize()
  54. {
  55. if (!hasValue)
  56. {
  57. return 0;
  58. }
  59. return codec.CalculateSizeWithTag(field);
  60. }
  61. public IExtensionValue Clone()
  62. {
  63. return new ExtensionValue<T>(codec)
  64. {
  65. hasValue = hasValue,
  66. field = field is IDeepCloneable<T> ? (field as IDeepCloneable<T>).Clone() : field
  67. };
  68. }
  69. public bool Equals(IExtensionValue other)
  70. {
  71. if (ReferenceEquals(this, other))
  72. return true;
  73. return other is ExtensionValue<T>
  74. && codec.Equals((other as ExtensionValue<T>).codec)
  75. && hasValue.Equals((other as ExtensionValue<T>).hasValue)
  76. && Equals(field, (other as ExtensionValue<T>).field);
  77. // we check for equality in the codec since we could have equal field values however the values could be written in different ways
  78. }
  79. public override int GetHashCode()
  80. {
  81. unchecked
  82. {
  83. int hash = 17;
  84. hash = hash * 31 + hasValue.GetHashCode();
  85. hash = hash * 31 + field.GetHashCode();
  86. hash = hash * 31 + codec.GetHashCode();
  87. return hash;
  88. }
  89. }
  90. public void MergeFrom(CodedInputStream input)
  91. {
  92. hasValue = true;
  93. codec.ValueMerger(input, ref field);
  94. }
  95. public void MergeFrom(IExtensionValue value)
  96. {
  97. if (value is ExtensionValue<T>)
  98. {
  99. var extensionValue = value as ExtensionValue<T>;
  100. if (extensionValue.hasValue)
  101. {
  102. hasValue |= codec.FieldMerger(ref field, extensionValue.field);
  103. }
  104. }
  105. }
  106. public void WriteTo(CodedOutputStream output)
  107. {
  108. if (hasValue)
  109. {
  110. output.WriteTag(codec.Tag);
  111. codec.ValueWriter(output, field);
  112. if (codec.EndTag != 0)
  113. {
  114. output.WriteTag(codec.EndTag);
  115. }
  116. }
  117. }
  118. public T GetValue() => field;
  119. public void SetValue(T value)
  120. {
  121. hasValue = true;
  122. field = value;
  123. }
  124. public bool HasValue => hasValue;
  125. }
  126. internal sealed class RepeatedExtensionValue<T> : IExtensionValue
  127. {
  128. private RepeatedField<T> field;
  129. private readonly FieldCodec<T> codec;
  130. internal RepeatedExtensionValue(FieldCodec<T> codec)
  131. {
  132. this.codec = codec;
  133. field = new RepeatedField<T>();
  134. }
  135. public int CalculateSize()
  136. {
  137. return field.CalculateSize(codec);
  138. }
  139. public IExtensionValue Clone()
  140. {
  141. return new RepeatedExtensionValue<T>(codec)
  142. {
  143. field = field.Clone()
  144. };
  145. }
  146. public bool Equals(IExtensionValue other)
  147. {
  148. if (ReferenceEquals(this, other))
  149. return true;
  150. return other is RepeatedExtensionValue<T>
  151. && field.Equals((other as RepeatedExtensionValue<T>).field)
  152. && codec.Equals((other as RepeatedExtensionValue<T>).codec);
  153. }
  154. public override int GetHashCode()
  155. {
  156. unchecked
  157. {
  158. int hash = 17;
  159. hash = hash * 31 + field.GetHashCode();
  160. hash = hash * 31 + codec.GetHashCode();
  161. return hash;
  162. }
  163. }
  164. public void MergeFrom(CodedInputStream input)
  165. {
  166. field.AddEntriesFrom(input, codec);
  167. }
  168. public void MergeFrom(IExtensionValue value)
  169. {
  170. if (value is RepeatedExtensionValue<T>)
  171. {
  172. field.Add((value as RepeatedExtensionValue<T>).field);
  173. }
  174. }
  175. public void WriteTo(CodedOutputStream output)
  176. {
  177. field.WriteTo(output, codec);
  178. }
  179. public RepeatedField<T> GetValue() => field;
  180. }
  181. }