|
| 1 | +using System.Runtime.CompilerServices; |
| 2 | + |
| 3 | +namespace Phazor.Components.Tools; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// <see cref="WeakReference"/> wrapper that allows to retain value based on it's usage |
| 7 | +/// </summary> |
| 8 | +/// <typeparam name="T"> |
| 9 | +/// Type of the value |
| 10 | +/// </typeparam> |
| 11 | +internal class AdaptiveWeakReference<T> |
| 12 | + where T : class |
| 13 | +{ |
| 14 | + private const ushort DefaultWeaknessBreakpoint = 15; |
| 15 | + |
| 16 | + private readonly Func<T> _valueFactory; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// <see cref="_weaknessFactor"/> less than <see cref="_weaknessBreakpoint"/>: reference must be weak |
| 20 | + /// <see cref="_weaknessFactor"/> greater or equal to <see cref="_weaknessBreakpoint"/>: reference must be strong |
| 21 | + /// </summary> |
| 22 | + private readonly ushort _weaknessBreakpoint; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Container for weak reference. If value is initialized, always contains reference to it. When strong |
| 26 | + /// reference is also initialized, weak reference will retrain its value because GC would not collect target |
| 27 | + /// object. |
| 28 | + /// </summary> |
| 29 | + private readonly WeakReference<T?> _reference; |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Strong reference to value. Initialized when <see cref="_weaknessFactor"/> greater or equal |
| 33 | + /// to <see cref="_weaknessBreakpoint"/> |
| 34 | + /// </summary> |
| 35 | + private T? _value; |
| 36 | + |
| 37 | + private ushort _weaknessFactor; |
| 38 | + private ushort _strengthenStreak; |
| 39 | + private ushort _weakenStreak; |
| 40 | + |
| 41 | + public AdaptiveWeakReference(Func<T> valueFactory, ushort weaknessBreakpoint = DefaultWeaknessBreakpoint) |
| 42 | + { |
| 43 | + _valueFactory = valueFactory; |
| 44 | + _weaknessBreakpoint = weaknessBreakpoint; |
| 45 | + |
| 46 | + _value = null; |
| 47 | + _reference = new WeakReference<T?>(null); |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Gets or sets wrapped value. When getting an uninitialized value, it will be created with provided value |
| 52 | + /// factory. When value is initialized in getter or set via setter it will either be weak or strong reference |
| 53 | + /// based on current <see cref="_weaknessFactor"/>. |
| 54 | + /// </summary> |
| 55 | + public T Value |
| 56 | + { |
| 57 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 58 | + get => GetValue(); |
| 59 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 60 | + set => SetValue(value); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Checks whether value is initialized either with weak or strong reference. |
| 65 | + /// </summary> |
| 66 | + public bool HasValue |
| 67 | + { |
| 68 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 69 | + get => _value is not null || _reference.TryGetTarget(out _); |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Removes both weak and strong reference |
| 74 | + /// </summary> |
| 75 | + public void Clear() |
| 76 | + { |
| 77 | + _value = null; |
| 78 | + _reference.SetTarget(null); |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Increases the <see cref="_weaknessFactor"/>, changing weak to strong reference if required |
| 83 | + /// </summary> |
| 84 | + public void Strengthen() |
| 85 | + { |
| 86 | + if (_weaknessFactor is ushort.MaxValue) |
| 87 | + return; |
| 88 | + |
| 89 | + if (_strengthenStreak is not ushort.MaxValue) |
| 90 | + _strengthenStreak++; |
| 91 | + |
| 92 | + _weakenStreak = 0; |
| 93 | + |
| 94 | + _weaknessFactor = ushort.MaxValue - _weaknessFactor < _strengthenStreak |
| 95 | + ? ushort.MaxValue |
| 96 | + : (ushort)(_weaknessFactor + _strengthenStreak); |
| 97 | + |
| 98 | + if (_weaknessFactor >= _weaknessBreakpoint) |
| 99 | + ConvertToStrongReference(); |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Decreases the <see cref="_weaknessFactor"/>, changing strong to weak reference if required |
| 104 | + /// </summary> |
| 105 | + public void Weaken() |
| 106 | + { |
| 107 | + if (_weaknessFactor is 0) |
| 108 | + return; |
| 109 | + |
| 110 | + if (_weakenStreak is not ushort.MaxValue) |
| 111 | + _weakenStreak++; |
| 112 | + |
| 113 | + _strengthenStreak = 0; |
| 114 | + |
| 115 | + _weaknessFactor = _weaknessFactor < _weakenStreak |
| 116 | + ? ushort.MinValue |
| 117 | + : (ushort)(_weaknessFactor - _weakenStreak); |
| 118 | + |
| 119 | + if (_weaknessFactor < _weaknessBreakpoint) |
| 120 | + ConvertToWeakReference(); |
| 121 | + } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// If current value is initialized – returns it, otherwise – creates and sets the value, and then returns it |
| 125 | + /// </summary> |
| 126 | + private T GetValue() |
| 127 | + { |
| 128 | + if (_value is not null) |
| 129 | + return _value; |
| 130 | + |
| 131 | + if (_reference.TryGetTarget(out T? value)) |
| 132 | + return value; |
| 133 | + |
| 134 | + value = _valueFactory.Invoke(); |
| 135 | + SetValue(value); |
| 136 | + |
| 137 | + return value; |
| 138 | + } |
| 139 | + |
| 140 | + private void SetValue(T value) |
| 141 | + { |
| 142 | + _value = _weaknessFactor < _weaknessBreakpoint ? null : value; |
| 143 | + _reference.SetTarget(value); |
| 144 | + } |
| 145 | + |
| 146 | + private void ConvertToWeakReference() |
| 147 | + { |
| 148 | + _reference.SetTarget(_value); |
| 149 | + _value = null; |
| 150 | + } |
| 151 | + |
| 152 | + private void ConvertToStrongReference() |
| 153 | + { |
| 154 | + _reference.TryGetTarget(out _value); |
| 155 | + } |
| 156 | +} |
0 commit comments