From 1803b9fef9fe055b7125f91d8dc63734c18675cb Mon Sep 17 00:00:00 2001 From: FICTURE7 Date: Fri, 9 Apr 2021 20:52:28 +0400 Subject: [PATCH] Add Counter --- ARMeilleure/Common/Counter.cs | 48 +++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 ARMeilleure/Common/Counter.cs diff --git a/ARMeilleure/Common/Counter.cs b/ARMeilleure/Common/Counter.cs new file mode 100644 index 000000000..d3d28730e --- /dev/null +++ b/ARMeilleure/Common/Counter.cs @@ -0,0 +1,48 @@ +namespace ARMeilleure.Common +{ + /// + /// Represents an 8-bit counter. + /// + class Counter + { + private readonly int _index; + private readonly EntryTable _countTable; + + /// + /// Initializes a new instance of the class from the specified + /// instance and index. + /// + /// instance + /// Index in the + private Counter(EntryTable countTable, int index) + { + _countTable = countTable; + _index = index; + } + + /// + /// Gets a reference to the value of the counter. + /// + public ref byte Value => ref _countTable.GetValue(_index); + + /// + /// Tries to create a instance from the specified instance. + /// + /// from which to create the + /// instance if success; otherwise + /// if success; otherwise + public static bool TryCreate(EntryTable countTable, out Counter counter) + { + if (countTable.TryAllocate(out int index)) + { + counter = new Counter(countTable, index); + + return true; + } + + counter = null; + + return false; + } + } +} \ No newline at end of file