| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationFlowFieldPayloadLease.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | |
| | | 10 | | namespace Trailblazer.Pathing; |
| | | 11 | | |
| | | 12 | | /// <summary>Generation-validates one active reference to an immutable flow payload.</summary> |
| | | 13 | | internal readonly struct NavigationFlowFieldPayloadLease : IDisposable |
| | | 14 | | { |
| | | 15 | | private readonly NavigationFlowFieldPayloadCache? _owner; |
| | | 16 | | private readonly int _slot; |
| | | 17 | | private readonly ulong _generation; |
| | | 18 | | |
| | | 19 | | internal NavigationFlowFieldPayloadLease( |
| | | 20 | | NavigationFlowFieldPayloadCache owner, |
| | | 21 | | int slot, |
| | | 22 | | ulong generation) |
| | | 23 | | { |
| | 1575 | 24 | | _owner = owner; |
| | 1575 | 25 | | _slot = slot; |
| | 1575 | 26 | | _generation = generation; |
| | 1575 | 27 | | } |
| | | 28 | | |
| | | 29 | | internal NavigationFlowFieldStatus TryGetPayload( |
| | 305 | 30 | | out NavigationFlowFieldPayload payload) => _owner == null |
| | 305 | 31 | | ? ReturnStale(out payload) |
| | 305 | 32 | | : _owner.TryGetPayload(_slot, _generation, out payload); |
| | | 33 | | |
| | | 34 | | internal bool IsOwnedBy( |
| | | 35 | | NavigationFlowFieldPayloadCache owner, |
| | | 36 | | out int slot, |
| | | 37 | | out ulong generation) |
| | | 38 | | { |
| | 1184 | 39 | | slot = _slot; |
| | 1184 | 40 | | generation = _generation; |
| | 1184 | 41 | | return ReferenceEquals(_owner, owner); |
| | | 42 | | } |
| | | 43 | | |
| | 435 | 44 | | public void Dispose() => _owner?.Return(_slot, _generation); |
| | | 45 | | |
| | | 46 | | private static NavigationFlowFieldStatus ReturnStale( |
| | | 47 | | out NavigationFlowFieldPayload payload) |
| | | 48 | | { |
| | 3 | 49 | | payload = null!; |
| | 3 | 50 | | return NavigationFlowFieldStatus.Stale; |
| | | 51 | | } |
| | | 52 | | } |