AbstractMessage.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using Google.ProtocolBuffers.Collections;
  20. using Google.ProtocolBuffers.Descriptors;
  21. namespace Google.ProtocolBuffers {
  22. /// <summary>
  23. /// Implementation of the non-generic IMessage interface as far as possible.
  24. /// </summary>
  25. public abstract class AbstractMessage<TMessage, TBuilder> : IMessage<TMessage, TBuilder>
  26. where TMessage : AbstractMessage<TMessage, TBuilder>
  27. where TBuilder : AbstractBuilder<TMessage, TBuilder> {
  28. /// <summary>
  29. /// The serialized size if it's already been computed, or null
  30. /// if we haven't computed it yet.
  31. /// </summary>
  32. private int? memoizedSize = null;
  33. #region Unimplemented members of IMessage
  34. public abstract MessageDescriptor DescriptorForType { get; }
  35. public abstract IDictionary<FieldDescriptor, object> AllFields { get; }
  36. public abstract bool HasField(FieldDescriptor field);
  37. public abstract object this[FieldDescriptor field] { get; }
  38. public abstract int GetRepeatedFieldCount(FieldDescriptor field);
  39. public abstract object this[FieldDescriptor field, int index] { get; }
  40. public abstract UnknownFieldSet UnknownFields { get; }
  41. public abstract TMessage DefaultInstanceForType { get; }
  42. public abstract TBuilder CreateBuilderForType();
  43. #endregion
  44. public IBuilder WeakCreateBuilderForType() {
  45. return CreateBuilderForType();
  46. }
  47. public IMessage WeakDefaultInstanceForType {
  48. get { return DefaultInstanceForType; }
  49. }
  50. public virtual bool IsInitialized {
  51. get {
  52. // Check that all required fields are present.
  53. foreach (FieldDescriptor field in DescriptorForType.Fields) {
  54. if (field.IsRequired && !HasField(field)) {
  55. return false;
  56. }
  57. }
  58. // Check that embedded messages are initialized.
  59. foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
  60. FieldDescriptor field = entry.Key;
  61. if (field.MappedType == MappedType.Message) {
  62. if (field.IsRepeated) {
  63. // We know it's an IList<T>, but not the exact type - so
  64. // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
  65. foreach (IMessage element in (IEnumerable) entry.Value) {
  66. if (!element.IsInitialized) {
  67. return false;
  68. }
  69. }
  70. } else {
  71. if (!((IMessage)entry.Value).IsInitialized) {
  72. return false;
  73. }
  74. }
  75. }
  76. }
  77. return true;
  78. }
  79. }
  80. public sealed override string ToString() {
  81. return TextFormat.PrintToString(this);
  82. }
  83. public virtual void WriteTo(CodedOutputStream output) {
  84. foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
  85. FieldDescriptor field = entry.Key;
  86. if (field.IsRepeated) {
  87. // We know it's an IList<T>, but not the exact type - so
  88. // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
  89. foreach (object element in (IEnumerable)entry.Value) {
  90. output.WriteField(field.FieldType, field.FieldNumber, element);
  91. }
  92. } else {
  93. output.WriteField(field.FieldType, field.FieldNumber, entry.Value);
  94. }
  95. }
  96. UnknownFieldSet unknownFields = UnknownFields;
  97. if (DescriptorForType.Options.MessageSetWireFormat) {
  98. unknownFields.WriteAsMessageSetTo(output);
  99. } else {
  100. unknownFields.WriteTo(output);
  101. }
  102. }
  103. public virtual int SerializedSize {
  104. get {
  105. if (memoizedSize != null) {
  106. return memoizedSize.Value;
  107. }
  108. int size = 0;
  109. foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
  110. FieldDescriptor field = entry.Key;
  111. if (field.IsRepeated) {
  112. foreach (object element in (IEnumerable) entry.Value) {
  113. size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, element);
  114. }
  115. } else {
  116. size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, entry.Value);
  117. }
  118. }
  119. UnknownFieldSet unknownFields = UnknownFields;
  120. if (DescriptorForType.Options.MessageSetWireFormat) {
  121. size += unknownFields.SerializedSizeAsMessageSet;
  122. } else {
  123. size += unknownFields.SerializedSize;
  124. }
  125. memoizedSize = size;
  126. return size;
  127. }
  128. }
  129. public ByteString ToByteString() {
  130. ByteString.CodedBuilder output = new ByteString.CodedBuilder(SerializedSize);
  131. WriteTo(output.CodedOutput);
  132. return output.Build();
  133. }
  134. public byte[] ToByteArray() {
  135. byte[] result = new byte[SerializedSize];
  136. CodedOutputStream output = CodedOutputStream.CreateInstance(result);
  137. WriteTo(output);
  138. output.CheckNoSpaceLeft();
  139. return result;
  140. }
  141. public void WriteTo(Stream output) {
  142. CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output);
  143. WriteTo(codedOutput);
  144. codedOutput.Flush();
  145. }
  146. public override bool Equals(object other) {
  147. if (other == this) {
  148. return true;
  149. }
  150. IMessage otherMessage = other as IMessage;
  151. if (otherMessage == null || otherMessage.DescriptorForType != DescriptorForType) {
  152. return false;
  153. }
  154. return Dictionaries.Equals(AllFields, otherMessage.AllFields);
  155. }
  156. public override int GetHashCode() {
  157. int hash = 41;
  158. hash = (19 * hash) + DescriptorForType.GetHashCode();
  159. hash = (53 * hash) + Dictionaries.GetHashCode(AllFields);
  160. return hash;
  161. }
  162. }
  163. }