AbstractMessage.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. // TODO(jonskeet): Cleaner to use a Nullable<int>?
  29. /// <summary>
  30. /// The serialized size if it's already been computed, or -1
  31. /// if we haven't computed it yet.
  32. /// </summary>
  33. private int memoizedSize = -1;
  34. #region Unimplemented members of IMessage
  35. public abstract MessageDescriptor DescriptorForType { get; }
  36. public abstract IDictionary<FieldDescriptor, object> AllFields { get; }
  37. public abstract bool HasField(FieldDescriptor field);
  38. public abstract object this[FieldDescriptor field] { get; }
  39. public abstract int GetRepeatedFieldCount(FieldDescriptor field);
  40. public abstract object this[FieldDescriptor field, int index] { get; }
  41. public abstract UnknownFieldSet UnknownFields { get; }
  42. public abstract TMessage DefaultInstanceForType { get; }
  43. public abstract TBuilder CreateBuilderForType();
  44. #endregion
  45. public IBuilder WeakCreateBuilderForType() {
  46. return CreateBuilderForType();
  47. }
  48. public IMessage WeakDefaultInstanceForType {
  49. get { return DefaultInstanceForType; }
  50. }
  51. public virtual bool IsInitialized {
  52. get {
  53. // Check that all required fields are present.
  54. foreach (FieldDescriptor field in DescriptorForType.Fields) {
  55. if (field.IsRequired && !HasField(field)) {
  56. return false;
  57. }
  58. }
  59. // Check that embedded messages are initialized.
  60. foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
  61. FieldDescriptor field = entry.Key;
  62. if (field.MappedType == MappedType.Message) {
  63. if (field.IsRepeated) {
  64. // We know it's an IList<T>, but not the exact type - so
  65. // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
  66. foreach (IMessage element in (IEnumerable) entry.Value) {
  67. if (!element.IsInitialized) {
  68. return false;
  69. }
  70. }
  71. } else {
  72. if (!((IMessage)entry.Value).IsInitialized) {
  73. return false;
  74. }
  75. }
  76. }
  77. }
  78. return true;
  79. }
  80. }
  81. public sealed override string ToString() {
  82. return TextFormat.PrintToString(this);
  83. }
  84. public virtual void WriteTo(CodedOutputStream output) {
  85. foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
  86. FieldDescriptor field = entry.Key;
  87. if (field.IsRepeated) {
  88. // We know it's an IList<T>, but not the exact type - so
  89. // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
  90. foreach (object element in (IEnumerable)entry.Value) {
  91. output.WriteField(field.FieldType, field.FieldNumber, element);
  92. }
  93. } else {
  94. output.WriteField(field.FieldType, field.FieldNumber, entry.Value);
  95. }
  96. }
  97. UnknownFieldSet unknownFields = UnknownFields;
  98. if (DescriptorForType.Options.MessageSetWireFormat) {
  99. unknownFields.WriteAsMessageSetTo(output);
  100. } else {
  101. unknownFields.WriteTo(output);
  102. }
  103. }
  104. public virtual int SerializedSize {
  105. get {
  106. int size = memoizedSize;
  107. if (size != -1) {
  108. return size;
  109. }
  110. size = 0;
  111. foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
  112. FieldDescriptor field = entry.Key;
  113. if (field.IsRepeated) {
  114. foreach (object element in (IEnumerable) entry.Value) {
  115. size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, element);
  116. }
  117. } else {
  118. size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, entry.Value);
  119. }
  120. }
  121. UnknownFieldSet unknownFields = UnknownFields;
  122. if (DescriptorForType.Options.MessageSetWireFormat) {
  123. size += unknownFields.SerializedSizeAsMessageSet;
  124. } else {
  125. size += unknownFields.SerializedSize;
  126. }
  127. memoizedSize = size;
  128. return size;
  129. }
  130. }
  131. public ByteString ToByteString() {
  132. ByteString.CodedBuilder output = new ByteString.CodedBuilder(SerializedSize);
  133. WriteTo(output.CodedOutput);
  134. return output.Build();
  135. }
  136. public byte[] ToByteArray() {
  137. byte[] result = new byte[SerializedSize];
  138. CodedOutputStream output = CodedOutputStream.CreateInstance(result);
  139. WriteTo(output);
  140. output.CheckNoSpaceLeft();
  141. return result;
  142. }
  143. public void WriteTo(Stream output) {
  144. CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output);
  145. WriteTo(codedOutput);
  146. codedOutput.Flush();
  147. }
  148. public override bool Equals(object other) {
  149. if (other == this) {
  150. return true;
  151. }
  152. IMessage otherMessage = other as IMessage;
  153. if (otherMessage == null || otherMessage.DescriptorForType != DescriptorForType) {
  154. return false;
  155. }
  156. return Dictionaries.Equals(AllFields, otherMessage.AllFields);
  157. }
  158. public override int GetHashCode() {
  159. int hash = 41;
  160. hash = (19 * hash) + DescriptorForType.GetHashCode();
  161. hash = (53 * hash) + Dictionaries.GetHashCode(AllFields);
  162. return hash;
  163. }
  164. }
  165. }