ExtendableMessageLite.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 System;
  35. using System.Collections;
  36. using System.Collections.Generic;
  37. using System.IO;
  38. using Google.ProtocolBuffers.Collections;
  39. namespace Google.ProtocolBuffers
  40. {
  41. public abstract partial class ExtendableMessageLite<TMessage, TBuilder> : GeneratedMessageLite<TMessage, TBuilder>
  42. where TMessage : GeneratedMessageLite<TMessage, TBuilder>
  43. where TBuilder : GeneratedBuilderLite<TMessage, TBuilder>
  44. {
  45. protected ExtendableMessageLite()
  46. {
  47. }
  48. private readonly FieldSet extensions = FieldSet.CreateInstance();
  49. /// <summary>
  50. /// Access for the builder.
  51. /// </summary>
  52. internal FieldSet Extensions
  53. {
  54. get { return extensions; }
  55. }
  56. public override bool Equals(object obj)
  57. {
  58. ExtendableMessageLite<TMessage, TBuilder> other = obj as ExtendableMessageLite<TMessage, TBuilder>;
  59. return !ReferenceEquals(null, other) &&
  60. Dictionaries.Equals(extensions.AllFields, other.extensions.AllFields);
  61. }
  62. public override int GetHashCode()
  63. {
  64. return Dictionaries.GetHashCode(extensions.AllFields);
  65. }
  66. /// <summary>
  67. /// writes the extensions to the text stream
  68. /// </summary>
  69. public override void PrintTo(TextWriter writer)
  70. {
  71. foreach (KeyValuePair<IFieldDescriptorLite, object> entry in extensions.AllFields)
  72. {
  73. string fn = string.Format("[{0}]", entry.Key.FullName);
  74. if (entry.Key.IsRepeated)
  75. {
  76. foreach (object o in ((IEnumerable) entry.Value))
  77. {
  78. PrintField(fn, true, o, writer);
  79. }
  80. }
  81. else
  82. {
  83. PrintField(fn, true, entry.Value, writer);
  84. }
  85. }
  86. }
  87. /// <summary>
  88. /// Checks if a singular extension is present.
  89. /// </summary>
  90. public bool HasExtension<TExtension>(GeneratedExtensionLite<TMessage, TExtension> extension)
  91. {
  92. VerifyExtensionContainingType(extension);
  93. return extensions.HasField(extension.Descriptor);
  94. }
  95. /// <summary>
  96. /// Returns the number of elements in a repeated extension.
  97. /// </summary>
  98. public int GetExtensionCount<TExtension>(GeneratedExtensionLite<TMessage, IList<TExtension>> extension)
  99. {
  100. VerifyExtensionContainingType(extension);
  101. return extensions.GetRepeatedFieldCount(extension.Descriptor);
  102. }
  103. /// <summary>
  104. /// Returns the value of an extension.
  105. /// </summary>
  106. public TExtension GetExtension<TExtension>(GeneratedExtensionLite<TMessage, TExtension> extension)
  107. {
  108. VerifyExtensionContainingType(extension);
  109. object value = extensions[extension.Descriptor];
  110. if (value == null)
  111. {
  112. return extension.DefaultValue;
  113. }
  114. else
  115. {
  116. return (TExtension) extension.FromReflectionType(value);
  117. }
  118. }
  119. /// <summary>
  120. /// Returns one element of a repeated extension.
  121. /// </summary>
  122. public TExtension GetExtension<TExtension>(GeneratedExtensionLite<TMessage, IList<TExtension>> extension,
  123. int index)
  124. {
  125. VerifyExtensionContainingType(extension);
  126. return (TExtension) extension.SingularFromReflectionType(extensions[extension.Descriptor, index]);
  127. }
  128. /// <summary>
  129. /// Called to check if all extensions are initialized.
  130. /// </summary>
  131. protected bool ExtensionsAreInitialized
  132. {
  133. get { return extensions.IsInitialized; }
  134. }
  135. public override bool IsInitialized
  136. {
  137. get { return ExtensionsAreInitialized; }
  138. }
  139. /// <summary>
  140. /// Used by subclasses to serialize extensions. Extension ranges may be
  141. /// interleaves with field numbers, but we must write them in canonical
  142. /// (sorted by field number) order. This class helps us to write individual
  143. /// ranges of extensions at once.
  144. ///
  145. /// TODO(jonskeet): See if we can improve this in terms of readability.
  146. /// </summary>
  147. protected class ExtensionWriter
  148. {
  149. private readonly IEnumerator<KeyValuePair<IFieldDescriptorLite, object>> iterator;
  150. private readonly FieldSet extensions;
  151. private KeyValuePair<IFieldDescriptorLite, object>? next = null;
  152. internal ExtensionWriter(ExtendableMessageLite<TMessage, TBuilder> message)
  153. {
  154. extensions = message.extensions;
  155. iterator = message.extensions.GetEnumerator();
  156. if (iterator.MoveNext())
  157. {
  158. next = iterator.Current;
  159. }
  160. }
  161. public void WriteUntil(int end, ICodedOutputStream output)
  162. {
  163. while (next != null && next.Value.Key.FieldNumber < end)
  164. {
  165. extensions.WriteField(next.Value.Key, next.Value.Value, output);
  166. if (iterator.MoveNext())
  167. {
  168. next = iterator.Current;
  169. }
  170. else
  171. {
  172. next = null;
  173. }
  174. }
  175. }
  176. }
  177. protected ExtensionWriter CreateExtensionWriter(ExtendableMessageLite<TMessage, TBuilder> message)
  178. {
  179. return new ExtensionWriter(message);
  180. }
  181. /// <summary>
  182. /// Called by subclasses to compute the size of extensions.
  183. /// </summary>
  184. protected int ExtensionsSerializedSize
  185. {
  186. get { return extensions.SerializedSize; }
  187. }
  188. internal void VerifyExtensionContainingType<TExtension>(GeneratedExtensionLite<TMessage, TExtension> extension)
  189. {
  190. if (!ReferenceEquals(extension.ContainingTypeDefaultInstance, DefaultInstanceForType))
  191. {
  192. // This can only happen if someone uses unchecked operations.
  193. throw new ArgumentException(
  194. String.Format("Extension is for type \"{0}\" which does not match message type \"{1}\".",
  195. extension.ContainingTypeDefaultInstance, DefaultInstanceForType
  196. ));
  197. }
  198. }
  199. }
  200. }