AbstractMessage.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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<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. #endregion
  42. #region New abstract methods to be overridden by implementations, allow explicit interface implementation
  43. protected abstract IMessage DefaultInstanceForTypeImpl { get; }
  44. protected abstract IBuilder CreateBuilderForTypeImpl();
  45. #endregion
  46. #region Methods simply proxying to the "Impl" methods, explicitly implementing IMessage
  47. IMessage IMessage.DefaultInstanceForType {
  48. get { return DefaultInstanceForTypeImpl; }
  49. }
  50. IBuilder IMessage.CreateBuilderForType() {
  51. return CreateBuilderForTypeImpl();
  52. }
  53. #endregion
  54. public bool IsInitialized {
  55. get {
  56. // Check that all required fields are present.
  57. foreach (FieldDescriptor field in DescriptorForType.Fields) {
  58. if (field.IsRequired && !HasField(field)) {
  59. return false;
  60. }
  61. }
  62. // Check that embedded messages are initialized.
  63. foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
  64. FieldDescriptor field = entry.Key;
  65. if (field.MappedType == MappedType.Message) {
  66. if (field.IsRepeated) {
  67. // We know it's an IList<T>, but not the exact type - so
  68. // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
  69. foreach (IMessage element in (IEnumerable)entry.Value) {
  70. if (!element.IsInitialized) {
  71. return false;
  72. }
  73. }
  74. } else {
  75. if (!((IMessage)entry.Value).IsInitialized) {
  76. return false;
  77. }
  78. }
  79. }
  80. }
  81. return true;
  82. }
  83. }
  84. public sealed override string ToString() {
  85. return TextFormat.PrintToString(this);
  86. }
  87. public void WriteTo(CodedOutputStream output) {
  88. foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
  89. FieldDescriptor field = entry.Key;
  90. if (field.IsRepeated) {
  91. // We know it's an IList<T>, but not the exact type - so
  92. // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
  93. foreach (object element in (IEnumerable)entry.Value) {
  94. output.WriteField(field.FieldType, field.FieldNumber, element);
  95. }
  96. } else {
  97. output.WriteField(field.FieldType, field.FieldNumber, entry.Value);
  98. }
  99. }
  100. UnknownFieldSet unknownFields = UnknownFields;
  101. if (DescriptorForType.Options.IsMessageSetWireFormat) {
  102. unknownFields.WriteAsMessageSetTo(output);
  103. } else {
  104. unknownFields.WriteTo(output);
  105. }
  106. }
  107. public int SerializedSize {
  108. get {
  109. int size = memoizedSize;
  110. if (size != -1) {
  111. return size;
  112. }
  113. size = 0;
  114. foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
  115. FieldDescriptor field = entry.Key;
  116. if (field.IsRepeated) {
  117. foreach (object element in (IEnumerable)entry.Value) {
  118. size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, element);
  119. }
  120. } else {
  121. size += CodedOutputStream.ComputeFieldSize(field.FieldType, field.FieldNumber, entry.Value);
  122. }
  123. }
  124. UnknownFieldSet unknownFields = UnknownFields;
  125. if (DescriptorForType.Options.IsMessageSetWireFormat) {
  126. size += unknownFields.SerializedSizeAsMessageSet;
  127. } else {
  128. size += unknownFields.SerializedSize;
  129. }
  130. memoizedSize = size;
  131. return size;
  132. }
  133. }
  134. public ByteString ToByteString() {
  135. ByteString.CodedBuilder output = new ByteString.CodedBuilder(SerializedSize);
  136. WriteTo(output.CodedOutput);
  137. return output.Build();
  138. }
  139. public byte[] ToByteArray() {
  140. byte[] result = new byte[SerializedSize];
  141. CodedOutputStream output = CodedOutputStream.CreateInstance(result);
  142. WriteTo(output);
  143. output.CheckNoSpaceLeft();
  144. return result;
  145. }
  146. public void WriteTo(Stream output) {
  147. CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output);
  148. WriteTo(codedOutput);
  149. codedOutput.Flush();
  150. }
  151. public override bool Equals(object other) {
  152. if (other == this) {
  153. return true;
  154. }
  155. IMessage otherMessage = other as IMessage;
  156. if (otherMessage == null || otherMessage.DescriptorForType != DescriptorForType) {
  157. return false;
  158. }
  159. // TODO(jonskeet): Check that dictionaries support equality appropriately
  160. // (I suspect they don't!)
  161. return AllFields.Equals(otherMessage.AllFields);
  162. }
  163. public override int GetHashCode() {
  164. int hash = 41;
  165. hash = (19 * hash) + DescriptorForType.GetHashCode();
  166. hash = (53 * hash) + AllFields.GetHashCode();
  167. return hash;
  168. }
  169. #region IMessage Members
  170. MessageDescriptor IMessage.DescriptorForType {
  171. get { throw new NotImplementedException(); }
  172. }
  173. IDictionary<FieldDescriptor, object> IMessage.AllFields {
  174. get { throw new NotImplementedException(); }
  175. }
  176. bool IMessage.HasField(FieldDescriptor field) {
  177. throw new NotImplementedException();
  178. }
  179. object IMessage.this[FieldDescriptor field] {
  180. get { throw new NotImplementedException(); }
  181. }
  182. int IMessage.GetRepeatedFieldCount(FieldDescriptor field) {
  183. throw new NotImplementedException();
  184. }
  185. object IMessage.this[FieldDescriptor field, int index] {
  186. get { throw new NotImplementedException(); }
  187. }
  188. UnknownFieldSet IMessage.UnknownFields {
  189. get { throw new NotImplementedException(); }
  190. }
  191. bool IMessage.IsInitialized {
  192. get { throw new NotImplementedException(); }
  193. }
  194. void IMessage.WriteTo(CodedOutputStream output) {
  195. throw new NotImplementedException();
  196. }
  197. int IMessage.SerializedSize {
  198. get { throw new NotImplementedException(); }
  199. }
  200. bool IMessage.Equals(object other) {
  201. throw new NotImplementedException();
  202. }
  203. int IMessage.GetHashCode() {
  204. throw new NotImplementedException();
  205. }
  206. string IMessage.ToString() {
  207. throw new NotImplementedException();
  208. }
  209. ByteString IMessage.ToByteString() {
  210. throw new NotImplementedException();
  211. }
  212. byte[] IMessage.ToByteArray() {
  213. throw new NotImplementedException();
  214. }
  215. void IMessage.WriteTo(Stream output) {
  216. throw new NotImplementedException();
  217. }
  218. #endregion
  219. }
  220. }