|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using OpenSense.Components; |
| 7 | + |
| 8 | +namespace OpenSense.WPF.Pipeline { |
| 9 | + internal static class TypeDisplayHelpers { |
| 10 | + |
| 11 | + public static string FindInputDataTypeName(ComponentConfiguration configuration, IPortMetadata inputMetadata, IReadOnlyList<ComponentConfiguration> configurations) { |
| 12 | + Debug.Assert(inputMetadata.Direction == PortDirection.Input); |
| 13 | + var localOutputs = configuration.FindOutputPortDataTypes(configurations); |
| 14 | + var localInputs = configuration.FindInputPortDataTypes(configurations, exclude: inputMetadata); |
| 15 | + string result; |
| 16 | + if (inputMetadata.CanConnectDataType(null, localOutputs, localInputs)) { |
| 17 | + result = "Any Type"; |
| 18 | + } else { |
| 19 | + var inputPortDataType = inputMetadata.GetTransmissionDataType(null, localOutputs, localInputs); |
| 20 | + if (inputPortDataType is null) { |
| 21 | + result = "Type Unknown"; |
| 22 | + } else { |
| 23 | + result = GetCSharpStyleTypeName(inputPortDataType); |
| 24 | + } |
| 25 | + } |
| 26 | + return result; |
| 27 | + } |
| 28 | + |
| 29 | + public static string FindOutputDataTypeName(ComponentConfiguration configuration, IPortMetadata outputMetadata, IReadOnlyList<ComponentConfiguration> configurations) { |
| 30 | + Debug.Assert(outputMetadata.Direction == PortDirection.Output); |
| 31 | + var localInputs = configuration.FindInputPortDataTypes(configurations); |
| 32 | + var localOutputs = configuration.FindOutputPortDataTypes(configurations, exclude: outputMetadata); |
| 33 | + string result; |
| 34 | + if (outputMetadata.CanConnectDataType(null, localInputs, localOutputs)) { |
| 35 | + result = "Any Type"; |
| 36 | + } else { |
| 37 | + var outputPortDataType = outputMetadata.GetTransmissionDataType(null, localInputs, localOutputs); |
| 38 | + if (outputPortDataType is null) { |
| 39 | + result = "Type Unknown"; |
| 40 | + } else { |
| 41 | + result = GetCSharpStyleTypeName(outputPortDataType); |
| 42 | + } |
| 43 | + } |
| 44 | + return result; |
| 45 | + } |
| 46 | + |
| 47 | + private static string GetCSharpStyleTypeName(Type type) { |
| 48 | + if (TypeMappings.TryGetValue(type, out var name)) { |
| 49 | + return name; |
| 50 | + } |
| 51 | + if (type.IsArray) { |
| 52 | + return $"{GetCSharpStyleTypeName(type.GetElementType())}[]"; |
| 53 | + } |
| 54 | + name = $"{type.Namespace}.{type.Name}"; |
| 55 | + if (!type.IsGenericType) { |
| 56 | + return name; |
| 57 | + } |
| 58 | + var sb = new StringBuilder(); |
| 59 | + var innerText = string.Join(", ", type.GetGenericArguments().Select(GetCSharpStyleTypeName)); |
| 60 | + if (name.StartsWith("System.ValueTuple`")) { |
| 61 | + sb.Append('('); |
| 62 | + sb.Append(innerText); |
| 63 | + sb.Append(')'); |
| 64 | + } else { |
| 65 | + sb.Append(name.Substring(0, name.IndexOf('`'))); |
| 66 | + sb.Append('<'); |
| 67 | + sb.Append(innerText); |
| 68 | + sb.Append('>'); |
| 69 | + } |
| 70 | + var result = sb.ToString(); |
| 71 | + return result; |
| 72 | + } |
| 73 | + |
| 74 | + private static readonly Dictionary<Type, string> TypeMappings = new Dictionary<Type, string>() { |
| 75 | + { typeof(object), "object" }, |
| 76 | + { typeof(string), "string" }, |
| 77 | + { typeof(bool), "bool" }, |
| 78 | + { typeof(float), "float" }, |
| 79 | + { typeof(double), "double" }, |
| 80 | + { typeof(int), "int" }, |
| 81 | + { typeof(uint), "uint" }, |
| 82 | + { typeof(long), "long" }, |
| 83 | + { typeof(ulong), "ulong" }, |
| 84 | + { typeof(short), "short" }, |
| 85 | + { typeof(ushort), "ushort" }, |
| 86 | + |
| 87 | + }; |
| 88 | + } |
| 89 | +} |
0 commit comments