< Summary

Information
Class: SwiftCollections.Lazy.SwiftLazyDisposable<T>
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Lazy/SwiftLazyDisposable.cs
Line coverage
100%
Covered lines: 14
Uncovered lines: 0
Coverable lines: 14
Total lines: 137
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
ToString()100%44100%
Dispose()100%44100%
Finalize()100%11100%

File(s)

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Lazy/SwiftLazyDisposable.cs

#LineLine coverage
 1//=======================================================================
 2// SwiftLazyDisposable.cs
 3//=======================================================================
 4// MIT License, Copyright (c) 2024–present David Oravsky (mrdav30)
 5// See LICENSE file in the project root for full license information.
 6//=======================================================================
 7
 8using System;
 9using System.Threading;
 10
 11namespace SwiftCollections.Lazy;
 12
 13/// <summary>
 14/// A lazily initialized disposable object.
 15/// This class extends <see cref="Lazy{T}"/> to support <see cref="IDisposable"/> objects,
 16/// ensuring proper resource cleanup when disposed.
 17/// </summary>
 18/// <typeparam name="T">The type of the lazily initialized object, which must implement <see cref="IDisposable"/>.</type
 19public class SwiftLazyDisposable<T> : Lazy<T>, IDisposable where T : IDisposable
 20{
 21    #region Fields
 22
 23    /// <summary>
 24    /// Indicates whether the lazy instance has been disposed.
 25    /// Prevents multiple disposal attempts, ensuring safe resource cleanup.
 26    /// </summary>
 27    private volatile bool _disposed;
 28
 29    #endregion
 30
 31    #region Constructors
 32
 33    /// <summary>
 34    ///  Initializes a new instance of the <see cref="SwiftLazyDisposable{T}"/> class.
 35    ///  When lazy initialization occurs, the default constructor is used.
 36    /// </summary>
 237    public SwiftLazyDisposable() : base() { }
 38
 39    /// <summary>
 40    ///  Initializes a new instance of the <see cref="SwiftLazyDisposable{T}"/> class.
 41    ///  When lazy initialization occurs, the default constructor of the target type
 42    ///  and the specified initialization mode are used.
 43    /// </summary>
 44    /// <param name="isThreadSafe">
 45    ///  true to make this instance usable concurrently by multiple threads;
 46    ///  false to make the instance usable by only one thread at a time.
 47    /// </param>
 248    public SwiftLazyDisposable(bool isThreadSafe) : base(isThreadSafe) { }
 49
 50    /// <summary>
 51    ///  Initializes a new instance of the <see cref="SwiftLazyDisposable{T}"/> class
 52    ///  that uses the default constructor of T and the specified thread-safety mode.
 53    /// </summary>
 54    /// <param name="mode">
 55    ///  One of the enumeration values that specifies the thread safety mode.
 56    /// </param>
 257    public SwiftLazyDisposable(LazyThreadSafetyMode mode) : base(mode) { }
 58
 59    /// <summary>
 60    ///  Initializes a new instance of the <see cref="SwiftLazyDisposable{T}"/> class.
 61    ///  When lazy initialization occurs, the specified initialization function is used.
 62    /// </summary>
 63    /// <param name="valueFactory">
 64    ///  The delegate that is invoked to produce the lazily initialized value when it is needed.
 65    /// </param>
 14066    public SwiftLazyDisposable(Func<T> valueFactory) : base(valueFactory) { }
 67
 68    /// <summary>
 69    ///  Initializes a new instance of the <see cref="SwiftLazyDisposable{T}"/> class.
 70    ///  When lazy initialization occurs, the specified initialization function
 71    ///  and initialization mode are used.
 72    /// </summary>
 73    /// <param name="valueFactory">
 74    ///  The delegate that is invoked to produce the lazily initialized value when it is needed.
 75    /// </param>
 76    /// <param name="isThreadSafe">
 77    ///  true to make this instance usable concurrently by multiple threads;
 78    ///  false to make this instance usable by only one thread at a time.
 79    /// </param>
 280    public SwiftLazyDisposable(Func<T> valueFactory, bool isThreadSafe) : base(valueFactory, isThreadSafe) { }
 81
 82    /// <summary>
 83    ///  Initializes a new instance of the <see cref="SwiftLazyDisposable{T}"/> class
 84    ///  using the specified initialization function and thread-safety mode.
 85    /// </summary>
 86    /// <param name="valueFactory">
 87    ///  The delegate that is invoked to produce the lazily initialized value when it is needed.
 88    /// </param>
 89    /// <param name="mode">
 90    ///  One of the enumeration values that specifies the thread safety mode.
 91    /// </param>
 1892    public SwiftLazyDisposable(Func<T> valueFactory, LazyThreadSafetyMode mode) : base(valueFactory, mode) { }
 93
 94    #endregion
 95
 96    #region Utility
 97
 98    /// <summary>
 99    /// Returns a string that represents the current state of the object.
 100    /// </summary>
 101    /// <remarks>
 102    /// This method provides a user-friendly description of the object's state,
 103    /// which can be useful for debugging or logging purposes.
 104    /// </remarks>
 105    /// <returns>
 106    /// A string representation of the value if it has been created; otherwise,
 107    /// a string indicating that the value has not been created.
 108    /// </returns>
 4109    public override string ToString() => IsValueCreated ? Value.ToString() ?? string.Empty : "LazyDisposable (Not Create
 110
 111    #endregion
 112
 113    #region IDisposable Implementation
 114
 115    /// <summary>
 116    /// Disposes the lazily initialized value if it has been created.
 117    /// Ensures that disposal is only performed once.
 118    /// </summary>
 119    public void Dispose()
 120    {
 70121        if (_disposed || !this.IsValueCreated)
 33122            return;
 123
 37124        _disposed = true;
 37125        this.Value.Dispose();
 126
 127        // Suppress finalization since we've manually disposed
 37128        GC.SuppressFinalize(this);
 37129    }
 130
 131    /// <summary>
 132    /// Finalizer to ensure disposal if Dispose() is never called.
 133    /// </summary>
 67134    ~SwiftLazyDisposable() => Dispose();
 135
 136    #endregion
 137}