AbstractMessage.cs 6.5 KB

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