AbstractMessage.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 System.Collections;
  33. using System.Collections.Generic;
  34. using System.IO;
  35. using Google.ProtocolBuffers.Collections;
  36. using Google.ProtocolBuffers.Descriptors;
  37. namespace Google.ProtocolBuffers {
  38. /// <summary>
  39. /// Implementation of the non-generic IMessage interface as far as possible.
  40. /// </summary>
  41. public abstract class AbstractMessage<TMessage, TBuilder> : IMessage<TMessage, TBuilder>
  42. where TMessage : AbstractMessage<TMessage, TBuilder>
  43. where TBuilder : AbstractBuilder<TMessage, TBuilder> {
  44. /// <summary>
  45. /// The serialized size if it's already been computed, or null
  46. /// if we haven't computed it yet.
  47. /// </summary>
  48. private int? memoizedSize = null;
  49. #region Unimplemented members of IMessage
  50. public abstract MessageDescriptor DescriptorForType { get; }
  51. public abstract IDictionary<FieldDescriptor, object> AllFields { get; }
  52. public abstract bool HasField(FieldDescriptor field);
  53. public abstract object this[FieldDescriptor field] { get; }
  54. public abstract int GetRepeatedFieldCount(FieldDescriptor field);
  55. public abstract object this[FieldDescriptor field, int index] { get; }
  56. public abstract UnknownFieldSet UnknownFields { get; }
  57. public abstract TMessage DefaultInstanceForType { get; }
  58. public abstract TBuilder CreateBuilderForType();
  59. public abstract TBuilder ToBuilder();
  60. #endregion
  61. public IBuilder WeakCreateBuilderForType() {
  62. return CreateBuilderForType();
  63. }
  64. public IBuilder WeakToBuilder() {
  65. return ToBuilder();
  66. }
  67. public IMessage WeakDefaultInstanceForType {
  68. get { return DefaultInstanceForType; }
  69. }
  70. public virtual bool IsInitialized {
  71. get {
  72. // Check that all required fields are present.
  73. foreach (FieldDescriptor field in DescriptorForType.Fields) {
  74. if (field.IsRequired && !HasField(field)) {
  75. return false;
  76. }
  77. }
  78. // Check that embedded messages are initialized.
  79. foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
  80. FieldDescriptor field = entry.Key;
  81. if (field.MappedType == MappedType.Message) {
  82. if (field.IsRepeated) {
  83. // We know it's an IList<T>, but not the exact type - so
  84. // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
  85. foreach (IMessage element in (IEnumerable) entry.Value) {
  86. if (!element.IsInitialized) {
  87. return false;
  88. }
  89. }
  90. } else {
  91. if (!((IMessage)entry.Value).IsInitialized) {
  92. return false;
  93. }
  94. }
  95. }
  96. }
  97. return true;
  98. }
  99. }
  100. public sealed override string ToString() {
  101. return TextFormat.PrintToString(this);
  102. }
  103. public virtual void WriteTo(CodedOutputStream output) {
  104. foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
  105. FieldDescriptor field = entry.Key;
  106. if (field.IsRepeated) {
  107. // We know it's an IList<T>, but not the exact type - so
  108. // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
  109. IEnumerable valueList = (IEnumerable) entry.Value;
  110. if (field.IsPacked) {
  111. output.WriteTag(field.FieldNumber, WireFormat.WireType.LengthDelimited);
  112. int dataSize = 0;
  113. foreach (object element in valueList) {
  114. dataSize += CodedOutputStream.ComputeFieldSizeNoTag(field.FieldType, element);
  115. }
  116. output.WriteRawVarint32((uint)dataSize);
  117. foreach (object element in valueList) {
  118. output.WriteFieldNoTag(field.FieldType, element);
  119. }
  120. } else {
  121. foreach (object element in valueList) {
  122. output.WriteField(field.FieldType, field.FieldNumber, element);
  123. }
  124. }
  125. } else {
  126. output.WriteField(field.FieldType, field.FieldNumber, entry.Value);
  127. }
  128. }
  129. UnknownFieldSet unknownFields = UnknownFields;
  130. if (DescriptorForType.Options.MessageSetWireFormat) {
  131. unknownFields.WriteAsMessageSetTo(output);
  132. } else {
  133. unknownFields.WriteTo(output);
  134. }
  135. }
  136. public virtual int SerializedSize {
  137. get {
  138. if (memoizedSize != null) {
  139. return memoizedSize.Value;
  140. }
  141. int size = 0;
  142. foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
  143. FieldDescriptor field = entry.Key;
  144. if (field.IsRepeated) {
  145. IEnumerable valueList = (IEnumerable) entry.Value;
  146. if (field.IsPacked) {
  147. int dataSize = 0;
  148. foreach (object element in valueList) {
  149. dataSize += CodedOutputStream.ComputeFieldSizeNoTag(field.FieldType, element);
  150. }
  151. size += dataSize;
  152. size += CodedOutputStream.ComputeTagSize(field.FieldNumber);
  153. size += CodedOutputStream.ComputeRawVarint32Size((uint)dataSize);
  154. } else {
  155. foreach (object element in valueList) {
  156. size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, element);
  157. }
  158. }
  159. } else {
  160. size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, entry.Value);
  161. }
  162. }
  163. UnknownFieldSet unknownFields = UnknownFields;
  164. if (DescriptorForType.Options.MessageSetWireFormat) {
  165. size += unknownFields.SerializedSizeAsMessageSet;
  166. } else {
  167. size += unknownFields.SerializedSize;
  168. }
  169. memoizedSize = size;
  170. return size;
  171. }
  172. }
  173. public ByteString ToByteString() {
  174. ByteString.CodedBuilder output = new ByteString.CodedBuilder(SerializedSize);
  175. WriteTo(output.CodedOutput);
  176. return output.Build();
  177. }
  178. public byte[] ToByteArray() {
  179. byte[] result = new byte[SerializedSize];
  180. CodedOutputStream output = CodedOutputStream.CreateInstance(result);
  181. WriteTo(output);
  182. output.CheckNoSpaceLeft();
  183. return result;
  184. }
  185. public void WriteTo(Stream output) {
  186. CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output);
  187. WriteTo(codedOutput);
  188. codedOutput.Flush();
  189. }
  190. public void WriteDelimitedTo(Stream output) {
  191. CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output);
  192. codedOutput.WriteRawVarint32((uint) SerializedSize);
  193. WriteTo(codedOutput);
  194. codedOutput.Flush();
  195. }
  196. public override bool Equals(object other) {
  197. if (other == this) {
  198. return true;
  199. }
  200. IMessage otherMessage = other as IMessage;
  201. if (otherMessage == null || otherMessage.DescriptorForType != DescriptorForType) {
  202. return false;
  203. }
  204. return Dictionaries.Equals(AllFields, otherMessage.AllFields) && UnknownFields.Equals(otherMessage.UnknownFields);
  205. }
  206. public override int GetHashCode() {
  207. int hash = 41;
  208. hash = (19 * hash) + DescriptorForType.GetHashCode();
  209. hash = (53 * hash) + Dictionaries.GetHashCode(AllFields);
  210. hash = (29 * hash) + UnknownFields.GetHashCode();
  211. return hash;
  212. }
  213. }
  214. }