1 | unit UMemory;
|
---|
2 |
|
---|
3 | {$mode Delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils;
|
---|
9 |
|
---|
10 | type
|
---|
11 |
|
---|
12 | { TMemory }
|
---|
13 |
|
---|
14 | TMemory = class
|
---|
15 | private
|
---|
16 | FData: PByte;
|
---|
17 | FSize: Integer;
|
---|
18 | function GetItem(Index: Integer): Byte;
|
---|
19 | procedure SetItem(Index: Integer; AValue: Byte);
|
---|
20 | procedure SetSize(AValue: Integer); virtual;
|
---|
21 | public
|
---|
22 | procedure Clear(Value: Byte = 0);
|
---|
23 | procedure Assign(Source: TMemory);
|
---|
24 | constructor Create;
|
---|
25 | destructor Destroy; override;
|
---|
26 | property Data: PByte read FData;
|
---|
27 | property Size: Integer read FSize write SetSize;
|
---|
28 | property Items[Index: Integer]: Byte read GetItem write SetItem; default;
|
---|
29 | end;
|
---|
30 |
|
---|
31 | { TPositionMemory }
|
---|
32 |
|
---|
33 | TPositionMemory = class(TMemory)
|
---|
34 | private
|
---|
35 | FPosition: Integer;
|
---|
36 | protected
|
---|
37 | procedure SetSize(AValue: Integer); override;
|
---|
38 | public
|
---|
39 | procedure WriteByte(Value: Byte);
|
---|
40 | function ReadByte: Byte;
|
---|
41 | property Position: Integer read FPosition write FPosition;
|
---|
42 | end;
|
---|
43 |
|
---|
44 | implementation
|
---|
45 |
|
---|
46 | { TPositionMemory }
|
---|
47 |
|
---|
48 | procedure TPositionMemory.SetSize(AValue: Integer);
|
---|
49 | begin
|
---|
50 | inherited SetSize(AValue);
|
---|
51 | if FPosition > FSize then FPosition := FSize;
|
---|
52 | end;
|
---|
53 |
|
---|
54 | procedure TPositionMemory.WriteByte(Value: Byte);
|
---|
55 | begin
|
---|
56 | if FPosition >= Size then Size := FPosition + 1;
|
---|
57 | Items[FPosition] := Value;
|
---|
58 | Inc(FPosition);
|
---|
59 | end;
|
---|
60 |
|
---|
61 | function TPositionMemory.ReadByte: Byte;
|
---|
62 | begin
|
---|
63 | if FPosition >= Size then Size := FPosition + 1;
|
---|
64 | Result := Items[FPosition];
|
---|
65 | Inc(FPosition);
|
---|
66 | end;
|
---|
67 |
|
---|
68 | { TMemory }
|
---|
69 |
|
---|
70 | procedure TMemory.SetSize(AValue: Integer);
|
---|
71 | begin
|
---|
72 | if FSize = AValue then Exit;
|
---|
73 | FSize := AValue;
|
---|
74 | FData := ReAllocMem(FData, FSize);
|
---|
75 | end;
|
---|
76 |
|
---|
77 | function TMemory.GetItem(Index: Integer): Byte;
|
---|
78 | begin
|
---|
79 | Result := PByte(FData + Index)^;
|
---|
80 | end;
|
---|
81 |
|
---|
82 | procedure TMemory.SetItem(Index: Integer; AValue: Byte);
|
---|
83 | begin
|
---|
84 | PByte(FData + Index)^ := AValue;
|
---|
85 | end;
|
---|
86 |
|
---|
87 | procedure TMemory.Clear(Value: Byte);
|
---|
88 | begin
|
---|
89 | FillChar(FData^, Size, Value);
|
---|
90 | end;
|
---|
91 |
|
---|
92 | procedure TMemory.Assign(Source: TMemory);
|
---|
93 | begin
|
---|
94 | Size := Source.Size;
|
---|
95 | Move(Source.Data^, FData^, Size);
|
---|
96 | end;
|
---|
97 |
|
---|
98 | constructor TMemory.Create;
|
---|
99 | begin
|
---|
100 | FData := nil;
|
---|
101 | FSize := 0;
|
---|
102 | end;
|
---|
103 |
|
---|
104 | destructor TMemory.Destroy;
|
---|
105 | begin
|
---|
106 | Size := 0;
|
---|
107 | inherited Destroy;
|
---|
108 | end;
|
---|
109 |
|
---|
110 | end.
|
---|
111 |
|
---|