Skip to content

Commit 8142d07

Browse files
committed
Added Converts<T>
1 parent 6908ddd commit 8142d07

File tree

1 file changed

+326
-0
lines changed

1 file changed

+326
-0
lines changed
Lines changed: 326 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
4+
namespace NumSharp.Utilities
5+
{
6+
/// <summary>
7+
/// Provides various methods related to <see cref="System.Convert"/> based on give <typeparamref name="T"/>.
8+
/// </summary>
9+
public static class Converts<T>
10+
{
11+
static Converts()
12+
{
13+
if (typeof(T).GetTypeCode() == NPTypeCode.Empty)
14+
throw new NotSupportedException($"Unable to perform conversions in Converts<T> for type {typeof(T).Name}");
15+
}
16+
17+
#region Cached Converters
18+
19+
#if _REGEN
20+
#region Compute
21+
%foreach supported_dtypes,supported_dtypes_lowercase%
22+
/// <summary>
23+
/// Converts <typeparamref name="T"/> to <see cref="#1"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
24+
/// </summary>
25+
/// <param name="obj">The object to convert to <see cref="#1"/></param>
26+
/// <returns>A <see cref="#1"/></returns>
27+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static #2 To#1(T obj) => _to#1(obj);
28+
private static readonly Func<T, #2> _to#1 = Converts.FindConverter<T, #2>();
29+
30+
%
31+
/// <summary>
32+
/// Converts <typeparamref name="T"/> to <see cref="String"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
33+
/// </summary>
34+
/// <param name="obj">The object to convert to <see cref="String"/></param>
35+
/// <returns>A <see cref="String"/></returns>
36+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static String ToString(T obj) => _toString(obj);
37+
private static readonly Func<T, string> _toString = Converts.FindConverter<T, string>();
38+
#endregion
39+
#else
40+
41+
#region Compute
42+
43+
/// <summary>
44+
/// Converts <typeparamref name="T"/> to <see cref="bool"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
45+
/// </summary>
46+
/// <param name="obj">The object to convert to <see cref="bool"/></param>
47+
/// <returns>A <see cref="bool"/></returns>
48+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
49+
public static bool ToBoolean(T obj) => _toBoolean(obj);
50+
51+
private static readonly Func<T, bool> _toBoolean = Converts.FindConverter<T, bool>();
52+
53+
/// <summary>
54+
/// Converts <typeparamref name="T"/> to <see cref="Byte"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
55+
/// </summary>
56+
/// <param name="obj">The object to convert to <see cref="Byte"/></param>
57+
/// <returns>A <see cref="Byte"/></returns>
58+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
59+
public static byte ToByte(T obj) => _toByte(obj);
60+
61+
private static readonly Func<T, byte> _toByte = Converts.FindConverter<T, byte>();
62+
63+
/// <summary>
64+
/// Converts <typeparamref name="T"/> to <see cref="Int16"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
65+
/// </summary>
66+
/// <param name="obj">The object to convert to <see cref="Int16"/></param>
67+
/// <returns>A <see cref="Int16"/></returns>
68+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
69+
public static short ToInt16(T obj) => _toInt16(obj);
70+
71+
private static readonly Func<T, short> _toInt16 = Converts.FindConverter<T, short>();
72+
73+
/// <summary>
74+
/// Converts <typeparamref name="T"/> to <see cref="UInt16"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
75+
/// </summary>
76+
/// <param name="obj">The object to convert to <see cref="UInt16"/></param>
77+
/// <returns>A <see cref="UInt16"/></returns>
78+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
79+
public static ushort ToUInt16(T obj) => _toUInt16(obj);
80+
81+
private static readonly Func<T, ushort> _toUInt16 = Converts.FindConverter<T, ushort>();
82+
83+
/// <summary>
84+
/// Converts <typeparamref name="T"/> to <see cref="Int32"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
85+
/// </summary>
86+
/// <param name="obj">The object to convert to <see cref="Int32"/></param>
87+
/// <returns>A <see cref="Int32"/></returns>
88+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
89+
public static int ToInt32(T obj) => _toInt32(obj);
90+
91+
private static readonly Func<T, int> _toInt32 = Converts.FindConverter<T, int>();
92+
93+
/// <summary>
94+
/// Converts <typeparamref name="T"/> to <see cref="UInt32"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
95+
/// </summary>
96+
/// <param name="obj">The object to convert to <see cref="UInt32"/></param>
97+
/// <returns>A <see cref="UInt32"/></returns>
98+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
99+
public static uint ToUInt32(T obj) => _toUInt32(obj);
100+
101+
private static readonly Func<T, uint> _toUInt32 = Converts.FindConverter<T, uint>();
102+
103+
/// <summary>
104+
/// Converts <typeparamref name="T"/> to <see cref="Int64"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
105+
/// </summary>
106+
/// <param name="obj">The object to convert to <see cref="Int64"/></param>
107+
/// <returns>A <see cref="Int64"/></returns>
108+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
109+
public static long ToInt64(T obj) => _toInt64(obj);
110+
111+
private static readonly Func<T, long> _toInt64 = Converts.FindConverter<T, long>();
112+
113+
/// <summary>
114+
/// Converts <typeparamref name="T"/> to <see cref="UInt64"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
115+
/// </summary>
116+
/// <param name="obj">The object to convert to <see cref="UInt64"/></param>
117+
/// <returns>A <see cref="UInt64"/></returns>
118+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
119+
public static ulong ToUInt64(T obj) => _toUInt64(obj);
120+
121+
private static readonly Func<T, ulong> _toUInt64 = Converts.FindConverter<T, ulong>();
122+
123+
/// <summary>
124+
/// Converts <typeparamref name="T"/> to <see cref="Char"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
125+
/// </summary>
126+
/// <param name="obj">The object to convert to <see cref="Char"/></param>
127+
/// <returns>A <see cref="Char"/></returns>
128+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
129+
public static char ToChar(T obj) => _toChar(obj);
130+
131+
private static readonly Func<T, char> _toChar = Converts.FindConverter<T, char>();
132+
133+
/// <summary>
134+
/// Converts <typeparamref name="T"/> to <see cref="Double"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
135+
/// </summary>
136+
/// <param name="obj">The object to convert to <see cref="Double"/></param>
137+
/// <returns>A <see cref="Double"/></returns>
138+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
139+
public static double ToDouble(T obj) => _toDouble(obj);
140+
141+
private static readonly Func<T, double> _toDouble = Converts.FindConverter<T, double>();
142+
143+
/// <summary>
144+
/// Converts <typeparamref name="T"/> to <see cref="Single"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
145+
/// </summary>
146+
/// <param name="obj">The object to convert to <see cref="Single"/></param>
147+
/// <returns>A <see cref="Single"/></returns>
148+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
149+
public static float ToSingle(T obj) => _toSingle(obj);
150+
151+
private static readonly Func<T, float> _toSingle = Converts.FindConverter<T, float>();
152+
153+
/// <summary>
154+
/// Converts <typeparamref name="T"/> to <see cref="Decimal"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
155+
/// </summary>
156+
/// <param name="obj">The object to convert to <see cref="Decimal"/></param>
157+
/// <returns>A <see cref="Decimal"/></returns>
158+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
159+
public static decimal ToDecimal(T obj) => _toDecimal(obj);
160+
161+
private static readonly Func<T, decimal> _toDecimal = Converts.FindConverter<T, decimal>();
162+
163+
/// <summary>
164+
/// Converts <typeparamref name="T"/> to <see cref="UString"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
165+
/// </summary>
166+
/// <param name="obj">The object to convert to <see cref="UString"/></param>
167+
/// <returns>A <see cref="UString"/></returns>
168+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
169+
public static UString ToUString(T obj) => _toUString(obj);
170+
171+
private static readonly Func<T, UString> _toUString = Converts.FindConverter<T, UString>();
172+
173+
/// <summary>
174+
/// Converts <typeparamref name="T"/> to <see cref="String"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
175+
/// </summary>
176+
/// <param name="obj">The object to convert to <see cref="String"/></param>
177+
/// <returns>A <see cref="String"/></returns>
178+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
179+
public static String ToString(T obj) => _toString(obj);
180+
181+
private static readonly Func<T, string> _toString = Converts.FindConverter<T, string>();
182+
183+
#endregion
184+
185+
#endif
186+
187+
#if _REGEN
188+
#region Compute
189+
%foreach supported_dtypes,supported_dtypes_lowercase%
190+
/// <summary>
191+
/// Converts <see cref="#1"/> to <typeparamref name="T"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
192+
/// </summary>
193+
/// <param name="obj">The object to convert to <typeparamref name="T"/> from <see cref="#1"/></param>
194+
/// <returns>A <typeparamref name="T"/></returns>
195+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static T From(#2 obj) => _from#1(obj);
196+
private static readonly Func<#2, T> _from#1 = Converts.FindConverter<#2, T>();
197+
198+
%
199+
/// <summary>
200+
/// Converts <see cref="String"/> to <typeparamref name="T"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
201+
/// </summary>
202+
/// <param name="obj">The object to convert to <typeparamref name="T"/> from <see cref="String"/></param>
203+
/// <returns>A <typeparamref name="T"/></returns>
204+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static T From(string obj) => _fromString(obj);
205+
private static readonly Func<string, T> _fromString = Converts.FindConverter<string, T>();
206+
#endregion
207+
#else
208+
209+
#region Compute
210+
/// <summary>
211+
/// Converts <see cref="Boolean"/> to <typeparamref name="T"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
212+
/// </summary>
213+
/// <param name="obj">The object to convert to <typeparamref name="T"/> from <see cref="Boolean"/></param>
214+
/// <returns>A <typeparamref name="T"/></returns>
215+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static T From(bool obj) => _fromBoolean(obj);
216+
private static readonly Func<bool, T> _fromBoolean = Converts.FindConverter<bool, T>();
217+
218+
/// <summary>
219+
/// Converts <see cref="Byte"/> to <typeparamref name="T"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
220+
/// </summary>
221+
/// <param name="obj">The object to convert to <typeparamref name="T"/> from <see cref="Byte"/></param>
222+
/// <returns>A <typeparamref name="T"/></returns>
223+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static T From(byte obj) => _fromByte(obj);
224+
private static readonly Func<byte, T> _fromByte = Converts.FindConverter<byte, T>();
225+
226+
/// <summary>
227+
/// Converts <see cref="Int16"/> to <typeparamref name="T"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
228+
/// </summary>
229+
/// <param name="obj">The object to convert to <typeparamref name="T"/> from <see cref="Int16"/></param>
230+
/// <returns>A <typeparamref name="T"/></returns>
231+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static T From(short obj) => _fromInt16(obj);
232+
private static readonly Func<short, T> _fromInt16 = Converts.FindConverter<short, T>();
233+
234+
/// <summary>
235+
/// Converts <see cref="UInt16"/> to <typeparamref name="T"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
236+
/// </summary>
237+
/// <param name="obj">The object to convert to <typeparamref name="T"/> from <see cref="UInt16"/></param>
238+
/// <returns>A <typeparamref name="T"/></returns>
239+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static T From(ushort obj) => _fromUInt16(obj);
240+
private static readonly Func<ushort, T> _fromUInt16 = Converts.FindConverter<ushort, T>();
241+
242+
/// <summary>
243+
/// Converts <see cref="Int32"/> to <typeparamref name="T"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
244+
/// </summary>
245+
/// <param name="obj">The object to convert to <typeparamref name="T"/> from <see cref="Int32"/></param>
246+
/// <returns>A <typeparamref name="T"/></returns>
247+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static T From(int obj) => _fromInt32(obj);
248+
private static readonly Func<int, T> _fromInt32 = Converts.FindConverter<int, T>();
249+
250+
/// <summary>
251+
/// Converts <see cref="UInt32"/> to <typeparamref name="T"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
252+
/// </summary>
253+
/// <param name="obj">The object to convert to <typeparamref name="T"/> from <see cref="UInt32"/></param>
254+
/// <returns>A <typeparamref name="T"/></returns>
255+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static T From(uint obj) => _fromUInt32(obj);
256+
private static readonly Func<uint, T> _fromUInt32 = Converts.FindConverter<uint, T>();
257+
258+
/// <summary>
259+
/// Converts <see cref="Int64"/> to <typeparamref name="T"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
260+
/// </summary>
261+
/// <param name="obj">The object to convert to <typeparamref name="T"/> from <see cref="Int64"/></param>
262+
/// <returns>A <typeparamref name="T"/></returns>
263+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static T From(long obj) => _fromInt64(obj);
264+
private static readonly Func<long, T> _fromInt64 = Converts.FindConverter<long, T>();
265+
266+
/// <summary>
267+
/// Converts <see cref="UInt64"/> to <typeparamref name="T"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
268+
/// </summary>
269+
/// <param name="obj">The object to convert to <typeparamref name="T"/> from <see cref="UInt64"/></param>
270+
/// <returns>A <typeparamref name="T"/></returns>
271+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static T From(ulong obj) => _fromUInt64(obj);
272+
private static readonly Func<ulong, T> _fromUInt64 = Converts.FindConverter<ulong, T>();
273+
274+
/// <summary>
275+
/// Converts <see cref="Char"/> to <typeparamref name="T"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
276+
/// </summary>
277+
/// <param name="obj">The object to convert to <typeparamref name="T"/> from <see cref="Char"/></param>
278+
/// <returns>A <typeparamref name="T"/></returns>
279+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static T From(char obj) => _fromChar(obj);
280+
private static readonly Func<char, T> _fromChar = Converts.FindConverter<char, T>();
281+
282+
/// <summary>
283+
/// Converts <see cref="Double"/> to <typeparamref name="T"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
284+
/// </summary>
285+
/// <param name="obj">The object to convert to <typeparamref name="T"/> from <see cref="Double"/></param>
286+
/// <returns>A <typeparamref name="T"/></returns>
287+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static T From(double obj) => _fromDouble(obj);
288+
private static readonly Func<double, T> _fromDouble = Converts.FindConverter<double, T>();
289+
290+
/// <summary>
291+
/// Converts <see cref="Single"/> to <typeparamref name="T"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
292+
/// </summary>
293+
/// <param name="obj">The object to convert to <typeparamref name="T"/> from <see cref="Single"/></param>
294+
/// <returns>A <typeparamref name="T"/></returns>
295+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static T From(float obj) => _fromSingle(obj);
296+
private static readonly Func<float, T> _fromSingle = Converts.FindConverter<float, T>();
297+
298+
/// <summary>
299+
/// Converts <see cref="Decimal"/> to <typeparamref name="T"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
300+
/// </summary>
301+
/// <param name="obj">The object to convert to <typeparamref name="T"/> from <see cref="Decimal"/></param>
302+
/// <returns>A <typeparamref name="T"/></returns>
303+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static T From(decimal obj) => _fromDecimal(obj);
304+
private static readonly Func<decimal, T> _fromDecimal = Converts.FindConverter<decimal, T>();
305+
306+
/// <summary>
307+
/// Converts <see cref="UString"/> to <typeparamref name="T"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
308+
/// </summary>
309+
/// <param name="obj">The object to convert to <typeparamref name="T"/> from <see cref="UString"/></param>
310+
/// <returns>A <typeparamref name="T"/></returns>
311+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static T From(UString obj) => _fromUString(obj);
312+
private static readonly Func<UString, T> _fromUString = Converts.FindConverter<UString, T>();
313+
314+
/// <summary>
315+
/// Converts <see cref="String"/> to <typeparamref name="T"/> using staticly cached <see cref="Converts.FindConverter{TIn,TOut}"/>.
316+
/// </summary>
317+
/// <param name="obj">The object to convert to <typeparamref name="T"/> from <see cref="String"/></param>
318+
/// <returns>A <typeparamref name="T"/></returns>
319+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static T From(string obj) => _fromString(obj);
320+
private static readonly Func<string, T> _fromString = Converts.FindConverter<string, T>();
321+
#endregion
322+
#endif
323+
324+
#endregion
325+
}
326+
}

0 commit comments

Comments
 (0)