-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
VPtr.cs
212 lines (158 loc) · 6.53 KB
/
VPtr.cs
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
/*!
* Copyright (c) 2016 Denis Kuzmin <[email protected]> github/3F
* Copyright (c) Conari contributors https://github.com/3F/Conari/graphs/contributors
* Licensed under the MIT License (MIT).
* See accompanying LICENSE.txt file or visit https://github.com/3F/Conari
*/
using System;
using System.Diagnostics;
using System.Runtime.Serialization;
using net.r_eg.Conari.Extension;
namespace net.r_eg.Conari.Types
{
/// <summary>
/// Variable long pointer.
/// </summary>
[DebuggerDisplay("{(IsLong ? \"long: \" : \"IntPtr: \") + ToString()}")]
[Serializable]
public struct VPtr: ISerializable
{
private readonly IntPtr vIntPtr;
private readonly long vLong;
public static readonly VPtr Zero;
public static readonly VPtr ZeroLong = new(0);
public bool IsLong { get; private set; }
public static implicit operator IntPtr(VPtr n) => n.vIntPtr;
public static implicit operator long(VPtr n) => n.vLong;
public static implicit operator VPtr(IntPtr n) => new(n);
public static implicit operator VPtr(long n) => new(n);
public static VPtr operator +(VPtr input, int offset)
{
if(input.IsLong) return input.vLong + offset;
return input.vIntPtr + offset;
}
public static VPtr operator +(VPtr input, VPtr offset)
{
if(input.IsLong) return input.vLong + GetLongOfs(offset);
return Calc(input.vIntPtr, GetLongOfs(offset), plus: true);
}
public static VPtr operator +(VPtr input, IntPtr offset) => input + new VPtr(offset);
public static VPtr operator +(VPtr input, long offset) => input + new VPtr(offset);
public static VPtr operator ++(VPtr input) => input += 1;
public static VPtr operator -(VPtr input, int offset)
{
if(input.IsLong) return input.vLong - offset;
return input.vIntPtr - offset;
}
public static VPtr operator -(VPtr input, VPtr offset)
{
if(input.IsLong) return input.vLong - GetLongOfs(offset);
return Calc(input.vIntPtr, GetLongOfs(offset), plus: false);
}
public static VPtr operator -(VPtr input, IntPtr offset) => input - new VPtr(offset);
public static VPtr operator -(VPtr input, long offset) => input - new VPtr(offset);
public static VPtr operator --(VPtr input) => input -= 1;
public static bool operator >(VPtr a, long b) => a.IsLong ? a.vLong > b : a.vIntPtr.ToInt64() > b;
public static bool operator <(VPtr a, long b) => a.IsLong ? a.vLong < b : a.vIntPtr.ToInt64() < b;
public static bool operator >=(VPtr a, long b) => a.IsLong ? a.vLong >= b : a.vIntPtr.ToInt64() >= b;
public static bool operator <=(VPtr a, long b) => a.IsLong ? a.vLong <= b : a.vIntPtr.ToInt64() <= b;
public static VPtr Add(VPtr input, VPtr offset) => input + offset;
public static VPtr Add(VPtr input, IntPtr offset) => input + offset;
public static VPtr Add(VPtr input, long offset) => input + offset;
public static VPtr Add(VPtr input, int offset) => input + offset;
public static VPtr Sub(VPtr input, VPtr offset) => input - offset;
public static VPtr Sub(VPtr input, IntPtr offset) => input - offset;
public static VPtr Sub(VPtr input, long offset) => input - offset;
public static VPtr Sub(VPtr input, int offset) => input - offset;
public static VPtr MakePtr(long value) => new(new IntPtr(value));
public static VPtr MakeLong(long value) => new(value);
public static VPtr MakePtr(IntPtr value) => new(value);
public static VPtr MakeLong(IntPtr value) => new(value.ToInt64());
public static bool operator ==(VPtr a, VPtr b)
{
if(a.IsLong && b.IsLong) return a.vLong == b.vLong;
if(a.IsLong) return a.vLong == b.vIntPtr.ToInt64();
if(b.IsLong) return b.vLong == a.vIntPtr.ToInt64();
return a.Equals(b);
}
public static bool operator !=(VPtr a, VPtr b) => !(a == b);
public override bool Equals(object obj)
{
if(obj is null || !(obj is VPtr)) {
return false;
}
var b = (VPtr)obj;
return vIntPtr == b.vIntPtr
&& vLong == b.vLong
&& IsLong == b.IsLong;
}
public override int GetHashCode()
{
return 0.CalculateHashCode
(
vIntPtr,
vLong,
IsLong
);
}
public override string ToString() => IsLong ? vLong.ToString() : "0x" + vIntPtr.ToString("x");
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
if(info == null) throw new ArgumentNullException(nameof(info));
info.AddValue(nameof(vIntPtr), vIntPtr);
info.AddValue(nameof(vLong), vLong);
info.AddValue(nameof(IsLong), IsLong);
}
public VPtr(IntPtr input, int offset)
: this(input)
{
vIntPtr = input + offset;
}
public VPtr(long input, int offset)
: this(input)
{
vLong = input + offset;
}
public VPtr(VPtr origin, int offset)
: this()
{
if(origin.IsLong)
{
vLong = origin.vLong + offset;
}
else
{
vIntPtr = origin.vIntPtr + offset;
}
}
public VPtr(IntPtr input)
: this()
{
vIntPtr = input;
}
public VPtr(long input)
: this()
{
vLong = input;
IsLong = true;
}
private static VPtr Calc(IntPtr input, long offset, bool plus)
{
int inc = offset < 0 ? int.MinValue : int.MaxValue;
IntPtr ret = input;
long sum = 0;
while(true)
{
long next = unchecked(sum + inc);
if((inc < 0 && next >= sum) || (inc > 0 && next < sum)) break; // overflow
sum = next;
if((inc < 0 && sum <= offset) || (inc > 0 && sum >= offset)) break;
ret = plus ? ret + inc : ret - inc;
}
inc -= (int)(sum - offset);
return plus ? ret + inc : ret - inc;
}
private static long GetLongOfs(VPtr input)
=> input.IsLong ? input.vLong : input.vIntPtr.ToInt64();
}
}