Add Counter

This commit is contained in:
FICTURE7 2021-04-09 20:52:28 +04:00
parent 98ac020097
commit 1803b9fef9

View file

@ -0,0 +1,48 @@
namespace ARMeilleure.Common
{
/// <summary>
/// Represents an 8-bit counter.
/// </summary>
class Counter
{
private readonly int _index;
private readonly EntryTable<byte> _countTable;
/// <summary>
/// Initializes a new instance of the <see cref="Counter"/> class from the specified
/// <see cref="EntryTable{byte}"/> instance and index.
/// </summary>
/// <param name="countTable"><see cref="EntryTable{byte}"/> instance</param>
/// <param name="index">Index in the <see cref="EntryTable{TEntry}"/></param>
private Counter(EntryTable<byte> countTable, int index)
{
_countTable = countTable;
_index = index;
}
/// <summary>
/// Gets a reference to the value of the counter.
/// </summary>
public ref byte Value => ref _countTable.GetValue(_index);
/// <summary>
/// Tries to create a <see cref="Counter"/> instance from the specified <see cref="EntryTable{byte}"/> instance.
/// </summary>
/// <param name="countTable"><see cref="EntryTable{TEntry}"/> from which to create the <see cref="Counter"/></param>
/// <param name="counter"><see cref="Counter"/> instance if success; otherwise <see langword="null"/></param>
/// <returns><see langword="true"/> if success; otherwise <see langword="false"/></returns>
public static bool TryCreate(EntryTable<byte> countTable, out Counter counter)
{
if (countTable.TryAllocate(out int index))
{
counter = new Counter(countTable, index);
return true;
}
counter = null;
return false;
}
}
}