ExtendableMessage.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.Generic;
  18. using Google.ProtocolBuffers.Collections;
  19. using Google.ProtocolBuffers.Descriptors;
  20. namespace Google.ProtocolBuffers {
  21. public abstract class ExtendableMessage<TMessage, TBuilder> : GeneratedMessage<TMessage, TBuilder>
  22. where TMessage : GeneratedMessage<TMessage, TBuilder>
  23. where TBuilder : GeneratedBuilder<TMessage, TBuilder> {
  24. protected ExtendableMessage() {}
  25. private readonly FieldSet extensions = FieldSet.CreateInstance();
  26. /// <summary>
  27. /// Access for the builder.
  28. /// </summary>
  29. internal FieldSet Extensions {
  30. get { return extensions; }
  31. }
  32. /// <summary>
  33. /// Checks if a singular extension is present.
  34. /// </summary>
  35. public bool HasExtension<TExtension>(GeneratedExtensionBase<TExtension> extension) {
  36. return extensions.HasField(extension.Descriptor);
  37. }
  38. /// <summary>
  39. /// Returns the number of elements in a repeated extension.
  40. /// </summary>
  41. public int GetExtensionCount<TExtension>(GeneratedExtensionBase<IList<TExtension>> extension) {
  42. return extensions.GetRepeatedFieldCount(extension.Descriptor);
  43. }
  44. /// <summary>
  45. /// Returns the value of an extension.
  46. /// </summary>
  47. public TExtension GetExtension<TExtension>(GeneratedExtensionBase<TExtension> extension) {
  48. object value = extensions[extension.Descriptor];
  49. if (value == null) {
  50. return (TExtension) extension.MessageDefaultInstance;
  51. } else {
  52. return (TExtension) extension.FromReflectionType(value);
  53. }
  54. }
  55. /// <summary>
  56. /// Returns one element of a repeated extension.
  57. /// </summary>
  58. public TExtension GetExtension<TExtension>(GeneratedExtensionBase<IList<TExtension>> extension, int index) {
  59. return (TExtension) extension.SingularFromReflectionType(extensions[extension.Descriptor, index]);
  60. }
  61. /// <summary>
  62. /// Called by subclasses to check if all extensions are initialized.
  63. /// </summary>
  64. protected bool ExtensionsAreInitialized {
  65. get { return extensions.IsInitialized; }
  66. }
  67. #region Reflection
  68. public override IDictionary<FieldDescriptor, object> AllFields {
  69. get {
  70. IDictionary<FieldDescriptor, object> result = GetMutableFieldMap();
  71. foreach(KeyValuePair<FieldDescriptor, object> entry in extensions.AllFields) {
  72. result[entry.Key] = entry.Value;
  73. }
  74. return Dictionaries.AsReadOnly(result);
  75. }
  76. }
  77. public override bool HasField(FieldDescriptor field) {
  78. if (field.IsExtension) {
  79. VerifyContainingType(field);
  80. return extensions.HasField(field);
  81. } else {
  82. return base.HasField(field);
  83. }
  84. }
  85. public override object this[FieldDescriptor field] {
  86. get {
  87. if (field.IsExtension) {
  88. VerifyContainingType(field);
  89. object value = extensions[field];
  90. if (value == null) {
  91. // Lacking an ExtensionRegistry, we have no way to determine the
  92. // extension's real type, so we return a DynamicMessage.
  93. // TODO(jonskeet): Work out what this means
  94. return DynamicMessage.GetDefaultInstance(field.MessageType);
  95. } else {
  96. return value;
  97. }
  98. } else {
  99. return base[field];
  100. }
  101. }
  102. }
  103. public override int GetRepeatedFieldCount(FieldDescriptor field) {
  104. if (field.IsExtension) {
  105. VerifyContainingType(field);
  106. return extensions.GetRepeatedFieldCount(field);
  107. } else {
  108. return base.GetRepeatedFieldCount(field);
  109. }
  110. }
  111. public override object this[FieldDescriptor field, int index] {
  112. get {
  113. if (field.IsExtension) {
  114. VerifyContainingType(field);
  115. return extensions[field, index];
  116. } else {
  117. return base[field, index];
  118. }
  119. }
  120. }
  121. internal void VerifyContainingType(FieldDescriptor field) {
  122. if (field.ContainingType != DescriptorForType) {
  123. throw new ArgumentException("FieldDescriptor does not match message type.");
  124. }
  125. }
  126. #endregion
  127. /// <summary>
  128. /// Used by subclasses to serialize extensions. Extension ranges may be
  129. /// interleaves with field numbers, but we must write them in canonical
  130. /// (sorted by field number) order. This class helps us to write individual
  131. /// ranges of extensions at once.
  132. ///
  133. /// TODO(jonskeet): See if we can improve this in terms of readability.
  134. /// </summary>
  135. protected class ExtensionWriter {
  136. readonly IEnumerator<KeyValuePair<FieldDescriptor, object>> iterator;
  137. readonly FieldSet extensions;
  138. KeyValuePair<FieldDescriptor, object>? next = null;
  139. internal ExtensionWriter(ExtendableMessage<TMessage, TBuilder> message) {
  140. extensions = message.extensions;
  141. iterator = message.extensions.GetEnumerator();
  142. if (iterator.MoveNext()) {
  143. next = iterator.Current;
  144. }
  145. }
  146. public void WriteUntil(int end, CodedOutputStream output) {
  147. while (next != null && next.Value.Key.FieldNumber < end) {
  148. extensions.WriteField(next.Value.Key, next.Value.Value, output);
  149. if (iterator.MoveNext()) {
  150. next = iterator.Current;
  151. } else {
  152. next = null;
  153. }
  154. }
  155. }
  156. }
  157. protected ExtensionWriter CreateExtensionWriter(ExtendableMessage<TMessage, TBuilder> message) {
  158. return new ExtensionWriter(message);
  159. }
  160. /// <summary>
  161. /// Called by subclasses to compute the size of extensions.
  162. /// </summary>
  163. protected int ExtensionsSerializedSize {
  164. get { return extensions.SerializedSize; }
  165. }
  166. internal void VerifyExtensionContainingType<TExtension>(GeneratedExtensionBase<TExtension> extension) {
  167. if (extension.Descriptor.ContainingType != DescriptorForType) {
  168. // This can only happen if someone uses unchecked operations.
  169. throw new ArgumentException("Extension is for type \"" + extension.Descriptor.ContainingType.FullName
  170. + "\" which does not match message type \"" + DescriptorForType.FullName + "\".");
  171. }
  172. }
  173. }
  174. }