| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="UnitConverterRegistry.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | |
| | | 10 | | namespace MyNet.Primitives.Conversion; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Provides a registry for unit converters, allowing for the registration and retrieval of converters based on their as |
| | | 14 | | /// </summary> |
| | | 15 | | public static class UnitConverterRegistry |
| | | 16 | | { |
| | 9 | 17 | | private static readonly Dictionary<Type, object> Converters = []; |
| | | 18 | | |
| | | 19 | | static UnitConverterRegistry() |
| | | 20 | | { |
| | 9 | 21 | | Register(new DataSizeConverter()); |
| | 9 | 22 | | Register(new LengthConverter()); |
| | 9 | 23 | | Register(new MassConverter()); |
| | 9 | 24 | | Register(new TimeConverter()); |
| | 9 | 25 | | Register(new TemperatureConverter()); |
| | 9 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Registers a unit converter for a specific unit type. The converter is stored in the registry using the unit type |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="converter">The unit converter to register.</param> |
| | | 32 | | /// <typeparam name="TUnit">The type of the unit.</typeparam> |
| | | 33 | | public static void Register<TUnit>(IUnitConverter<TUnit> converter) |
| | 45 | 34 | | where TUnit : struct, Enum => Converters[typeof(TUnit)] = converter; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Retrieves a registered unit converter for a specific unit type. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <typeparam name="TUnit">The type of the unit.</typeparam> |
| | | 40 | | /// <returns>The registered unit converter.</returns> |
| | | 41 | | public static IUnitConverter<TUnit> Get<TUnit>() |
| | | 42 | | where TUnit : struct, Enum |
| | 255 | 43 | | => Converters.TryGetValue(typeof(TUnit), out var converter) |
| | 255 | 44 | | ? (IUnitConverter<TUnit>)converter |
| | 255 | 45 | | : throw new InvalidOperationException($"No converter registered for unit type '{typeof(TUnit).Name}'."); |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Retrieves a registered unit converter for a specific unit type, using the provided type as the key. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="type">The type of the unit.</param> |
| | | 51 | | /// <returns>The registered unit converter.</returns> |
| | | 52 | | public static IUnitConverter<TUnit> Get<TUnit>(Type type) |
| | | 53 | | where TUnit : struct, Enum |
| | 0 | 54 | | => Converters.TryGetValue(type, out var converter) |
| | 0 | 55 | | ? (IUnitConverter<TUnit>)converter |
| | 0 | 56 | | : throw new InvalidOperationException($"No converter registered for unit type '{type.Name}'."); |
| | | 57 | | } |
| | | 58 | | |