interface
uses
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.StdCtrls;
type
TForm1 =
class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
type
TMyRecordA =
record
Details:
string[255];
FileName:
string[255];
FileDate: TDateTime;
FileSize: Integer;
Recordsize: Integer;
end;
TMyRecordB =
record
Details:
string[255];
x: Real;
y: Real;
z: Real;
end;
TMyRecordC =
record
FileDate: TDateTime;
FileSize: Integer;
x: Real;
y: Real;
z: Real;
end;
TGenericRecord<TRecordType> =
class
value : TRecordType ;
/// <summary>
/// Convert Byte to Record , outputtype : TRecordType
/// </summary>
/// <param name="ABuffer">
/// Byte Array
/// </param>
function ByteArrayToMyRecord(ABuffer: TBytes): TRecordType;
// class function ByteArrayToMyRecord(ABuffer: TBytes): TRecordType; static;
/// <summary>
/// Store a Record into a BYTE Array
/// </summary>
/// <param name="aRecord">
/// adjust this record definition to your needs
/// </param>
function MyRecordToByteArray(aRecord: TRecordType): TBytes;
// class function MyRecordToByteArray(aRecord: TRecordType): TBytes; static;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TGenericRecord<TRecordType>.MyRecordToByteArray(aRecord: TRecordType): TBytes;
var
LSource: PAnsiChar;
begin
LSource := PAnsiChar(@aRecord);
SetLength(Result, SizeOf(TRecordType));
Move(LSource[0], Result[0], SizeOf(TRecordType));
end;
function TGenericRecord<TRecordType>.ByteArrayToMyRecord(ABuffer: TBytes): TRecordType;
var
LDest: PAnsiChar;
begin
LDest := PAnsiChar(@Result);
Move(ABuffer[0], LDest[0], SizeOf(TRecordType));
end ;
procedure TForm1.Button1Click(Sender: TObject);
var a_record : TGenericRecord<TMyRecordA>;
b_record : TGenericRecord<TMyRecordB>;
c_record : TGenericRecord<TMyRecordC>;
ABuffer : TBytes;
begin
/// create class
a_record :=TGenericRecord<TMyRecordA>.Create;
b_record :=TGenericRecord<TMyRecordB>.Create;
c_record :=TGenericRecord<TMyRecordC>.Create;
/// einb paar werte zuweisen .....
a_record.value.Details := '
#1 bla bla';
a_record.value.FileName := '
#2 bla bla';
a_record.value.Recordsize := 100;
a_record.value.FileSize := 222;
b_record.value.x := 1;
b_record.value.y := 2;
b_record.value.z := 3;
/// im buffer daten zwischenlagern
ABuffer := a_record.MyRecordToByteArray(a_record.value);
/// löschen oder mit anderen daten arbeiten
a_record.value.Details := '
.....';
a_record.value.FileName := '
----';
a_record.value.Recordsize := -1;
a_record.value.FileSize := -2;
// Daten wieder aus dem Buffer zurücklesen
a_record.value := a_record.ByteArrayToMyRecord(ABuffer);
/// alte daten sind wieder da :-)
memo1.lines.add( a_record.value.Details) ;
memo1.lines.add( a_record.value.Filename) ;
/// free class
a_record.Free;
b_record.Free;
c_record.Free;
end;
end