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