program dp_180297;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.Generics.Collections,
System.Classes,
System.SysUtils,
System.Diagnostics,
BaseData
in '
BaseData.pas',
BaseDataFactory
in '
BaseDataFactory.pas';
type
TData1 =
class( TBaseData )
public
property Value1 : Integer
index $000604
read GetData;
// Offset 6 Bit-Width 4
property Value2 : Integer
index $000010
read GetData;
// Offset 0 Bit-Width 16
end;
procedure ValueCheck( AInstance : TData1 );
begin
Assert( AInstance.Value1 = 9 );
Assert( AInstance.Value2 = 65151 );
end;
procedure PerformanceCheck( AInstance : TData1 );
const
C_ROUNDS = 1000000;
var
LWatch : TStopwatch;
LValue : Integer;
LIdx : Integer;
begin
LWatch := TStopwatch.StartNew;
for LIdx := 1
to C_ROUNDS
do
begin
LValue := AInstance.Value1;
end;
LWatch.Stop;
Writeln( C_ROUNDS, '
Rounds in ', LWatch.ElapsedMilliseconds, '
ms' );
end;
procedure FactoryTest;
var
LStream : TStream;
LData : TBytes;
LInstance : TBaseData;
begin
TBaseDataFactory.RegisterType( $00, TData1 );
LStream := TMemoryStream.Create;
try
LData := TBytes.Create( $00
{ Type } , $02
{ Size of Data } , $FE, $7F );
LStream.
Write( LData, Length( LData ) );
LStream.Seek( 0, soFromBeginning );
LInstance := TBaseDataFactory.CreateFromStream( LStream );
try
ValueCheck( LInstance
as TData1 );
finally
LInstance.Free;
end;
finally
LStream.Free;
end;
end;
procedure Main;
var
LData : TBytes;
LInstance : TData1;
begin
LData := TBytes.Create( $FE, $7F );
// 1111111001111111
// ......====......
// Value1 ^ = 1001(bin) = 9(decimal)
LInstance := TData1.Create( LData );
try
ValueCheck( LInstance );
PerformanceCheck( LInstance );
finally
LInstance.Free;
end;
end;
begin
ReportMemoryLeaksOnShutdown := True;
try
Main;
FactoryTest;
except
on E :
Exception do
Writeln( E.ClassName, '
: ', E.
Message );
end;
ReadLn;
end.