-
Notifications
You must be signed in to change notification settings - Fork 2
/
ObjectTree.pas
executable file
·132 lines (107 loc) · 3.19 KB
/
ObjectTree.pas
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
{*
* Object Tree: Object tree written in Pascal/Delphi
* Jonas Raoni Soares da Silva <http://raoni.org>
* https://github.com/jonasraoni/object-tree
*}
unit ObjectTree;
interface
uses
SysUtils, Classes;
type
TObjectTree = class;
TObjectTree = class
private
FData: TObject;
FOwner: TObjectTree;
FList: TList;
FOwnData: Boolean;
function GetItem(const Index: Integer): TObjectTree;
function GetCount: Integer;
function GetObject(const Index: Integer): TObject;
public
constructor Create( const AOwnData: Boolean = False ); overload;
constructor Create( AOwner: TObjectTree; const AOwnData: Boolean = False ); overload;
destructor Destroy; override;
property Data: TObject read FData write FData;
property Owner: TObjectTree read FOwner;
property Items[ const Index: Integer ]: TObjectTree read GetItem; default;
property Objects[ const Index: Integer ]: TObject read GetObject;
property Count: Integer read GetCount;
property OwnData: Boolean read FOwnData write FOwnData;
function Add( AObject: TObject ): Integer;
procedure Insert( AObject: TObject; const Index: Integer );
procedure Move( const CurIndex, NewIndex: Integer );
procedure Exchange( const OldIndex, NewIndex: Integer );
procedure Delete( const Index: Integer);
procedure Clear;
end;
implementation
{ TObjectTree }
function TObjectTree.Add( AObject: TObject ): Integer;
begin
TObjectTree( FList[ FList.Add( TObjectTree.Create( Self, FOwnData ) ) ] ).Data := AObject;
end;
procedure TObjectTree.Clear;
begin
while FList.Count > 0 do
Delete( 0 );
end;
constructor TObjectTree.Create( AOwner: TObjectTree; const AOwnData: Boolean );
begin
FOwner := AOwner;
Create( AOwnData );
end;
constructor TObjectTree.Create( const AOwnData: Boolean );
begin
FOwnData := AOwnData;
FList := TList.Create;
end;
procedure TObjectTree.Delete(const Index: Integer);
begin
try
with Items[ Index ] do begin
if OwnData and Assigned( Data ) then
Data.Free;
Free;
end;
FList.Delete( Index );
except
raise Exception.Create( ClassName + '.Delete :: Error while removing the item.' );
end;
end;
destructor TObjectTree.Destroy;
begin
if FOwnData and Assigned( FData ) then
FData.Free;
Clear;
FList.Free;
inherited;
end;
procedure TObjectTree.Exchange(const OldIndex, NewIndex: Integer);
begin
FList.Exchange( OldIndex, NewIndex );
end;
function TObjectTree.GetCount: Integer;
begin
Result := FList.Count;
end;
function TObjectTree.GetItem(const Index: Integer): TObjectTree;
begin
Result := FList[ Index ];
end;
function TObjectTree.GetObject(const Index: Integer): TObject;
begin
Result := Items[ Index ];
if Assigned( Result ) then
Result := TObjectTree( Result ).Data;
end;
procedure TObjectTree.Insert( AObject: TObject; const Index: Integer);
begin
FList.Insert( Index, TObjectTree.Create( Self, FOwnData ) );
TObjectTree( FList[ Index ] ).Data := AObject;
end;
procedure TObjectTree.Move(const CurIndex, NewIndex: Integer);
begin
FList.Move( CurIndex, NewIndex );
end;
end.