Reputation: 35502
I have the following method in C#:
public T Read<T>()
{
T[] t = new T[1];
int s = Marshal.SizeOf(typeof(T));
if (index + s > size)
throw new Exception("Error 101 Celebrity");
GCHandle handle = GCHandle.Alloc(t, GCHandleType.Pinned);
Marshal.Copy(dataRead, index, handle.AddrOfPinnedObject(), s);
index += s;
return t[0];
}
dataRead is a byte[] array. index and size are integer type.
The function reads a type from dataRead(byte[]) and increases the index(index+=type).
All over the net,when I google "Delphi generics" - all it appears is Trecords and classes,which is not what I need.
How do I make that code in Delphi?
Upvotes: 0
Views: 385
Reputation: 1941
function TReader.Read <T>: T;
begin
if FIndex + SizeOf (T) > Length (FDataRead) then
raise Exception.Create ('Error 101 Celebrity');
Move (FDataRead[FIndex], Result, SizeOf (T));
Inc (FIndex, SizeOf (T));
end;
Upvotes: 4