IMessage.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // http://github.com/jskeet/dotnet-protobufs/
  5. // Original C++/Java/Python code:
  6. // http://code.google.com/p/protobuf/
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. //
  12. // * Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. // * Redistributions in binary form must reproduce the above
  15. // copyright notice, this list of conditions and the following disclaimer
  16. // in the documentation and/or other materials provided with the
  17. // distribution.
  18. // * Neither the name of Google Inc. nor the names of its
  19. // contributors may be used to endorse or promote products derived from
  20. // this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. #endregion
  34. using System;
  35. using System.Collections.Generic;
  36. using System.IO;
  37. using Google.ProtocolBuffers.Descriptors;
  38. namespace Google.ProtocolBuffers {
  39. /// <summary>
  40. /// Non-generic interface used for all parts of the API which don't require
  41. /// any type knowledge.
  42. /// </summary>
  43. public interface IMessage : IMessageLite {
  44. /// <summary>
  45. /// Returns the message's type's descriptor. This differs from the
  46. /// Descriptor property of each generated message class in that this
  47. /// method is an abstract method of IMessage whereas Descriptor is
  48. /// a static property of a specific class. They return the same thing.
  49. /// </summary>
  50. MessageDescriptor DescriptorForType { get; }
  51. /// <summary>
  52. /// Returns a collection of all the fields in this message which are set
  53. /// and their corresponding values. A singular ("required" or "optional")
  54. /// field is set iff HasField() returns true for that field. A "repeated"
  55. /// field is set iff GetRepeatedFieldSize() is greater than zero. The
  56. /// values are exactly what would be returned by calling
  57. /// GetField(FieldDescriptor) for each field. The map
  58. /// is guaranteed to be a sorted map, so iterating over it will return fields
  59. /// in order by field number.
  60. /// </summary>
  61. IDictionary<FieldDescriptor, object> AllFields { get; }
  62. /// <summary>
  63. /// Returns true if the given field is set. This is exactly equivalent
  64. /// to calling the generated "Has" property corresponding to the field.
  65. /// </summary>
  66. /// <exception cref="ArgumentException">the field is a repeated field,
  67. /// or it's not a field of this type</exception>
  68. bool HasField(FieldDescriptor field);
  69. /// <summary>
  70. /// Obtains the value of the given field, or the default value if
  71. /// it isn't set. For value type fields, the boxed value is returned.
  72. /// For enum fields, the EnumValueDescriptor for the enum is returned.
  73. /// For embedded message fields, the sub-message
  74. /// is returned. For repeated fields, an IList&lt;T&gt; is returned.
  75. /// </summary>
  76. object this[FieldDescriptor field] { get; }
  77. /// <summary>
  78. /// Returns the number of elements of a repeated field. This is
  79. /// exactly equivalent to calling the generated "Count" property
  80. /// corresponding to the field.
  81. /// </summary>
  82. /// <exception cref="ArgumentException">the field is not a repeated field,
  83. /// or it's not a field of this type</exception>
  84. int GetRepeatedFieldCount(FieldDescriptor field);
  85. /// <summary>
  86. /// Gets an element of a repeated field. For value type fields
  87. /// excluding enums, the boxed value is returned. For embedded
  88. /// message fields, the sub-message is returned. For enums, the
  89. /// relevant EnumValueDescriptor is returned.
  90. /// </summary>
  91. /// <exception cref="ArgumentException">the field is not a repeated field,
  92. /// or it's not a field of this type</exception>
  93. /// <exception cref="ArgumentOutOfRangeException">the index is out of
  94. /// range for the repeated field's value</exception>
  95. object this[FieldDescriptor field, int index] { get; }
  96. /// <summary>
  97. /// Returns the unknown fields for this message.
  98. /// </summary>
  99. UnknownFieldSet UnknownFields { get; }
  100. /// <summary>
  101. /// Returns true iff all required fields in the message and all embedded
  102. /// messages are set.
  103. /// </summary>
  104. new bool IsInitialized { get; }
  105. /// <summary>
  106. /// Serializes the message and writes it to the given output stream.
  107. /// This does not flush or close the stream.
  108. /// </summary>
  109. /// <remarks>
  110. /// Protocol Buffers are not self-delimiting. Therefore, if you write
  111. /// any more data to the stream after the message, you must somehow ensure
  112. /// that the parser on the receiving end does not interpret this as being
  113. /// part of the protocol message. One way of doing this is by writing the size
  114. /// of the message before the data, then making sure you limit the input to
  115. /// that size when receiving the data. Alternatively, use WriteDelimitedTo(Stream).
  116. /// </remarks>
  117. new void WriteTo(CodedOutputStream output);
  118. /// <summary>
  119. /// Like WriteTo(Stream) but writes the size of the message as a varint before
  120. /// writing the data. This allows more data to be written to the stream after the
  121. /// message without the need to delimit the message data yourself. Use
  122. /// IBuilder.MergeDelimitedFrom(Stream) or the static method
  123. /// YourMessageType.ParseDelimitedFrom(Stream) to parse messages written by this method.
  124. /// </summary>
  125. /// <param name="output"></param>
  126. new void WriteDelimitedTo(Stream output);
  127. /// <summary>
  128. /// Returns the number of bytes required to encode this message.
  129. /// The result is only computed on the first call and memoized after that.
  130. /// </summary>
  131. new int SerializedSize { get; }
  132. #region Comparison and hashing
  133. /// <summary>
  134. /// Compares the specified object with this message for equality.
  135. /// Returns true iff the given object is a message of the same type
  136. /// (as defined by DescriptorForType) and has identical values
  137. /// for all its fields.
  138. /// </summary>
  139. new bool Equals(object other);
  140. /// <summary>
  141. /// Returns the hash code value for this message.
  142. /// TODO(jonskeet): Specify the hash algorithm, but better than the Java one!
  143. /// </summary>
  144. new int GetHashCode();
  145. #endregion
  146. #region Convenience methods
  147. /// <summary>
  148. /// Converts the message to a string in protocol buffer text format.
  149. /// This is just a trivial wrapper around TextFormat.PrintToString.
  150. /// </summary>
  151. new string ToString();
  152. /// <summary>
  153. /// Serializes the message to a ByteString. This is a trivial wrapper
  154. /// around WriteTo(CodedOutputStream).
  155. /// </summary>
  156. new ByteString ToByteString();
  157. /// <summary>
  158. /// Serializes the message to a byte array. This is a trivial wrapper
  159. /// around WriteTo(CodedOutputStream).
  160. /// </summary>
  161. new byte[] ToByteArray();
  162. /// <summary>
  163. /// Serializes the message and writes it to the given stream.
  164. /// This is just a wrapper around WriteTo(CodedOutputStream). This
  165. /// does not flush or close the stream.
  166. /// </summary>
  167. /// <param name="output"></param>
  168. new void WriteTo(Stream output);
  169. #endregion
  170. /// <summary>
  171. /// Creates a builder for the type, but in a weakly typed manner. This
  172. /// is typically implemented by strongly typed messages by just returning
  173. /// the result of CreateBuilderForType.
  174. /// </summary>
  175. new IBuilder WeakCreateBuilderForType();
  176. /// <summary>
  177. /// Creates a builder with the same contents as this message. This
  178. /// is typically implemented by strongly typed messages by just returning
  179. /// the result of ToBuilder.
  180. /// </summary>
  181. new IBuilder WeakToBuilder();
  182. new IMessage WeakDefaultInstanceForType { get; }
  183. }
  184. public interface IMessage<TMessage> : IMessage, IMessageLite<TMessage> {
  185. /// <summary>
  186. /// Returns an instance of this message type with all fields set to
  187. /// their default values. This may or may not be a singleton. This differs
  188. /// from the DefaultInstance property of each generated message class in that this
  189. /// method is an abstract method of IMessage whereas DefaultInstance is
  190. /// a static property of a specific class. They return the same thing.
  191. /// </summary>
  192. new TMessage DefaultInstanceForType { get; }
  193. }
  194. /// <summary>
  195. /// Type-safe interface for all generated messages to implement.
  196. /// </summary>
  197. public interface IMessage<TMessage, TBuilder> : IMessage<TMessage>, IMessageLite<TMessage, TBuilder>
  198. where TMessage : IMessage<TMessage, TBuilder>
  199. where TBuilder : IBuilder<TMessage, TBuilder> {
  200. #region Builders
  201. /// <summary>
  202. /// Constructs a new builder for a message of the same type as this message.
  203. /// </summary>
  204. new TBuilder CreateBuilderForType();
  205. /// <summary>
  206. /// Creates a builder with the same contents as this current instance.
  207. /// This is typically implemented by strongly typed messages by just
  208. /// returning the result of ToBuilder().
  209. /// </summary>
  210. new TBuilder ToBuilder();
  211. #endregion
  212. }
  213. }