GeneratedBuilder.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. using System;
  17. using System.Collections;
  18. using System.Collections.Generic;
  19. using Google.ProtocolBuffers.Descriptors;
  20. using Google.ProtocolBuffers.FieldAccess;
  21. namespace Google.ProtocolBuffers {
  22. /// <summary>
  23. /// All generated protocol message builder classes extend this class. It implements
  24. /// most of the IBuilder interface using reflection. Users can ignore this class
  25. /// as an implementation detail.
  26. /// </summary>
  27. public abstract class GeneratedBuilder<TMessage, TBuilder> : AbstractBuilder<TMessage, TBuilder>
  28. where TMessage : GeneratedMessage <TMessage, TBuilder>
  29. where TBuilder : GeneratedBuilder<TMessage, TBuilder> {
  30. /// <summary>
  31. /// Returns the message being built at the moment.
  32. /// </summary>
  33. protected abstract TMessage MessageBeingBuilt { get; }
  34. protected internal FieldAccessorTable InternalFieldAccessors {
  35. get { return MessageBeingBuilt.FieldAccesseorsFromBuilder; }
  36. }
  37. public override bool IsInitialized {
  38. get { return MessageBeingBuilt.IsInitialized; }
  39. }
  40. public override IDictionary<FieldDescriptor, object> AllFields {
  41. get { return MessageBeingBuilt.AllFields; }
  42. }
  43. public override object this[FieldDescriptor field] {
  44. get {
  45. // For repeated fields, the underlying list object is still modifiable at this point.
  46. // Make sure not to expose the modifiable list to the caller.
  47. return field.IsRepeated
  48. ? InternalFieldAccessors[field].GetRepeatedWrapper(this)
  49. : MessageBeingBuilt[field];
  50. }
  51. set {
  52. InternalFieldAccessors[field].SetValue(ThisBuilder, value);
  53. }
  54. }
  55. /// <summary>
  56. /// Adds all of the specified values to the given collection.
  57. /// </summary>
  58. protected void AddRange<T>(IEnumerable<T> source, IList<T> destination) {
  59. List<T> list = destination as List<T>;
  60. if (list != null) {
  61. list.AddRange(source);
  62. } else {
  63. foreach (T element in source) {
  64. destination.Add(element);
  65. }
  66. }
  67. }
  68. /// <summary>
  69. /// Called by derived classes to parse an unknown field.
  70. /// </summary>
  71. /// <returns>true unless the tag is an end-group tag</returns>
  72. protected virtual bool ParseUnknownField(CodedInputStream input, UnknownFieldSet.Builder unknownFields,
  73. ExtensionRegistry extensionRegistry, uint tag) {
  74. return unknownFields.MergeFieldFrom(tag, input);
  75. }
  76. public override MessageDescriptor DescriptorForType {
  77. get { return MessageBeingBuilt.DescriptorForType; }
  78. }
  79. public override int GetRepeatedFieldCount(FieldDescriptor field) {
  80. return MessageBeingBuilt.GetRepeatedFieldCount(field);
  81. }
  82. public override object this[FieldDescriptor field, int index] {
  83. get { return MessageBeingBuilt[field, index]; }
  84. set { InternalFieldAccessors[field].SetRepeated(this, index, value); }
  85. }
  86. public override bool HasField(FieldDescriptor field) {
  87. return MessageBeingBuilt.HasField(field);
  88. }
  89. public override IBuilder CreateBuilderForField(FieldDescriptor field) {
  90. return InternalFieldAccessors[field].CreateBuilder();
  91. }
  92. public override TBuilder ClearField(FieldDescriptor field) {
  93. InternalFieldAccessors[field].Clear(this);
  94. return ThisBuilder;
  95. }
  96. public override TBuilder MergeFrom(TMessage other) {
  97. if (other.DescriptorForType != InternalFieldAccessors.Descriptor) {
  98. throw new ArgumentException("Message type mismatch");
  99. }
  100. foreach (KeyValuePair<FieldDescriptor, object> entry in 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 && HasField(field)) {
  108. // Merge singular embedded messages
  109. IMessage oldValue = (IMessage)this[field];
  110. this[field] = oldValue.WeakCreateBuilderForType()
  111. .WeakMergeFrom(oldValue)
  112. .WeakMergeFrom((IMessage)entry.Value)
  113. .WeakBuildPartial();
  114. } else {
  115. // Just overwrite
  116. this[field] = entry.Value;
  117. }
  118. }
  119. return ThisBuilder;
  120. }
  121. public override TBuilder MergeUnknownFields(UnknownFieldSet unknownFields) {
  122. TMessage result = MessageBeingBuilt;
  123. result.SetUnknownFields(UnknownFieldSet.CreateBuilder(result.UnknownFields)
  124. .MergeFrom(unknownFields)
  125. .Build());
  126. return ThisBuilder;
  127. }
  128. public override TBuilder AddRepeatedField(FieldDescriptor field, object value) {
  129. InternalFieldAccessors[field].AddRepeated(this, value);
  130. return ThisBuilder;
  131. }
  132. /// <summary>
  133. /// Like Build(), but will wrap UninitializedMessageException in
  134. /// InvalidProtocolBufferException.
  135. /// TODO(jonskeet): This used to be generated for each class. Find out why.
  136. /// </summary>
  137. public TMessage BuildParsed() {
  138. if (!IsInitialized) {
  139. throw new UninitializedMessageException(MessageBeingBuilt).AsInvalidProtocolBufferException();
  140. }
  141. return BuildPartial();
  142. }
  143. /// <summary>
  144. /// Implementation of <see cref="IBuilder{TMessage, TBuilder}.Build" />.
  145. /// TODO(jonskeet): This used to be generated for each class. Find out why.
  146. /// </summary>
  147. public override TMessage Build() {
  148. if (!IsInitialized) {
  149. throw new UninitializedMessageException(MessageBeingBuilt);
  150. }
  151. return BuildPartial();
  152. }
  153. public override UnknownFieldSet UnknownFields {
  154. get { return MessageBeingBuilt.UnknownFields; }
  155. set { MessageBeingBuilt.SetUnknownFields(value); }
  156. }
  157. }
  158. }