|
| 1 | +namespace Platform::Converters |
| 2 | +{ |
| 3 | + template <typename ...> class ConverterBase; |
| 4 | + template <typename TSource, typename TTarget> class ConverterBase<TSource, TTarget> : public IConverter<TSource, TTarget> |
| 5 | + { |
| 6 | + public: virtual TTarget Convert(TSource source) = 0; |
| 7 | + |
| 8 | + protected: static void ConvertFromObject(ILGenerator &il) |
| 9 | + { |
| 10 | + auto returnDefault = il.DefineLabel(); |
| 11 | + il.Emit(OpCodes.Brfalse_S, returnDefault); |
| 12 | + il.LoadArgument(1); |
| 13 | + il.Emit(OpCodes.Castclass, typeof(IConvertible)); |
| 14 | + il.Emit(OpCodes.Ldnull); |
| 15 | + il.Emit(OpCodes.Callvirt, GetMethodForConversionToTargetType()); |
| 16 | + il.Return(); |
| 17 | + il.MarkLabel(returnDefault); |
| 18 | + LoadDefault(il, typeof(TTarget)); |
| 19 | + } |
| 20 | + |
| 21 | + protected: static std::string GetNewName() { return Guid.NewGuid().ToString("N"); } |
| 22 | + |
| 23 | + protected: static TypeBuilder CreateTypeInheritedFrom<TBaseClass>() |
| 24 | + { |
| 25 | + auto assemblyName = AssemblyName(GetNewName()); |
| 26 | + auto assembly = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); |
| 27 | + auto module = assembly.DefineDynamicModule(GetNewName()); |
| 28 | + auto type = module.DefineType(GetNewName(), TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed, typeof(TBaseClass)); |
| 29 | + return type; |
| 30 | + } |
| 31 | + |
| 32 | + protected: static void EmitConvertMethod(TypeBuilder typeBuilder, std::function<void(ILGenerator)> emitConversion) |
| 33 | + { |
| 34 | + typeBuilder.EmitFinalVirtualMethod<Converter<TSource, TTarget>>("Convert", il => |
| 35 | + { |
| 36 | + il.LoadArgument(1); |
| 37 | + if (typeof(TSource) == typeof(void*) && typeof(TTarget) != typeof(void*)) |
| 38 | + { |
| 39 | + ConvertFromObject(il); |
| 40 | + } |
| 41 | + else if (typeof(TSource) != typeof(void*) && typeof(TTarget) == typeof(void*)) |
| 42 | + { |
| 43 | + il.Box(typeof(TSource)); |
| 44 | + } |
| 45 | + else |
| 46 | + { |
| 47 | + emitConversion(il); |
| 48 | + } |
| 49 | + il.Return(); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + protected: static MethodInfo GetMethodForConversionToTargetType() |
| 54 | + { |
| 55 | + auto targetType = typeof(TTarget); |
| 56 | + auto convertibleType = typeof(IConvertible); |
| 57 | + auto typeParameters = Types<IFormatProvider>.Array; |
| 58 | + if (targetType == typeof(bool)) |
| 59 | + { |
| 60 | + return convertibleType.GetMethod("ToBoolean", typeParameters); |
| 61 | + } |
| 62 | + else if (targetType == typeof(std::uint8_t)) |
| 63 | + { |
| 64 | + return convertibleType.GetMethod("ToByte", typeParameters); |
| 65 | + } |
| 66 | + else if (targetType == typeof(char)) |
| 67 | + { |
| 68 | + return convertibleType.GetMethod("ToChar", typeParameters); |
| 69 | + } |
| 70 | + else if (targetType == typeof(DateTime)) |
| 71 | + { |
| 72 | + return convertibleType.GetMethod("ToDateTime", typeParameters); |
| 73 | + } |
| 74 | + } |
| 75 | + else if (targetType == typeof(double)) |
| 76 | + { |
| 77 | + return convertibleType.GetMethod("ToDouble", typeParameters); |
| 78 | + } |
| 79 | + else if (targetType == typeof(std::int16_t)) |
| 80 | + { |
| 81 | + return convertibleType.GetMethod("ToInt16", typeParameters); |
| 82 | + } |
| 83 | + else if (targetType == typeof(std::int32_t)) |
| 84 | + { |
| 85 | + return convertibleType.GetMethod("ToInt32", typeParameters); |
| 86 | + } |
| 87 | + else if (targetType == typeof(std::int64_t)) |
| 88 | + { |
| 89 | + return convertibleType.GetMethod("ToInt64", typeParameters); |
| 90 | + } |
| 91 | + else if (targetType == typeof(std::int8_t)) |
| 92 | + { |
| 93 | + return convertibleType.GetMethod("ToSByte", typeParameters); |
| 94 | + } |
| 95 | + else if (targetType == typeof(float)) |
| 96 | + { |
| 97 | + return convertibleType.GetMethod("ToSingle", typeParameters); |
| 98 | + } |
| 99 | + else if (targetType == typeof(std::string)) |
| 100 | + { |
| 101 | + return convertibleType.GetMethod("ToString", typeParameters); |
| 102 | + } |
| 103 | + else if (targetType == typeof(std::uint16_t)) |
| 104 | + { |
| 105 | + return convertibleType.GetMethod("ToUInt16", typeParameters); |
| 106 | + } |
| 107 | + else if (targetType == typeof(std::uint32_t)) |
| 108 | + { |
| 109 | + return convertibleType.GetMethod("ToUInt32", typeParameters); |
| 110 | + } |
| 111 | + else if (targetType == typeof(std::uint64_t)) |
| 112 | + { |
| 113 | + return convertibleType.GetMethod("ToUInt64", typeParameters); |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + throw std::logic_error("Not supported exception."); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + protected: static void LoadDefault(ILGenerator &il, Type targetType) |
| 122 | + { |
| 123 | + if (targetType == typeof(std::string)) |
| 124 | + { |
| 125 | + il.Emit(OpCodes.Ldsfld, targetType.GetField("Empty", BindingFlags.Static | BindingFlags.Public)); |
| 126 | + } |
| 127 | + else if (targetType == typeof(DateTime)) |
| 128 | + { |
| 129 | + il.Emit(OpCodes.Ldsfld, targetType.GetField("MinValue", BindingFlags.Static | BindingFlags.Public)); |
| 130 | + } |
| 131 | + } |
| 132 | + else if (targetType == typeof(float)) |
| 133 | + { |
| 134 | + il.LoadConstant(0.0F); |
| 135 | + } |
| 136 | + else if (targetType == typeof(double)) |
| 137 | + { |
| 138 | + il.LoadConstant(0.0D); |
| 139 | + } |
| 140 | + else if (targetType == typeof(std::int64_t) || targetType == typeof(std::uint64_t)) |
| 141 | + { |
| 142 | + il.LoadConstant(0L); |
| 143 | + } |
| 144 | + else |
| 145 | + { |
| 146 | + il.LoadConstant(0); |
| 147 | + } |
| 148 | + } |
| 149 | + }; |
| 150 | +} |
0 commit comments