|  |  | 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="int" /> cell reference. | 
|  |  | 11 |  |     /// </summary> | 
|  |  | 12 |  |     [Serializable] | 
|  |  | 13 |  |     public struct IntCellValue | 
|  |  | 14 |  |     { | 
|  |  | 15 |  |         /// <summary> | 
|  |  | 16 |  |         ///     The cached value of the referenced <see cref="DataTable" /> cell. | 
|  |  | 17 |  |         /// </summary> | 
|  |  | 18 |  |         int m_CachedValue; | 
|  |  | 19 |  |  | 
|  |  | 20 |  |         /// <summary> | 
|  |  | 21 |  |         ///     The cached <see cref="DataTable" /> version found when last updating <see cref="m_CachedValue" />. | 
|  |  | 22 |  |         /// </summary> | 
|  |  | 23 |  |         ulong m_TableVersion; | 
|  |  | 24 |  | #pragma warning disable IDE1006 | 
|  |  | 25 |  |         // ReSharper disable InconsistentNaming | 
|  |  | 26 |  |         /// <summary> | 
|  |  | 27 |  |         ///     The <see cref="DataTableBase" /> polled for cell data. | 
|  |  | 28 |  |         /// </summary> | 
|  |  | 29 |  |         public DataTableBase DataTable; | 
|  |  | 30 |  |  | 
|  |  | 31 |  |         /// <summary> | 
|  |  | 32 |  |         ///     The unique column identifier used when accessing the <see cref="DataTable" />. | 
|  |  | 33 |  |         /// </summary> | 
|  |  | 34 |  |         public int ColumnIdentifier; | 
|  |  | 35 |  |  | 
|  |  | 36 |  |         /// <summary> | 
|  |  | 37 |  |         ///     The unique row identifier used when accessing the <see cref="DataTable" />. | 
|  |  | 38 |  |         /// </summary> | 
|  |  | 39 |  |         public int RowIdentifier; | 
|  |  | 40 |  |         // ReSharper enable InconsistentNaming | 
|  |  | 41 |  | #pragma warning restore IDE1006 | 
|  |  | 42 |  |  | 
|  |  | 43 |  |         /// <summary> | 
|  |  | 44 |  |         ///     Creates an <see cref="IntCellValue" />. | 
|  |  | 45 |  |         /// </summary> | 
|  |  | 46 |  |         /// <param name="dataTable">The referenced <see cref="DataTableBase" />.</param> | 
|  |  | 47 |  |         /// <param name="rowIdentifier">The unique row identifier to use when polling the <paramref name="dataTable" />. | 
|  |  | 48 |  |         /// <param name="columnIdentifier">The unique column identifier to use when polling the <paramref name="dataTabl | 
|  |  | 49 |  |         public IntCellValue(DataTableBase dataTable, int rowIdentifier, int columnIdentifier) | 
|  | 0 | 50 |  |         { | 
|  | 0 | 51 |  |             DataTable = dataTable; | 
|  | 0 | 52 |  |             RowIdentifier = rowIdentifier; | 
|  | 0 | 53 |  |             ColumnIdentifier = columnIdentifier; | 
|  | 0 | 54 |  |             m_TableVersion = 0; | 
|  | 0 | 55 |  |             m_CachedValue = default; | 
|  | 0 | 56 |  |             Get(); | 
|  | 0 | 57 |  |         } | 
|  |  | 58 |  |  | 
|  |  | 59 |  |         /// <summary> | 
|  |  | 60 |  |         ///     Get the <see cref="int" /> value referenced from the <see cref="DataTableBase" />. | 
|  |  | 61 |  |         /// </summary> | 
|  |  | 62 |  |         /// <remarks> | 
|  |  | 63 |  |         ///     This will evaluate if the version of the table matches the internally cached version, and will update | 
|  |  | 64 |  |         ///     the cached reference if necessary. | 
|  |  | 65 |  |         /// </remarks> | 
|  |  | 66 |  |         /// <returns>An <see cref="int" /> value.</returns> | 
|  |  | 67 |  |         public int Get() | 
|  | 0 | 68 |  |         { | 
|  | 0 | 69 |  |             if (m_TableVersion == DataTable.GetDataVersion()) | 
|  | 0 | 70 |  |             { | 
|  | 0 | 71 |  |                 return m_CachedValue; | 
|  |  | 72 |  |             } | 
|  |  | 73 |  |  | 
|  | 0 | 74 |  |             m_CachedValue = DataTable.GetInt(RowIdentifier, ColumnIdentifier); | 
|  | 0 | 75 |  |             m_TableVersion = DataTable.GetDataVersion(); | 
|  | 0 | 76 |  |             return m_CachedValue; | 
|  | 0 | 77 |  |         } | 
|  |  | 78 |  |  | 
|  |  | 79 |  |         /// <summary> | 
|  |  | 80 |  |         ///     Get the internally cached version of the <see cref="DataTableBase" />'s data version. | 
|  |  | 81 |  |         /// </summary> | 
|  |  | 82 |  |         /// <returns>A version number.</returns> | 
|  |  | 83 |  |         public ulong GetDataVersion() | 
|  | 0 | 84 |  |         { | 
|  | 0 | 85 |  |             return m_TableVersion; | 
|  | 0 | 86 |  |         } | 
|  |  | 87 |  |  | 
|  |  | 88 |  |         /// <summary> | 
|  |  | 89 |  |         ///     Get the cached value without a version check. | 
|  |  | 90 |  |         /// </summary> | 
|  |  | 91 |  |         /// <remarks> | 
|  |  | 92 |  |         ///     This can respond with a default value if a <see cref="Get" /> call has not been made yet to populate the | 
|  |  | 93 |  |         ///     internally cached value. | 
|  |  | 94 |  |         /// </remarks> | 
|  |  | 95 |  |         /// <returns>An <see cref="int" /> value.</returns> | 
|  |  | 96 |  |         public int GetUnsafe() | 
|  | 0 | 97 |  |         { | 
|  | 0 | 98 |  |             return m_CachedValue; | 
|  | 0 | 99 |  |         } | 
|  |  | 100 |  |  | 
|  |  | 101 |  |         /// <summary> | 
|  |  | 102 |  |         ///     Sets the cached value of the struct and by default, updates the associated <see cref="DataTableBase" />. | 
|  |  | 103 |  |         /// </summary> | 
|  |  | 104 |  |         /// <remarks>Updating the <see cref="DataTableBase" /> will update the cached table version.</remarks> | 
|  |  | 105 |  |         /// <param name="newValue">An <see cref="int" /> value.</param> | 
|  |  | 106 |  |         /// <param name="updateTable">Should the value be pushed back to the referenced <see cref="DataTableBase" /> cel | 
|  |  | 107 |  |         public void Set(int newValue, bool updateTable = true) | 
|  | 0 | 108 |  |         { | 
|  | 0 | 109 |  |             m_CachedValue = newValue; | 
|  | 0 | 110 |  |             if (updateTable) | 
|  | 0 | 111 |  |             { | 
|  | 0 | 112 |  |                 m_TableVersion = DataTable.SetInt(RowIdentifier, ColumnIdentifier, newValue); | 
|  | 0 | 113 |  |             } | 
|  | 0 | 114 |  |         } | 
|  |  | 115 |  |  | 
|  |  | 116 |  |         /// <summary> | 
|  |  | 117 |  |         ///     Get the <see cref="Serializable.SerializableTypes" /> which this struct supports. | 
|  |  | 118 |  |         /// </summary> | 
|  |  | 119 |  |         /// <returns>A <see cref="Serializable.SerializableTypes" />.</returns> | 
|  |  | 120 |  |         public static Serializable.SerializableTypes GetSupportedType() | 
|  | 0 | 121 |  |         { | 
|  | 0 | 122 |  |             return Serializable.SerializableTypes.Int; | 
|  | 0 | 123 |  |         } | 
|  |  | 124 |  |     } | 
|  |  | 125 |  | } |