I have several record types to read from file, for example
PDescriptorBlockHeader = ^TDescriptorBlockHeader;
TDescriptorBlockHeader = packed record
BlockType: UInt32;
BlockAttributes: UInt32; // +4
OffsetToFirstEvent: UInt16; // +8
OsId: byte; // +10
OsVersion: byte;
DisplayableSize: UInt64; // +12
FormatLogicalAddress: UInt64; // +20
SessionId: UInt64; // +28
ControlBlockID: UInt32; // +36
StringStorage: MTF_TAPE_ADDRESS; // +40
OsSpecificData: MTF_TAPE_ADDRESS; // +44
StringType: byte; // +48
Reserved: byte; // +49
HeaderChecksum: UInt16; //+50
end;
and I want to use common function to read from file
type
TReaderHelper = class
class procedure ReadToStruct<T:record>(stream: TFileStream; offset: Int64);
end;
implementation
class procedure TReaderHelper.ReadToStruct<T>(stream: TFileStream; offset: Int64);
var
rd: integer;
begin
stream.Position := offset;
if stream.Position <> offset then
raise Exception.Create('Seek error');
rd := stream.Read(T, sizeof(T));
if rd <> sizeof(T) then
raise Exception.Create('Read ' + IntToStr(rd) + ' instead of ' + IntToStr(sizeof(T)));
end;
Compiler gives me error E2571 Type parameter 'T' doesn't have class or interface constraint at rd := stream.Read(T, sizeof(T));. Is it possible to pass that generic record as parameter to TFileStream.Read function?