Ignore:
Timestamp:
Feb 8, 2012, 9:47:54 AM (13 years ago)
Author:
chronos
Message:
  • Added: TGMemoryStream implementation.
  • Added: Additional methods to TGList.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Generics/NativeGenerics/Units/GenericStream.pas

    r320 r322  
    2424    procedure Write(Item: TItem); virtual; abstract;
    2525    procedure WriteArray(Item: array of TItem); virtual; abstract;
     26    procedure WriteList(List: TGList<TItem>); virtual; abstract;
    2627    function Read: TItem; virtual; abstract;
    2728    function ReadArray(Count: TIndex): TItemArray; virtual; abstract;
     
    3536  end;
    3637
     38  TGMemoryStream<TItem> = class(TGStream<TItem>)
     39  private
     40    FList: TGList<TItem>;
     41    FPosition: TIndex;
     42  public
     43    procedure Assign(Source: TGStream<TItem>); override;
     44    procedure Write(Item: TItem); override;
     45    procedure WriteArray(Values: array of TItem); override;
     46    procedure WriteList(List: TGList<TItem>); override;
     47    function Read: TItem; override;
     48    function ReadArray(Count: TIndex): TItemArray; override;
     49    function ReadList(List: TGList<TItem>; Count: TIndex): TIndex;
     50    function Insert(Count: TIndex): Integer; override;
     51    function Remove(Count: TIndex): Integer; override;
     52    function Seek(Offset: TIndex; Origin: TSeekOrigin = soCurrent): TIndex; override;
     53    constructor Create; override;
     54    destructor Destroy; override;
     55    property List: TGList<TItem> read FList;
     56  end;
     57
    3758
    3859implementation
    3960
     61
     62{ TGStream }
    4063
    4164procedure TGStream<TItem>.Assign(Source: TGStream<TItem>);
     
    85108end;
    86109
     110{ TMemoryStreamByte }
     111
     112procedure TGMemoryStream<TItem>.Assign(Source: TGStream<TItem>);
     113begin
     114  inherited;
     115  if Source is TGMemoryStream<TItem> then begin
     116    FList.Assign(TGMemoryStream<TItem>(Source).FList);
     117    FPosition := TGMemoryStream<TItem>(Source).FPosition;
     118  end;
     119end;
     120
     121procedure TGMemoryStream<TItem>.Write(Item: TItem);
     122begin
     123  if FList.Count < (FPosition + 1) then
     124    FList.Count := FPosition + 1;
     125  FList[FPosition] := Item;
     126  Inc(FPosition);
     127end;
     128
     129procedure TGMemoryStream<TItem>.WriteArray(Values: array of TItem);
     130begin
     131  if FList.Count < (FPosition + Length(Values)) then
     132    FList.Count := FPosition + Length(Values);
     133  FList.ReplaceArray(FPosition, Values);
     134  Inc(FPosition, Length(Values));
     135end;
     136
     137procedure TGMemoryStream<TItem>.WriteList(List: TGList<TItem>);
     138begin
     139  FList.ReplaceList(FPosition, List);
     140end;
     141
     142function TGMemoryStream<TItem>.Read: TItem;
     143begin
     144  Result := FList[FPosition];
     145  Inc(FPosition);
     146end;
     147
     148function TGMemoryStream<TItem>.ReadArray(Count: TIndex): TItemArray;
     149begin
     150  Result := FList.GetArray(FPosition, Count);
     151end;
     152
     153function TGMemoryStream<TItem>.ReadList(List: TGList<TItem>; Count: TIndex): TIndex;
     154begin
     155  if (FPosition + Count) > FList.Count then
     156    Count := FList.Count - FPosition;
     157  FList.GetList(List, FPosition, Count);
     158  Result := Count;
     159end;
     160
     161function TGMemoryStream<TItem>.Insert(Count: TIndex): TIndex;
     162begin
     163  FList.InsertCount(FPosition, Count);
     164  Result := Count;
     165end;
     166
     167function TGMemoryStream<TItem>.Remove(Count: TIndex): TIndex;
     168begin
     169  Result := FList.Count - FPosition;
     170  if Count < Result then Result := Count;
     171  FList.DeleteItems(FPosition, Count);
     172end;
     173
     174function TGMemoryStream<TItem>.Seek(Offset: TIndex; Origin: TSeekOrigin): TIndex;
     175begin
     176  case Origin of
     177    soBeginning: FPosition := Offset;
     178    soCurrent: FPosition := FPosition + Offset;
     179    soEnd: FPosition := FList.Count + Offset;
     180  end;
     181  if FPosition > FList.Count then FPosition := FList.Count;
     182  if FPosition < 0 then FPosition := 0;
     183  Result := FPosition;
     184end;
     185
     186constructor TGMemoryStream<TItem>.Create;
     187begin
     188  inherited;
     189  FList := TGList<TItem>.Create;
     190end;
     191
     192destructor TGMemoryStream<TItem>.Destroy;
     193begin
     194  FList.Free;
     195  inherited Destroy;
     196end;
     197
     198
    87199end.
Note: See TracChangeset for help on using the changeset viewer.