IMessage.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.Generic;
  18. using System.IO;
  19. using Google.ProtocolBuffers.Descriptors;
  20. namespace Google.ProtocolBuffers {
  21. /// <summary>
  22. /// Non-generic interface implemented by all Protocol Buffers messages.
  23. /// Some members are repeated in the generic interface but with a
  24. /// type-specific signature. Type-safe implementations
  25. /// are encouraged to implement these non-generic members explicitly,
  26. /// and the generic members implicitly.
  27. /// </summary>
  28. public interface IMessage {
  29. /// <summary>
  30. /// Returns the message's type's descriptor. This differs from the
  31. /// Descriptor property of each generated message class in that this
  32. /// method is an abstract method of IMessage whereas Descriptor is
  33. /// a static property of a specific class. They return the same thing.
  34. /// </summary>
  35. MessageDescriptor DescriptorForType { get; }
  36. /// <summary>
  37. /// Returns a collection of all the fields in this message which are set
  38. /// and their corresponding values. A singular ("required" or "optional")
  39. /// field is set iff HasField() returns true for that field. A "repeated"
  40. /// field is set iff GetRepeatedFieldSize() is greater than zero. The
  41. /// values are exactly what would be returned by calling
  42. /// GetField(FieldDescriptor) for each field. The map
  43. /// is guaranteed to be a sorted map, so iterating over it will return fields
  44. /// in order by field number.
  45. /// </summary>
  46. IDictionary<FieldDescriptor, object> AllFields { get; }
  47. /// <summary>
  48. /// Returns true if the given field is set. This is exactly equivalent
  49. /// to calling the generated "Has" property corresponding to the field.
  50. /// </summary>
  51. /// <exception cref="ArgumentException">the field is a repeated field,
  52. /// or it's not a field of this type</exception>
  53. bool HasField(FieldDescriptor field);
  54. /// <summary>
  55. /// Obtains the value of the given field, or the default value if
  56. /// it isn't set. For value type fields including enums, the boxed
  57. /// value is returned. For embedded message fields, the sub-message
  58. /// is returned. For repeated fields, an IList&lt;T&gt; is returned.
  59. /// </summary>
  60. object this[FieldDescriptor field] { get; }
  61. /// <summary>
  62. /// Returns the number of elements of a repeated field. This is
  63. /// exactly equivalent to calling the generated "Count" property
  64. /// corresponding to the field.
  65. /// </summary>
  66. /// <exception cref="ArgumentException">the field is not a repeated field,
  67. /// or it's not a field of this type</exception>
  68. int GetRepeatedFieldCount(FieldDescriptor field);
  69. /// <summary>
  70. /// Gets an element of a repeated field. For value type fields
  71. /// including enums, the boxed value is returned. For embedded
  72. /// message fields, the sub-message is returned.
  73. /// </summary>
  74. /// <exception cref="ArgumentException">the field is not a repeated field,
  75. /// or it's not a field of this type</exception>
  76. /// <exception cref="ArgumentOutOfRangeException">the index is out of
  77. /// range for the repeated field's value</exception>
  78. object this[FieldDescriptor field, int index] { get; }
  79. /// <summary>
  80. /// Returns the unknown fields for this message.
  81. /// </summary>
  82. UnknownFieldSet UnknownFields { get; }
  83. /// <summary>
  84. /// Returns true iff all required fields in the message and all embedded
  85. /// messages are set.
  86. /// </summary>
  87. bool IsInitialized { get; }
  88. /// <summary>
  89. /// Serializes the message and writes it to the given output stream.
  90. /// This does not flush or close the stream.
  91. /// </summary>
  92. /// <param name="output"></param>
  93. void WriteTo(CodedOutputStream output);
  94. /// <summary>
  95. /// Returns the number of bytes required to encode this message.
  96. /// The result is only computed on the first call and memoized after that.
  97. /// </summary>
  98. int SerializedSize { get; }
  99. #region Comparison and hashing
  100. /// <summary>
  101. /// Compares the specified object with this message for equality.
  102. /// Returns true iff the given object is a message of the same type
  103. /// (as defined by DescriptorForType) and has identical values
  104. /// for all its fields.
  105. /// </summary>
  106. bool Equals(object other);
  107. /// <summary>
  108. /// Returns the hash code value for this message.
  109. /// TODO(jonskeet): Specify the hash algorithm, but better than the Java one!
  110. /// </summary>
  111. int GetHashCode();
  112. #endregion
  113. #region Convenience methods
  114. /// <summary>
  115. /// Converts the message to a string in protocol buffer text format.
  116. /// This is just a trivial wrapper around TextFormat.PrintToString.
  117. /// </summary>
  118. string ToString();
  119. /// <summary>
  120. /// Serializes the message to a ByteString. This is a trivial wrapper
  121. /// around WriteTo(CodedOutputStream).
  122. /// </summary>
  123. ByteString ToByteString();
  124. /// <summary>
  125. /// Serializes the message to a byte array. This is a trivial wrapper
  126. /// around WriteTo(CodedOutputStream).
  127. /// </summary>
  128. byte[] ToByteArray();
  129. /// <summary>
  130. /// Serializes the message and writes it to the given stream.
  131. /// This is just a wrapper around WriteTo(CodedOutputStream). This
  132. /// does not flush or close the stream.
  133. /// </summary>
  134. /// <param name="output"></param>
  135. void WriteTo(Stream output);
  136. #endregion
  137. #region Weakly typed members
  138. /// <summary>
  139. /// Returns an instance of this message type with all fields set to
  140. /// their default values. This may or may not be a singleton. This differs
  141. /// from the DefaultInstance property of each generated message class in that this
  142. /// method is an abstract method of IMessage whereas DefaultInstance is
  143. /// a static property of a specific class. They return the same thing.
  144. /// </summary>
  145. IMessage DefaultInstanceForType { get; }
  146. /// <summary>
  147. /// Constructs a new builder for a message of the same type as this message.
  148. /// </summary>
  149. IBuilder CreateBuilderForType();
  150. #endregion
  151. }
  152. /// <summary>
  153. /// Type-safe interface for all generated messages to implement.
  154. /// </summary>
  155. public interface IMessage<T> : IMessage where T : IMessage<T> {
  156. /// <summary>
  157. /// Returns an instance of this message type with all fields set to
  158. /// their default values. This may or may not be a singleton. This differs
  159. /// from the DefaultInstance property of each generated message class in that this
  160. /// method is an abstract method of IMessage whereas DefaultInstance is
  161. /// a static property of a specific class. They return the same thing.
  162. /// </summary>
  163. new T DefaultInstanceForType { get; }
  164. #region Builders
  165. /// <summary>
  166. /// Constructs a new builder for a message of the same type as this message.
  167. /// </summary>
  168. new IBuilder<T> CreateBuilderForType();
  169. #endregion
  170. }
  171. }