IMessage.cs 6.9 KB

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