source: trunk/Packages/Common/UMemory.pas

Last change on this file was 18, checked in by chronos, 12 years ago
  • Used external packages are now stored in uncompressed form rather in zipped files. This allow better package version synchronisation.
File size: 2.2 KB
Line 
1unit UMemory;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils;
9
10type
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
44implementation
45
46{ TPositionMemory }
47
48procedure TPositionMemory.SetSize(AValue: Integer);
49begin
50 inherited SetSize(AValue);
51 if FPosition > FSize then FPosition := FSize;
52end;
53
54procedure TPositionMemory.WriteByte(Value: Byte);
55begin
56 if FPosition >= Size then Size := FPosition + 1;
57 Items[FPosition] := Value;
58 Inc(FPosition);
59end;
60
61function TPositionMemory.ReadByte: Byte;
62begin
63 if FPosition >= Size then Size := FPosition + 1;
64 Result := Items[FPosition];
65 Inc(FPosition);
66end;
67
68{ TMemory }
69
70procedure TMemory.SetSize(AValue: Integer);
71begin
72 if FSize = AValue then Exit;
73 FSize := AValue;
74 FData := ReAllocMem(FData, FSize);
75end;
76
77function TMemory.GetItem(Index: Integer): Byte;
78begin
79 Result := PByte(FData + Index)^;
80end;
81
82procedure TMemory.SetItem(Index: Integer; AValue: Byte);
83begin
84 PByte(FData + Index)^ := AValue;
85end;
86
87procedure TMemory.Clear(Value: Byte);
88begin
89 FillChar(FData^, Size, Value);
90end;
91
92procedure TMemory.Assign(Source: TMemory);
93begin
94 Size := Source.Size;
95 Move(Source.Data^, FData^, Size);
96end;
97
98constructor TMemory.Create;
99begin
100 FData := nil;
101 FSize := 0;
102end;
103
104destructor TMemory.Destroy;
105begin
106 Size := 0;
107 inherited Destroy;
108end;
109
110end.
111
Note: See TracBrowser for help on using the repository browser.