| | | 1 | | using SwiftCollections; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | |
| | | 6 | | namespace Chronicler; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Stores stable-link resolution strategies for external or runtime-owned objects. |
| | | 10 | | /// </summary> |
| | | 11 | | public sealed class ChronicleLinkRegistry |
| | | 12 | | { |
| | 113 | 13 | | private readonly SwiftDictionary<ChronicleLinkKey, object> _resolvers = new(); |
| | 113 | 14 | | private readonly SwiftDictionary<ChronicleLinkKey, object> _registeredInstances = new(); |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Registers a custom link resolver for the given type and optional slot. |
| | | 18 | | /// </summary> |
| | | 19 | | public void RegisterResolver<T>(IRecordLinkResolver<T> resolver, string? slot = null) |
| | 13 | 20 | | { |
| | 13 | 21 | | _resolvers[new ChronicleLinkKey(typeof(T), slot)] = resolver ?? throw new ArgumentNullException(nameof(resolver) |
| | 12 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Registers a concrete instance so the chronicler can save and load it through a stable identifier. |
| | | 26 | | /// </summary> |
| | | 27 | | public void RegisterInstance<T>(string id, T value, string? slot = null) |
| | 25 | 28 | | { |
| | 25 | 29 | | if (string.IsNullOrWhiteSpace(id)) |
| | 3 | 30 | | throw new ArgumentException("A registered link id must not be null or empty.", nameof(id)); |
| | | 31 | | |
| | 22 | 32 | | RegisteredLinkTable<T> table = GetOrCreateInstanceTable<T>(slot); |
| | 22 | 33 | | table.Register(id, value); |
| | 22 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Removes a previously registered concrete instance. |
| | | 38 | | /// </summary> |
| | | 39 | | public bool UnregisterInstance<T>(string id, string? slot = null) |
| | 6 | 40 | | { |
| | 6 | 41 | | if (string.IsNullOrWhiteSpace(id)) |
| | 3 | 42 | | return false; |
| | | 43 | | |
| | 3 | 44 | | ChronicleLinkKey key = new(typeof(T), slot); |
| | 3 | 45 | | if (!_registeredInstances.TryGetValue(key, out object tableObject)) |
| | 1 | 46 | | return false; |
| | | 47 | | |
| | 2 | 48 | | return ((RegisteredLinkTable<T>)tableObject).Unregister(id); |
| | 6 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Attempts to resolve a stable identifier into an instance of the requested type. |
| | | 53 | | /// </summary> |
| | | 54 | | public bool TryResolve<T>(string id, [MaybeNullWhen(false)] out T value, string? slot = null) |
| | 22 | 55 | | { |
| | 22 | 56 | | ChronicleLinkKey key = new(typeof(T), slot); |
| | 22 | 57 | | if (_resolvers.TryGetValue(key, out object resolverObject) |
| | 22 | 58 | | && ((IRecordLinkResolver<T>)resolverObject).TryResolveReference(id, out value)) |
| | 4 | 59 | | { |
| | 4 | 60 | | return true; |
| | | 61 | | } |
| | | 62 | | |
| | 18 | 63 | | if (_registeredInstances.TryGetValue(key, out object tableObject) |
| | 18 | 64 | | && ((RegisteredLinkTable<T>)tableObject).TryResolve(id, out value)) |
| | 5 | 65 | | { |
| | 5 | 66 | | return true; |
| | | 67 | | } |
| | | 68 | | |
| | 13 | 69 | | value = default!; |
| | 13 | 70 | | return false; |
| | 22 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Attempts to read a stable identifier from a concrete instance. |
| | | 75 | | /// </summary> |
| | | 76 | | public bool TryGetReferenceId<T>(T value, [NotNullWhen(true)] out string? id, string? slot = null) |
| | 26 | 77 | | { |
| | 26 | 78 | | ChronicleLinkKey key = new(typeof(T), slot); |
| | 26 | 79 | | if (_resolvers.TryGetValue(key, out object resolverObject) |
| | 26 | 80 | | && ((IRecordLinkResolver<T>)resolverObject).TryGetReferenceId(value, out id)) |
| | 8 | 81 | | { |
| | 8 | 82 | | return true; |
| | | 83 | | } |
| | | 84 | | |
| | 18 | 85 | | if (_registeredInstances.TryGetValue(key, out object tableObject) |
| | 18 | 86 | | && ((RegisteredLinkTable<T>)tableObject).TryGetReferenceId(value, out id)) |
| | 15 | 87 | | { |
| | 15 | 88 | | return true; |
| | | 89 | | } |
| | | 90 | | |
| | 3 | 91 | | id = null; |
| | 3 | 92 | | return false; |
| | 26 | 93 | | } |
| | | 94 | | |
| | | 95 | | private RegisteredLinkTable<T> GetOrCreateInstanceTable<T>(string? slot) |
| | 22 | 96 | | { |
| | 22 | 97 | | ChronicleLinkKey key = new(typeof(T), slot); |
| | 22 | 98 | | if (_registeredInstances.TryGetValue(key, out object tableObject)) |
| | 1 | 99 | | return (RegisteredLinkTable<T>)tableObject; |
| | | 100 | | |
| | 21 | 101 | | var table = new RegisteredLinkTable<T>(); |
| | 21 | 102 | | _registeredInstances[key] = table; |
| | 21 | 103 | | return table; |
| | 22 | 104 | | } |
| | | 105 | | |
| | | 106 | | private readonly struct ChronicleLinkKey : IEquatable<ChronicleLinkKey> |
| | | 107 | | { |
| | | 108 | | private readonly Type _type; |
| | | 109 | | private readonly string _slot; |
| | | 110 | | |
| | | 111 | | public ChronicleLinkKey(Type type, string? slot) |
| | 88 | 112 | | { |
| | 88 | 113 | | _type = type ?? throw new ArgumentNullException(nameof(type)); |
| | 88 | 114 | | _slot = slot ?? string.Empty; |
| | 88 | 115 | | } |
| | | 116 | | |
| | | 117 | | public bool Equals(ChronicleLinkKey other) |
| | 37 | 118 | | { |
| | 37 | 119 | | return _type == other._type |
| | 37 | 120 | | && string.Equals(_slot, other._slot, StringComparison.Ordinal); |
| | 37 | 121 | | } |
| | | 122 | | |
| | 2 | 123 | | public override bool Equals(object? obj) => obj is ChronicleLinkKey other && Equals(other); |
| | | 124 | | |
| | | 125 | | public override int GetHashCode() |
| | 175 | 126 | | { |
| | | 127 | | unchecked |
| | 175 | 128 | | { |
| | 175 | 129 | | return (_type.GetHashCode() * 397) ^ _slot.GetHashCode(); |
| | | 130 | | } |
| | 175 | 131 | | } |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | private sealed class RegisteredLinkTable<T> |
| | | 135 | | { |
| | 21 | 136 | | private readonly SwiftDictionary<string, T> _byId = new(8, StringComparer.Ordinal); |
| | | 137 | | |
| | | 138 | | public void Register(string id, T value) |
| | 22 | 139 | | { |
| | 22 | 140 | | _byId[id] = value; |
| | 22 | 141 | | } |
| | | 142 | | |
| | | 143 | | public bool Unregister(string id) |
| | 2 | 144 | | { |
| | 2 | 145 | | return _byId.Remove(id); |
| | 2 | 146 | | } |
| | | 147 | | |
| | | 148 | | public bool TryResolve(string id, [MaybeNullWhen(false)] out T value) |
| | 5 | 149 | | { |
| | 5 | 150 | | return _byId.TryGetValue(id, out value); |
| | 5 | 151 | | } |
| | | 152 | | |
| | | 153 | | public bool TryGetReferenceId(T value, [NotNullWhen(true)] out string? id) |
| | 16 | 154 | | { |
| | 65 | 155 | | foreach (KeyValuePair<string, T> pair in _byId) |
| | 16 | 156 | | { |
| | 16 | 157 | | if (!ValuesMatch(pair.Value, value)) |
| | 1 | 158 | | continue; |
| | | 159 | | |
| | 15 | 160 | | id = pair.Key; |
| | 15 | 161 | | return true; |
| | | 162 | | } |
| | | 163 | | |
| | 1 | 164 | | id = null; |
| | 1 | 165 | | return false; |
| | 16 | 166 | | } |
| | | 167 | | |
| | | 168 | | private static bool ValuesMatch(T left, T right) |
| | 16 | 169 | | { |
| | 16 | 170 | | if (typeof(T).IsValueType) |
| | 2 | 171 | | return EqualityComparer<T>.Default.Equals(left, right); |
| | | 172 | | |
| | 14 | 173 | | return ReferenceEquals(left, right); |
| | 16 | 174 | | } |
| | | 175 | | } |
| | | 176 | | } |