| | | 1 | | //======================================================================= |
| | | 2 | | // RecordLinks.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 Chronicler; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Records stable IDs for links to host-owned values. |
| | | 14 | | /// </summary> |
| | | 15 | | public static class RecordLinks |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Records a link that must resolve as soon as it is read. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <typeparam name="T">The declared type used for registry lookup.</typeparam> |
| | | 21 | | /// <param name="chronicler">The active pass supplied to <see cref="IRecordable.RecordData"/>.</param> |
| | | 22 | | /// <param name="value"> |
| | | 23 | | /// The value to identify on save. On load, receives the resolved value or the default for a missing or null entry. |
| | | 24 | | /// An unresolved ID leaves this value unchanged. |
| | | 25 | | /// </param> |
| | | 26 | | /// <param name="name">The stable field name to use on both save and load.</param> |
| | | 27 | | /// <param name="slot">An optional identity domain. Null or empty selects the default slot.</param> |
| | | 28 | | /// <exception cref="InvalidOperationException"> |
| | | 29 | | /// A non-null value has no stable ID on save, or a present link ID cannot resolve on load. |
| | | 30 | | /// </exception> |
| | | 31 | | public static void Look<T>(IChronicler chronicler, ref T value, string name, string? slot = null) |
| | | 32 | | { |
| | 34 | 33 | | chronicler.LookLink(ref value, name, slot); |
| | 28 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Records a link whose assignment may wait until the graph has loaded. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <typeparam name="T">The declared type used for registry lookup.</typeparam> |
| | | 40 | | /// <param name="chronicler">The active pass supplied to <see cref="IRecordable.RecordData"/>.</param> |
| | | 41 | | /// <param name="value">The value to identify on save. The callback applies any loaded value.</param> |
| | | 42 | | /// <param name="name">The stable field name to use on both save and load.</param> |
| | | 43 | | /// <param name="assignLoadedValue"> |
| | | 44 | | /// Applies the resolved value, or the default of <typeparamref name="T"/> for a missing or null entry. |
| | | 45 | | /// The load context retains this callback while the link is queued. |
| | | 46 | | /// </param> |
| | | 47 | | /// <param name="slot">An optional identity domain. Null or empty selects the default slot.</param> |
| | | 48 | | /// <remarks> |
| | | 49 | | /// On load, the callback runs immediately if the target is available or the entry is missing or null. |
| | | 50 | | /// Otherwise, <see cref="ChronicleContext.ResolveDeferredLinks"/> invokes it when the target resolves. |
| | | 51 | | /// </remarks> |
| | | 52 | | /// <exception cref="ArgumentNullException"><paramref name="assignLoadedValue"/> is null.</exception> |
| | | 53 | | /// <exception cref="InvalidOperationException">A non-null value has no stable ID on save.</exception> |
| | | 54 | | public static void LookDeferred<T>( |
| | | 55 | | IChronicler chronicler, |
| | | 56 | | T value, |
| | | 57 | | string name, |
| | | 58 | | Action<T> assignLoadedValue, |
| | | 59 | | string? slot = null) |
| | | 60 | | { |
| | 23 | 61 | | if (assignLoadedValue == null) |
| | 1 | 62 | | throw new ArgumentNullException(nameof(assignLoadedValue)); |
| | | 63 | | |
| | 22 | 64 | | chronicler.LookLink( |
| | 22 | 65 | | ref value, |
| | 22 | 66 | | name, |
| | 22 | 67 | | slot, |
| | 22 | 68 | | RecordLinkResolveMode.Deferred, |
| | 22 | 69 | | assignLoadedValue); |
| | 22 | 70 | | } |
| | | 71 | | } |