source: Generics/NativeGenerics/Generic/GenericStream.pas

Last change on this file was 496, checked in by chronos, 7 years ago
  • Modified: New native generics classes working under FPC 3.0 transformed from TemplateGenerics package.
File size: 2.1 KB
Line 
1unit GenericStream;
2
3{$mode delphi}
4
5interface
6
7type
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
37implementation
38
39procedure TGStream<T>.Assign(Source: TGStream<T>);
40begin
41end;
42
43procedure TGStream<T>.SetPosition(AValue: Integer);
44begin
45 Seek(AValue, soBegin);
46end;
47
48function TGStream<T>.GetPosition: Integer;
49begin
50 Result := Seek(0, soCurrent);
51end;
52
53procedure TGStream<T>.SetSize(AValue: Integer);
54var
55 StreamSize: Integer;
56 OldPosition: Integer;
57begin
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;
69end;
70
71function TGStream<T>.GetSize: Integer;
72var
73 OldPosition: Integer;
74begin
75 OldPosition := Position;
76 Result := Seek(0, soEnd);
77 Position := OldPosition;
78end;
79
80constructor TGStream<T>.Create;
81begin
82 inherited;
83end;
84
85end.
Note: See TracBrowser for help on using the repository browser.