Skip to content

Commit ca74a96

Browse files
authored
Merge pull request #75 from linksplatform/Auto-translation-added
Translation by CSharpToCppTranslator
2 parents 892fad6 + ab04028 commit ca74a96

File tree

11 files changed

+381
-0
lines changed

11 files changed

+381
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace Platform::Converters::Benchmarks
2+
{
3+
class Int32ToUInt64ConverterBenchmarks
4+
{
5+
private: static UncheckedConverter<std::int32_t, std::uint64_t> _int32ToUInt64converter;
6+
private: static UncheckedConverter<void*, std::uint64_t> _objectToUInt64Converter;
7+
private: static IFormatProvider *_formatProvider;
8+
9+
public: static void Setup()
10+
{
11+
_int32ToUInt64converter = UncheckedConverter<std::int32_t, std::uint64_t>.Default;
12+
_objectToUInt64Converter = UncheckedConverter<void*, std::uint64_t>.Default;
13+
_formatProvider = CultureInfo.InvariantCulture;
14+
}
15+
16+
public: static std::uint64_t ConverterWrapperWithNoInlining(std::int32_t value) { return _int32ToUInt64converter.Convert(value); }
17+
18+
public: static std::uint64_t ConverterWrapperWithAggressiveInlining(std::int32_t value) { return _int32ToUInt64converter.Convert(value); }
19+
20+
public: static std::uint64_t ConverterWrapper(std::int32_t value) { return _int32ToUInt64converter.Convert(value); }
21+
22+
public: std::uint64_t ConverterFromLocalStaticField() { return _int32ToUInt64converter.Convert(2); }
23+
24+
public: std::uint64_t ConverterFromGlobalStaticField() { return 2; }
25+
26+
public: std::uint64_t SystemConvertObjectToUInt64() { return Convert.ToUInt64((void*)2); }
27+
28+
public: std::uint64_t ConvertObjectToUInt64() { return _objectToUInt64Converter.Convert(2); }
29+
30+
public: std::uint64_t SystemConvertToUInt64() { return Convert.ToUInt64(2); }
31+
32+
public: std::uint64_t SystemConvertChangeType() { return (std::uint64_t)Convert.ChangeType(2, this->typeof(std::uint64_t)); }
33+
34+
public: std::uint64_t IConvertibleToUInt64() { return ((IConvertible)2).ToUInt64(_formatProvider); }
35+
36+
public: std::uint64_t IConvertibleToType() { return (std::uint64_t)((IConvertible)2).ToType(this->typeof(std::uint64_t), _formatProvider); }
37+
38+
public: std::uint64_t StaticFunctionWithoutInlining() { return this->ConverterWrapperWithNoInlining(2); }
39+
40+
public: std::uint64_t StaticFunctionWithAggressiveInlining() { return this->ConverterWrapperWithAggressiveInlining(2); }
41+
42+
public: std::uint64_t StaticFunction() { return this->ConverterWrapper(2); }
43+
};
44+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Platform::Converters::Benchmarks
2+
{
3+
class Program
4+
{
5+
static void Main()
6+
{
7+
BenchmarkRunner.Run<Int32ToUInt64ConverterBenchmarks>();
8+
BenchmarkRunner.Run<UInt64ToUInt64ConverterBenchmarks>();
9+
}
10+
};
11+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace Platform::Converters::Benchmarks
2+
{
3+
class UInt64ToUInt64ConverterBenchmarks
4+
{
5+
private: static UncheckedConverter<std::uint64_t, std::uint64_t> _uInt64ToUInt64Converter;
6+
private: static IFormatProvider *_formatProvider;
7+
8+
public: static void Setup()
9+
{
10+
_uInt64ToUInt64Converter = UncheckedConverter<std::uint64_t, std::uint64_t>.Default;
11+
_formatProvider = CultureInfo.InvariantCulture;
12+
}
13+
14+
public: static std::uint64_t ConverterWrapperWithNoInlining(std::uint64_t value) { return _uInt64ToUInt64Converter.Convert(value); }
15+
16+
public: static std::uint64_t ConverterWrapperWithAggressiveInlining(std::uint64_t value) { return _uInt64ToUInt64Converter.Convert(value); }
17+
18+
public: static std::uint64_t ConverterWrapper(std::uint64_t value) { return _uInt64ToUInt64Converter.Convert(value); }
19+
20+
public: std::uint64_t ConverterFromLocalStaticField() { return _uInt64ToUInt64Converter.Convert(2UL); }
21+
22+
public: std::uint64_t ConverterFromGlobalStaticField() { return 2UL; }
23+
24+
public: std::uint64_t SystemConvertToUInt64() { return Convert.ToUInt64(2UL); }
25+
26+
public: std::uint64_t SystemConvertObjectToUInt64() { return Convert.ToUInt64((void*)2UL); }
27+
28+
public: std::uint64_t SystemConvertChangeType() { return (std::uint64_t)Convert.ChangeType(2UL, this->typeof(std::uint64_t)); }
29+
30+
public: std::uint64_t IConvertibleToUInt64() { return ((IConvertible)2UL).ToUInt64(_formatProvider); }
31+
32+
public: std::uint64_t IConvertibleToType() { return (std::uint64_t)((IConvertible)2UL).ToType(this->typeof(std::uint64_t), _formatProvider); }
33+
34+
public: std::uint64_t StaticFunctionWithoutInlining() { return this->ConverterWrapperWithNoInlining(2UL); }
35+
36+
public: std::uint64_t StaticFunctionWithAggressiveInlining() { return this->ConverterWrapperWithAggressiveInlining(2UL); }
37+
38+
public: std::uint64_t StaticFunction() { return this->ConverterWrapper(2UL); }
39+
};
40+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace Platform::Converters::Tests
2+
{
3+
TEST_CLASS(ConverterTests)
4+
{
5+
public: TEST_METHOD(SameTypeTest)
6+
{
7+
auto result = 2UL;
8+
Assert::AreEqual(2UL, result);
9+
result = CheckedConverter<std::uint64_t, std::uint64_t>.Default.Convert(2UL);
10+
Assert::AreEqual(2UL, result);
11+
}
12+
13+
public: TEST_METHOD(Int32ToUInt64Test)
14+
{
15+
auto result = 2;
16+
Assert::AreEqual(2UL, result);
17+
result = CheckedConverter<std::int32_t, std::uint64_t>.Default.Convert(2);
18+
Assert::AreEqual(2UL, result);
19+
}
20+
21+
public: TEST_METHOD(SignExtensionTest)
22+
{
23+
auto result = UncheckedSignExtendingConverter<std::uint8_t, std::int64_t>.Default.Convert(128);
24+
Assert::AreEqual(-128L, result);
25+
result = 128;
26+
Assert::AreEqual(128L, result);
27+
}
28+
29+
public: TEST_METHOD(ObjectTest)
30+
{
31+
TestObjectConversion("1");
32+
TestObjectConversion(DateTime.UtcNow);
33+
TestObjectConversion(1.0F);
34+
TestObjectConversion(1.0D);
35+
TestObjectConversion(1.0M);
36+
TestObjectConversion(1UL);
37+
TestObjectConversion(1L);
38+
TestObjectConversion(1U);
39+
TestObjectConversion(1);
40+
TestObjectConversion((char)1);
41+
TestObjectConversion((std::uint16_t)1);
42+
TestObjectConversion((std::int16_t)1);
43+
TestObjectConversion((std::uint8_t)1);
44+
TestObjectConversion((std::int8_t)1);
45+
TestObjectConversion(true);
46+
}
47+
48+
private: template <typename T> static void TestObjectConversion(T value) { Assert::AreEqual(value, value); }
49+
};
50+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Platform::Converters
2+
{
3+
template <typename ...> class CachingConverterDecorator;
4+
template <typename TSource, typename TTarget> class CachingConverterDecorator<TSource, TTarget> : public IConverter<TSource, TTarget>
5+
{
6+
private: readonly IConverter<TSource, TTarget> *_baseConverter;
7+
private: readonly IDictionary<TSource, TTarget> *_cache;
8+
9+
public: CachingConverterDecorator(IConverter<TSource, TTarget> &baseConverter, IDictionary<TSource, TTarget> &cache) { (_baseConverter, _cache) = (baseConverter, cache); }
10+
11+
public: CachingConverterDecorator(IConverter<TSource, TTarget> &baseConverter) : this(baseConverter, Dictionary<TSource, TTarget>()) { }
12+
13+
public: TTarget Convert(TSource source) { return _cache.GetOrAdd(source, _baseConverter.Convert); }
14+
};
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Platform::Converters
2+
{
3+
template <typename ...> class CheckedConverter;
4+
template <typename TSource, typename TTarget> class CheckedConverter<TSource, TTarget> : public ConverterBase<TSource, TTarget>
5+
{
6+
public: static CheckedConverter<TSource, TTarget> Default
7+
{
8+
get;
9+
} = CompileCheckedConverter();
10+
11+
private: static CheckedConverter<TSource, TTarget> CompileCheckedConverter()
12+
{
13+
auto type = CreateTypeInheritedFrom<CheckedConverter<TSource, TTarget>>();
14+
EmitConvertMethod(type, il => il.CheckedConvert<TSource, TTarget>());
15+
return (CheckedConverter<TSource, TTarget>)Activator.CreateInstance(type.CreateTypeInfo());
16+
}
17+
};
18+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Platform::Converters
2+
{
3+
template <typename ...> class IConverter;
4+
template <typename TSource, typename TTarget> class IConverter<TSource, TTarget>
5+
{
6+
public:
7+
virtual TTarget Convert(TSource source) = 0;
8+
};
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Platform::Converters
2+
{
3+
template <typename ...> class IConverter;
4+
template <typename T> class IConverter<T> : public IConverter<T, T>
5+
{
6+
public:
7+
};
8+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Platform::Converters
2+
{
3+
template <typename ...> class UncheckedConverter;
4+
template <typename TSource, typename TTarget> class UncheckedConverter<TSource, TTarget> : public ConverterBase<TSource, TTarget>
5+
{
6+
public: static UncheckedConverter<TSource, TTarget> Default
7+
{
8+
get;
9+
} = CompileUncheckedConverter();
10+
11+
private: static UncheckedConverter<TSource, TTarget> CompileUncheckedConverter()
12+
{
13+
auto type = CreateTypeInheritedFrom<UncheckedConverter<TSource, TTarget>>();
14+
EmitConvertMethod(type, il => il.UncheckedConvert<TSource, TTarget>());
15+
return (UncheckedConverter<TSource, TTarget>)Activator.CreateInstance(type.CreateTypeInfo());
16+
}
17+
};
18+
}

0 commit comments

Comments
 (0)