Ignore:
Timestamp:
Feb 7, 2012, 2:03:20 PM (12 years ago)
Author:
chronos
Message:
File:
1 edited

Legend:

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

    r313 r320  
    11unit GenericStream;
    22
    3 {$mode Delphi}{$H+}
     3{$mode delphi}{$H+}
    44
    55interface
    66
    77uses
    8   Classes, SysUtils;
     8  SysUtils, Classes, GenericList;
    99
    1010type
    1111  TGStream<TItem> = class
     12  public
     13    type
     14      TIndex = NativeInt;
     15      TItemArray = array of TItem;
     16      TSeekOrigin = (soBeginning, soCurrent, soEnd);
     17  private
     18    procedure SetSize(AValue: TIndex);
     19    function GetSize: TIndex;
     20    procedure SetPosition(AValue: TIndex);
     21    function GetPosition: TIndex;
     22  public
     23    procedure Assign(Source: TGStream<TItem>); virtual;
     24    procedure Write(Item: TItem); virtual; abstract;
     25    procedure WriteArray(Item: array of TItem); virtual; abstract;
     26    function Read: TItem; virtual; abstract;
     27    function ReadArray(Count: TIndex): TItemArray; virtual; abstract;
     28    function Insert(Count: TIndex): TIndex; virtual; abstract;
     29    function Remove(Count: TIndex): TIndex; virtual; abstract;
     30    function Seek(Offset: TIndex; Origin: TSeekOrigin = soCurrent):
     31      TIndex; virtual; abstract;
     32    constructor Create; virtual;
     33    property Position: TIndex read GetPosition write SetPosition;
     34    property Size: TIndex read GetSize write SetSize;
     35  end;
    1236
    13   end;
    1437
    1538implementation
    1639
     40
     41procedure TGStream<TItem>.Assign(Source: TGStream<TItem>);
     42begin
     43end;
     44
     45procedure TGStream<TItem>.SetPosition(AValue: TIndex);
     46begin
     47  Seek(AValue, soBeginning);
     48end;
     49
     50function TGStream<TItem>.GetPosition: TIndex;
     51begin
     52  Result := Seek(0, soCurrent);
     53end;
     54
     55procedure TGStream<TItem>.SetSize(AValue: TIndex);
     56var
     57  StreamSize: TIndex;
     58  OldPosition: TIndex;
     59begin
     60  OldPosition := Seek(0, soCurrent);
     61  StreamSize := Size;
     62  if AValue > StreamSize then begin
     63    Seek(StreamSize, soBeginning);
     64    Insert(AValue - StreamSize);
     65  end else
     66  if AValue < StreamSize then begin
     67    Seek(AValue, soBeginning);
     68    Remove(StreamSize - AValue);
     69  end;
     70  Position := OldPosition;
     71end;
     72
     73function TGStream<TItem>.GetSize: TIndex;
     74var
     75  OldPosition: Integer;
     76begin
     77  OldPosition := Position;
     78  Result := Seek(0, soEnd);
     79  Position := OldPosition;
     80end;
     81
     82constructor TGStream<TItem>.Create;
     83begin
     84  inherited;
     85end;
     86
    1787end.
    18 
Note: See TracChangeset for help on using the changeset viewer.