IBuilderLite.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 {
  48. /// <summary>
  49. /// Returns true iff all required fields in the message and all
  50. /// embedded messages are set.
  51. /// </summary>
  52. 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. IBuilder WeakClear();
  109. IBuilder WeakClearField(FieldDescriptor field);
  110. IBuilder WeakMergeFrom(IMessage message);
  111. IBuilder WeakMergeFrom(ByteString data);
  112. IBuilder WeakMergeFrom(ByteString data, ExtensionRegistry registry);
  113. IBuilder WeakMergeFrom(CodedInputStream input);
  114. IBuilder WeakMergeFrom(CodedInputStream input, ExtensionRegistry registry);
  115. IMessage WeakBuild();
  116. IMessage WeakBuildPartial();
  117. IBuilder WeakClone();
  118. 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
  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. TBuilder Clear();
  135. /// <summary>
  136. /// Merge the specified other message into the message being
  137. /// built. Merging occurs as follows. For each field:
  138. /// For singular primitive fields, if the field is set in <paramref name="other"/>,
  139. /// then <paramref name="other"/>'s value overwrites the value in this message.
  140. /// For singular message fields, if the field is set in <paramref name="other"/>,
  141. /// it is merged into the corresponding sub-message of this message using the same
  142. /// merging rules.
  143. /// For repeated fields, the elements in <paramref name="other"/> are concatenated
  144. /// with the elements in this message.
  145. /// </summary>
  146. /// <param name="other"></param>
  147. /// <returns></returns>
  148. TBuilder MergeFrom(TMessage other);
  149. /// <summary>
  150. /// Merge the specified other message which may be a different implementation of
  151. /// the same message descriptor.
  152. /// </summary>
  153. TBuilder MergeFrom(IMessage other);
  154. /// <summary>
  155. /// Constructs the final message. Once this is called, this Builder instance
  156. /// is no longer valid, and calling any other method may throw a
  157. /// NullReferenceException. If you need to continue working with the builder
  158. /// after calling Build, call Clone first.
  159. /// </summary>
  160. /// <exception cref="UninitializedMessageException">the message
  161. /// is missing one or more required fields; use BuildPartial to bypass
  162. /// this check</exception>
  163. TMessage Build();
  164. /// <summary>
  165. /// Like Build(), but does not throw an exception if the message is missing
  166. /// required fields. Instead, a partial message is returned.
  167. /// </summary>
  168. TMessage BuildPartial();
  169. /// <summary>
  170. /// Clones this builder.
  171. /// TODO(jonskeet): Explain depth of clone.
  172. /// </summary>
  173. TBuilder Clone();
  174. /// <summary>
  175. /// Parses a message of this type from the input and merges it with this
  176. /// message, as if using MergeFrom(IMessage&lt;T&gt;).
  177. /// </summary>
  178. /// <remarks>
  179. /// Warning: This does not verify that all required fields are present
  180. /// in the input message. If you call Build() without setting all
  181. /// required fields, it will throw an UninitializedMessageException.
  182. /// There are a few good ways to deal with this:
  183. /// <list>
  184. /// <item>Call IsInitialized to verify to verify that all required fields are
  185. /// set before building.</item>
  186. /// <item>Parse the message separately using one of the static ParseFrom
  187. /// methods, then use MergeFrom(IMessage&lt;T&gt;) to merge it with
  188. /// this one. ParseFrom will throw an InvalidProtocolBufferException
  189. /// (an IOException) if some required fields are missing.
  190. /// Use BuildPartial to build, which ignores missing required fields.
  191. /// </list>
  192. /// </remarks>
  193. TBuilder MergeFrom(CodedInputStream input);
  194. /// <summary>
  195. /// Like MergeFrom(CodedInputStream), but also parses extensions.
  196. /// The extensions that you want to be able to parse must be registered
  197. /// in <paramref name="extensionRegistry"/>. Extensions not in the registry
  198. /// will be treated as unknown fields.
  199. /// </summary>
  200. TBuilder MergeFrom(CodedInputStream input, ExtensionRegistry extensionRegistry);
  201. /// <summary>
  202. /// Get's the message's type's default instance.
  203. /// <see cref="IMessage{TMessage}.DefaultInstanceForType" />
  204. /// </summary>
  205. TMessage DefaultInstanceForType { get; }
  206. /// <summary>
  207. /// Clears the field. This is exactly equivalent to calling the generated
  208. /// Clear method corresponding to the field.
  209. /// </summary>
  210. /// <param name="field"></param>
  211. /// <returns></returns>
  212. TBuilder ClearField(FieldDescriptor field);
  213. /// <summary>
  214. /// Appends the given value as a new element for the specified repeated field.
  215. /// </summary>
  216. /// <exception cref="ArgumentException">the field is not a repeated field,
  217. /// the field does not belong to this builder's type, or the value is
  218. /// of the incorrect type
  219. /// </exception>
  220. TBuilder AddRepeatedField(FieldDescriptor field, object value);
  221. /// <summary>
  222. /// Merge some unknown fields into the set for this message.
  223. /// </summary>
  224. TBuilder MergeUnknownFields(UnknownFieldSet unknownFields);
  225. /// <summary>
  226. /// Like MergeFrom(Stream), but does not read until the end of the file.
  227. /// Instead, the size of the message (encoded as a varint) is read first,
  228. /// then the message data. Use Message.WriteDelimitedTo(Stream) to
  229. /// write messages in this format.
  230. /// </summary>
  231. /// <param name="input"></param>
  232. TBuilder MergeDelimitedFrom(Stream input);
  233. /// <summary>
  234. /// Like MergeDelimitedFrom(Stream) but supporting extensions.
  235. /// </summary>
  236. TBuilder MergeDelimitedFrom(Stream input, ExtensionRegistry extensionRegistry);
  237. #region Convenience methods
  238. /// <summary>
  239. /// Parse <paramref name="data"/> as a message of this type and merge
  240. /// it with the message being built. This is just a small wrapper around
  241. /// MergeFrom(CodedInputStream).
  242. /// </summary>
  243. TBuilder MergeFrom(ByteString data);
  244. /// <summary>
  245. /// Parse <paramref name="data"/> as a message of this type and merge
  246. /// it with the message being built. This is just a small wrapper around
  247. /// MergeFrom(CodedInputStream, ExtensionRegistry).
  248. /// </summary>
  249. TBuilder MergeFrom(ByteString data, ExtensionRegistry extensionRegistry);
  250. /// <summary>
  251. /// Parse <paramref name="data"/> as a message of this type and merge
  252. /// it with the message being built. This is just a small wrapper around
  253. /// MergeFrom(CodedInputStream).
  254. /// </summary>
  255. TBuilder MergeFrom(byte[] data);
  256. /// <summary>
  257. /// Parse <paramref name="data"/> as a message of this type and merge
  258. /// it with the message being built. This is just a small wrapper around
  259. /// MergeFrom(CodedInputStream, ExtensionRegistry).
  260. /// </summary>
  261. TBuilder MergeFrom(byte[] data, ExtensionRegistry extensionRegistry);
  262. /// <summary>
  263. /// Parse <paramref name="input"/> as a message of this type and merge
  264. /// it with the message being built. This is just a small wrapper around
  265. /// MergeFrom(CodedInputStream). Note that this method always reads
  266. /// the entire input (unless it throws an exception). If you want it to
  267. /// stop earlier, you will need to wrap the input in a wrapper
  268. /// stream which limits reading. Or, use IMessage.WriteDelimitedTo(Stream)
  269. /// to write your message and MmergeDelimitedFrom(Stream) to read it.
  270. /// Despite usually reading the entire stream, this method never closes the stream.
  271. /// </summary>
  272. TBuilder MergeFrom(Stream input);
  273. /// <summary>
  274. /// Parse <paramref name="input"/> as a message of this type and merge
  275. /// it with the message being built. This is just a small wrapper around
  276. /// MergeFrom(CodedInputStream, ExtensionRegistry).
  277. /// </summary>
  278. TBuilder MergeFrom(Stream input, ExtensionRegistry extensionRegistry);
  279. #endregion
  280. }
  281. }