AbstractMessage.cs 6.4 KB

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