IMessage.cs 6.0 KB

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