ExtensionValue.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. using System.Linq;
  35. namespace Google.Protobuf
  36. {
  37. internal interface IExtensionValue : IEquatable<IExtensionValue>, IDeepCloneable<IExtensionValue>
  38. {
  39. void MergeFrom(CodedInputStream input);
  40. void MergeFrom(IExtensionValue value);
  41. void WriteTo(CodedOutputStream output);
  42. int CalculateSize();
  43. bool IsInitialized();
  44. }
  45. internal sealed class ExtensionValue<T> : IExtensionValue
  46. {
  47. private T field;
  48. private FieldCodec<T> codec;
  49. internal ExtensionValue(FieldCodec<T> codec)
  50. {
  51. this.codec = codec;
  52. field = codec.DefaultValue;
  53. }
  54. public int CalculateSize()
  55. {
  56. return codec.CalculateSizeWithTag(field);
  57. }
  58. public IExtensionValue Clone()
  59. {
  60. return new ExtensionValue<T>(codec)
  61. {
  62. field = field is IDeepCloneable<T> ? (field as IDeepCloneable<T>).Clone() : field
  63. };
  64. }
  65. public bool Equals(IExtensionValue other)
  66. {
  67. if (ReferenceEquals(this, other))
  68. return true;
  69. return other is ExtensionValue<T>
  70. && codec.Equals((other as ExtensionValue<T>).codec)
  71. && Equals(field, (other as ExtensionValue<T>).field);
  72. // we check for equality in the codec since we could have equal field values however the values could be written in different ways
  73. }
  74. public override int GetHashCode()
  75. {
  76. unchecked
  77. {
  78. int hash = 17;
  79. hash = hash * 31 + field.GetHashCode();
  80. hash = hash * 31 + codec.GetHashCode();
  81. return hash;
  82. }
  83. }
  84. public void MergeFrom(CodedInputStream input)
  85. {
  86. codec.ValueMerger(input, ref field);
  87. }
  88. public void MergeFrom(IExtensionValue value)
  89. {
  90. if (value is ExtensionValue<T>)
  91. {
  92. var extensionValue = value as ExtensionValue<T>;
  93. codec.FieldMerger(ref field, extensionValue.field);
  94. }
  95. }
  96. public void WriteTo(CodedOutputStream output)
  97. {
  98. output.WriteTag(codec.Tag);
  99. codec.ValueWriter(output, field);
  100. if (codec.EndTag != 0)
  101. {
  102. output.WriteTag(codec.EndTag);
  103. }
  104. }
  105. public T GetValue() => field;
  106. public void SetValue(T value)
  107. {
  108. field = value;
  109. }
  110. public bool IsInitialized()
  111. {
  112. if (field is IMessage)
  113. {
  114. return (field as IMessage).IsInitialized();
  115. }
  116. else
  117. {
  118. return true;
  119. }
  120. }
  121. }
  122. internal sealed class RepeatedExtensionValue<T> : IExtensionValue
  123. {
  124. private RepeatedField<T> field;
  125. private readonly FieldCodec<T> codec;
  126. internal RepeatedExtensionValue(FieldCodec<T> codec)
  127. {
  128. this.codec = codec;
  129. field = new RepeatedField<T>();
  130. }
  131. public int CalculateSize()
  132. {
  133. return field.CalculateSize(codec);
  134. }
  135. public IExtensionValue Clone()
  136. {
  137. return new RepeatedExtensionValue<T>(codec)
  138. {
  139. field = field.Clone()
  140. };
  141. }
  142. public bool Equals(IExtensionValue other)
  143. {
  144. if (ReferenceEquals(this, other))
  145. return true;
  146. return other is RepeatedExtensionValue<T>
  147. && field.Equals((other as RepeatedExtensionValue<T>).field)
  148. && codec.Equals((other as RepeatedExtensionValue<T>).codec);
  149. }
  150. public override int GetHashCode()
  151. {
  152. unchecked
  153. {
  154. int hash = 17;
  155. hash = hash * 31 + field.GetHashCode();
  156. hash = hash * 31 + codec.GetHashCode();
  157. return hash;
  158. }
  159. }
  160. public void MergeFrom(CodedInputStream input)
  161. {
  162. field.AddEntriesFrom(input, codec);
  163. }
  164. public void MergeFrom(IExtensionValue value)
  165. {
  166. if (value is RepeatedExtensionValue<T>)
  167. {
  168. field.Add((value as RepeatedExtensionValue<T>).field);
  169. }
  170. }
  171. public void WriteTo(CodedOutputStream output)
  172. {
  173. field.WriteTo(output, codec);
  174. }
  175. public RepeatedField<T> GetValue() => field;
  176. public bool IsInitialized()
  177. {
  178. for (int i = 0; i < field.Count; i++)
  179. {
  180. var element = field[i];
  181. if (element is IMessage)
  182. {
  183. if (!(element as IMessage).IsInitialized())
  184. {
  185. return false;
  186. }
  187. }
  188. else
  189. {
  190. break;
  191. }
  192. }
  193. return true;
  194. }
  195. }
  196. }