| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | |
|
| | 7 | | namespace GDX.DataTables.CellValues |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// A <see cref="DataTableBase" /> <see cref="Enum" /> cell reference. |
| | 11 | | /// </summary> |
| | 12 | | [Serializable] |
| | 13 | | public struct EnumIntCellValue |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// A cached type for reference at runtime. |
| | 17 | | /// </summary> |
| | 18 | | Type m_CachedType; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// The cached value of the referenced <see cref="DataTable" /> cell. |
| | 22 | | /// </summary> |
| | 23 | | int m_CachedValue; |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// The cached <see cref="DataTable" /> version found when last updating <see cref="m_CachedValue" />. |
| | 27 | | /// </summary> |
| | 28 | | ulong m_TableVersion; |
| | 29 | | #pragma warning disable IDE1006 |
| | 30 | | // ReSharper disable InconsistentNaming |
| | 31 | | /// <summary> |
| | 32 | | /// The <see cref="DataTableBase" /> polled for cell data. |
| | 33 | | /// </summary> |
| | 34 | | public DataTableBase DataTable; |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// The unique column identifier used when accessing the <see cref="DataTable" />. |
| | 38 | | /// </summary> |
| | 39 | | public int ColumnIdentifier; |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// The unique row identifier used when accessing the <see cref="DataTable" />. |
| | 43 | | /// </summary> |
| | 44 | | public int RowIdentifier; |
| | 45 | | // ReSharper enable InconsistentNaming |
| | 46 | | #pragma warning restore IDE1006 |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Creates a <see cref="FloatCellValue" />. |
| | 50 | | /// </summary> |
| | 51 | | /// <param name="dataTable">The referenced <see cref="DataTableBase" />.</param> |
| | 52 | | /// <param name="rowIdentifier">The unique row identifier to use when polling the <paramref name="dataTable" />. |
| | 53 | | /// <param name="columnIdentifier">The unique column identifier to use when polling the <paramref name="dataTabl |
| | 54 | | public EnumIntCellValue(DataTableBase dataTable, int rowIdentifier, int columnIdentifier) |
| 0 | 55 | | { |
| 0 | 56 | | DataTable = dataTable; |
| 0 | 57 | | RowIdentifier = rowIdentifier; |
| 0 | 58 | | ColumnIdentifier = columnIdentifier; |
| 0 | 59 | | m_TableVersion = 0; |
| 0 | 60 | | m_CachedValue = default; |
| 0 | 61 | | m_CachedType = null; |
| 0 | 62 | | Get(); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Get the <see cref="float" /> value referenced from the <see cref="DataTableBase" />. |
| | 67 | | /// </summary> |
| | 68 | | /// <remarks> |
| | 69 | | /// This will evaluate if the version of the table matches the internally cached version, and will update |
| | 70 | | /// the cached reference if necessary. |
| | 71 | | /// </remarks> |
| | 72 | | /// <returns>A <see cref="float" /> value.</returns> |
| | 73 | | public int Get() |
| 0 | 74 | | { |
| 0 | 75 | | if (m_TableVersion == DataTable.GetDataVersion()) |
| 0 | 76 | | { |
| 0 | 77 | | return m_CachedValue; |
| | 78 | | } |
| | 79 | |
|
| 0 | 80 | | m_CachedValue = DataTable.GetEnumInt(RowIdentifier, ColumnIdentifier); |
| 0 | 81 | | m_TableVersion = DataTable.GetDataVersion(); |
| 0 | 82 | | return m_CachedValue; |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Get the <see cref="Enum" /> value of the <see cref="DataTableBase" /> cell's value. |
| | 87 | | /// </summary> |
| | 88 | | /// <remarks> |
| | 89 | | /// Values are stored as <see cref="int" />, there are some cases where having the <see cref="Enum" /> could |
| | 90 | | /// useful. |
| | 91 | | /// </remarks> |
| | 92 | | /// <returns>The transposed value.</returns> |
| | 93 | | public Enum GetEnum() |
| 0 | 94 | | { |
| 0 | 95 | | m_CachedType ??= Type.GetType(DataTable.GetTypeNameForColumn(ColumnIdentifier)); |
| | 96 | |
|
| 0 | 97 | | if (m_CachedType != null) |
| 0 | 98 | | { |
| 0 | 99 | | return (Enum)Enum.Parse(m_CachedType, Get().ToString()); |
| | 100 | | } |
| | 101 | |
|
| 0 | 102 | | return null; |
| 0 | 103 | | } |
| | 104 | |
|
| | 105 | | /// <summary> |
| | 106 | | /// Get the internally cached version of the <see cref="DataTableBase" />'s data version. |
| | 107 | | /// </summary> |
| | 108 | | /// <returns>A version number.</returns> |
| | 109 | | public ulong GetDataVersion() |
| 0 | 110 | | { |
| 0 | 111 | | return m_TableVersion; |
| 0 | 112 | | } |
| | 113 | |
|
| | 114 | | /// <summary> |
| | 115 | | /// Get the cached value without a version check. |
| | 116 | | /// </summary> |
| | 117 | | /// <remarks> |
| | 118 | | /// This can respond with a default value if a <see cref="Get" /> call has not been made yet to populate the |
| | 119 | | /// internally cached value. |
| | 120 | | /// </remarks> |
| | 121 | | /// <returns>A <see cref="int" /> value.</returns> |
| | 122 | | public int GetUnsafe() |
| 0 | 123 | | { |
| 0 | 124 | | return m_CachedValue; |
| 0 | 125 | | } |
| | 126 | |
|
| | 127 | | /// <summary> |
| | 128 | | /// Sets the cached value of the struct and by default, updates the associated <see cref="DataTableBase" />. |
| | 129 | | /// </summary> |
| | 130 | | /// <remarks>Updating the <see cref="DataTableBase" /> will update the cached table version.</remarks> |
| | 131 | | /// <param name="newValue">A <see cref="float" /> value.</param> |
| | 132 | | /// <param name="updateTable">Should the value be pushed back to the referenced <see cref="DataTableBase" /> cel |
| | 133 | | public void Set(int newValue, bool updateTable = true) |
| 0 | 134 | | { |
| 0 | 135 | | m_CachedValue = newValue; |
| 0 | 136 | | if (updateTable) |
| 0 | 137 | | { |
| 0 | 138 | | m_TableVersion = DataTable.SetEnumInt(RowIdentifier, ColumnIdentifier, newValue); |
| 0 | 139 | | } |
| 0 | 140 | | } |
| | 141 | |
|
| | 142 | | /// <summary> |
| | 143 | | /// Get the <see cref="Serializable.SerializableTypes" /> which this struct supports. |
| | 144 | | /// </summary> |
| | 145 | | /// <returns>A <see cref="Serializable.SerializableTypes" />.</returns> |
| | 146 | | public static Serializable.SerializableTypes GetSupportedType() |
| 0 | 147 | | { |
| 0 | 148 | | return Serializable.SerializableTypes.EnumInt; |
| 0 | 149 | | } |
| | 150 | | } |
| | 151 | | } |