ExtendableBuilderLite.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 partial class ExtendableBuilderLite<TMessage, TBuilder> : GeneratedBuilderLite<TMessage, TBuilder>
  40. where TMessage : ExtendableMessageLite<TMessage, TBuilder>
  41. where TBuilder : GeneratedBuilderLite<TMessage, TBuilder>
  42. {
  43. protected ExtendableBuilderLite()
  44. {
  45. }
  46. /// <summary>
  47. /// Checks if a singular extension is present
  48. /// </summary>
  49. public bool HasExtension<TExtension>(GeneratedExtensionLite<TMessage, 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>(GeneratedExtensionLite<TMessage, 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>(GeneratedExtensionLite<TMessage, 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>(GeneratedExtensionLite<TMessage, IList<TExtension>> extension,
  71. int index)
  72. {
  73. return MessageBeingBuilt.GetExtension(extension, index);
  74. }
  75. /// <summary>
  76. /// Sets the value of an extension.
  77. /// </summary>
  78. public TBuilder SetExtension<TExtension>(GeneratedExtensionLite<TMessage, TExtension> extension,
  79. TExtension value)
  80. {
  81. ExtendableMessageLite<TMessage, TBuilder> message = MessageBeingBuilt;
  82. message.VerifyExtensionContainingType(extension);
  83. message.Extensions[extension.Descriptor] = extension.ToReflectionType(value);
  84. return ThisBuilder;
  85. }
  86. /// <summary>
  87. /// Sets the value of one element of a repeated extension.
  88. /// </summary>
  89. public TBuilder SetExtension<TExtension>(GeneratedExtensionLite<TMessage, IList<TExtension>> extension,
  90. int index, TExtension value)
  91. {
  92. ExtendableMessageLite<TMessage, TBuilder> message = MessageBeingBuilt;
  93. message.VerifyExtensionContainingType(extension);
  94. message.Extensions[extension.Descriptor, index] = extension.SingularToReflectionType(value);
  95. return ThisBuilder;
  96. }
  97. /// <summary>
  98. /// Appends a value to a repeated extension.
  99. /// </summary>
  100. public TBuilder AddExtension<TExtension>(GeneratedExtensionLite<TMessage, IList<TExtension>> extension,
  101. TExtension value)
  102. {
  103. ExtendableMessageLite<TMessage, TBuilder> message = MessageBeingBuilt;
  104. message.VerifyExtensionContainingType(extension);
  105. message.Extensions.AddRepeatedField(extension.Descriptor, extension.SingularToReflectionType(value));
  106. return ThisBuilder;
  107. }
  108. /// <summary>
  109. /// Clears an extension.
  110. /// </summary>
  111. public TBuilder ClearExtension<TExtension>(GeneratedExtensionLite<TMessage, TExtension> extension)
  112. {
  113. ExtendableMessageLite<TMessage, TBuilder> message = MessageBeingBuilt;
  114. message.VerifyExtensionContainingType(extension);
  115. message.Extensions.ClearField(extension.Descriptor);
  116. return ThisBuilder;
  117. }
  118. /// <summary>
  119. /// Called by subclasses to parse an unknown field or an extension.
  120. /// </summary>
  121. /// <returns>true unless the tag is an end-group tag</returns>
  122. protected override bool ParseUnknownField(ICodedInputStream input,
  123. ExtensionRegistry extensionRegistry, uint tag, string fieldName)
  124. {
  125. FieldSet extensions = MessageBeingBuilt.Extensions;
  126. WireFormat.WireType wireType = WireFormat.GetTagWireType(tag);
  127. int fieldNumber = WireFormat.GetTagFieldNumber(tag);
  128. IGeneratedExtensionLite extension = extensionRegistry[DefaultInstanceForType, fieldNumber];
  129. if (extension == null) //unknown field
  130. {
  131. return input.SkipField();
  132. }
  133. IFieldDescriptorLite field = extension.Descriptor;
  134. // Unknown field or wrong wire type. Skip.
  135. if (field == null)
  136. {
  137. return input.SkipField();
  138. }
  139. WireFormat.WireType expectedType = field.IsPacked
  140. ? WireFormat.WireType.LengthDelimited
  141. : WireFormat.GetWireType(field.FieldType);
  142. if (wireType != expectedType)
  143. {
  144. expectedType = WireFormat.GetWireType(field.FieldType);
  145. if (wireType == expectedType)
  146. {
  147. //Allowed as of 2.3, this is unpacked data for a packed array
  148. }
  149. else if (field.IsRepeated && wireType == WireFormat.WireType.LengthDelimited &&
  150. (expectedType == WireFormat.WireType.Varint || expectedType == WireFormat.WireType.Fixed32 ||
  151. expectedType == WireFormat.WireType.Fixed64))
  152. {
  153. //Allowed as of 2.3, this is packed data for an unpacked array
  154. }
  155. else
  156. {
  157. return input.SkipField();
  158. }
  159. }
  160. if (!field.IsRepeated && wireType != WireFormat.GetWireType(field.FieldType)) //invalid wire type
  161. {
  162. return input.SkipField();
  163. }
  164. switch (field.FieldType)
  165. {
  166. case FieldType.Group:
  167. case FieldType.Message:
  168. {
  169. if (!field.IsRepeated)
  170. {
  171. IMessageLite message = extensions[extension.Descriptor] as IMessageLite;
  172. IBuilderLite subBuilder = (message ?? extension.MessageDefaultInstance).WeakToBuilder();
  173. if (field.FieldType == FieldType.Group)
  174. {
  175. input.ReadGroup(field.FieldNumber, subBuilder, extensionRegistry);
  176. }
  177. else
  178. {
  179. input.ReadMessage(subBuilder, extensionRegistry);
  180. }
  181. extensions[field] = subBuilder.WeakBuild();
  182. }
  183. else
  184. {
  185. List<IMessageLite> list = new List<IMessageLite>();
  186. if (field.FieldType == FieldType.Group)
  187. {
  188. input.ReadGroupArray(tag, fieldName, list, extension.MessageDefaultInstance,
  189. extensionRegistry);
  190. }
  191. else
  192. {
  193. input.ReadMessageArray(tag, fieldName, list, extension.MessageDefaultInstance,
  194. extensionRegistry);
  195. }
  196. foreach (IMessageLite m in list)
  197. {
  198. extensions.AddRepeatedField(field, m);
  199. }
  200. return true;
  201. }
  202. break;
  203. }
  204. case FieldType.Enum:
  205. {
  206. if (!field.IsRepeated)
  207. {
  208. object unknown;
  209. IEnumLite value = null;
  210. if (input.ReadEnum(ref value, out unknown, field.EnumType))
  211. {
  212. extensions[field] = value;
  213. }
  214. }
  215. else
  216. {
  217. ICollection<object> unknown;
  218. List<IEnumLite> list = new List<IEnumLite>();
  219. input.ReadEnumArray(tag, fieldName, list, out unknown, field.EnumType);
  220. foreach (IEnumLite en in list)
  221. {
  222. extensions.AddRepeatedField(field, en);
  223. }
  224. }
  225. break;
  226. }
  227. default:
  228. {
  229. if (!field.IsRepeated)
  230. {
  231. object value = null;
  232. if (input.ReadPrimitiveField(field.FieldType, ref value))
  233. {
  234. extensions[field] = value;
  235. }
  236. }
  237. else
  238. {
  239. List<object> list = new List<object>();
  240. input.ReadPrimitiveArray(field.FieldType, tag, fieldName, list);
  241. foreach (object oval in list)
  242. {
  243. extensions.AddRepeatedField(field, oval);
  244. }
  245. }
  246. break;
  247. }
  248. }
  249. return true;
  250. }
  251. #region Reflection
  252. public object this[IFieldDescriptorLite field, int index]
  253. {
  254. set
  255. {
  256. if (field.IsExtension)
  257. {
  258. ExtendableMessageLite<TMessage, TBuilder> message = MessageBeingBuilt;
  259. message.Extensions[field, index] = value;
  260. }
  261. else
  262. {
  263. throw new NotSupportedException("Not supported in the lite runtime.");
  264. }
  265. }
  266. }
  267. public object this[IFieldDescriptorLite field]
  268. {
  269. set
  270. {
  271. if (field.IsExtension)
  272. {
  273. ExtendableMessageLite<TMessage, TBuilder> message = MessageBeingBuilt;
  274. message.Extensions[field] = value;
  275. }
  276. else
  277. {
  278. throw new NotSupportedException("Not supported in the lite runtime.");
  279. }
  280. }
  281. }
  282. public TBuilder ClearField(IFieldDescriptorLite field)
  283. {
  284. if (field.IsExtension)
  285. {
  286. ExtendableMessageLite<TMessage, TBuilder> message = MessageBeingBuilt;
  287. message.Extensions.ClearField(field);
  288. return ThisBuilder;
  289. }
  290. else
  291. {
  292. throw new NotSupportedException("Not supported in the lite runtime.");
  293. }
  294. }
  295. public TBuilder AddRepeatedField(IFieldDescriptorLite field, object value)
  296. {
  297. if (field.IsExtension)
  298. {
  299. ExtendableMessageLite<TMessage, TBuilder> message = MessageBeingBuilt;
  300. message.Extensions.AddRepeatedField(field, value);
  301. return ThisBuilder;
  302. }
  303. else
  304. {
  305. throw new NotSupportedException("Not supported in the lite runtime.");
  306. }
  307. }
  308. protected void MergeExtensionFields(ExtendableMessageLite<TMessage, TBuilder> other)
  309. {
  310. MessageBeingBuilt.Extensions.MergeFrom(other.Extensions);
  311. }
  312. #endregion
  313. }
  314. }