forked from SharpGenTools/SharpGenTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeLong.cs
More file actions
253 lines (212 loc) · 9.55 KB
/
Copy pathNativeLong.cs
File metadata and controls
253 lines (212 loc) · 9.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
using System;
using System.Runtime.InteropServices;
namespace SharpGen.Runtime;
[StructLayout(LayoutKind.Explicit)]
public readonly struct NativeLong : IEquatable<NativeLong>, IComparable<NativeLong>, IComparable
#if NET6_0_OR_GREATER
, IFormattable
#endif
{
[FieldOffset(0)]
private readonly nint _pointerValue;
[FieldOffset(0)]
private readonly int _intValue;
// See https://en.cppreference.com/w/cpp/language/types
internal static readonly bool UseInt = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
public NativeLong(int value)
{
if (UseInt)
{
_pointerValue = 0;
_intValue = value;
}
else
{
_intValue = 0;
_pointerValue = (nint) value;
}
}
public NativeLong(long value)
{
if (UseInt)
{
_pointerValue = 0;
_intValue = (int) value;
}
else
{
_intValue = 0;
_pointerValue = (nint) value;
}
}
public NativeLong(IntPtr value)
{
if (UseInt)
{
_pointerValue = 0;
_intValue = (int) value;
}
else
{
_intValue = 0;
_pointerValue = (nint) value;
}
}
/// <inheritdoc cref="object.ToString" />
public override string ToString() =>
UseInt ? _intValue.ToString() : _pointerValue.ToString();
public string ToString(string format) =>
UseInt ? _intValue.ToString(format) : _pointerValue.ToString(format);
#if NET6_0_OR_GREATER
public string ToString(IFormatProvider formatProvider) =>
UseInt ? _intValue.ToString(formatProvider) : _pointerValue.ToString(formatProvider);
/// <inheritdoc cref="IFormattable.ToString(string,System.IFormatProvider)" />
public string ToString(string format, IFormatProvider formatProvider) =>
UseInt ? _intValue.ToString(format, formatProvider) : _pointerValue.ToString(format, formatProvider);
#endif
/// <inheritdoc cref="object.GetHashCode" />
public override int GetHashCode() =>
UseInt ? _intValue.GetHashCode() : _pointerValue.GetHashCode();
/// <inheritdoc cref="IEquatable{T}.Equals(T)" />
public bool Equals(NativeLong other) =>
UseInt ? _intValue == other._intValue : _pointerValue.Equals(other._pointerValue);
/// <inheritdoc cref="object.Equals(object)" />
public override bool Equals(object obj) =>
obj switch
{
null => false,
NativeLong other => Equals(other),
int other => Equals(new NativeLong(other)),
long other => Equals(new NativeLong(other)),
IntPtr other => Equals(new NativeLong(other)),
_ => false
};
public static NativeLong operator +(NativeLong left, NativeLong right) =>
UseInt
? new NativeLong(left._intValue + right._intValue)
: new NativeLong(left._pointerValue + right._pointerValue);
public static NativeLong operator +(NativeLong value) => value;
public static NativeLong operator -(NativeLong left, NativeLong right) =>
UseInt
? new NativeLong(left._intValue - right._intValue)
: new NativeLong(left._pointerValue - right._pointerValue);
public static NativeLong operator -(NativeLong value) =>
UseInt
? new NativeLong(-value._intValue)
: new NativeLong(-value._pointerValue);
public static NativeLong operator *(NativeLong left, NativeLong right) =>
UseInt
? new NativeLong(left._intValue * right._intValue)
: new NativeLong(left._pointerValue * right._pointerValue);
public static NativeLong operator /(NativeLong left, NativeLong right) =>
UseInt
? new NativeLong(left._intValue / right._intValue)
: new NativeLong(left._pointerValue / right._pointerValue);
public static NativeLong operator %(NativeLong left, NativeLong right) =>
UseInt
? new NativeLong(left._intValue % right._intValue)
: new NativeLong(left._pointerValue % right._pointerValue);
public static NativeLong operator >>(NativeLong left, int right) =>
UseInt
? new NativeLong(left._intValue >> right)
: new NativeLong(left._pointerValue >> right);
public static NativeLong operator <<(NativeLong left, int right) =>
UseInt
? new NativeLong(left._intValue << right)
: new NativeLong(left._pointerValue << right);
public static NativeLong operator &(NativeLong left, NativeLong right) =>
UseInt
? new NativeLong(left._intValue & right._intValue)
: new NativeLong(left._pointerValue & right._pointerValue);
public static NativeLong operator |(NativeLong left, NativeLong right) =>
UseInt
? new NativeLong(left._intValue | right._intValue)
: new NativeLong(left._pointerValue | right._pointerValue);
public static NativeLong operator ^(NativeLong left, NativeLong right) =>
UseInt
? new NativeLong(left._intValue ^ right._intValue)
: new NativeLong(left._pointerValue ^ right._pointerValue);
public static NativeLong operator ~(NativeLong value) =>
UseInt
? new NativeLong(~value._intValue)
: new NativeLong(~value._pointerValue);
/// <summary>
/// Tests for equality between two objects.
/// </summary>
/// <param name = "left">The first value to compare.</param>
/// <param name = "right">The second value to compare.</param>
/// <returns><c>true</c> if <paramref name = "left" /> has the same value as <paramref name = "right" />; otherwise, <c>false</c>.</returns>
public static bool operator ==(NativeLong left, NativeLong right) => left.Equals(right);
/// <summary>
/// Tests for inequality between two objects.
/// </summary>
/// <param name = "left">The first value to compare.</param>
/// <param name = "right">The second value to compare.</param>
/// <returns><c>true</c> if <paramref name = "left" /> has a different value than <paramref name = "right" />; otherwise, <c>false</c>.</returns>
public static bool operator !=(NativeLong left, NativeLong right) => !left.Equals(right);
/// <summary>
/// Performs an explicit conversion from <see cref = "NativeLong" /> to <see cref = "int" />.
/// </summary>
/// <param name = "value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator int(NativeLong value) =>
UseInt ? value._intValue : (int) value._pointerValue;
/// <summary>
/// Performs an implicit conversion from <see cref = "NativeLong" /> to <see cref = "long" />.
/// </summary>
/// <param name = "value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator long(NativeLong value) =>
UseInt ? value._intValue : (long) value._pointerValue;
/// <summary>
/// Performs an implicit conversion from <see cref = "NativeLong" /> to <see cref = "long" />.
/// </summary>
/// <param name = "value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator IntPtr(NativeLong value) =>
UseInt ? (IntPtr) value._intValue : (IntPtr) value._pointerValue;
/// <summary>
/// Performs an implicit conversion to <see cref = "NativeLong" /> from <see cref = "int" />.
/// </summary>
/// <param name = "value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator NativeLong(int value) => new NativeLong(value);
/// <summary>
/// Performs an explicit conversion to <see cref = "NativeLong" /> from <see cref = "long" />.
/// </summary>
/// <param name = "value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator NativeLong(long value) => new NativeLong(value);
/// <summary>
/// Performs an explicit conversion to <see cref = "NativeLong" /> from <see cref = "IntPtr" />.
/// </summary>
/// <param name = "value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator NativeLong(IntPtr value) => new NativeLong(value);
/// <inheritdoc cref="IComparable.CompareTo" />
public int CompareTo(NativeLong other)
{
if (UseInt)
return _intValue.CompareTo(other._intValue);
#if !NET5_0
return ((long)_pointerValue).CompareTo((long) other._pointerValue);
#else
return _pointerValue.CompareTo(other._pointerValue);
#endif
}
/// <inheritdoc cref="IComparable.CompareTo" />
public int CompareTo(object obj) =>
obj switch
{
null => 1,
NativeLong other => CompareTo(other),
int other => CompareTo(new NativeLong(other)),
long other => CompareTo(new NativeLong(other)),
IntPtr other => CompareTo(new NativeLong(other)),
_ => throw new ArgumentException($"Object must be convertible to {nameof(NativeLong)} type")
};
public static bool operator <(NativeLong left, NativeLong right) => left.CompareTo(right) < 0;
public static bool operator >(NativeLong left, NativeLong right) => left.CompareTo(right) > 0;
public static bool operator <=(NativeLong left, NativeLong right) => left.CompareTo(right) <= 0;
public static bool operator >=(NativeLong left, NativeLong right) => left.CompareTo(right) >= 0;
}