|
| 1 | +using System; |
| 2 | +using System.ComponentModel; |
| 3 | +using System.Diagnostics.CodeAnalysis; |
| 4 | +using System.Globalization; |
| 5 | +using System.Numerics; |
| 6 | +using Avalonia.Data.Converters; |
| 7 | + |
| 8 | +namespace StabilityMatrix.Avalonia.Converters; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Converts a possibly boxed nullable value type to its default value |
| 12 | +/// </summary> |
| 13 | +public class NullableDefaultNumericConverter<TSource, TTarget> : IValueConverter |
| 14 | + where TSource : unmanaged, INumber<TSource> |
| 15 | + where TTarget : unmanaged, INumber<TTarget> |
| 16 | +{ |
| 17 | + public ReturnBehavior NanHandling { get; set; } = ReturnBehavior.DefaultValue; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Unboxes a nullable value type |
| 21 | + /// </summary> |
| 22 | + private TSource Unbox(TTarget? value) |
| 23 | + { |
| 24 | + if (!value.HasValue) |
| 25 | + { |
| 26 | + return default; |
| 27 | + } |
| 28 | + |
| 29 | + if (TTarget.IsNaN(value.Value)) |
| 30 | + { |
| 31 | + return NanHandling switch |
| 32 | + { |
| 33 | + ReturnBehavior.DefaultValue => default, |
| 34 | + ReturnBehavior.Throw => throw new InvalidCastException("Cannot convert NaN to a numeric type"), |
| 35 | + _ |
| 36 | + => throw new InvalidEnumArgumentException( |
| 37 | + nameof(NanHandling), |
| 38 | + (int)NanHandling, |
| 39 | + typeof(ReturnBehavior) |
| 40 | + ) |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + return (TSource)System.Convert.ChangeType(value.Value, typeof(TSource)); |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Convert a value type to a nullable value type |
| 49 | + /// </summary> |
| 50 | + public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) |
| 51 | + { |
| 52 | + if (targetType != typeof(TTarget?) && !targetType.IsAssignableTo(typeof(TTarget))) |
| 53 | + { |
| 54 | + // ReSharper disable once LocalizableElement |
| 55 | + throw new ArgumentException( |
| 56 | + $"Convert Target type {targetType.Name} must be assignable to {typeof(TTarget).Name}" |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + return (TTarget?)System.Convert.ChangeType(value, typeof(TTarget)); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Convert a nullable value type to a value type |
| 65 | + /// </summary> |
| 66 | + public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) |
| 67 | + { |
| 68 | + if (!targetType.IsAssignableTo(typeof(TSource))) |
| 69 | + { |
| 70 | + // ReSharper disable once LocalizableElement |
| 71 | + throw new ArgumentException( |
| 72 | + $"ConvertBack Target type {targetType.Name} must be assignable to {typeof(TSource).Name}" |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + return Unbox((TTarget?)value); |
| 77 | + } |
| 78 | + |
| 79 | + public enum ReturnBehavior |
| 80 | + { |
| 81 | + DefaultValue, |
| 82 | + Throw |
| 83 | + } |
| 84 | +} |
0 commit comments