ExtendableBuilder.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 Google.ProtocolBuffers.Descriptors;
  37. namespace Google.ProtocolBuffers
  38. {
  39. public abstract class ExtendableBuilder<TMessage, TBuilder> : GeneratedBuilder<TMessage, TBuilder>
  40. where TMessage : ExtendableMessage<TMessage, TBuilder>
  41. where TBuilder : GeneratedBuilder<TMessage, TBuilder>
  42. {
  43. protected ExtendableBuilder()
  44. {
  45. }
  46. /// <summary>
  47. /// Checks if a singular extension is present
  48. /// </summary>
  49. public bool HasExtension<TExtension>(GeneratedExtensionBase<TExtension> extension)
  50. {
  51. return MessageBeingBuilt.HasExtension(extension);
  52. }
  53. /// <summary>
  54. /// Returns the number of elements in a repeated extension.
  55. /// </summary>
  56. public int GetExtensionCount<TExtension>(GeneratedExtensionBase<IList<TExtension>> extension)
  57. {
  58. return MessageBeingBuilt.GetExtensionCount(extension);
  59. }
  60. /// <summary>
  61. /// Returns the value of an extension.
  62. /// </summary>
  63. public TExtension GetExtension<TExtension>(GeneratedExtensionBase<TExtension> extension)
  64. {
  65. return MessageBeingBuilt.GetExtension(extension);
  66. }
  67. /// <summary>
  68. /// Returns one element of a repeated extension.
  69. /// </summary>
  70. public TExtension GetExtension<TExtension>(GeneratedExtensionBase<IList<TExtension>> extension, int index)
  71. {
  72. return MessageBeingBuilt.GetExtension(extension, index);
  73. }
  74. /// <summary>
  75. /// Sets the value of an extension.
  76. /// </summary>
  77. public TBuilder SetExtension<TExtension>(GeneratedExtensionBase<TExtension> extension, TExtension value)
  78. {
  79. ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
  80. message.VerifyExtensionContainingType(extension);
  81. message.Extensions[extension.Descriptor] = extension.ToReflectionType(value);
  82. return ThisBuilder;
  83. }
  84. /// <summary>
  85. /// Sets the value of one element of a repeated extension.
  86. /// </summary>
  87. public TBuilder SetExtension<TExtension>(GeneratedExtensionBase<IList<TExtension>> extension, int index,
  88. TExtension value)
  89. {
  90. ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
  91. message.VerifyExtensionContainingType(extension);
  92. message.Extensions[extension.Descriptor, index] = extension.SingularToReflectionType(value);
  93. return ThisBuilder;
  94. }
  95. /// <summary>
  96. /// Appends a value to a repeated extension.
  97. /// </summary>
  98. public TBuilder AddExtension<TExtension>(GeneratedExtensionBase<IList<TExtension>> extension, TExtension value)
  99. {
  100. ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
  101. message.VerifyExtensionContainingType(extension);
  102. message.Extensions.AddRepeatedField(extension.Descriptor, extension.SingularToReflectionType(value));
  103. return ThisBuilder;
  104. }
  105. /// <summary>
  106. /// Clears an extension.
  107. /// </summary>
  108. public TBuilder ClearExtension<TExtension>(GeneratedExtensionBase<TExtension> extension)
  109. {
  110. ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
  111. message.VerifyExtensionContainingType(extension);
  112. message.Extensions.ClearField(extension.Descriptor);
  113. return ThisBuilder;
  114. }
  115. /// <summary>
  116. /// Called by subclasses to parse an unknown field or an extension.
  117. /// </summary>
  118. /// <returns>true unless the tag is an end-group tag</returns>
  119. [CLSCompliant(false)]
  120. protected override bool ParseUnknownField(ICodedInputStream input, UnknownFieldSet.Builder unknownFields,
  121. ExtensionRegistry extensionRegistry, uint tag, string fieldName)
  122. {
  123. return unknownFields.MergeFieldFrom(input, extensionRegistry, this, tag, fieldName);
  124. }
  125. // ---------------------------------------------------------------
  126. // Reflection
  127. public override object this[FieldDescriptor field, int index]
  128. {
  129. set
  130. {
  131. if (field.IsExtension)
  132. {
  133. ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
  134. message.VerifyContainingType(field);
  135. message.Extensions[field, index] = value;
  136. }
  137. else
  138. {
  139. base[field, index] = value;
  140. }
  141. }
  142. }
  143. public override object this[FieldDescriptor field]
  144. {
  145. set
  146. {
  147. if (field.IsExtension)
  148. {
  149. ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
  150. message.VerifyContainingType(field);
  151. message.Extensions[field] = value;
  152. }
  153. else
  154. {
  155. base[field] = value;
  156. }
  157. }
  158. }
  159. public override TBuilder ClearField(FieldDescriptor field)
  160. {
  161. if (field.IsExtension)
  162. {
  163. ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
  164. message.VerifyContainingType(field);
  165. message.Extensions.ClearField(field);
  166. return ThisBuilder;
  167. }
  168. else
  169. {
  170. return base.ClearField(field);
  171. }
  172. }
  173. public override TBuilder AddRepeatedField(FieldDescriptor field, object value)
  174. {
  175. if (field.IsExtension)
  176. {
  177. ExtendableMessage<TMessage, TBuilder> message = MessageBeingBuilt;
  178. message.VerifyContainingType(field);
  179. message.Extensions.AddRepeatedField(field, value);
  180. return ThisBuilder;
  181. }
  182. else
  183. {
  184. return base.AddRepeatedField(field, value);
  185. }
  186. }
  187. protected void MergeExtensionFields(ExtendableMessage<TMessage, TBuilder> other)
  188. {
  189. MessageBeingBuilt.Extensions.MergeFrom(other.Extensions);
  190. }
  191. }
  192. }