AbstractBuilder.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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;
  36. using System.Collections.Generic;
  37. using System.IO;
  38. using Google.ProtocolBuffers.Descriptors;
  39. namespace Google.ProtocolBuffers {
  40. /// <summary>
  41. /// Implementation of the non-generic IMessage interface as far as possible.
  42. /// </summary>
  43. public abstract class AbstractBuilder<TMessage, TBuilder> : AbstractBuilderLite<TMessage, TBuilder>, IBuilder<TMessage, TBuilder>
  44. where TMessage : AbstractMessage<TMessage, TBuilder>
  45. where TBuilder : AbstractBuilder<TMessage, TBuilder> {
  46. #region Unimplemented members of IBuilder
  47. public abstract UnknownFieldSet UnknownFields { get; set; }
  48. public abstract IDictionary<FieldDescriptor, object> AllFields { get; }
  49. public abstract object this[FieldDescriptor field] { get; set; }
  50. public abstract MessageDescriptor DescriptorForType { get; }
  51. public abstract int GetRepeatedFieldCount(FieldDescriptor field);
  52. public abstract object this[FieldDescriptor field, int index] { get; set; }
  53. public abstract bool HasField(FieldDescriptor field);
  54. public abstract IBuilder CreateBuilderForField(FieldDescriptor field);
  55. public abstract TBuilder ClearField(FieldDescriptor field);
  56. public abstract TBuilder AddRepeatedField(FieldDescriptor field, object value);
  57. #endregion
  58. public TBuilder SetUnknownFields(UnknownFieldSet fields) {
  59. UnknownFields = fields;
  60. return ThisBuilder;
  61. }
  62. public override TBuilder Clear() {
  63. foreach(FieldDescriptor field in AllFields.Keys) {
  64. ClearField(field);
  65. }
  66. return ThisBuilder;
  67. }
  68. public sealed override TBuilder MergeFrom(IMessageLite other) {
  69. if (other is IMessage) {
  70. return MergeFrom((IMessage) other);
  71. }
  72. throw new ArgumentException("MergeFrom(Message) can only merge messages of the same type.");
  73. }
  74. /// <summary>
  75. /// Merge the specified other message into the message being
  76. /// built. Merging occurs as follows. For each field:
  77. /// For singular primitive fields, if the field is set in <paramref name="other"/>,
  78. /// then <paramref name="other"/>'s value overwrites the value in this message.
  79. /// For singular message fields, if the field is set in <paramref name="other"/>,
  80. /// it is merged into the corresponding sub-message of this message using the same
  81. /// merging rules.
  82. /// For repeated fields, the elements in <paramref name="other"/> are concatenated
  83. /// with the elements in this message.
  84. /// </summary>
  85. /// <param name="other"></param>
  86. /// <returns></returns>
  87. public abstract TBuilder MergeFrom(TMessage other);
  88. public virtual TBuilder MergeFrom(IMessage other) {
  89. if (other.DescriptorForType != DescriptorForType) {
  90. throw new ArgumentException("MergeFrom(IMessage) can only merge messages of the same type.");
  91. }
  92. // Note: We don't attempt to verify that other's fields have valid
  93. // types. Doing so would be a losing battle. We'd have to verify
  94. // all sub-messages as well, and we'd have to make copies of all of
  95. // them to insure that they don't change after verification (since
  96. // the Message interface itself cannot enforce immutability of
  97. // implementations).
  98. // TODO(jonskeet): Provide a function somewhere called MakeDeepCopy()
  99. // which allows people to make secure deep copies of messages.
  100. foreach (KeyValuePair<FieldDescriptor, object> entry in other.AllFields) {
  101. FieldDescriptor field = entry.Key;
  102. if (field.IsRepeated) {
  103. // Concatenate repeated fields
  104. foreach (object element in (IEnumerable) entry.Value) {
  105. AddRepeatedField(field, element);
  106. }
  107. } else if (field.MappedType == MappedType.Message) {
  108. // Merge singular messages
  109. IMessageLite existingValue = (IMessageLite)this[field];
  110. if (existingValue == existingValue.WeakDefaultInstanceForType) {
  111. this[field] = entry.Value;
  112. } else {
  113. this[field] = existingValue.WeakCreateBuilderForType()
  114. .WeakMergeFrom(existingValue)
  115. .WeakMergeFrom((IMessageLite)entry.Value)
  116. .WeakBuild();
  117. }
  118. } else {
  119. // Overwrite simple values
  120. this[field] = entry.Value;
  121. }
  122. }
  123. return ThisBuilder;
  124. }
  125. public override TBuilder MergeFrom(CodedInputStream input, ExtensionRegistry extensionRegistry) {
  126. UnknownFieldSet.Builder unknownFields = UnknownFieldSet.CreateBuilder(UnknownFields);
  127. unknownFields.MergeFrom(input, extensionRegistry, this);
  128. UnknownFields = unknownFields.Build();
  129. return ThisBuilder;
  130. }
  131. public virtual TBuilder MergeUnknownFields(UnknownFieldSet unknownFields) {
  132. UnknownFields = UnknownFieldSet.CreateBuilder(UnknownFields)
  133. .MergeFrom(unknownFields)
  134. .Build();
  135. return ThisBuilder;
  136. }
  137. public virtual IBuilder SetField(FieldDescriptor field, object value) {
  138. this[field] = value;
  139. return ThisBuilder;
  140. }
  141. public virtual IBuilder SetRepeatedField(FieldDescriptor field, int index, object value) {
  142. this[field, index] = value;
  143. return ThisBuilder;
  144. }
  145. #region Explicit Implementations
  146. IMessage IBuilder.WeakBuild() {
  147. return Build();
  148. }
  149. IBuilder IBuilder.WeakAddRepeatedField(FieldDescriptor field, object value) {
  150. return AddRepeatedField(field, value);
  151. }
  152. IBuilder IBuilder.WeakClear() {
  153. return Clear();
  154. }
  155. IBuilder IBuilder.WeakMergeFrom(IMessage message) {
  156. return MergeFrom(message);
  157. }
  158. IBuilder IBuilder.WeakMergeFrom(CodedInputStream input) {
  159. return MergeFrom(input);
  160. }
  161. IBuilder IBuilder.WeakMergeFrom(CodedInputStream input, ExtensionRegistry registry) {
  162. return MergeFrom(input, registry);
  163. }
  164. IBuilder IBuilder.WeakMergeFrom(ByteString data) {
  165. return MergeFrom(data);
  166. }
  167. IBuilder IBuilder.WeakMergeFrom(ByteString data, ExtensionRegistry registry) {
  168. return MergeFrom(data, registry);
  169. }
  170. IMessage IBuilder.WeakBuildPartial() {
  171. return BuildPartial();
  172. }
  173. IBuilder IBuilder.WeakClone() {
  174. return Clone();
  175. }
  176. IMessage IBuilder.WeakDefaultInstanceForType {
  177. get { return DefaultInstanceForType; }
  178. }
  179. IBuilder IBuilder.WeakClearField(FieldDescriptor field) {
  180. return ClearField(field);
  181. }
  182. #endregion
  183. }
  184. }