Make Counter<T> disposable

This commit is contained in:
FICTURE7 2021-04-11 13:54:33 +04:00
parent 883faaff01
commit 6f4cb3c0ad

View file

@ -6,8 +6,9 @@ namespace ARMeilleure.Common
/// Represents a numeric counter. /// Represents a numeric counter.
/// </summary> /// </summary>
/// <typeparam name="T">Type of the counter</typeparam> /// <typeparam name="T">Type of the counter</typeparam>
class Counter<T> where T : unmanaged class Counter<T> : IDisposable where T : unmanaged
{ {
private bool _disposed;
private readonly int _index; private readonly int _index;
private readonly EntryTable<T> _countTable; private readonly EntryTable<T> _countTable;
@ -26,7 +27,18 @@ namespace ARMeilleure.Common
/// <summary> /// <summary>
/// Gets a reference to the value of the counter. /// Gets a reference to the value of the counter.
/// </summary> /// </summary>
public ref T Value => ref _countTable.GetValue(_index); public ref T Value
{
get
{
if (_disposed)
{
throw new ObjectDisposedException(null);
}
return ref _countTable.GetValue(_index);
}
}
/// <summary> /// <summary>
/// Tries to create a <see cref="Counter"/> instance from the specified <see cref="EntryTable{byte}"/> instance. /// Tries to create a <see cref="Counter"/> instance from the specified <see cref="EntryTable{byte}"/> instance.
@ -64,5 +76,18 @@ namespace ARMeilleure.Common
return false; return false;
} }
/// <summary>
/// Releases all resources used by the <see cref="Counter{T}"/> instance.
/// </summary>
public void Dispose()
{
if (!_disposed)
{
_countTable.Free(_index);
_disposed = true;
}
}
} }
} }