Changeset 405 for Generics


Ignore:
Timestamp:
Aug 14, 2012, 7:04:04 AM (12 years ago)
Author:
chronos
Message:
  • Modified: TemplateGenerics TBinarySerializer now use TListByte class instead TStreamByte.
Location:
Generics/TemplateGenerics
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • Generics/TemplateGenerics/Additional/UBinarySerializer.pas

    r342 r405  
    11unit UBinarySerializer;
    22
    3 {$mode objfpc}{$H+}
     3{$mode delphi}{$H+}
    44
    55interface
     
    1515  TBinarySerializer = class
    1616  private
    17     FStream: TStreamByte;
     17    FGrow: Boolean;
     18    FList: TListByte;
    1819    FEndianness: TEndianness;
    1920    SwapData: Boolean;
     
    2122    procedure ReverseByteOrder(var Buffer; Count: Integer);
    2223  public
     24    Position: Integer;
     25    OwnsList: Boolean;
     26    procedure Write(var Buffer; Count: Integer); inline;
     27    procedure Read(var Buffer; Count: Integer); inline;
     28
    2329    procedure Assign(Source: TBinarySerializer);
    2430    procedure WriteByte(Data: Byte);
     
    4854    procedure ReadStream(AStream: TStream; Count: Integer);
    4955    procedure ReadStreamPart(AStream: TStream; Count: Integer);
     56    procedure ReadList(List: TListByte; StartIndex, Count: Integer);
    5057    constructor Create; overload;
    51     constructor Create(AStream: TStreamByte); overload;
    5258    procedure Clear;
    5359    destructor Destroy; override;
    5460    property Endianness: TEndianness read FEndianness write SetEndianness;
    55     property Stream: TStreamByte read FStream write FStream;
     61    property List: TListByte read FList write FList;
     62    property Grow: Boolean read FGrow write FGrow;
    5663  end;
    5764
     
    6572  StringLength: Longint;
    6673begin
    67   FStream.ReadBuffer(StringLength, SizeOf(StringLength));
     74  FList.ReadBuffer(StringLength, SizeOf(StringLength));
    6875  Result := ReadString(StringLength);
    6976end;
     
    7582  OldPosition: Integer;
    7683begin
    77   OldPosition := FStream.Position;
     84  OldPosition := Position;
    7885  Result := '';
    7986  I := 1;
    8087  repeat
    81     if FStream.Position >= FStream.Size then Break;
     88    if Position >= FList.Count then Break;
    8289    Data := Chr(ReadByte);
    8390    if Data <> Terminator[I] then begin
     
    8895  if not (I > Length(Terminator)) then begin
    8996    Result := '';
    90     FStream.Position := OldPosition;
     97    Position := OldPosition;
    9198  end;
    9299end;
     
    94101function TBinarySerializer.ReadByte: Byte;
    95102begin
    96   FStream.ReadBuffer(Result, SizeOf(Byte));
     103  Read(Result, SizeOf(Byte));
    97104end;
    98105
    99106function TBinarySerializer.ReadCardinal: Cardinal;
    100107begin
    101   FStream.ReadBuffer(Result, SizeOf(Cardinal));
     108  Read(Result, SizeOf(Cardinal));
    102109  if SwapData then Result := SwapEndian(Result);
    103110end;
     
    105112function TBinarySerializer.ReadInt64: Int64;
    106113begin
    107   FStream.ReadBuffer(Result, SizeOf(Int64));
     114  Read(Result, SizeOf(Int64));
    108115  if SwapData then Result := SwapEndian(Result);
    109116end;
     
    113120  if Length > 0 then begin
    114121    SetLength(Result, Length);
    115     FStream.ReadBuffer(Result[1], Length);
     122    Read(Result[1], Length);
    116123  end else Result := '';
    117124end;
     
    121128  Count: Byte;
    122129begin
    123   FStream.ReadBuffer(Count, 1);
     130  Read(Count, 1);
    124131  Result := ReadString(Count);
    125132end;
     
    131138  if Count > 0 then begin
    132139    SetLength(Buffer, Count);
    133     FStream.ReadBuffer(Buffer[0], Count);
     140    Read(Buffer[0], Count);
    134141    AStream.Size := Count;
    135142    AStream.Position := 0;
     
    144151  if Count > 0 then begin
    145152    SetLength(Buffer, Count);
    146     FStream.ReadBuffer(Buffer[0], Count);
     153    Read(Buffer[0], Count);
    147154    if AStream.Size < (AStream.Position + Count) then
    148155      AStream.Size := AStream.Position + Count;
    149     AStream.Write(Buffer[0], Count);
     156    Write(Buffer[0], Count);
     157  end;
     158end;
     159
     160procedure TBinarySerializer.ReadList(List: TListByte; StartIndex, Count: Integer
     161  );
     162var
     163  Buffer: array of Byte;
     164begin
     165  if Count > (List.Count - StartIndex) then Count := (List.Count - StartIndex); // Limit max. stream size
     166  if Count > 0 then begin
     167    SetLength(Buffer, Count);
     168    Read(Pointer(Buffer)^, Count);
     169    List.ReplaceBuffer(StartIndex, Pointer(Buffer)^, Count);
    150170  end;
    151171end;
     
    159179    SetLength(Buffer, Count);
    160180    AStream.ReadBuffer(Pointer(Buffer)^, Count);
    161     FStream.WriteBuffer(Pointer(Buffer)^, Count);
     181    Write(Pointer(Buffer)^, Count);
    162182  end;
    163183end;
     
    170190  if Count > 0 then begin
    171191    SetLength(Buffer, Count);
    172     List.ReadBuffer(Pointer(Buffer)^, Count);
    173     FStream.WriteBuffer(Pointer(Buffer)^, Count);
     192    List.GetBuffer(StartIndex, PByte(Buffer), Count);
     193    Write(Pointer(Buffer)^, Count);
    174194  end;
    175195end;
     
    179199  inherited;
    180200  Endianness := enLittle;
    181   FStream := nil;
    182 end;
    183 
    184 constructor TBinarySerializer.Create(AStream: TStreamByte);
    185 begin
    186   inherited Create;
    187   Endianness := enLittle;
    188   FStream := AStream;
     201  FList := nil;
     202  FGrow := True;
    189203end;
    190204
    191205procedure TBinarySerializer.Clear;
    192206begin
    193   Stream.Size := 0;
     207  FList.Count := 0;
    194208end;
    195209
    196210destructor TBinarySerializer.Destroy;
    197211begin
     212  if OwnsList then FList.Free;
    198213  inherited Destroy;
    199214end;
     
    206221function TBinarySerializer.ReadDouble: Double;
    207222begin
    208   FStream.ReadBuffer(Result, SizeOf(Double));
     223  Read(Result, SizeOf(Double));
    209224end;
    210225
    211226function TBinarySerializer.ReadSingle: Single;
    212227begin
    213   FStream.ReadBuffer(Result, SizeOf(Single));
     228  Read(Result, SizeOf(Single));
    214229end;
    215230
    216231function TBinarySerializer.ReadWord: Word;
    217232begin
    218   FStream.ReadBuffer(Result, SizeOf(Word));
     233  Read(Result, SizeOf(Word));
    219234  if SwapData then Result := SwapEndian(Result);
    220235end;
     
    246261end;
    247262
     263procedure TBinarySerializer.Write(var Buffer; Count: Integer);
     264var
     265  NewCount: Integer;
     266begin
     267  if FGrow then begin
     268    NewCount := Position + Count;
     269    if FList.Count < NewCount then
     270      FList.Count := NewCount;
     271  end;
     272  FList.ReplaceBuffer(Position, Buffer, Count);
     273  Inc(Position, Count);
     274end;
     275
     276procedure TBinarySerializer.Read(var Buffer; Count: Integer);
     277begin
     278  FList.GetBuffer(Position, Buffer, Count);
     279  Inc(Position, Count);
     280end;
     281
    248282procedure TBinarySerializer.Assign(Source: TBinarySerializer);
    249283begin
    250   FStream := Source.FStream;
     284  FList := Source.FList;
    251285end;
    252286
     
    262296procedure TBinarySerializer.WriteByte(Data: Byte);
    263297begin
    264   FStream.WriteBuffer(Data, SizeOf(Byte));
     298  Write(Data, SizeOf(Byte));
    265299end;
    266300
     
    298332    SetLength(Buffer, Count);
    299333    AStream.ReadBuffer(Pointer(Buffer)^, Count);
    300     FStream.WriteBuffer(Pointer(Buffer)^, Count);
     334    Write(Pointer(Buffer)^, Count);
    301335  end;
    302336end;
  • Generics/TemplateGenerics/Generic/GenericList.inc

    r383 r405  
    3939    procedure SetCount(const AValue: TGListIndex); virtual;
    4040  public
     41    type
     42      PItem = ^TGListItem;
    4143    function CompareMem(P1, P2: Pointer; Length: cardinal): Boolean; inline;
    4244    function Add(Item: TGListItem): TGListIndex;
     
    5759    function GetArray(Index, ACount: TGListIndex): TGListItemArray;
    5860    procedure GetList(List: TGList; Index, ACount: TGListIndex);
     61    procedure GetBuffer(Index: TGListIndex; var Buffer; Count: TGListIndex);
    5962    function Implode(Separator: string; Converter: TGListToStringConverter): string;
    6063    function IndexOf(Item: TGListItem; Start: TGListIndex = 0): TGListIndex; virtual;
     
    7376    procedure ReplaceListPart(Index: TGListIndex; Source: TGList;
    7477      SourceIndex, SourceCount: TGListIndex);
     78    procedure ReplaceBuffer(Index: TGListIndex; var Buffer; Count: TGListIndex);
    7579    procedure Sort(Compare: TGListSortCompare);
    7680    procedure SetArray(Values: array of TGListItem);
     
    102106begin
    103107  FCount := 0;
     108end;
     109
     110procedure TGList.GetBuffer(Index: TGListIndex; var Buffer; Count: TGListIndex);
     111var
     112  P: PItem;
     113  I: TGListIndex;
     114begin
     115  if (Index + Count) > FCount then
     116    raise EListError.CreateFmt(SListIndexError, [Index + Count]);
     117  P := PItem(@Buffer);
     118  I := 0;
     119  while I < Count do begin
     120    P^ := Items[Index + I];
     121    Inc(P, 1);
     122    I := I + 1;
     123  end;
     124end;
     125
     126procedure TGList.ReplaceBuffer(Index: TGListIndex; var Buffer; Count: TGListIndex);
     127var
     128  P: PItem;
     129  I: TGListIndex;
     130begin
     131  if (Index + Count) > FCount then
     132    raise EListError.CreateFmt(SListIndexError, [Index + Count]);
     133  P := PItem(@Buffer);
     134  I := 0;
     135  while I < Count do begin
     136    Items[Index + I] := P^;
     137    Inc(P, 1);
     138    I := I + 1;
     139  end;
    104140end;
    105141
Note: See TracChangeset for help on using the changeset viewer.