|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections; |
| 5 | +using System.Collections.Concurrent; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Diagnostics.CodeAnalysis; |
| 8 | +using System.Reflection; |
| 9 | +using System.Runtime.CompilerServices; |
| 10 | + |
| 11 | +namespace System.ComponentModel |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Concurrent dictionary that maps MemberInfo object key to an object. |
| 15 | + /// Uses ConditionalWeakTable for the collectible keys (if MemberInfo.IsCollectible is true) and |
| 16 | + /// ConcurrentDictionary for non-collectible keys. |
| 17 | + /// </summary> |
| 18 | + internal sealed class ContextAwareConcurrentDictionary<TKey, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TValue> : IEnumerable<KeyValuePair<TKey, TValue>> |
| 19 | + where TKey : MemberInfo |
| 20 | + where TValue : class? |
| 21 | + { |
| 22 | + private readonly ConcurrentDictionary<TKey, TValue> _defaultTable = new ConcurrentDictionary<TKey, TValue>(); |
| 23 | + private readonly ConditionalWeakTable<TKey, TValue> _collectibleTable = new ConditionalWeakTable<TKey, TValue>(); |
| 24 | + |
| 25 | + public TValue? this[TKey key] |
| 26 | + { |
| 27 | + get |
| 28 | + { |
| 29 | + return TryGetValue(key, out TValue? value) ? value : default; |
| 30 | + } |
| 31 | + |
| 32 | + set |
| 33 | + { |
| 34 | + if (!key.IsCollectible) |
| 35 | + { |
| 36 | + _defaultTable[key] = value!; |
| 37 | + } |
| 38 | + else |
| 39 | + { |
| 40 | + _collectibleTable.AddOrUpdate(key, value!); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public bool Contains(TKey key) |
| 46 | + { |
| 47 | + return !key.IsCollectible ? _defaultTable.ContainsKey(key) : _collectibleTable.TryGetValue(key, out _); |
| 48 | + } |
| 49 | + |
| 50 | + public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) |
| 51 | + { |
| 52 | + return !key.IsCollectible ? _defaultTable.TryGetValue(key, out value) : _collectibleTable.TryGetValue(key, out value); |
| 53 | + } |
| 54 | + |
| 55 | + public void TryAdd(TKey key, TValue value) |
| 56 | + { |
| 57 | + if (!key.IsCollectible) |
| 58 | + { |
| 59 | + _defaultTable.TryAdd(key, value); |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + _collectibleTable.TryAdd(key, value); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public void Clear() |
| 68 | + { |
| 69 | + _defaultTable.Clear(); |
| 70 | + _collectibleTable.Clear(); |
| 71 | + } |
| 72 | + |
| 73 | + public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => new Enumerator(_defaultTable.GetEnumerator(), ((IEnumerable<KeyValuePair<TKey, TValue>>)_collectibleTable).GetEnumerator()); |
| 74 | + |
| 75 | + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// A wrapper around the base HashTable enumerator that skips |
| 79 | + /// collected keys and captures the key value to prevent it |
| 80 | + /// from being collected during enumeration. |
| 81 | + /// </summary> |
| 82 | + private sealed class Enumerator : IEnumerator<KeyValuePair<TKey, TValue>> |
| 83 | + { |
| 84 | + private readonly IEnumerator<KeyValuePair<TKey, TValue>> _defaultEnumerator; |
| 85 | + private readonly IEnumerator<KeyValuePair<TKey, TValue>> _collectibleEnumerator; |
| 86 | + private bool _enumeratingCollectibleEnumerator; |
| 87 | + |
| 88 | + // Captured key of the WeakHashtable so that it is not collected during enumeration |
| 89 | + private KeyValuePair<TKey, TValue> _current; |
| 90 | + |
| 91 | + public Enumerator(IEnumerator<KeyValuePair<TKey, TValue>> defaultEnumerator, IEnumerator<KeyValuePair<TKey, TValue>> collectibleEnumerator) |
| 92 | + { |
| 93 | + _defaultEnumerator = defaultEnumerator; |
| 94 | + _collectibleEnumerator = collectibleEnumerator; |
| 95 | + _enumeratingCollectibleEnumerator = false; |
| 96 | + } |
| 97 | + |
| 98 | + ~Enumerator() |
| 99 | + { |
| 100 | + Dispose(); |
| 101 | + } |
| 102 | + |
| 103 | + public object Current => ((IEnumerator<KeyValuePair<TKey, TValue>>)this).Current; |
| 104 | + |
| 105 | + KeyValuePair<TKey, TValue> IEnumerator<KeyValuePair<TKey, TValue>>.Current |
| 106 | + { |
| 107 | + get |
| 108 | + { |
| 109 | + // Call the default enumerator to ensure we are in a correct state |
| 110 | + // and use the cached current value for the collectible part of the enumeration. |
| 111 | + return !_enumeratingCollectibleEnumerator ? _defaultEnumerator.Current : _current; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public void Dispose() |
| 116 | + { |
| 117 | + _defaultEnumerator.Dispose(); |
| 118 | + _collectibleEnumerator.Dispose(); |
| 119 | + |
| 120 | + GC.SuppressFinalize(this); |
| 121 | + } |
| 122 | + |
| 123 | + public bool MoveNext() |
| 124 | + { |
| 125 | + if (!_enumeratingCollectibleEnumerator) |
| 126 | + { |
| 127 | + // Try to get the next item from the default enumerator. |
| 128 | + if (_defaultEnumerator.MoveNext()) |
| 129 | + { |
| 130 | + return true; |
| 131 | + } |
| 132 | + // If we are done with the default enumerator, switch to the collectible enumerator. |
| 133 | + _enumeratingCollectibleEnumerator = true; |
| 134 | + } |
| 135 | + |
| 136 | + if (_collectibleEnumerator.MoveNext()) |
| 137 | + { |
| 138 | + _current = _collectibleEnumerator.Current; |
| 139 | + return true; |
| 140 | + } |
| 141 | + |
| 142 | + // Reset the captured current value to allow the object collection. |
| 143 | + _current = default; |
| 144 | + return false; |
| 145 | + } |
| 146 | + |
| 147 | + public void Reset() |
| 148 | + { |
| 149 | + _defaultEnumerator.Reset(); |
| 150 | + _collectibleEnumerator.Reset(); |
| 151 | + _current = default; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments