IMessage.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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, the boxed value is returned.
  57. /// For enum fields, the EnumValueDescriptor for the enum is returned.
  58. /// For embedded message fields, the sub-message
  59. /// is returned. For repeated fields, an IList&lt;T&gt; is returned.
  60. /// </summary>
  61. object this[FieldDescriptor field] { get; }
  62. /// <summary>
  63. /// Returns the number of elements of a repeated field. This is
  64. /// exactly equivalent to calling the generated "Count" property
  65. /// corresponding to the field.
  66. /// </summary>
  67. /// <exception cref="ArgumentException">the field is not a repeated field,
  68. /// or it's not a field of this type</exception>
  69. int GetRepeatedFieldCount(FieldDescriptor field);
  70. /// <summary>
  71. /// Gets an element of a repeated field. For value type fields
  72. /// including enums, the boxed value is returned. For embedded
  73. /// message fields, the sub-message is returned.
  74. /// </summary>
  75. /// <exception cref="ArgumentException">the field is not a repeated field,
  76. /// or it's not a field of this type</exception>
  77. /// <exception cref="ArgumentOutOfRangeException">the index is out of
  78. /// range for the repeated field's value</exception>
  79. object this[FieldDescriptor field, int index] { get; }
  80. /// <summary>
  81. /// Returns the unknown fields for this message.
  82. /// </summary>
  83. UnknownFieldSet UnknownFields { get; }
  84. /// <summary>
  85. /// Returns true iff all required fields in the message and all embedded
  86. /// messages are set.
  87. /// </summary>
  88. bool IsInitialized { get; }
  89. /// <summary>
  90. /// Serializes the message and writes it to the given output stream.
  91. /// This does not flush or close the stream.
  92. /// </summary>
  93. /// <param name="output"></param>
  94. void WriteTo(CodedOutputStream output);
  95. /// <summary>
  96. /// Returns the number of bytes required to encode this message.
  97. /// The result is only computed on the first call and memoized after that.
  98. /// </summary>
  99. int SerializedSize { get; }
  100. #region Comparison and hashing
  101. /// <summary>
  102. /// Compares the specified object with this message for equality.
  103. /// Returns true iff the given object is a message of the same type
  104. /// (as defined by DescriptorForType) and has identical values
  105. /// for all its fields.
  106. /// </summary>
  107. bool Equals(object other);
  108. /// <summary>
  109. /// Returns the hash code value for this message.
  110. /// TODO(jonskeet): Specify the hash algorithm, but better than the Java one!
  111. /// </summary>
  112. int GetHashCode();
  113. #endregion
  114. #region Convenience methods
  115. /// <summary>
  116. /// Converts the message to a string in protocol buffer text format.
  117. /// This is just a trivial wrapper around TextFormat.PrintToString.
  118. /// </summary>
  119. string ToString();
  120. /// <summary>
  121. /// Serializes the message to a ByteString. This is a trivial wrapper
  122. /// around WriteTo(CodedOutputStream).
  123. /// </summary>
  124. ByteString ToByteString();
  125. /// <summary>
  126. /// Serializes the message to a byte array. This is a trivial wrapper
  127. /// around WriteTo(CodedOutputStream).
  128. /// </summary>
  129. byte[] ToByteArray();
  130. /// <summary>
  131. /// Serializes the message and writes it to the given stream.
  132. /// This is just a wrapper around WriteTo(CodedOutputStream). This
  133. /// does not flush or close the stream.
  134. /// </summary>
  135. /// <param name="output"></param>
  136. void WriteTo(Stream output);
  137. #endregion
  138. #region Weakly typed members
  139. /// <summary>
  140. /// Returns an instance of this message type with all fields set to
  141. /// their default values. This may or may not be a singleton. This differs
  142. /// from the DefaultInstance property of each generated message class in that this
  143. /// method is an abstract method of IMessage whereas DefaultInstance is
  144. /// a static property of a specific class. They return the same thing.
  145. /// </summary>
  146. IMessage DefaultInstanceForType { get; }
  147. /// <summary>
  148. /// Constructs a new builder for a message of the same type as this message.
  149. /// </summary>
  150. IBuilder CreateBuilderForType();
  151. #endregion
  152. }
  153. /// <summary>
  154. /// Type-safe interface for all generated messages to implement.
  155. /// </summary>
  156. public interface IMessage<T> : IMessage where T : IMessage<T> {
  157. /// <summary>
  158. /// Returns an instance of this message type with all fields set to
  159. /// their default values. This may or may not be a singleton. This differs
  160. /// from the DefaultInstance property of each generated message class in that this
  161. /// method is an abstract method of IMessage whereas DefaultInstance is
  162. /// a static property of a specific class. They return the same thing.
  163. /// </summary>
  164. new T DefaultInstanceForType { get; }
  165. #region Builders
  166. /// <summary>
  167. /// Constructs a new builder for a message of the same type as this message.
  168. /// </summary>
  169. new IBuilder<T> CreateBuilderForType();
  170. #endregion
  171. }
  172. }