ExtendableMessage.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 to check if all extensions are initialized.
  63. /// </summary>
  64. protected bool ExtensionsAreInitialized {
  65. get { return extensions.IsInitialized; }
  66. }
  67. public override bool IsInitialized {
  68. get {
  69. return base.IsInitialized && ExtensionsAreInitialized;
  70. }
  71. }
  72. #region Reflection
  73. public override IDictionary<FieldDescriptor, object> AllFields {
  74. get {
  75. IDictionary<FieldDescriptor, object> result = GetMutableFieldMap();
  76. foreach(KeyValuePair<FieldDescriptor, object> entry in extensions.AllFields) {
  77. result[entry.Key] = entry.Value;
  78. }
  79. return Dictionaries.AsReadOnly(result);
  80. }
  81. }
  82. public override bool HasField(FieldDescriptor field) {
  83. if (field.IsExtension) {
  84. VerifyContainingType(field);
  85. return extensions.HasField(field);
  86. } else {
  87. return base.HasField(field);
  88. }
  89. }
  90. public override object this[FieldDescriptor field] {
  91. get {
  92. if (field.IsExtension) {
  93. VerifyContainingType(field);
  94. object value = extensions[field];
  95. if (value == null) {
  96. // Lacking an ExtensionRegistry, we have no way to determine the
  97. // extension's real type, so we return a DynamicMessage.
  98. // TODO(jonskeet): Work out what this means
  99. return DynamicMessage.GetDefaultInstance(field.MessageType);
  100. } else {
  101. return value;
  102. }
  103. } else {
  104. return base[field];
  105. }
  106. }
  107. }
  108. public override int GetRepeatedFieldCount(FieldDescriptor field) {
  109. if (field.IsExtension) {
  110. VerifyContainingType(field);
  111. return extensions.GetRepeatedFieldCount(field);
  112. } else {
  113. return base.GetRepeatedFieldCount(field);
  114. }
  115. }
  116. public override object this[FieldDescriptor field, int index] {
  117. get {
  118. if (field.IsExtension) {
  119. VerifyContainingType(field);
  120. return extensions[field, index];
  121. } else {
  122. return base[field, index];
  123. }
  124. }
  125. }
  126. internal void VerifyContainingType(FieldDescriptor field) {
  127. if (field.ContainingType != DescriptorForType) {
  128. throw new ArgumentException("FieldDescriptor does not match message type.");
  129. }
  130. }
  131. #endregion
  132. /// <summary>
  133. /// Used by subclasses to serialize extensions. Extension ranges may be
  134. /// interleaves with field numbers, but we must write them in canonical
  135. /// (sorted by field number) order. This class helps us to write individual
  136. /// ranges of extensions at once.
  137. ///
  138. /// TODO(jonskeet): See if we can improve this in terms of readability.
  139. /// </summary>
  140. protected class ExtensionWriter {
  141. readonly IEnumerator<KeyValuePair<FieldDescriptor, object>> iterator;
  142. readonly FieldSet extensions;
  143. KeyValuePair<FieldDescriptor, object>? next = null;
  144. internal ExtensionWriter(ExtendableMessage<TMessage, TBuilder> message) {
  145. extensions = message.extensions;
  146. iterator = message.extensions.GetEnumerator();
  147. if (iterator.MoveNext()) {
  148. next = iterator.Current;
  149. }
  150. }
  151. public void WriteUntil(int end, CodedOutputStream output) {
  152. while (next != null && next.Value.Key.FieldNumber < end) {
  153. extensions.WriteField(next.Value.Key, next.Value.Value, output);
  154. if (iterator.MoveNext()) {
  155. next = iterator.Current;
  156. } else {
  157. next = null;
  158. }
  159. }
  160. }
  161. }
  162. protected ExtensionWriter CreateExtensionWriter(ExtendableMessage<TMessage, TBuilder> message) {
  163. return new ExtensionWriter(message);
  164. }
  165. /// <summary>
  166. /// Called by subclasses to compute the size of extensions.
  167. /// </summary>
  168. protected int ExtensionsSerializedSize {
  169. get { return extensions.SerializedSize; }
  170. }
  171. internal void VerifyExtensionContainingType<TExtension>(GeneratedExtensionBase<TExtension> extension) {
  172. if (extension.Descriptor.ContainingType != DescriptorForType) {
  173. // This can only happen if someone uses unchecked operations.
  174. throw new ArgumentException("Extension is for type \"" + extension.Descriptor.ContainingType.FullName
  175. + "\" which does not match message type \"" + DescriptorForType.FullName + "\".");
  176. }
  177. }
  178. }
  179. }