2
2
using System . Linq ;
3
3
using System . Reflection ;
4
4
using System . Runtime . CompilerServices ;
5
+ using Platform . Exceptions ;
5
6
using Platform . Reflection ;
6
7
using Platform . Reflection . Sigil ;
7
8
8
9
namespace Platform . Converters
9
10
{
10
11
/// <remarks>
11
- /// Shorter version of ConvertationHelpers.
12
- /// Укороченная версия от ConvertationHelpers.
13
- ///
14
12
/// Возможно нужно несколько разных способов разрешения конфликта.
15
13
/// ExceptionConflictReation (выбрасывает исключение, если обнаружен конфликт, не предпринимая никаких действий)
16
14
/// > Max: Max (если число больше его максимального размера в этом размере, то берём максимальное)
17
15
/// > Max: 0 (если число больше его максимального размера в этом размере, то берём обнуляем)
18
16
/// и т.п. (например определённые пользователем)
19
17
///
20
- /// Текущая логика алгоритма "Closest"
18
+ /// Текущая логика алгоритма "Closest value "
21
19
/// </remarks>
22
20
public static class To
23
21
{
@@ -33,7 +31,7 @@ static SignProcessor()
33
31
{
34
32
ToSignedFunc = DelegateHelpers . Compile < Func < T , object > > ( emiter =>
35
33
{
36
- EnsureSupportedUnsignedType ( ) ;
34
+ Ensure . Always . IsUnsignedInteger < T > ( ) ;
37
35
emiter . LoadArgument ( 0 ) ;
38
36
var method = typeof ( To ) . GetTypeInfo ( ) . GetMethod ( "Signed" , Types < T > . Array . ToArray ( ) ) ;
39
37
emiter . Call ( method ) ;
@@ -43,7 +41,7 @@ static SignProcessor()
43
41
44
42
ToUnsignedFunc = DelegateHelpers . Compile < Func < T , object > > ( emiter =>
45
43
{
46
- EnsureSupportedSignedType ( ) ;
44
+ Ensure . Always . IsSignedInteger < T > ( ) ;
47
45
emiter . LoadArgument ( 0 ) ;
48
46
var method = typeof ( To ) . GetTypeInfo ( ) . GetMethod ( "Unsigned" , Types < T > . Array . ToArray ( ) ) ;
49
47
emiter . Call ( method ) ;
@@ -53,7 +51,7 @@ static SignProcessor()
53
51
54
52
ToUnsignedAsFunc = DelegateHelpers . Compile < Func < object , T > > ( emiter =>
55
53
{
56
- EnsureSupportedUnsignedType ( ) ;
54
+ Ensure . Always . IsUnsignedInteger < T > ( ) ;
57
55
emiter . LoadArgument ( 0 ) ;
58
56
var signedVersion = CachedTypeInfo < T > . SignedVersion ;
59
57
emiter . UnboxAny ( signedVersion ) ;
@@ -62,24 +60,6 @@ static SignProcessor()
62
60
emiter . Return ( ) ;
63
61
} ) ;
64
62
}
65
-
66
- private static void EnsureSupportedUnsignedType ( )
67
- {
68
- var type = typeof ( T ) ;
69
- if ( type != typeof ( ulong ) && type != typeof ( uint ) && type != typeof ( ushort ) && type != typeof ( byte ) )
70
- {
71
- throw new NotSupportedException ( ) ;
72
- }
73
- }
74
-
75
- private static void EnsureSupportedSignedType ( )
76
- {
77
- var type = typeof ( T ) ;
78
- if ( type != typeof ( long ) && type != typeof ( int ) && type != typeof ( short ) && type != typeof ( sbyte ) )
79
- {
80
- throw new NotSupportedException ( ) ;
81
- }
82
- }
83
63
}
84
64
85
65
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
@@ -136,26 +116,37 @@ private static void EnsureSupportedSignedType()
136
116
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
137
117
public static ulong UInt64 ( char value ) => value ;
138
118
119
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
139
120
public static long Signed ( ulong value ) => ( long ) value ;
140
121
122
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
141
123
public static int Signed ( uint value ) => ( int ) value ;
142
124
125
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
143
126
public static short Signed ( ushort value ) => ( short ) value ;
144
127
128
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
145
129
public static sbyte Signed ( byte value ) => ( sbyte ) value ;
146
130
131
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
147
132
public static object Signed < T > ( T value ) => SignProcessor < T > . ToSignedFunc ( value ) ;
148
133
134
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
149
135
public static ulong Unsigned ( long value ) => ( ulong ) value ;
150
136
137
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
151
138
public static uint Unsigned ( int value ) => ( uint ) value ;
152
139
140
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
153
141
public static ushort Unsigned ( short value ) => ( ushort ) value ;
154
142
143
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
155
144
public static byte Unsigned ( sbyte value ) => ( byte ) value ;
156
145
146
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
157
147
public static object Unsigned < T > ( T value ) => SignProcessor < T > . ToUnsignedFunc ( value ) ;
158
148
149
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
159
150
public static T UnsignedAs < T > ( object value ) => SignProcessor < T > . ToUnsignedAsFunc ( value ) ;
160
151
}
161
152
}
0 commit comments