-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelegateDisposable.cs
71 lines (66 loc) · 3.85 KB
/
DelegateDisposable.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using JetBrains.Annotations;
namespace Chasm.Utilities
{
/// <summary>
/// <para>Represents a <see cref="IDisposable"/>, that invokes an action when disposed.</para>
/// </summary>
[MustDisposeResource]
public class DelegateDisposable : IDisposable
{
private Action? disposeAction;
/// <summary>
/// <para>Initializes a new instance of the <see cref="DelegateDisposable"/> class with the specified <paramref name="dispose"/> action.</para>
/// </summary>
/// <param name="dispose">The action to invoke when the <see cref="DelegateDisposable"/> is disposed.</param>
public DelegateDisposable(Action dispose)
{
if (dispose is null) throw new ArgumentNullException(nameof(dispose));
disposeAction = dispose;
}
/// <inheritdoc/>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <inheritdoc cref="Dispose()"/>
/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing) Interlocked.Exchange(ref disposeAction, null)?.Invoke();
}
/// <summary>
/// <para>Runs the <paramref name="setup"/> action, and returns a <see cref="DelegateDisposable"/>, that will call <paramref name="dispose"/> when it's disposed.</para>
/// </summary>
/// <param name="setup">The action that is invoked immediately.</param>
/// <param name="dispose">The action that is invoked when the returned <see cref="DelegateDisposable"/> is disposed.</param>
/// <returns>A <see cref="DelegateDisposable"/> that will call <paramref name="dispose"/> when it's disposed.</returns>
/// <exception cref="ArgumentNullException"><paramref name="setup"/> or <paramref name="dispose"/> is <see langword="null"/>.</exception>
[MustDisposeResource, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static DelegateDisposable Create([InstantHandle] Action setup, Action dispose)
{
if (setup is null || dispose is null)
throw new ArgumentNullException(setup is null ? nameof(setup) : nameof(dispose));
setup();
return new DelegateDisposable(dispose);
}
/// <summary>
/// <para>Runs the <paramref name="setup"/> action, and returns a <see cref="DelegateDisposable"/>, that will call <paramref name="dispose"/> with a state argument returned by <paramref name="setup"/> when it's disposed.</para>
/// </summary>
/// <param name="setup">The action that is invoked immediately and returns a state argument.</param>
/// <param name="dispose">The action that is invoked with a state argument returned by <paramref name="setup"/> when the returned <see cref="DelegateDisposable"/> is disposed.</param>
/// <returns>A <see cref="DelegateDisposable"/> that will call <paramref name="dispose"/> with a state argument returned by <paramref name="setup"/> when it's disposed.</returns>
/// <exception cref="ArgumentNullException"><paramref name="setup"/> or <paramref name="dispose"/> is <see langword="null"/>.</exception>
[MustDisposeResource, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static DelegateDisposable Create<TState>([InstantHandle] Func<TState> setup, Action<TState> dispose)
{
if (setup is null || dispose is null)
throw new ArgumentNullException(setup is null ? nameof(setup) : nameof(dispose));
TState arg = setup();
return new DelegateDisposable(() => dispose(arg));
}
}
}