|
| 1 | +namespace Python.Runtime.Codecs |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.Generic; |
| 5 | + using System.Linq; |
| 6 | + using System.Reflection; |
| 7 | + |
| 8 | + public sealed class TupleCodec<TTuple> : IPyObjectEncoder, IPyObjectDecoder |
| 9 | + { |
| 10 | + TupleCodec() { } |
| 11 | + public static TupleCodec<TTuple> Instance { get; } = new TupleCodec<TTuple>(); |
| 12 | + |
| 13 | + public bool CanEncode(Type type) |
| 14 | + => type.Namespace == typeof(TTuple).Namespace && type.Name.StartsWith(typeof(TTuple).Name + '`') |
| 15 | + || type == typeof(object) || type == typeof(TTuple); |
| 16 | + |
| 17 | + public PyObject TryEncode(object value) |
| 18 | + { |
| 19 | + if (value == null) return null; |
| 20 | + |
| 21 | + var tupleType = value.GetType(); |
| 22 | + if (tupleType == typeof(object)) return null; |
| 23 | + if (!this.CanEncode(tupleType)) return null; |
| 24 | + if (tupleType == typeof(TTuple)) return new PyTuple(); |
| 25 | + |
| 26 | + long fieldCount = tupleType.GetGenericArguments().Length; |
| 27 | + var tuple = Runtime.PyTuple_New(fieldCount); |
| 28 | + Exceptions.ErrorCheck(tuple); |
| 29 | + int fieldIndex = 0; |
| 30 | + foreach (FieldInfo field in tupleType.GetFields()) |
| 31 | + { |
| 32 | + var item = field.GetValue(value); |
| 33 | + IntPtr pyItem = Converter.ToPython(item); |
| 34 | + Runtime.XIncref(pyItem); |
| 35 | + Runtime.PyTuple_SetItem(tuple, fieldIndex, pyItem); |
| 36 | + fieldIndex++; |
| 37 | + } |
| 38 | + return new PyTuple(Runtime.SelfIncRef(tuple)); |
| 39 | + } |
| 40 | + |
| 41 | + public bool CanDecode(PyObject objectType, Type targetType) |
| 42 | + => objectType.Handle == Runtime.PyTuple && this.CanEncode(targetType); |
| 43 | + |
| 44 | + public bool TryDecode<T>(PyObject pyObj, out T value) |
| 45 | + { |
| 46 | + if (pyObj == null) throw new ArgumentNullException(nameof(pyObj)); |
| 47 | + |
| 48 | + value = default; |
| 49 | + |
| 50 | + if (!Runtime.PyTuple_Check(pyObj.Handle)) return false; |
| 51 | + |
| 52 | + if (typeof(T) == typeof(object)) |
| 53 | + { |
| 54 | + bool converted = Decode(pyObj, out object result); |
| 55 | + if (converted) |
| 56 | + { |
| 57 | + value = (T)result; |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + var itemTypes = typeof(T).GetGenericArguments(); |
| 65 | + long itemCount = Runtime.PyTuple_Size(pyObj.Handle); |
| 66 | + if (itemTypes.Length != itemCount) return false; |
| 67 | + |
| 68 | + if (itemCount == 0) |
| 69 | + { |
| 70 | + value = (T)EmptyTuple; |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + var elements = new object[itemCount]; |
| 75 | + for (int itemIndex = 0; itemIndex < itemTypes.Length; itemIndex++) |
| 76 | + { |
| 77 | + IntPtr pyItem = Runtime.PyTuple_GetItem(pyObj.Handle, itemIndex); |
| 78 | + if (!Converter.ToManaged(pyItem, itemTypes[itemIndex], out elements[itemIndex], setError: false)) |
| 79 | + { |
| 80 | + return false; |
| 81 | + } |
| 82 | + } |
| 83 | + var factory = tupleCreate[itemCount].MakeGenericMethod(itemTypes); |
| 84 | + value = (T)factory.Invoke(null, elements); |
| 85 | + return true; |
| 86 | + } |
| 87 | + |
| 88 | + static bool Decode(PyObject tuple, out object value) |
| 89 | + { |
| 90 | + long itemCount = Runtime.PyTuple_Size(tuple.Handle); |
| 91 | + if (itemCount == 0) |
| 92 | + { |
| 93 | + value = EmptyTuple; |
| 94 | + return true; |
| 95 | + } |
| 96 | + var elements = new object[itemCount]; |
| 97 | + var itemTypes = new Type[itemCount]; |
| 98 | + value = null; |
| 99 | + for (int itemIndex = 0; itemIndex < elements.Length; itemIndex++) |
| 100 | + { |
| 101 | + var pyItem = Runtime.PyTuple_GetItem(tuple.Handle, itemIndex); |
| 102 | + if (!Converter.ToManaged(pyItem, typeof(object), out elements[itemIndex], setError: false)) |
| 103 | + { |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + itemTypes[itemIndex] = elements[itemIndex]?.GetType() ?? typeof(object); |
| 108 | + } |
| 109 | + |
| 110 | + var factory = tupleCreate[itemCount].MakeGenericMethod(itemTypes); |
| 111 | + value = factory.Invoke(null, elements); |
| 112 | + return true; |
| 113 | + } |
| 114 | + |
| 115 | + static readonly MethodInfo[] tupleCreate = |
| 116 | + typeof(TTuple).GetMethods(BindingFlags.Public | BindingFlags.Static) |
| 117 | + .Where(m => m.Name == nameof(Tuple.Create)) |
| 118 | + .OrderBy(m => m.GetParameters().Length) |
| 119 | + .ToArray(); |
| 120 | + |
| 121 | + static readonly object EmptyTuple = tupleCreate[0].Invoke(null, parameters: new object[0]); |
| 122 | + |
| 123 | + public static void Register() |
| 124 | + { |
| 125 | + PyObjectConversions.RegisterEncoder(Instance); |
| 126 | + PyObjectConversions.RegisterDecoder(Instance); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments