IBuilder.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 for all members whose signatures don't require knowledge of
  41. /// the type being built. The generic interface extends this one. Some methods return
  42. /// either an IBuilder or an IMessage; in these cases the generic interface redeclares
  43. /// the same method with a type-specific signature. Implementations are encouraged to
  44. /// use explicit interface implemenation for the non-generic form. This mirrors
  45. /// how IEnumerable and IEnumerable&lt;T&gt; work.
  46. /// </summary>
  47. public interface IBuilder : IBuilderLite {
  48. /// <summary>
  49. /// Returns true iff all required fields in the message and all
  50. /// embedded messages are set.
  51. /// </summary>
  52. new bool IsInitialized { get; }
  53. /// <summary>
  54. /// Only present in the nongeneric interface - useful for tests, but
  55. /// not as much in real life.
  56. /// </summary>
  57. IBuilder SetField(FieldDescriptor field, object value);
  58. /// <summary>
  59. /// Only present in the nongeneric interface - useful for tests, but
  60. /// not as much in real life.
  61. /// </summary>
  62. IBuilder SetRepeatedField(FieldDescriptor field, int index, object value);
  63. /// <summary>
  64. /// Behaves like the equivalent property in IMessage&lt;T&gt;.
  65. /// The returned map may or may not reflect future changes to the builder.
  66. /// Either way, the returned map is unmodifiable.
  67. /// </summary>
  68. IDictionary<FieldDescriptor, object> AllFields { get; }
  69. /// <summary>
  70. /// Allows getting and setting of a field.
  71. /// <see cref="IMessage{TMessage, TBuilder}.Item(FieldDescriptor)"/>
  72. /// </summary>
  73. /// <param name="field"></param>
  74. /// <returns></returns>
  75. object this[FieldDescriptor field] { get; set; }
  76. /// <summary>
  77. /// Get the message's type descriptor.
  78. /// <see cref="IMessage{TMessage, TBuilder}.DescriptorForType"/>
  79. /// </summary>
  80. MessageDescriptor DescriptorForType { get; }
  81. /// <summary>
  82. /// <see cref="IMessage{TMessage, TBuilder}.GetRepeatedFieldCount"/>
  83. /// </summary>
  84. /// <param name="field"></param>
  85. /// <returns></returns>
  86. int GetRepeatedFieldCount(FieldDescriptor field);
  87. /// <summary>
  88. /// Allows getting and setting of a repeated field value.
  89. /// <see cref="IMessage{TMessage, TBuilder}.Item(FieldDescriptor, int)"/>
  90. /// </summary>
  91. object this[FieldDescriptor field, int index] { get; set; }
  92. /// <summary>
  93. /// <see cref="IMessage{TMessage, TBuilder}.HasField"/>
  94. /// </summary>
  95. bool HasField(FieldDescriptor field);
  96. /// <summary>
  97. /// <see cref="IMessage{TMessage, TBuilder}.UnknownFields"/>
  98. /// </summary>
  99. UnknownFieldSet UnknownFields { get; set; }
  100. /// <summary>
  101. /// Create a builder for messages of the appropriate type for the given field.
  102. /// Messages built with this can then be passed to the various mutation properties
  103. /// and methods.
  104. /// </summary>
  105. IBuilder CreateBuilderForField(FieldDescriptor field);
  106. #region Methods which are like those of the generic form, but without any knowledge of the type parameters
  107. IBuilder WeakAddRepeatedField(FieldDescriptor field, object value);
  108. new IBuilder WeakClear();
  109. IBuilder WeakClearField(FieldDescriptor field);
  110. IBuilder WeakMergeFrom(IMessage message);
  111. new IBuilder WeakMergeFrom(ByteString data);
  112. new IBuilder WeakMergeFrom(ByteString data, ExtensionRegistry registry);
  113. new IBuilder WeakMergeFrom(CodedInputStream input);
  114. new IBuilder WeakMergeFrom(CodedInputStream input, ExtensionRegistry registry);
  115. new IMessage WeakBuild();
  116. new IMessage WeakBuildPartial();
  117. new IBuilder WeakClone();
  118. new IMessage WeakDefaultInstanceForType { get; }
  119. #endregion
  120. }
  121. /// <summary>
  122. /// Interface implemented by Protocol Message builders.
  123. /// TODO(jonskeet): Consider "SetXXX" methods returning the builder, as well as the properties.
  124. /// </summary>
  125. /// <typeparam name="TMessage">Type of message</typeparam>
  126. /// <typeparam name="TBuilder">Type of builder</typeparam>
  127. public interface IBuilder<TMessage, TBuilder> : IBuilder, IBuilderLite<TMessage, TBuilder>
  128. where TMessage : IMessage<TMessage, TBuilder>
  129. where TBuilder : IBuilder<TMessage, TBuilder> {
  130. TBuilder SetUnknownFields(UnknownFieldSet unknownFields);
  131. /// <summary>
  132. /// Resets all fields to their default values.
  133. /// </summary>
  134. new TBuilder Clear();
  135. /// <summary>
  136. /// Merge the specified other message which may be a different implementation of
  137. /// the same message descriptor.
  138. /// </summary>
  139. TBuilder MergeFrom(IMessage other);
  140. /// <summary>
  141. /// Constructs the final message. Once this is called, this Builder instance
  142. /// is no longer valid, and calling any other method may throw a
  143. /// NullReferenceException. If you need to continue working with the builder
  144. /// after calling Build, call Clone first.
  145. /// </summary>
  146. /// <exception cref="UninitializedMessageException">the message
  147. /// is missing one or more required fields; use BuildPartial to bypass
  148. /// this check</exception>
  149. new TMessage Build();
  150. /// <summary>
  151. /// Like Build(), but does not throw an exception if the message is missing
  152. /// required fields. Instead, a partial message is returned.
  153. /// </summary>
  154. new TMessage BuildPartial();
  155. /// <summary>
  156. /// Clones this builder.
  157. /// TODO(jonskeet): Explain depth of clone.
  158. /// </summary>
  159. new TBuilder Clone();
  160. /// <summary>
  161. /// Parses a message of this type from the input and merges it with this
  162. /// message, as if using MergeFrom(IMessage&lt;T&gt;).
  163. /// </summary>
  164. /// <remarks>
  165. /// Warning: This does not verify that all required fields are present
  166. /// in the input message. If you call Build() without setting all
  167. /// required fields, it will throw an UninitializedMessageException.
  168. /// There are a few good ways to deal with this:
  169. /// <list>
  170. /// <item>Call IsInitialized to verify to verify that all required fields are
  171. /// set before building.</item>
  172. /// <item>Parse the message separately using one of the static ParseFrom
  173. /// methods, then use MergeFrom(IMessage&lt;T&gt;) to merge it with
  174. /// this one. ParseFrom will throw an InvalidProtocolBufferException
  175. /// (an IOException) if some required fields are missing.
  176. /// Use BuildPartial to build, which ignores missing required fields.
  177. /// </list>
  178. /// </remarks>
  179. new TBuilder MergeFrom(CodedInputStream input);
  180. /// <summary>
  181. /// Like MergeFrom(CodedInputStream), but also parses extensions.
  182. /// The extensions that you want to be able to parse must be registered
  183. /// in <paramref name="extensionRegistry"/>. Extensions not in the registry
  184. /// will be treated as unknown fields.
  185. /// </summary>
  186. new TBuilder MergeFrom(CodedInputStream input, ExtensionRegistry extensionRegistry);
  187. /// <summary>
  188. /// Get's the message's type's default instance.
  189. /// <see cref="IMessage{TMessage}.DefaultInstanceForType" />
  190. /// </summary>
  191. new TMessage DefaultInstanceForType { get; }
  192. /// <summary>
  193. /// Clears the field. This is exactly equivalent to calling the generated
  194. /// Clear method corresponding to the field.
  195. /// </summary>
  196. /// <param name="field"></param>
  197. /// <returns></returns>
  198. TBuilder ClearField(FieldDescriptor field);
  199. /// <summary>
  200. /// Appends the given value as a new element for the specified repeated field.
  201. /// </summary>
  202. /// <exception cref="ArgumentException">the field is not a repeated field,
  203. /// the field does not belong to this builder's type, or the value is
  204. /// of the incorrect type
  205. /// </exception>
  206. TBuilder AddRepeatedField(FieldDescriptor field, object value);
  207. /// <summary>
  208. /// Merge some unknown fields into the set for this message.
  209. /// </summary>
  210. TBuilder MergeUnknownFields(UnknownFieldSet unknownFields);
  211. /// <summary>
  212. /// Like MergeFrom(Stream), but does not read until the end of the file.
  213. /// Instead, the size of the message (encoded as a varint) is read first,
  214. /// then the message data. Use Message.WriteDelimitedTo(Stream) to
  215. /// write messages in this format.
  216. /// </summary>
  217. /// <param name="input"></param>
  218. new TBuilder MergeDelimitedFrom(Stream input);
  219. /// <summary>
  220. /// Like MergeDelimitedFrom(Stream) but supporting extensions.
  221. /// </summary>
  222. new TBuilder MergeDelimitedFrom(Stream input, ExtensionRegistry extensionRegistry);
  223. #region Convenience methods
  224. /// <summary>
  225. /// Parse <paramref name="data"/> as a message of this type and merge
  226. /// it with the message being built. This is just a small wrapper around
  227. /// MergeFrom(CodedInputStream).
  228. /// </summary>
  229. new TBuilder MergeFrom(ByteString data);
  230. /// <summary>
  231. /// Parse <paramref name="data"/> as a message of this type and merge
  232. /// it with the message being built. This is just a small wrapper around
  233. /// MergeFrom(CodedInputStream, extensionRegistry).
  234. /// </summary>
  235. new TBuilder MergeFrom(ByteString data, ExtensionRegistry extensionRegistry);
  236. /// <summary>
  237. /// Parse <paramref name="data"/> as a message of this type and merge
  238. /// it with the message being built. This is just a small wrapper around
  239. /// MergeFrom(CodedInputStream).
  240. /// </summary>
  241. new TBuilder MergeFrom(byte[] data);
  242. /// <summary>
  243. /// Parse <paramref name="data"/> as a message of this type and merge
  244. /// it with the message being built. This is just a small wrapper around
  245. /// MergeFrom(CodedInputStream, extensionRegistry).
  246. /// </summary>
  247. new TBuilder MergeFrom(byte[] data, ExtensionRegistry extensionRegistry);
  248. /// <summary>
  249. /// Parse <paramref name="input"/> as a message of this type and merge
  250. /// it with the message being built. This is just a small wrapper around
  251. /// MergeFrom(CodedInputStream). Note that this method always reads
  252. /// the entire input (unless it throws an exception). If you want it to
  253. /// stop earlier, you will need to wrap the input in a wrapper
  254. /// stream which limits reading. Or, use IMessage.WriteDelimitedTo(Stream)
  255. /// to write your message and MmergeDelimitedFrom(Stream) to read it.
  256. /// Despite usually reading the entire stream, this method never closes the stream.
  257. /// </summary>
  258. new TBuilder MergeFrom(Stream input);
  259. /// <summary>
  260. /// Parse <paramref name="input"/> as a message of this type and merge
  261. /// it with the message being built. This is just a small wrapper around
  262. /// MergeFrom(CodedInputStream, extensionRegistry).
  263. /// </summary>
  264. new TBuilder MergeFrom(Stream input, ExtensionRegistry extensionRegistry);
  265. #endregion
  266. }
  267. }