|
| 1 | +namespace McMaster.Extensions.CommandLineUtils.Abstractions |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.Generic; |
| 5 | + using System.ComponentModel; |
| 6 | + using System.Diagnostics; |
| 7 | + using System.Globalization; |
| 8 | + using System.Text; |
| 9 | + |
| 10 | + /// <summary> |
| 11 | + /// A factory creating generic implementations of <see cref="IValueParser{T}"/>. The implementations are based |
| 12 | + /// on automatically located <see cref="TypeConverter"/> classes that are suitable for parsing. |
| 13 | + /// </summary> |
| 14 | + sealed class DefaultValueParserFactory |
| 15 | + { |
| 16 | + const int DefaultMaxCacheCapacity = 100; |
| 17 | + |
| 18 | + #region Private Fields |
| 19 | + |
| 20 | + private readonly int _maxCacheCapacity; |
| 21 | + |
| 22 | + [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] |
| 23 | + readonly Dictionary<Type, IValueParser> _parsersByTargetType = new Dictionary<Type, IValueParser>(); |
| 24 | + |
| 25 | + [DebuggerBrowsable(DebuggerBrowsableState.Never)] |
| 26 | + readonly HashSet<Type> _notSupportedTypes = new HashSet<Type>(); |
| 27 | + |
| 28 | + #endregion |
| 29 | + |
| 30 | + public DefaultValueParserFactory(int maxCacheCapacity = DefaultMaxCacheCapacity) |
| 31 | + { |
| 32 | + _maxCacheCapacity = maxCacheCapacity; |
| 33 | + if (_maxCacheCapacity < 1) throw new ArgumentOutOfRangeException(nameof(maxCacheCapacity)); |
| 34 | + } |
| 35 | + |
| 36 | + [DebuggerStepThrough] |
| 37 | + public bool TryGetParser<T>(out IValueParser<T> parser) |
| 38 | + { |
| 39 | + if (TryGetParser<T>(out IValueParser generalizedParser)) |
| 40 | + { |
| 41 | + parser = (IValueParser<T>)generalizedParser; |
| 42 | + return true; |
| 43 | + } |
| 44 | + |
| 45 | + parser = null; |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + public bool TryGetParser<T>(out IValueParser parser) |
| 50 | + { |
| 51 | + var targetType = typeof(T); |
| 52 | + if (_notSupportedTypes.Contains(targetType)) |
| 53 | + { |
| 54 | + parser = null; |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + if (_parsersByTargetType.TryGetValue(targetType, out parser)) |
| 59 | + { |
| 60 | + Debug.Assert(targetType == parser.TargetType); |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + var converter = TypeDescriptor.GetConverter(targetType); |
| 65 | + if (converter.CanConvertFrom(typeof(string))) |
| 66 | + { |
| 67 | + if (_parsersByTargetType.Count >= _maxCacheCapacity) |
| 68 | + _parsersByTargetType.Clear(); |
| 69 | + parser = new TypeConverterValueParser<T>(targetType, converter); |
| 70 | + _parsersByTargetType[targetType] = parser; |
| 71 | + Debug.Assert(_parsersByTargetType.Count <= _maxCacheCapacity); |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + parser = null; |
| 76 | + if (_notSupportedTypes.Count > _maxCacheCapacity) |
| 77 | + _notSupportedTypes.Clear(); |
| 78 | + _notSupportedTypes.Add(targetType); |
| 79 | + Debug.Assert(_notSupportedTypes.Count <= _maxCacheCapacity); |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + public IValueParser<T> GetParser<T>() |
| 84 | + { |
| 85 | + return TryGetParser<T>(out IValueParser<T> converter) |
| 86 | + ? converter |
| 87 | + : throw new NotSupportedException( |
| 88 | + new StringBuilder($"No suitable type converter found for {typeof(T)}.") |
| 89 | + .Append($" Make sure a type converter capable of parsing {typeof(string)} to {typeof(T)} exists and is discoverable.") |
| 90 | + .Append($" Did you forget to annotate the target type with {typeof(TypeConverterAttribute)}?") |
| 91 | + .ToString()); |
| 92 | + } |
| 93 | + |
| 94 | + private sealed class TypeConverterValueParser<T> : IValueParser<T> |
| 95 | + { |
| 96 | + public TypeConverterValueParser(Type targetType, TypeConverter typeConverter) |
| 97 | + { |
| 98 | + TargetType = targetType ?? throw new ArgumentNullException(nameof(targetType)); |
| 99 | + TypeConverter = typeConverter ?? throw new ArgumentNullException(nameof(typeConverter)); |
| 100 | + } |
| 101 | + |
| 102 | + public Type TargetType { get; } |
| 103 | + |
| 104 | + private TypeConverter TypeConverter { get; } |
| 105 | + |
| 106 | + public T Parse(string argName, string value, CultureInfo culture) |
| 107 | + { |
| 108 | + try |
| 109 | + { |
| 110 | + culture ??= CultureInfo.InvariantCulture; |
| 111 | + return (T)TypeConverter.ConvertFromString(null, culture, value); |
| 112 | + } |
| 113 | + catch (ArgumentException e) |
| 114 | + { |
| 115 | + throw new FormatException(e.Message, e); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + object IValueParser.Parse(string argName, string value, CultureInfo culture) => Parse(argName, value, culture); |
| 120 | + } |
| 121 | + |
| 122 | + } |
| 123 | +} |
0 commit comments