-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunit_a.pas
93 lines (75 loc) · 1.71 KB
/
unit_a.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
unit unit_a;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, unit_interfaces;
type
TArray = class(TList)
private
function GetItems(AIndex: Integer): IMyInterface;
procedure SetItems(AIndex: Integer; AValue: IMyInterface);
public
destructor Destroy; override;
function Add(Item: IMyInterface): Integer; reintroduce;
procedure PrintListInfo;
procedure PrintListInfoAsTObject;
procedure PrintListInfoAsTMyObject;
property Items[AIndex: Integer] : IMyInterface read GetItems write SetItems;
end;
implementation
uses
unit_obj;
{ TArray }
function TArray.GetItems(AIndex: Integer): IMyInterface;
begin
Result := IMyInterface(Inherited Items[AIndex]);
end;
procedure TArray.SetItems(AIndex: Integer; AValue: IMyInterface);
begin
Inherited Items[AIndex] := AValue;
end;
destructor TArray.Destroy;
var
i: Integer;
begin
for i := 0 to Count-1 do
Items[i].GetObject.Free;
inherited Destroy;
end;
function TArray.Add(Item: IMyInterface): Integer;
begin
Result := inherited Add(Item);
end;
procedure TArray.PrintListInfo;
var
i: Integer;
begin
for i := 0 to Count-1 do
begin
Items[i].DoSomething;
end;
end;
procedure TArray.PrintListInfoAsTObject;
var
i: Integer;
begin
for i := 0 to Count-1 do
begin
// lookup IMyInterface from Object
(Items[i].GetObject as IMyInterface).DoSomething;
end;
end;
procedure TArray.PrintListInfoAsTMyObject;
var
i: Integer;
begin
for i := 0 to Count-1 do
begin
// Esentially casting IMyInterface(TMyObject)
IMyInterface(TMyObject(Items[i].GetObject)).DoSomething;
// The same as
// TMyObject(Items[i].GetObject).DoSomething;
// which is probably a bit faster. Why have the interface?
end;
end;
end.