Skip to content

Commit a6c9402

Browse files
committed
Use generic with IBinaryInteger constraint
1 parent c7bc9aa commit a6c9402

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

src/native/managed/cdacreader/src/Target.cs

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,7 @@ public byte ReadUInt8(ulong address)
166166
}
167167

168168
public bool TryReadUInt8(ulong address, out byte value)
169-
=> TryReadUInt8(address, _reader, out value);
170-
171-
private static bool TryReadUInt8(ulong address, Reader reader, out byte value)
172-
{
173-
value = 0;
174-
fixed (byte* ptr = &value)
175-
{
176-
if (reader.ReadFromTarget(address, ptr, 1) < 0)
177-
return false;
178-
}
179-
180-
return true;
181-
}
169+
=> TryRead(address, isUnsigned: true, out value);
182170

183171
public uint ReadUInt32(ulong address)
184172
{
@@ -189,21 +177,38 @@ public uint ReadUInt32(ulong address)
189177
}
190178

191179
public bool TryReadUInt32(ulong address, out uint value)
192-
=> TryReadUInt32(address, _config.IsLittleEndian, _reader, out value);
180+
=> TryRead(address, isUnsigned: true, out value);
193181

194182
private static bool TryReadUInt32(ulong address, bool isLittleEndian, Reader reader, out uint value)
183+
=> TryRead(address, isLittleEndian, isUnsigned: true, reader, out value);
184+
185+
public ulong ReadUInt64(ulong address)
195186
{
196-
value = 0;
187+
if (!TryReadUInt64(address, out ulong value))
188+
throw new InvalidOperationException($"Failed to read uint32 at 0x{address:x8}.");
197189

198-
Span<byte> buffer = stackalloc byte[sizeof(uint)];
190+
return value;
191+
}
192+
193+
public bool TryReadUInt64(ulong address, out ulong value)
194+
=> TryRead(address, isUnsigned: true, out value);
195+
196+
private static bool TryReadUInt64(ulong address, bool isLittleEndian, Reader reader, out ulong value)
197+
=> TryRead(address, isLittleEndian, isUnsigned: true, reader, out value);
198+
199+
private bool TryRead<T>(ulong address, bool isUnsigned, out T value) where T : unmanaged, IBinaryInteger<T>
200+
=> TryRead(address, _config.IsLittleEndian, isUnsigned, _reader, out value);
201+
202+
private static bool TryRead<T>(ulong address, bool isLittleEndian, bool isUnsigned, Reader reader, out T value) where T : unmanaged, IBinaryInteger<T>
203+
{
204+
value = default;
205+
Span<byte> buffer = stackalloc byte[sizeof(T)];
199206
if (reader.ReadFromTarget(address, buffer) < 0)
200207
return false;
201208

202-
value = isLittleEndian
203-
? BinaryPrimitives.ReadUInt32LittleEndian(buffer)
204-
: BinaryPrimitives.ReadUInt32BigEndian(buffer);
205-
206-
return true;
209+
return isLittleEndian
210+
? T.TryReadLittleEndian(buffer, isUnsigned, out value)
211+
: T.TryReadBigEndian(buffer, isUnsigned, out value);
207212
}
208213

209214
public TargetPointer ReadPointer(ulong address)
@@ -225,22 +230,20 @@ private static bool TryReadPointer(ulong address, Configuration config, Reader r
225230
if (reader.ReadFromTarget(address, buffer) < 0)
226231
return false;
227232

228-
if (config.PointerSize == sizeof(uint))
233+
if (config.PointerSize == sizeof(uint)
234+
&& TryReadUInt32(address, config.IsLittleEndian, reader, out uint value32))
229235
{
230-
pointer = new TargetPointer(
231-
config.IsLittleEndian
232-
? BinaryPrimitives.ReadUInt32LittleEndian(buffer)
233-
: BinaryPrimitives.ReadUInt32BigEndian(buffer));
236+
pointer = new TargetPointer(value32);
237+
return true;
234238
}
235-
else if (config.PointerSize == sizeof(ulong))
239+
else if (config.PointerSize == sizeof(ulong)
240+
&& TryReadUInt64(address, config.IsLittleEndian, reader, out ulong value64))
236241
{
237-
pointer = new TargetPointer(
238-
config.IsLittleEndian
239-
? BinaryPrimitives.ReadUInt64LittleEndian(buffer)
240-
: BinaryPrimitives.ReadUInt64BigEndian(buffer));
242+
pointer = new TargetPointer(value64);
243+
return true;
241244
}
242245

243-
return true;
246+
return false;
244247
}
245248

246249
public byte ReadGlobalUInt8(string name)

0 commit comments

Comments
 (0)