AbstractMessage.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #endregion
  60. public IBuilder WeakCreateBuilderForType() {
  61. return CreateBuilderForType();
  62. }
  63. public IMessage WeakDefaultInstanceForType {
  64. get { return DefaultInstanceForType; }
  65. }
  66. public virtual bool IsInitialized {
  67. get {
  68. // Check that all required fields are present.
  69. foreach (FieldDescriptor field in DescriptorForType.Fields) {
  70. if (field.IsRequired && !HasField(field)) {
  71. return false;
  72. }
  73. }
  74. // Check that embedded messages are initialized.
  75. foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
  76. FieldDescriptor field = entry.Key;
  77. if (field.MappedType == MappedType.Message) {
  78. if (field.IsRepeated) {
  79. // We know it's an IList<T>, but not the exact type - so
  80. // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
  81. foreach (IMessage element in (IEnumerable) entry.Value) {
  82. if (!element.IsInitialized) {
  83. return false;
  84. }
  85. }
  86. } else {
  87. if (!((IMessage)entry.Value).IsInitialized) {
  88. return false;
  89. }
  90. }
  91. }
  92. }
  93. return true;
  94. }
  95. }
  96. public sealed override string ToString() {
  97. return TextFormat.PrintToString(this);
  98. }
  99. public virtual void WriteTo(CodedOutputStream output) {
  100. foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
  101. FieldDescriptor field = entry.Key;
  102. if (field.IsRepeated) {
  103. // We know it's an IList<T>, but not the exact type - so
  104. // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
  105. foreach (object element in (IEnumerable)entry.Value) {
  106. output.WriteField(field.FieldType, field.FieldNumber, element);
  107. }
  108. } else {
  109. output.WriteField(field.FieldType, field.FieldNumber, entry.Value);
  110. }
  111. }
  112. UnknownFieldSet unknownFields = UnknownFields;
  113. if (DescriptorForType.Options.MessageSetWireFormat) {
  114. unknownFields.WriteAsMessageSetTo(output);
  115. } else {
  116. unknownFields.WriteTo(output);
  117. }
  118. }
  119. public virtual int SerializedSize {
  120. get {
  121. if (memoizedSize != null) {
  122. return memoizedSize.Value;
  123. }
  124. int size = 0;
  125. foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
  126. FieldDescriptor field = entry.Key;
  127. if (field.IsRepeated) {
  128. foreach (object element in (IEnumerable) entry.Value) {
  129. size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, element);
  130. }
  131. } else {
  132. size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, entry.Value);
  133. }
  134. }
  135. UnknownFieldSet unknownFields = UnknownFields;
  136. if (DescriptorForType.Options.MessageSetWireFormat) {
  137. size += unknownFields.SerializedSizeAsMessageSet;
  138. } else {
  139. size += unknownFields.SerializedSize;
  140. }
  141. memoizedSize = size;
  142. return size;
  143. }
  144. }
  145. public ByteString ToByteString() {
  146. ByteString.CodedBuilder output = new ByteString.CodedBuilder(SerializedSize);
  147. WriteTo(output.CodedOutput);
  148. return output.Build();
  149. }
  150. public byte[] ToByteArray() {
  151. byte[] result = new byte[SerializedSize];
  152. CodedOutputStream output = CodedOutputStream.CreateInstance(result);
  153. WriteTo(output);
  154. output.CheckNoSpaceLeft();
  155. return result;
  156. }
  157. public void WriteTo(Stream output) {
  158. CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output);
  159. WriteTo(codedOutput);
  160. codedOutput.Flush();
  161. }
  162. public override bool Equals(object other) {
  163. if (other == this) {
  164. return true;
  165. }
  166. IMessage otherMessage = other as IMessage;
  167. if (otherMessage == null || otherMessage.DescriptorForType != DescriptorForType) {
  168. return false;
  169. }
  170. return Dictionaries.Equals(AllFields, otherMessage.AllFields);
  171. }
  172. public override int GetHashCode() {
  173. int hash = 41;
  174. hash = (19 * hash) + DescriptorForType.GetHashCode();
  175. hash = (53 * hash) + Dictionaries.GetHashCode(AllFields);
  176. return hash;
  177. }
  178. }
  179. }