-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathd2dClasses.pas
126 lines (112 loc) · 2.63 KB
/
d2dClasses.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
//---------------------------------------------------------------------------
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// License for the specific language governing rights and limitations
// under the License.
//---------------------------------------------------------------------------
unit d2dClasses;
interface
type
Td2dProtoObjectPrim = class(TObject, IInterface)
private
f_RefCount: Integer;
protected
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
protected
procedure Cleanup; virtual;
procedure FreeInstance; override;
public
destructor Destroy; override;
class function NewInstance: TObject; override;
function Use: Pointer;
property RefCount: Integer read f_RefCount;
end;
Td2dProtoObject = class(Td2dProtoObjectPrim)
protected
destructor Destroy;
end;
implementation
{.$DEFINE LogRefCount}
uses
Windows
{$IFDEF LogRefCount}
,d2dCore
{$ENDIF}
;
destructor Td2dProtoObjectPrim.Destroy;
begin
if InterlockedDecrement(f_RefCount) = 0 then
begin
Inc(f_RefCount);
try
try
Cleanup;
finally
inherited Destroy;
end;
finally
Dec(f_RefCount);
end;
end;
end;
procedure Td2dProtoObjectPrim.Cleanup;
begin
// empty in base class
end;
procedure Td2dProtoObjectPrim.FreeInstance;
begin
if f_RefCount = 0 then
begin
{$IFDEF LogRefCount}
gD2DE.System_Log('%s destroyed.', [ClassName]);
{$ENDIF}
inherited FreeInstance;
end
end;
class function Td2dProtoObjectPrim.NewInstance: TObject;
begin
Result := inherited NewInstance;
{$IFDEF LogRefCount}
gD2DE.System_Log('%s created.', [ClassName]);
{$ENDIF}
Td2dProtoObjectPrim(Result).Use;
end;
function Td2dProtoObjectPrim.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
if GetInterface(IID, Obj) then
Result := 0
else
Result := E_NOINTERFACE;
end;
function Td2dProtoObjectPrim.Use: Pointer;
begin
if Self <> nil then
InterlockedIncrement(f_RefCount);
Result := Self;
end;
function Td2dProtoObjectPrim._AddRef: Integer;
begin
Use;
Result := f_RefCount;
end;
function Td2dProtoObjectPrim._Release: Integer;
var
l_RC: Integer;
begin
l_RC := f_RefCount - 1;
Free;
Result := l_RC;
end;
destructor Td2dProtoObject.Destroy;
begin
Assert(False, 'This should never happen!');
inherited;
end;
end.