1
0
mirror of synced 2024-11-21 10:53:50 +00:00

Implemented value streaming

This commit is contained in:
Mark van Renswoude 2016-10-14 16:43:53 +02:00
parent fa2947a878
commit 1a22728f30
2 changed files with 117 additions and 46 deletions

View File

@ -40,9 +40,9 @@ type
private private
FValueType: TX2LogValueType; FValueType: TX2LogValueType;
protected protected
constructor Create(AValueType: TX2LogValueType; AStream: TStream); virtual; constructor Create(AValueType: TX2LogValueType; AStream: TStream = nil; ASize: Cardinal = 0); overload; virtual;
procedure LoadFromStream(AStream: TStream); virtual; abstract; procedure LoadFromStream(AStream: TStream; ASize: Cardinal); virtual; abstract;
procedure SaveToStream(AStream: TStream); virtual; abstract; procedure SaveToStream(AStream: TStream); virtual; abstract;
property ValueType: TX2LogValueType read FValueType; property ValueType: TX2LogValueType read FValueType;
@ -143,7 +143,6 @@ type
implementation implementation
uses uses
System.SysUtils, System.SysUtils,
System.Variants,
Vcl.ClipBrd, Vcl.ClipBrd,
Winapi.Windows, Winapi.Windows,
@ -197,7 +196,9 @@ type
private private
FValue: string; FValue: string;
protected protected
procedure LoadFromStream(AStream: TStream); override; constructor Create(const AValue: string); overload;
procedure LoadFromStream(AStream: TStream; ASize: Cardinal); override;
procedure SaveToStream(AStream: TStream); override; procedure SaveToStream(AStream: TStream); override;
property Value: string read FValue write FValue; property Value: string read FValue write FValue;
@ -208,7 +209,9 @@ type
private private
FValue: Boolean; FValue: Boolean;
protected protected
procedure LoadFromStream(AStream: TStream); override; constructor Create(AValue: Boolean); overload;
procedure LoadFromStream(AStream: TStream; ASize: Cardinal); override;
procedure SaveToStream(AStream: TStream); override; procedure SaveToStream(AStream: TStream); override;
property Value: Boolean read FValue write FValue; property Value: Boolean read FValue write FValue;
@ -219,7 +222,9 @@ type
private private
FValue: Int64; FValue: Int64;
protected protected
procedure LoadFromStream(AStream: TStream); override; constructor Create(AValue: Int64); overload;
procedure LoadFromStream(AStream: TStream; ASize: Cardinal); override;
procedure SaveToStream(AStream: TStream); override; procedure SaveToStream(AStream: TStream); override;
property Value: Int64 read FValue write FValue; property Value: Int64 read FValue write FValue;
@ -230,7 +235,9 @@ type
private private
FValue: Extended; FValue: Extended;
protected protected
procedure LoadFromStream(AStream: TStream); override; constructor Create(AValue: Extended); overload;
procedure LoadFromStream(AStream: TStream; ASize: Cardinal); override;
procedure SaveToStream(AStream: TStream); override; procedure SaveToStream(AStream: TStream); override;
property Value: Extended read FValue write FValue; property Value: Extended read FValue write FValue;
@ -241,7 +248,9 @@ type
private private
FValue: TDateTime; FValue: TDateTime;
protected protected
procedure LoadFromStream(AStream: TStream); override; constructor Create(AValue: TDateTime); overload;
procedure LoadFromStream(AStream: TStream; ASize: Cardinal); override;
procedure SaveToStream(AStream: TStream); override; procedure SaveToStream(AStream: TStream); override;
property Value: TDateTime read FValue write FValue; property Value: TDateTime read FValue write FValue;
@ -321,7 +330,7 @@ var
paramIndex: Integer; paramIndex: Integer;
param: TVarRec; param: TVarRec;
key: string; key: string;
value: Variant; value: TX2LogDictionaryValue;
begin begin
inherited Create; inherited Create;
@ -356,28 +365,29 @@ begin
param := AValues[paramIndex]; param := AValues[paramIndex];
case param.VType of case param.VType of
vtInteger: value := param.VInteger; vtInteger: value := TX2LogDictionaryIntValue.Create(param.VInteger);
vtBoolean: value := param.VBoolean; vtBoolean: value := TX2LogDictionaryBooleanValue.Create(param.VBoolean);
vtChar: value := string(param.VChar); vtChar: value := TX2LogDictionaryStringValue.Create(string(param.VChar));
vtExtended: value := param.VExtended^; vtExtended: value := TX2LogDictionaryFloatValue.Create(param.VExtended^);
vtString: value := string(param.VString^); vtString: value := TX2LogDictionaryStringValue.Create(string(param.VString^));
vtPChar: value := string(param.VPChar); vtPChar: value := TX2LogDictionaryStringValue.Create(string(param.VPChar));
vtWideChar: value := string(param.VWideChar); vtWideChar: value := TX2LogDictionaryStringValue.Create(param.VWideChar);
vtPWideChar: value := string(param.VPWideChar); vtPWideChar: value := TX2LogDictionaryStringValue.Create(param.VPWideChar);
vtAnsiString: value := string(PChar(param.VAnsiString)); vtAnsiString: value := TX2LogDictionaryStringValue.Create(PChar(param.VAnsiString));
vtCurrency: value := param.VCurrency^; vtCurrency: value := TX2LogDictionaryFloatValue.Create(param.VCurrency^);
vtWideString: value := string(WideString(param.VWideString)); vtWideString: value := TX2LogDictionaryStringValue.Create(WideString(param.VWideString));
vtInt64: value := param.VInt64^; vtInt64: value := TX2LogDictionaryIntValue.Create(param.VInt64^);
{$IF CompilerVersion >= 23} {$IF CompilerVersion >= 23}
vtUnicodeString: value := string(UnicodeString(param.VUnicodeString)); vtUnicodeString: value := TX2LogDictionaryStringValue.Create(UnicodeString(param.VUnicodeString));
{$IFEND} {$IFEND}
else else
raise Exception.CreateFmt('Unsupported value type %d at index %d', [param.VType, paramIndex]); raise Exception.CreateFmt('Unsupported value type %d at index %d', [param.VType, paramIndex]);
end; end;
FValues.Add(key, value);
Inc(paramIndex); Inc(paramIndex);
end; end;
end; end;
@ -719,76 +729,131 @@ end;
{ TX2LogDictionaryValue } { TX2LogDictionaryValue }
constructor TX2LogDictionaryValue.Create(AValueType: TX2LogValueType; AStream: TStream); constructor TX2LogDictionaryValue.Create(AValueType: TX2LogValueType; AStream: TStream; ASize: Cardinal);
begin begin
inherited Create;
FValueType := AValueType; FValueType := AValueType;
LoadFromStream(AStream); if Assigned(AStream) then
LoadFromStream(AStream, ASize);
end; end;
{ TX2LogDictionaryStringValue } { TX2LogDictionaryStringValue }
procedure TX2LogDictionaryStringValue.LoadFromStream(AStream: TStream); constructor TX2LogDictionaryStringValue.Create(const AValue: string);
begin begin
// TODO TX2LogDictionaryStringValue.LoadFromStream inherited Create(StringValue);
Value := AValue;
end;
procedure TX2LogDictionaryStringValue.LoadFromStream(AStream: TStream; ASize: Cardinal);
begin
Value := TStreamUtil.ReadString(AStream, nil, False, ASize);
end; end;
procedure TX2LogDictionaryStringValue.SaveToStream(AStream: TStream); procedure TX2LogDictionaryStringValue.SaveToStream(AStream: TStream);
begin begin
// TODO TX2LogDictionaryStringValue.SaveToStream TStreamUtil.WriteString(AStream, Value, nil, False);
end; end;
{ TX2LogDictionaryBooleanValue } { TX2LogDictionaryBooleanValue }
procedure TX2LogDictionaryBooleanValue.LoadFromStream(AStream: TStream); constructor TX2LogDictionaryBooleanValue.Create(AValue: Boolean);
begin begin
// TODO TX2LogDictionaryBooleanValue.LoadFromStream inherited Create(BooleanValue);
Value := AValue;
end;
procedure TX2LogDictionaryBooleanValue.LoadFromStream(AStream: TStream; ASize: Cardinal);
begin
if ASize <> SizeOf(Boolean) then
raise EInvalidOperation.CreateFmt('Size (%d) does not match Boolean', [ASize]);
AStream.ReadBuffer(FValue, ASize);
end; end;
procedure TX2LogDictionaryBooleanValue.SaveToStream(AStream: TStream); procedure TX2LogDictionaryBooleanValue.SaveToStream(AStream: TStream);
begin begin
// TODO TX2LogDictionaryBooleanValue.SaveToStream AStream.WriteBuffer(Value, SizeOf(Boolean));
end; end;
{ TX2LogDictionaryIntValue } { TX2LogDictionaryIntValue }
procedure TX2LogDictionaryIntValue.LoadFromStream(AStream: TStream); constructor TX2LogDictionaryIntValue.Create(AValue: Int64);
begin begin
// TODO TX2LogDictionaryIntValue.LoadFromStream inherited Create(IntValue);
Value := AValue;
end;
procedure TX2LogDictionaryIntValue.LoadFromStream(AStream: TStream; ASize: Cardinal);
begin
if ASize <> SizeOf(Int64) then
raise EInvalidOperation.CreateFmt('Size (%d) does not match Int64', [ASize]);
AStream.ReadBuffer(FValue, ASize);
end; end;
procedure TX2LogDictionaryIntValue.SaveToStream(AStream: TStream); procedure TX2LogDictionaryIntValue.SaveToStream(AStream: TStream);
begin begin
// TODO TX2LogDictionaryIntValue.SaveToStream AStream.WriteBuffer(Value, SizeOf(Int64));
end; end;
{ TX2LogDictionaryFloatValue } { TX2LogDictionaryFloatValue }
procedure TX2LogDictionaryFloatValue.LoadFromStream(AStream: TStream); constructor TX2LogDictionaryFloatValue.Create(AValue: Extended);
begin begin
// TODO TX2LogDictionaryFloatValue.LoadFromStream inherited Create(FloatValue);
Value := AValue;
end;
procedure TX2LogDictionaryFloatValue.LoadFromStream(AStream: TStream; ASize: Cardinal);
begin
if ASize <> SizeOf(Extended) then
raise EInvalidOperation.CreateFmt('Size (%d) does not match Extended', [ASize]);
AStream.ReadBuffer(FValue, ASize);
end; end;
procedure TX2LogDictionaryFloatValue.SaveToStream(AStream: TStream); procedure TX2LogDictionaryFloatValue.SaveToStream(AStream: TStream);
begin begin
// TODO TX2LogDictionaryFloatValue.SaveToStream AStream.WriteBuffer(Value, SizeOf(Extended));
end; end;
{ TX2LogDictionaryDateTimeValue } { TX2LogDictionaryDateTimeValue }
procedure TX2LogDictionaryDateTimeValue.LoadFromStream(AStream: TStream); constructor TX2LogDictionaryDateTimeValue.Create(AValue: TDateTime);
begin begin
// TODO TX2LogDictionaryDateTimeValue.LoadFromStream inherited Create(DateTimeValue);
Value := AValue;
end;
procedure TX2LogDictionaryDateTimeValue.LoadFromStream(AStream: TStream; ASize: Cardinal);
begin
if ASize <> SizeOf(TDateTime) then
raise EInvalidOperation.CreateFmt('Size (%d) does not match TDateTime', [ASize]);
AStream.ReadBuffer(FValue, ASize);
end; end;
procedure TX2LogDictionaryDateTimeValue.SaveToStream(AStream: TStream); procedure TX2LogDictionaryDateTimeValue.SaveToStream(AStream: TStream);
begin begin
// TODO TX2LogDictionaryDateTimeValue.SaveToStream AStream.WriteBuffer(Value, SizeOf(TDateTime));
end; end;

View File

@ -16,8 +16,8 @@ type
class function ReadCardinal(AStream: TStream): Cardinal; class function ReadCardinal(AStream: TStream): Cardinal;
class procedure WriteCardinal(AStream: TStream; AValue: Cardinal); class procedure WriteCardinal(AStream: TStream; AValue: Cardinal);
class function ReadString(AStream: TStream; AEncoding: TEncoding = nil): string; class function ReadString(AStream: TStream; AEncoding: TEncoding = nil; AReadSize: Boolean = True; ASize: Cardinal = 0): string;
class procedure WriteString(AStream: TStream; const AValue: string; AEncoding: TEncoding = nil); class procedure WriteString(AStream: TStream; const AValue: string; AEncoding: TEncoding = nil; AWriteSize: Boolean = True);
end; end;
implementation implementation
@ -57,13 +57,17 @@ begin
end; end;
class function TStreamUtil.ReadString(AStream: TStream; AEncoding: TEncoding): string; class function TStreamUtil.ReadString(AStream: TStream; AEncoding: TEncoding; AReadSize: Boolean; ASize: Cardinal): string;
var var
bytes: TBytes; bytes: TBytes;
bytesLength: Cardinal; bytesLength: Cardinal;
begin begin
bytesLength := ReadCardinal(AStream); if AReadSize then
bytesLength := ReadCardinal(AStream)
else
bytesLength := ASize;
if bytesLength > 0 then if bytesLength > 0 then
begin begin
SetLength(bytes, bytesLength); SetLength(bytes, bytesLength);
@ -75,7 +79,7 @@ begin
end; end;
class procedure TStreamUtil.WriteString(AStream: TStream; const AValue: string; AEncoding: TEncoding); class procedure TStreamUtil.WriteString(AStream: TStream; const AValue: string; AEncoding: TEncoding; AWriteSize: Boolean);
var var
bytes: TBytes; bytes: TBytes;
bytesLength: Cardinal; bytesLength: Cardinal;
@ -84,7 +88,9 @@ begin
bytes := GetEncoding(AEncoding).GetBytes(AValue); bytes := GetEncoding(AEncoding).GetBytes(AValue);
bytesLength := Length(bytes); bytesLength := Length(bytes);
WriteCardinal(AStream, bytesLength); if AWriteSize then
WriteCardinal(AStream, bytesLength);
if bytesLength > 0 then if bytesLength > 0 then
AStream.WriteBuffer(bytes[0], bytesLength); AStream.WriteBuffer(bytes[0], bytesLength);
end; end;