ExtensionRegistry.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using System;
  33. using System.Collections;
  34. using System.Collections.Generic;
  35. using System.Linq;
  36. namespace Google.Protobuf
  37. {
  38. /// <summary>
  39. /// Provides extensions to messages while parsing. This API is experimental and subject to change.
  40. /// </summary>
  41. public sealed class ExtensionRegistry : ICollection<Extension>, IDeepCloneable<ExtensionRegistry>
  42. {
  43. internal sealed class ExtensionComparer : IEqualityComparer<Extension>
  44. {
  45. public bool Equals(Extension a, Extension b)
  46. {
  47. return new ObjectIntPair<Type>(a.TargetType, a.FieldNumber).Equals(new ObjectIntPair<Type>(b.TargetType, b.FieldNumber));
  48. }
  49. public int GetHashCode(Extension a)
  50. {
  51. return new ObjectIntPair<Type>(a.TargetType, a.FieldNumber).GetHashCode();
  52. }
  53. internal static ExtensionComparer Instance = new ExtensionComparer();
  54. }
  55. private IDictionary<ObjectIntPair<Type>, Extension> extensions;
  56. /// <summary>
  57. /// Creates a new empty extension registry
  58. /// </summary>
  59. public ExtensionRegistry()
  60. {
  61. extensions = new Dictionary<ObjectIntPair<Type>, Extension>();
  62. }
  63. private ExtensionRegistry(IDictionary<ObjectIntPair<Type>, Extension> collection)
  64. {
  65. extensions = collection.ToDictionary(k => k.Key, v => v.Value);
  66. }
  67. /// <summary>
  68. /// Gets the total number of extensions in this extension registry
  69. /// </summary>
  70. public int Count => extensions.Count;
  71. /// <summary>
  72. /// Returns whether the registry is readonly
  73. /// </summary>
  74. bool ICollection<Extension>.IsReadOnly => false;
  75. internal bool ContainsInputField(CodedInputStream stream, Type target, out Extension extension)
  76. {
  77. return extensions.TryGetValue(new ObjectIntPair<Type>(target, WireFormat.GetTagFieldNumber(stream.LastTag)), out extension);
  78. }
  79. /// <summary>
  80. /// Adds the specified extension to the registry
  81. /// </summary>
  82. public void Add(Extension extension)
  83. {
  84. ProtoPreconditions.CheckNotNull(extension, nameof(extension));
  85. extensions.Add(new ObjectIntPair<Type>(extension.TargetType, extension.FieldNumber), extension);
  86. }
  87. /// <summary>
  88. /// Adds the specified extensions to the reigstry
  89. /// </summary>
  90. public void AddRange(IEnumerable<Extension> extensions)
  91. {
  92. ProtoPreconditions.CheckNotNull(extensions, nameof(extensions));
  93. foreach (var extension in extensions)
  94. {
  95. Add(extension);
  96. }
  97. }
  98. /// <summary>
  99. /// Clears the registry of all values
  100. /// </summary>
  101. public void Clear()
  102. {
  103. extensions.Clear();
  104. }
  105. /// <summary>
  106. /// Gets whether the extension registry contains the specified extension
  107. /// </summary>
  108. public bool Contains(Extension item)
  109. {
  110. ProtoPreconditions.CheckNotNull(item, nameof(item));
  111. return extensions.ContainsKey(new ObjectIntPair<Type>(item.TargetType, item.FieldNumber));
  112. }
  113. /// <summary>
  114. /// Copies the arrays in the registry set to the specified array at the specified index
  115. /// </summary>
  116. /// <param name="array">The array to copy to</param>
  117. /// <param name="arrayIndex">The array index to start at</param>
  118. void ICollection<Extension>.CopyTo(Extension[] array, int arrayIndex)
  119. {
  120. ProtoPreconditions.CheckNotNull(array, nameof(array));
  121. if (arrayIndex < 0 || arrayIndex >= array.Length)
  122. {
  123. throw new ArgumentOutOfRangeException(nameof(arrayIndex));
  124. }
  125. if (array.Length - arrayIndex < Count)
  126. {
  127. throw new ArgumentException("The provided array is shorter than the number of elements in the registry");
  128. }
  129. for (int i = 0; i < array.Length; i++)
  130. {
  131. Extension extension = array[i];
  132. extensions.Add(new ObjectIntPair<Type>(extension.TargetType, extension.FieldNumber), extension);
  133. }
  134. }
  135. /// <summary>
  136. /// Returns an enumerator to enumerate through the items in the registry
  137. /// </summary>
  138. /// <returns>Returns an enumerator for the extensions in this registry</returns>
  139. public IEnumerator<Extension> GetEnumerator()
  140. {
  141. return extensions.Values.GetEnumerator();
  142. }
  143. /// <summary>
  144. /// Removes the specified extension from the set
  145. /// </summary>
  146. /// <param name="item">The extension</param>
  147. /// <returns><c>true</c> if the extension was removed, otherwise <c>false</c></returns>
  148. public bool Remove(Extension item)
  149. {
  150. ProtoPreconditions.CheckNotNull(item, nameof(item));
  151. return extensions.Remove(new ObjectIntPair<Type>(item.TargetType, item.FieldNumber));
  152. }
  153. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  154. /// <summary>
  155. /// Clones the registry into a new registry
  156. /// </summary>
  157. public ExtensionRegistry Clone()
  158. {
  159. return new ExtensionRegistry(extensions);
  160. }
  161. }
  162. }