ExtensionRegistry.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. private IDictionary<ObjectIntPair<Type>, Extension> extensions;
  44. /// <summary>
  45. /// Creates a new empty extension registry
  46. /// </summary>
  47. public ExtensionRegistry()
  48. {
  49. extensions = new Dictionary<ObjectIntPair<Type>, Extension>();
  50. }
  51. private ExtensionRegistry(IDictionary<ObjectIntPair<Type>, Extension> collection)
  52. {
  53. extensions = collection.ToDictionary(k => k.Key, v => v.Value);
  54. }
  55. /// <summary>
  56. /// Gets the total number of extensions in this extension registry
  57. /// </summary>
  58. public int Count => extensions.Count;
  59. /// <summary>
  60. /// Returns whether the registry is readonly
  61. /// </summary>
  62. bool ICollection<Extension>.IsReadOnly => false;
  63. internal bool ContainsInputField(CodedInputStream stream, Type target, out Extension extension)
  64. {
  65. return extensions.TryGetValue(new ObjectIntPair<Type>(target, WireFormat.GetTagFieldNumber(stream.LastTag)), out extension);
  66. }
  67. /// <summary>
  68. /// Adds the specified extension to the registry
  69. /// </summary>
  70. public void Add(Extension extension)
  71. {
  72. ProtoPreconditions.CheckNotNull(extension, nameof(extension));
  73. extensions.Add(new ObjectIntPair<Type>(extension.TargetType, extension.FieldNumber), extension);
  74. }
  75. /// <summary>
  76. /// Adds the specified extensions to the reigstry
  77. /// </summary>
  78. public void AddRange(IEnumerable<Extension> extensions)
  79. {
  80. ProtoPreconditions.CheckNotNull(extensions, nameof(extensions));
  81. foreach (var extension in extensions)
  82. {
  83. Add(extension);
  84. }
  85. }
  86. /// <summary>
  87. /// Clears the registry of all values
  88. /// </summary>
  89. public void Clear()
  90. {
  91. extensions.Clear();
  92. }
  93. /// <summary>
  94. /// Gets whether the extension registry contains the specified extension
  95. /// </summary>
  96. public bool Contains(Extension item)
  97. {
  98. ProtoPreconditions.CheckNotNull(item, nameof(item));
  99. return extensions.ContainsKey(new ObjectIntPair<Type>(item.TargetType, item.FieldNumber));
  100. }
  101. /// <summary>
  102. /// Copies the arrays in the registry set to the specified array at the specified index
  103. /// </summary>
  104. /// <param name="array">The array to copy to</param>
  105. /// <param name="arrayIndex">The array index to start at</param>
  106. void ICollection<Extension>.CopyTo(Extension[] array, int arrayIndex)
  107. {
  108. ProtoPreconditions.CheckNotNull(array, nameof(array));
  109. if (arrayIndex < 0 || arrayIndex >= array.Length)
  110. {
  111. throw new ArgumentOutOfRangeException(nameof(arrayIndex));
  112. }
  113. if (array.Length - arrayIndex < Count)
  114. {
  115. throw new ArgumentException("The provided array is shorter than the number of elements in the registry");
  116. }
  117. for (int i = 0; i < array.Length; i++)
  118. {
  119. Extension extension = array[i];
  120. extensions.Add(new ObjectIntPair<Type>(extension.TargetType, extension.FieldNumber), extension);
  121. }
  122. }
  123. /// <summary>
  124. /// Returns an enumerator to enumerate through the items in the registry
  125. /// </summary>
  126. /// <returns>Returns an enumerator for the extensions in this registry</returns>
  127. public IEnumerator<Extension> GetEnumerator()
  128. {
  129. return extensions.Values.GetEnumerator();
  130. }
  131. /// <summary>
  132. /// Removes the specified extension from the set
  133. /// </summary>
  134. /// <param name="item">The extension</param>
  135. /// <returns><c>true</c> if the extension was removed, otherwise <c>false</c></returns>
  136. public bool Remove(Extension item)
  137. {
  138. ProtoPreconditions.CheckNotNull(item, nameof(item));
  139. return extensions.Remove(new ObjectIntPair<Type>(item.TargetType, item.FieldNumber));
  140. }
  141. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  142. /// <summary>
  143. /// Clones the registry into a new registry
  144. /// </summary>
  145. public ExtensionRegistry Clone()
  146. {
  147. return new ExtensionRegistry(extensions);
  148. }
  149. }
  150. }