csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | // Protocol Buffers - Google's data interchange format |
| 3 | // Copyright 2008 Google Inc. All rights reserved. |
| 4 | // http://github.com/jskeet/dotnet-protobufs/ |
| 5 | // Original C++/Java/Python code: |
| 6 | // http://code.google.com/p/protobuf/ |
| 7 | // |
| 8 | // Redistribution and use in source and binary forms, with or without |
| 9 | // modification, are permitted provided that the following conditions are |
| 10 | // met: |
| 11 | // |
| 12 | // * Redistributions of source code must retain the above copyright |
| 13 | // notice, this list of conditions and the following disclaimer. |
| 14 | // * Redistributions in binary form must reproduce the above |
| 15 | // copyright notice, this list of conditions and the following disclaimer |
| 16 | // in the documentation and/or other materials provided with the |
| 17 | // distribution. |
| 18 | // * Neither the name of Google Inc. nor the names of its |
| 19 | // contributors may be used to endorse or promote products derived from |
| 20 | // this software without specific prior written permission. |
| 21 | // |
| 22 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | #endregion |
| 34 | |
| 35 | using System.Collections.Generic; |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 36 | using System; |
| 37 | |
| 38 | namespace Google.ProtocolBuffers { |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 39 | |
| 40 | |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 41 | /// <summary> |
| 42 | /// A table of known extensions, searchable by name or field number. When |
| 43 | /// parsing a protocol message that might have extensions, you must provide |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 44 | /// an <see cref="ExtensionRegistryLite"/> in which you have registered any extensions |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 45 | /// that you want to be able to parse. Otherwise, those extensions will just |
| 46 | /// be treated like unknown fields. |
| 47 | /// </summary> |
| 48 | /// <example> |
| 49 | /// For example, if you had the <c>.proto</c> file: |
| 50 | /// <code> |
| 51 | /// option java_class = "MyProto"; |
| 52 | /// |
| 53 | /// message Foo { |
| 54 | /// extensions 1000 to max; |
| 55 | /// } |
| 56 | /// |
| 57 | /// extend Foo { |
| 58 | /// optional int32 bar; |
| 59 | /// } |
| 60 | /// </code> |
| 61 | /// |
| 62 | /// Then you might write code like: |
| 63 | /// |
| 64 | /// <code> |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 65 | /// ExtensionRegistryLite registry = ExtensionRegistryLite.CreateInstance(); |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 66 | /// registry.Add(MyProto.Bar); |
| 67 | /// MyProto.Foo message = MyProto.Foo.ParseFrom(input, registry); |
| 68 | /// </code> |
| 69 | /// </example> |
| 70 | /// |
| 71 | /// <remarks> |
| 72 | /// <para>You might wonder why this is necessary. Two alternatives might come to |
| 73 | /// mind. First, you might imagine a system where generated extensions are |
| 74 | /// automatically registered when their containing classes are loaded. This |
| 75 | /// is a popular technique, but is bad design; among other things, it creates a |
| 76 | /// situation where behavior can change depending on what classes happen to be |
| 77 | /// loaded. It also introduces a security vulnerability, because an |
| 78 | /// unprivileged class could cause its code to be called unexpectedly from a |
| 79 | /// privileged class by registering itself as an extension of the right type. |
| 80 | /// </para> |
| 81 | /// <para>Another option you might consider is lazy parsing: do not parse an |
| 82 | /// extension until it is first requested, at which point the caller must |
| 83 | /// provide a type to use. This introduces a different set of problems. First, |
| 84 | /// it would require a mutex lock any time an extension was accessed, which |
| 85 | /// would be slow. Second, corrupt data would not be detected until first |
| 86 | /// access, at which point it would be much harder to deal with it. Third, it |
| 87 | /// could violate the expectation that message objects are immutable, since the |
| 88 | /// type provided could be any arbitrary message class. An unprivileged user |
| 89 | /// could take advantage of this to inject a mutable object into a message |
| 90 | /// belonging to privileged code and create mischief.</para> |
| 91 | /// </remarks> |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 92 | public class ExtensionRegistryLite { |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 93 | |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 94 | private static readonly ExtensionRegistryLite empty = new ExtensionRegistryLite( |
| 95 | new Dictionary<ExtensionIntPair, IGeneratedExtensionLite>(), |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 96 | true); |
| 97 | |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 98 | protected readonly IDictionary<ExtensionIntPair, IGeneratedExtensionLite> extensionsByNumber; |
| 99 | protected readonly bool readOnly; |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 100 | |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 101 | protected ExtensionRegistryLite(IDictionary<ExtensionIntPair, IGeneratedExtensionLite> extensionsByNumber, |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 102 | bool readOnly) { |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 103 | this.extensionsByNumber = extensionsByNumber; |
| 104 | this.readOnly = readOnly; |
| 105 | } |
csharptest | 6b812f8 | 2010-11-05 11:06:13 -0500 | [diff] [blame^] | 106 | #if LITE |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 107 | /// <summary> |
| 108 | /// Construct a new, empty instance. |
| 109 | /// </summary> |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 110 | public static ExtensionRegistryLite CreateInstance() { |
| 111 | return new ExtensionRegistryLite( |
| 112 | new Dictionary<ExtensionIntPair, IGeneratedExtensionLite>(), false); |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /// <summary> |
| 116 | /// Get the unmodifiable singleton empty instance. |
| 117 | /// </summary> |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 118 | public static ExtensionRegistryLite Empty { |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 119 | get { return empty; } |
| 120 | } |
csharptest | 6b812f8 | 2010-11-05 11:06:13 -0500 | [diff] [blame^] | 121 | #endif |
csharptest | dce2b9d | 2010-11-04 19:57:23 -0500 | [diff] [blame] | 122 | public ExtensionRegistryLite AsReadOnly() { |
| 123 | return MakeReadOnly(); |
| 124 | } |
| 125 | |
| 126 | protected virtual ExtensionRegistryLite MakeReadOnly() { |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 127 | return new ExtensionRegistryLite(extensionsByNumber, true); |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /// <summary> |
| 131 | /// Finds an extension by containing type and field number. |
| 132 | /// A null reference is returned if the extension can't be found. |
| 133 | /// </summary> |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 134 | public IGeneratedExtensionLite this[IMessageLite containingType, int fieldNumber] { |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 135 | get { |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 136 | IGeneratedExtensionLite ret; |
| 137 | extensionsByNumber.TryGetValue(new ExtensionIntPair(containingType, fieldNumber), out ret); |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 138 | return ret; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /// <summary> |
| 143 | /// Add an extension from a generated file to the registry. |
| 144 | /// </summary> |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 145 | public virtual void Add(IGeneratedExtensionLite extension) { |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 146 | if (readOnly) { |
| 147 | throw new InvalidOperationException("Cannot add entries to a read-only extension registry"); |
| 148 | } |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 149 | extensionsByNumber.Add( |
| 150 | new ExtensionIntPair(extension.ContainingType, extension.Number), |
| 151 | extension); |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /// <summary> |
| 155 | /// Nested type just used to represent a pair of MessageDescriptor and int, as |
| 156 | /// the key into the "by number" map. |
| 157 | /// </summary> |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 158 | protected struct ExtensionIntPair : IEquatable<ExtensionIntPair> { |
| 159 | readonly object msgType; |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 160 | readonly int number; |
| 161 | |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 162 | internal ExtensionIntPair(object msgType, int number) { |
| 163 | this.msgType = msgType; |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 164 | this.number = number; |
| 165 | } |
| 166 | |
| 167 | public override int GetHashCode() { |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 168 | return msgType.GetHashCode() * ((1 << 16) - 1) + number; |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | public override bool Equals(object obj) { |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 172 | if (!(obj is ExtensionIntPair)) { |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 173 | return false; |
| 174 | } |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 175 | return Equals((ExtensionIntPair)obj); |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 176 | } |
| 177 | |
csharptest | d9c59e6 | 2010-11-04 19:36:28 -0500 | [diff] [blame] | 178 | public bool Equals(ExtensionIntPair other) { |
| 179 | return msgType.Equals(other.msgType) && number == other.number; |
csharptest | c07571a | 2010-11-04 14:25:56 -0500 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } |
| 183 | } |