-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathUComEnumerator.pas
121 lines (96 loc) · 2.86 KB
/
UComEnumerator.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
unit UComEnumerator;
interface
type
IEnumVariant = interface(IUnknown)
['{00020404-0000-0000-C000-000000000046}']
function Next(celt: LongWord; var rgvar : OleVariant;
out pceltFetched: LongWord): HResult; stdcall;
function Skip(celt: LongWord): HResult; stdcall;
function Reset: HResult; stdcall;
function Clone(out Enum: IEnumVariant): HResult; stdcall;
end;
// the "TComEnumerator" class basically just follows the outline of Borlands
// Enumerator classes. All methods are implemented as asked for in the Delphi
// 2005 Help
TComEnumerator = class
private
FEnum: IEnumVariant;
FCurrent: IInterface;
public
constructor Create(aEnum: IInterface);
function GetEnumerator: TComEnumerator;
function GetCurrent: IInterface;
function MoveNext: Boolean;
property Current: IInterface read GetCurrent;
end;
// use "GetCOMEnumerator" to get an "TComEnumerator" class for your
// for...in...do constructs
// - Paramter aEnum
// an interface that implements the default IEnumVariant interface as
// defined by Microsoft (and found in ActiveX.pas)
// - Return value
// the Enumerator class that is used in for...in...do constructs.
//
// Attention: Call GetCOMEnumerator() each time you want to pass an enumeration
// to Delphi's for...in...do construct. Do not cache this object, Delphi will
// automatically destroy it (using an internal try...finally...end block) once
// the for...in...do loop has been finished.
//
// Note: You can use GetCOMEnumerator() on any COM Enum Variant collection. To
// access your object directly simply cast the enumerated value to the COM type
// needed (provided that it is supported ;))
function GetCOMEnumerator(aEnum: IInterface): TComEnumerator;
implementation
function GetCOMEnumerator(aEnum: IInterface): TComEnumerator;
begin
Result := TComEnumerator.Create(aEnum);
end;
{ TComEnumerator }
constructor TComEnumerator.Create(aEnum: IInterface);
begin
inherited Create;
try
// save the enumeration
FEnum := aEnum as IEnumVariant;
except
// fetch developer errors :-O
FEnum := nil;
end;
end;
function TComEnumerator.GetCurrent: IInterface;
begin
// return the current object
Result := FCurrent;
end;
function TComEnumerator.GetEnumerator: TComEnumerator;
begin
// return the class itself as enumerator for the Delphi-Language
Result := Self;
end;
function TComEnumerator.MoveNext: Boolean;
var
OleCurrent: OleVariant;
Fetched: Cardinal;
begin
if FEnum <> nil then
begin
// fetch the next element from the collection list
FEnum.Next(1, OleCurrent, Fetched);
if Fetched = 1 then
begin
// another object was fetched
FCurrent := OleCurrent;
Result := True;
end
else
begin
// no more objects in enumaration
Result := False;
end;
end
else
begin
Result := False;
end;
end;
end.