-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjArray.pas
More file actions
95 lines (77 loc) · 2.49 KB
/
Copy pathObjArray.pas
File metadata and controls
95 lines (77 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// ----------------------------------------------------------------------------
// Simple array-object to keep a list of the objects currently used by Dll.
// ----------------------------------------------------------------------------
unit ObjArray;
interface
type
PObjArrayType = ^TObjArrayType;
TObjArrayType = array[0..0] of TObject;
TObjArray = class
private
FCount : Integer;
FGrow : Integer;
FObjects : PObjArrayType;
function GetObject( Index: Integer ) : TObject;
procedure SetObject( Index: Integer; Value: TObject );
procedure Grow( NewCount: Integer );
public
property Count: Integer read FCount;
property Objects[Index: Integer]: TObject read GetObject write SetObject;
function GetFreeIndex : Integer;
function IsValid( Index: Integer ) : Boolean;
constructor Create( InitCount, InitGrow: Integer );
destructor Destroy; override;
end;
implementation
function TObjArray.GetObject( Index: Integer ) : TObject;
begin
if IsValid(Index) then Result:= FObjects[Index]
else Result := nil;
end;
procedure TObjArray.SetObject( Index: Integer; Value: TObject );
begin
if (Index>=0) and (Index<FCount) then FObjects[Index] := Value;
end;
function TObjArray.GetFreeIndex : Integer;
var i : Integer;
begin
for i:=0 to FCount-1 do begin
if FObjects[i]=nil then begin Result:=i; exit; end;
end;
Result := FCount;
Grow( FCount + FGrow );
end;
procedure TObjArray.Grow( NewCount: Integer );
var NewValues : PObjArrayType;
i : Integer;
begin
if NewCount<=FCount then exit;
GetMem( NewValues, NewCount * sizeof(Pointer) );
for i:=0 to FCount-1 do NewValues[i]:=FObjects[i];
for i:=FCount to NewCount-1 do FObjects[i]:=nil;
FreeMem( FObjects, FCount * sizeof(Pointer) );
FObjects := NewValues;
FCount := NewCount;
end;
function TObjArray.IsValid( Index: Integer ) : Boolean;
begin
Result := False;
if (Index<0) or (Index>=FCount) then exit;
if FObjects[Index]=nil then exit;
Result := True;
end;
constructor TObjArray.Create( InitCount, InitGrow: Integer );
var i : Integer;
begin
inherited Create;
FCount := InitCount; if FCount<1 then FCount:=1;
FGrow := InitGrow; if FGrow <1 then FGrow :=1;
GetMem( FObjects, FCount * sizeof(Pointer) );
for i:=0 to FCount-1 do FObjects[i]:=nil;
end;
destructor TObjArray.Destroy;
begin
FreeMem( FObjects, FCount * sizeof(Pointer) );
inherited Destroy;
end;
end.