| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="WeakAction.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Reflection; |
| | | 9 | | |
| | | 10 | | namespace MyNet.Messaging; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Stores an <see cref="Action" /> without causing a hard reference |
| | | 14 | | /// to be created to the Action's owner. The owner can be garbage collected at any time. |
| | | 15 | | /// </summary> |
| | | 16 | | public class WeakAction |
| | | 17 | | { |
| | | 18 | | private Action? _staticAction; |
| | | 19 | | private WeakReference? _targetReference; |
| | | 20 | | private WeakReference? _actionTargetReference; |
| | | 21 | | private object? _liveReference; |
| | | 22 | | private MethodInfo? _method; |
| | | 23 | | |
| | | 24 | | public WeakAction(Action action, bool keepTargetAlive = false) |
| | 15 | 25 | | : this(action.Target, action, keepTargetAlive) |
| | | 26 | | { |
| | 15 | 27 | | } |
| | | 28 | | |
| | | 29 | | public WeakAction(object? target, Action action, bool keepTargetAlive = false) |
| | | 30 | | { |
| | 21 | 31 | | if (action.Method.IsStatic) |
| | | 32 | | { |
| | 6 | 33 | | _staticAction = action; |
| | 6 | 34 | | if (target != null) |
| | 0 | 35 | | _targetReference = new(target); |
| | 6 | 36 | | return; |
| | | 37 | | } |
| | | 38 | | |
| | 15 | 39 | | _method = action.Method; |
| | 15 | 40 | | _actionTargetReference = new(action.Target); |
| | 15 | 41 | | _liveReference = keepTargetAlive ? action.Target : null; |
| | 15 | 42 | | _targetReference = new(target); |
| | 15 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets a value indicating whether the WeakAction is static or not. |
| | | 47 | | /// </summary> |
| | 3 | 48 | | public bool IsStatic => _staticAction != null; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets the name of the method that this WeakAction represents. |
| | | 52 | | /// </summary> |
| | 3 | 53 | | public virtual string? MethodName => _staticAction?.Method.Name ?? _method?.Name; |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets a value indicating whether the Action's owner is still alive, or if it was collected |
| | | 57 | | /// by the Garbage Collector already. |
| | | 58 | | /// </summary> |
| | | 59 | | public virtual bool IsAlive |
| | | 60 | | { |
| | | 61 | | get |
| | | 62 | | { |
| | 12 | 63 | | if (_staticAction == null && _targetReference == null && _liveReference == null) |
| | 3 | 64 | | return false; |
| | | 65 | | |
| | 9 | 66 | | if (_staticAction != null) |
| | 0 | 67 | | return _targetReference?.IsAlive != false; |
| | | 68 | | |
| | | 69 | | // Non static action |
| | 9 | 70 | | return _liveReference != null || _targetReference is { IsAlive: true }; |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Gets the Action's owner. This object is stored as a WeakReference. |
| | | 76 | | /// </summary> |
| | 6 | 77 | | public object? Target => _targetReference?.Target; |
| | | 78 | | |
| | 3 | 79 | | protected object? ActionTarget => _liveReference ?? _actionTargetReference?.Target; |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Executes the action. This only happens if the action's owner is still alive. |
| | | 83 | | /// </summary> |
| | | 84 | | public void Execute() |
| | | 85 | | { |
| | 3 | 86 | | if (!IsAlive) |
| | 0 | 87 | | return; |
| | | 88 | | |
| | 3 | 89 | | if (_staticAction != null) |
| | | 90 | | { |
| | 0 | 91 | | _staticAction(); |
| | 0 | 92 | | return; |
| | | 93 | | } |
| | | 94 | | |
| | 3 | 95 | | var actionTarget = ActionTarget; |
| | 3 | 96 | | if (_method != null && actionTarget != null) |
| | | 97 | | { |
| | | 98 | | try |
| | | 99 | | { |
| | 3 | 100 | | _ = _method.Invoke(actionTarget, null); |
| | 3 | 101 | | } |
| | 0 | 102 | | catch |
| | | 103 | | { |
| | | 104 | | // Silently ignore invocation errors |
| | 0 | 105 | | } |
| | | 106 | | } |
| | 3 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Sets all references to null, marking this WeakAction for deletion. |
| | | 111 | | /// </summary> |
| | | 112 | | public void MarkForDeletion() |
| | | 113 | | { |
| | 3 | 114 | | _staticAction = null; |
| | 3 | 115 | | _targetReference = null; |
| | 3 | 116 | | _actionTargetReference = null; |
| | 3 | 117 | | _liveReference = null; |
| | 3 | 118 | | _method = null; |
| | 3 | 119 | | } |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Stores a generic Action<T> without causing a hard reference |
| | | 124 | | /// to be created to the Action's owner. The owner can be garbage collected at any time. |
| | | 125 | | /// </summary> |
| | | 126 | | /// <typeparam name="T">The type of the Action's parameter.</typeparam> |
| | | 127 | | public class WeakAction<T> : IExecuteWithObject |
| | | 128 | | { |
| | | 129 | | private Action<T?>? _staticAction; |
| | | 130 | | private WeakReference? _targetReference; |
| | | 131 | | private WeakReference? _actionTargetReference; |
| | | 132 | | private object? _liveReference; |
| | | 133 | | private MethodInfo? _method; |
| | | 134 | | |
| | | 135 | | public WeakAction(Action<T?> action, bool keepTargetAlive = false) |
| | | 136 | | : this(action.Target, action, keepTargetAlive) |
| | | 137 | | { |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | public WeakAction(object? target, Action<T?> action, bool keepTargetAlive = false) |
| | | 141 | | { |
| | | 142 | | if (action.Method.IsStatic) |
| | | 143 | | { |
| | | 144 | | _staticAction = action; |
| | | 145 | | if (target != null) |
| | | 146 | | _targetReference = new(target); |
| | | 147 | | return; |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | _method = action.Method; |
| | | 151 | | _actionTargetReference = new(action.Target); |
| | | 152 | | _liveReference = keepTargetAlive ? action.Target : null; |
| | | 153 | | _targetReference = new(target); |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | /// <summary> |
| | | 157 | | /// Gets the name of the method that this WeakAction represents. |
| | | 158 | | /// </summary> |
| | | 159 | | public string? MethodName => _staticAction?.Method.Name ?? _method?.Name; |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Gets a value indicating whether the Action's owner is still alive. |
| | | 163 | | /// </summary> |
| | | 164 | | public bool IsAlive => |
| | | 165 | | (_staticAction != null || _targetReference != null || _liveReference != null) && (_staticAction != null ? _targe |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Gets the Action's owner. This object is stored as a WeakReference. |
| | | 169 | | /// </summary> |
| | | 170 | | public object? Target => _targetReference?.Target; |
| | | 171 | | |
| | | 172 | | protected object? ActionTarget => _liveReference ?? _actionTargetReference?.Target; |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Executes the action. This only happens if the action's owner is still alive. |
| | | 176 | | /// The action's parameter is set to default(T). |
| | | 177 | | /// </summary> |
| | | 178 | | public void Execute() => Execute(default); |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Executes the action. This only happens if the action's owner is still alive. |
| | | 182 | | /// </summary> |
| | | 183 | | /// <param name="parameter">A parameter to be passed to the action.</param> |
| | | 184 | | public void Execute(T? parameter) |
| | | 185 | | { |
| | | 186 | | if (!IsAlive) |
| | | 187 | | return; |
| | | 188 | | |
| | | 189 | | if (_staticAction != null) |
| | | 190 | | { |
| | | 191 | | try |
| | | 192 | | { |
| | | 193 | | _staticAction(parameter); |
| | | 194 | | } |
| | | 195 | | catch |
| | | 196 | | { |
| | | 197 | | // Silently ignore invocation errors |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | return; |
| | | 201 | | } |
| | | 202 | | |
| | | 203 | | var actionTarget = ActionTarget; |
| | | 204 | | if (_method != null && actionTarget != null) |
| | | 205 | | { |
| | | 206 | | try |
| | | 207 | | { |
| | | 208 | | _ = _method.Invoke(actionTarget, [parameter]); |
| | | 209 | | } |
| | | 210 | | catch |
| | | 211 | | { |
| | | 212 | | // Silently ignore invocation errors |
| | | 213 | | } |
| | | 214 | | } |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | /// <summary> |
| | | 218 | | /// Executes the action with a parameter of type object. This parameter |
| | | 219 | | /// will be casted to T. This method implements <see cref="IExecuteWithObject.ExecuteWithObject" /> |
| | | 220 | | /// and can be useful if you store multiple WeakAction{T} instances but don't know in advance |
| | | 221 | | /// what type T represents. |
| | | 222 | | /// </summary> |
| | | 223 | | /// <param name="parameter">The parameter that will be passed to the action after being casted to T.</param> |
| | | 224 | | public void ExecuteWithObject(object? parameter) |
| | | 225 | | { |
| | | 226 | | var parameterCasted = (T?)parameter; |
| | | 227 | | Execute(parameterCasted); |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | /// <summary> |
| | | 231 | | /// Sets all references to null, marking this WeakAction for deletion. |
| | | 232 | | /// </summary> |
| | | 233 | | public void MarkForDeletion() |
| | | 234 | | { |
| | | 235 | | _staticAction = null; |
| | | 236 | | _targetReference = null; |
| | | 237 | | _actionTargetReference = null; |
| | | 238 | | _liveReference = null; |
| | | 239 | | _method = null; |
| | | 240 | | } |
| | | 241 | | } |
| | | 242 | | |