IMessage.cs 7.1 KB

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