1 | unit GenericStream;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | type
|
---|
8 | TSeekOrigin = (soBegin, soCurrent, soEnd);
|
---|
9 |
|
---|
10 | TGStream<T> = class
|
---|
11 | public
|
---|
12 | type
|
---|
13 | TItemArray = array of T;
|
---|
14 | private
|
---|
15 | procedure SetSize(AValue: Integer);
|
---|
16 | function GetSize: Integer;
|
---|
17 | procedure SetPosition(AValue: Integer);
|
---|
18 | function GetPosition: Integer;
|
---|
19 | public
|
---|
20 | procedure Assign(Source: TGStream<T>); virtual;
|
---|
21 | procedure Write(Item: T); virtual; abstract;
|
---|
22 | procedure WriteArray(Item: array of T); virtual; abstract;
|
---|
23 | procedure WriteStream(Stream: TGStream<T>; Count: Integer); virtual; abstract;
|
---|
24 | function Read: T; virtual; abstract;
|
---|
25 | function ReadArray(Count: Integer): TItemArray; virtual; abstract;
|
---|
26 | function ReadStream(Stream: TGStream<T>; Count: Integer): Integer; virtual; abstract;
|
---|
27 | function Insert(Count: Integer): Integer; virtual; abstract;
|
---|
28 | function Remove(Count: Integer): Integer; virtual; abstract;
|
---|
29 | function Seek(Offset: Integer; Origin: TSeekOrigin = soCurrent):
|
---|
30 | Integer; virtual; abstract;
|
---|
31 | constructor Create; virtual;
|
---|
32 | property Position: Integer read GetPosition write SetPosition;
|
---|
33 | property Size: Integer read GetSize write SetSize;
|
---|
34 | end;
|
---|
35 |
|
---|
36 |
|
---|
37 | implementation
|
---|
38 |
|
---|
39 | procedure TGStream<T>.Assign(Source: TGStream<T>);
|
---|
40 | begin
|
---|
41 | end;
|
---|
42 |
|
---|
43 | procedure TGStream<T>.SetPosition(AValue: Integer);
|
---|
44 | begin
|
---|
45 | Seek(AValue, soBegin);
|
---|
46 | end;
|
---|
47 |
|
---|
48 | function TGStream<T>.GetPosition: Integer;
|
---|
49 | begin
|
---|
50 | Result := Seek(0, soCurrent);
|
---|
51 | end;
|
---|
52 |
|
---|
53 | procedure TGStream<T>.SetSize(AValue: Integer);
|
---|
54 | var
|
---|
55 | StreamSize: Integer;
|
---|
56 | OldPosition: Integer;
|
---|
57 | begin
|
---|
58 | OldPosition := Seek(0, soCurrent);
|
---|
59 | StreamSize := Size;
|
---|
60 | if AValue > StreamSize then begin
|
---|
61 | Seek(StreamSize, soBegin);
|
---|
62 | Insert(AValue - StreamSize);
|
---|
63 | end else
|
---|
64 | if AValue < StreamSize then begin
|
---|
65 | Seek(AValue, soBegin);
|
---|
66 | Remove(StreamSize - AValue);
|
---|
67 | end;
|
---|
68 | Position := OldPosition;
|
---|
69 | end;
|
---|
70 |
|
---|
71 | function TGStream<T>.GetSize: Integer;
|
---|
72 | var
|
---|
73 | OldPosition: Integer;
|
---|
74 | begin
|
---|
75 | OldPosition := Position;
|
---|
76 | Result := Seek(0, soEnd);
|
---|
77 | Position := OldPosition;
|
---|
78 | end;
|
---|
79 |
|
---|
80 | constructor TGStream<T>.Create;
|
---|
81 | begin
|
---|
82 | inherited;
|
---|
83 | end;
|
---|
84 |
|
---|
85 | end.
|
---|