| | | 1 | | //======================================================================= |
| | | 2 | | // SwiftPooledObject.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 | | |
| | | 8 | | using SwiftCollections.Diagnostics; |
| | | 9 | | using System; |
| | | 10 | | using System.Threading; |
| | | 11 | | |
| | | 12 | | namespace SwiftCollections.Pool; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// A lease that wraps an object rented from an <see cref="ISwiftObjectPool{T}"/> and ensures it is automatically |
| | | 16 | | /// released back to the pool when disposed. Designed to simplify resource management and avoid manual release errors. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <typeparam name="T">The type of the object being pooled. Must be a reference type.</typeparam> |
| | | 19 | | public sealed class SwiftPooledObject<T> : IDisposable where T : class |
| | | 20 | | { |
| | | 21 | | #region Fields |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// The rented object that will be returned to the pool upon disposal. |
| | | 25 | | /// </summary> |
| | | 26 | | private T? _value; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// The pool from which the object was rented. |
| | | 30 | | /// </summary> |
| | | 31 | | private ISwiftObjectPool<T>? _pool; |
| | | 32 | | |
| | | 33 | | private int _disposed; |
| | | 34 | | |
| | | 35 | | #endregion |
| | | 36 | | |
| | | 37 | | #region Constructor |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Initializes a new instance of the <see cref="SwiftPooledObject{T}"/> class. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="value">The rented object.</param> |
| | | 43 | | /// <param name="pool">The pool that owns the object.</param> |
| | | 44 | | /// <exception cref="ArgumentNullException">Thrown if <paramref name="value"/> or <paramref name="pool"/> is null.</ |
| | 7 | 45 | | internal SwiftPooledObject(T value, ISwiftObjectPool<T> pool) |
| | | 46 | | { |
| | 7 | 47 | | SwiftThrowHelper.ThrowIfNull(value, nameof(value)); |
| | 6 | 48 | | SwiftThrowHelper.ThrowIfNull(pool, nameof(pool)); |
| | | 49 | | |
| | 5 | 50 | | _value = value; |
| | 5 | 51 | | _pool = pool; |
| | 5 | 52 | | } |
| | | 53 | | |
| | | 54 | | #endregion |
| | | 55 | | |
| | | 56 | | #region IDisposable Implementation |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Releases the rented object back to its pool. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <remarks> |
| | | 62 | | /// This method is automatically called when the <see cref="SwiftPooledObject{T}"/> goes out of scope in a |
| | | 63 | | /// using block or when manually disposed. |
| | | 64 | | /// </remarks> |
| | | 65 | | public void Dispose() |
| | | 66 | | { |
| | 6 | 67 | | if (Interlocked.Exchange(ref _disposed, 1) != 0) |
| | 1 | 68 | | return; |
| | | 69 | | |
| | 5 | 70 | | ISwiftObjectPool<T>? pool = _pool; |
| | 5 | 71 | | T? value = _value; |
| | | 72 | | |
| | 5 | 73 | | _pool = null; |
| | 5 | 74 | | _value = null; |
| | | 75 | | |
| | 5 | 76 | | pool!.Release(value!); |
| | 5 | 77 | | } |
| | | 78 | | |
| | | 79 | | #endregion |
| | | 80 | | } |