source: trunk/Packages/Common/UMemory.pas

Last change on this file was 41, checked in by chronos, 6 years ago
  • Modified: Build under Lazarus 2.0.
  • Modified: Used .lrj files instead of .lrt files.
  • Modified: Removed TemplateGenerics package.
File size: 2.6 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 procedure WriteMemory(Position: Integer; Memory: TMemory);
27 procedure ReadMemory(Position: Integer; Memory: TMemory);
28 property Data: PByte read FData;
29 property Size: Integer read FSize write SetSize;
30 property Items[Index: Integer]: Byte read GetItem write SetItem; default;
31 end;
32
33 { TPositionMemory }
34
35 TPositionMemory = class(TMemory)
36 private
37 FPosition: Integer;
38 protected
39 procedure SetSize(AValue: Integer); override;
40 public
41 procedure WriteByte(Value: Byte);
42 function ReadByte: Byte;
43 property Position: Integer read FPosition write FPosition;
44 end;
45
46implementation
47
48{ TPositionMemory }
49
50procedure TPositionMemory.SetSize(AValue: Integer);
51begin
52 inherited SetSize(AValue);
53 if FPosition > FSize then FPosition := FSize;
54end;
55
56procedure TPositionMemory.WriteByte(Value: Byte);
57begin
58 if FPosition >= Size then Size := FPosition + 1;
59 Items[FPosition] := Value;
60 Inc(FPosition);
61end;
62
63function TPositionMemory.ReadByte: Byte;
64begin
65 if FPosition >= Size then Size := FPosition + 1;
66 Result := Items[FPosition];
67 Inc(FPosition);
68end;
69
70{ TMemory }
71
72procedure TMemory.SetSize(AValue: Integer);
73begin
74 if FSize = AValue then Exit;
75 FSize := AValue;
76 FData := ReAllocMem(FData, FSize);
77end;
78
79function TMemory.GetItem(Index: Integer): Byte;
80begin
81 Result := PByte(FData + Index)^;
82end;
83
84procedure TMemory.SetItem(Index: Integer; AValue: Byte);
85begin
86 PByte(FData + Index)^ := AValue;
87end;
88
89procedure TMemory.Clear(Value: Byte);
90begin
91 FillChar(FData^, Size, Value);
92end;
93
94procedure TMemory.Assign(Source: TMemory);
95begin
96 Size := Source.Size;
97 Move(Source.Data^, FData^, Size);
98end;
99
100constructor TMemory.Create;
101begin
102 FData := nil;
103 FSize := 0;
104end;
105
106destructor TMemory.Destroy;
107begin
108 Size := 0;
109 inherited Destroy;
110end;
111
112procedure TMemory.WriteMemory(Position: Integer; Memory: TMemory);
113begin
114 Move(Memory.FData, PByte(PByte(@FData) + Position)^, Memory.Size);
115end;
116
117procedure TMemory.ReadMemory(Position: Integer; Memory: TMemory);
118begin
119 Move(PByte(PByte(@FData) + Position)^, Memory.FData, Memory.Size);
120end;
121
122end.
123
Note: See TracBrowser for help on using the repository browser.