AbstractMessage.cs 6.1 KB

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