|
| 1 | +using System; |
| 2 | + |
| 3 | +using JetBrains.Annotations; |
| 4 | + |
| 5 | +namespace CodeJam |
| 6 | +{ |
| 7 | + /// <summary>Helper methods for <see cref="IDisposable"/></summary> |
| 8 | + [PublicAPI] |
| 9 | + public static class InitDispose |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// Calls <paramref name="initAction"/> and |
| 13 | + /// creates <see cref="IDisposable"/> instance that calls <paramref name="disposeAction"/> on disposing. |
| 14 | + /// </summary> |
| 15 | + /// <param name="initAction">The init action.</param> |
| 16 | + /// <param name="disposeAction">The dispose action.</param> |
| 17 | + /// <returns> |
| 18 | + /// Instance of <see cref="IDisposable"/> that calls <paramref name="disposeAction"/> on disposing. |
| 19 | + /// </returns> |
| 20 | + [NotNull, Pure] |
| 21 | + public static IDisposable Create([NotNull] Action initAction, [NotNull] Action disposeAction) |
| 22 | + { |
| 23 | + Code.NotNull(initAction, nameof(initAction)); |
| 24 | + Code.NotNull(disposeAction, nameof(disposeAction)); |
| 25 | + |
| 26 | + initAction(); |
| 27 | + return Disposable.Create(disposeAction); |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Calls <paramref name="initAction"/> and |
| 32 | + /// creates <see cref="IDisposable"/> instance that calls <paramref name="disposeAction"/> on disposing. |
| 33 | + /// </summary> |
| 34 | + /// <param name="initAction">The init action.</param> |
| 35 | + /// <param name="disposeAction">The dispose action.</param> |
| 36 | + /// <returns> |
| 37 | + /// Instance of <see cref="IDisposable"/> that calls <paramref name="disposeAction"/> on disposing. |
| 38 | + /// </returns> |
| 39 | + [NotNull, Pure] |
| 40 | + public static IDisposable Create<T>([NotNull] Func<T> initAction, [NotNull] Action<T> disposeAction) |
| 41 | + { |
| 42 | + Code.NotNull(initAction, nameof(initAction)); |
| 43 | + Code.NotNull(disposeAction, nameof(disposeAction)); |
| 44 | + |
| 45 | + var initState = initAction(); |
| 46 | + return Disposable.Create(disposeAction, initState); |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Calls <paramref name="initDisposeAction"/> and |
| 51 | + /// creates <see cref="IDisposable"/> instance that calls <paramref name="initDisposeAction"/> on disposing. |
| 52 | + /// </summary> |
| 53 | + /// <param name="initDisposeAction">The init and dispose action.</param> |
| 54 | + /// <returns> |
| 55 | + /// Instance of <see cref="IDisposable"/> that calls <paramref name="initDisposeAction"/> on disposing. |
| 56 | + /// </returns> |
| 57 | + [NotNull, Pure] |
| 58 | + public static IDisposable Create([NotNull] Action initDisposeAction) |
| 59 | + { |
| 60 | + Code.NotNull(initDisposeAction, nameof(initDisposeAction)); |
| 61 | + |
| 62 | + initDisposeAction(); |
| 63 | + return Disposable.Create(initDisposeAction); |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Calls <paramref name="initDisposeAction"/> and |
| 68 | + /// creates <see cref="IDisposable"/> instance that calls <paramref name="initDisposeAction"/> on disposing. |
| 69 | + /// </summary> |
| 70 | + /// <param name="initDisposeAction"> |
| 71 | + /// The init and dispose action. |
| 72 | + /// <paramref name="initDisposeAction"/> takes true if this is initAction. |
| 73 | + /// It takes false if this is disposeAction. |
| 74 | + /// </param> |
| 75 | + /// <returns> |
| 76 | + /// Instance of <see cref="IDisposable"/> that calls <paramref name="initDisposeAction"/> on disposing. |
| 77 | + /// </returns> |
| 78 | + [NotNull, Pure] |
| 79 | + public static IDisposable Create([NotNull] Action<bool> initDisposeAction) |
| 80 | + { |
| 81 | + Code.NotNull(initDisposeAction, nameof(initDisposeAction)); |
| 82 | + |
| 83 | + initDisposeAction(true); |
| 84 | + return Disposable.Create<bool>(initDisposeAction, false); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments