-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathMVCBr.Factory.pas
349 lines (298 loc) · 7.47 KB
/
MVCBr.Factory.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
unit MVCBr.Factory;
{ *************************************************************************** }
{ }
{ MVCBr é o resultado de esforços de um grupo }
{ }
{ Copyright (C) 2017 MVCBr }
{ }
{ amarildo lacerda }
{ http://www.tireideletra.com.br }
{ }
{ }
{ *************************************************************************** }
{ }
{ Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 }
{ }
{ Unless required by applicable law or agreed to in writing, software }
{ distributed under the License is distributed on an "AS IS" BASIS, }
{ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. }
{ See the License for the specific language governing permissions and }
{ limitations under the License. }
{ }
{ *************************************************************************** }
interface
uses System.Classes, System.SysUtils, System.TypInfo;
Type
IMVCBrFactory = interface
['{D6FEBA20-5381-48FC-B980-694B1D001DAC}']
function This: TObject;
Procedure Lock;
procedure UnLock;
procedure Release;
function RefCount:Integer;
end;
TMVCBrFactory = class(TInterfacedObject, IMVCBrFactory)
private
FLock: TObject;
FIsUnsafe: boolean;
FDefaultSafeted: IInterface;
[Hide]
[unsafe]
FDefault: IInterface;
function GetDefault: IInterface;
public
//Constructor New;virtual;
constructor Create; overload; virtual;
constructor Create(AInterface: IInterface; AUnsafe: boolean = true);
overload; virtual;
destructor Destroy; override;
procedure SetUnsafe(AInterface: IInterface); virtual;
procedure SetSafeted(AInterface: IInterface); virtual;
[Hide]
property Default: IInterface read GetDefault;
class function NewInstance<TInterface: IInterface>(AClass: TClass)
: TInterface; static;
function This: TObject; virtual;
Procedure Lock; overload; virtual;
procedure UnLock; virtual;
procedure Release; virtual;
function RefCount:Integer;
end;
TMVCBrAggregatedFactory = class(TObject)
private
[unsafe]
FController: IInterface;
FDisabledRefCount: boolean; // unsafe/weak reference to controller
function GetDefault: IInterface;
procedure SetDisabledRefCount(const Value: boolean);
public
property Default: IInterface read GetDefault;
function QueryInterface(const IID: TGUID; out Obj): HResult; STDCALL;
function _AddRef: Integer; STDCALL;
function _Release: Integer; STDCALL;
procedure Release; virtual;
constructor Create(const Controller: IInterface);
destructor Destroy; override;
property DisabledRefCount: boolean read FDisabledRefCount
write SetDisabledRefCount;
end;
TMVCBrContainedFactory = class(TMVCBrAggregatedFactory)
public
function QueryInterface(const IID: TGUID; out Obj): HResult; STDCALL;
end;
TMVCBrHelperFactory<T: Class> = class(TInterfacedObject)
protected
FInstance: T;
public
constructor Create(Instance: T);
destructor Destroy; override;
property Default: T read FInstance;
end;
TMVCBrStaticFactory<T> = class(TObject)
protected
FInstance: T;
public
constructor Create(AInstance: T);
function GetInstance:T;virtual;
property Default: T read GetInstance;
end;
implementation
{ TMVCBrFactory }
constructor TMVCBrFactory.Create;
begin
inherited Create;
FDefaultSafeted := nil;
FIsUnsafe := false;
FDefault := nil;
FRefCount := 1;
FLock := TObject.Create;
end;
constructor TMVCBrFactory.Create(AInterface: IInterface;
AUnsafe: boolean = true);
begin
Create;
if AUnsafe then
SetUnsafe(AInterface)
else
begin
FDefaultSafeted := AInterface;
end;
end;
destructor TMVCBrFactory.Destroy;
begin
FDefault := nil;
FDefaultSafeted := nil;
freeAndNil(FLock);
inherited;
end;
function TMVCBrFactory.GetDefault: IInterface;
begin
if FIsUnsafe then
result := FDefault
else if assigned(FDefaultSafeted) then
result := FDefaultSafeted
else
result := self;
end;
procedure TMVCBrFactory.Lock;
begin
TMonitor.enter(FLock);
end;
{constructor TMVCBrFactory.New;
begin
Create;
end;
}
class function TMVCBrFactory.NewInstance<TInterface>(AClass: TClass)
: TInterface;
var
o: TObject;
pInfo: PTypeInfo;
IID: TGUID;
begin
o := AClass.Create;
pInfo := TypeInfo(TInterface);
IID := GetTypeData(pInfo).Guid;
if not supports(o, IID, result) then
begin
o.free;
raise Exception.Create('O objeto não implementa a interface solicitada');
end;
end;
function TMVCBrFactory.RefCount: Integer;
begin
result := inherited RefCount;
end;
procedure TMVCBrFactory.Release;
begin
end;
procedure TMVCBrFactory.SetSafeted(AInterface: IInterface);
begin
if FDefaultSafeted <> AInterface then
begin
FDefaultSafeted := AInterface;
FDefault := nil;
end;
FIsUnsafe := false;
end;
procedure TMVCBrFactory.SetUnsafe(AInterface: IInterface);
begin
if FDefault <> AInterface then
FDefault := AInterface;
FDefaultSafeted := nil;
FIsUnsafe := true;
end;
function TMVCBrFactory.This: TObject;
begin
result := self;
end;
procedure TMVCBrFactory.UnLock;
begin
TMonitor.Exit(FLock);
end;
{ TMVCBrSingletonFactory<T> }
(*
constructor TMVCBrSingletonFactory<T>.Create();
begin
inherited Create;
end;
class function TMVCBrSingletonFactory<T>.Default: T;
var
LClass: TClass;
begin
LClass := T;
if not assigned(FSingleton) then
FSingleton := LClass.Create as T;
result := FSingleton;
end;
class procedure TMVCBrSingletonFactory<T>.Release;
begin
if assigned(FSingleton) then
FSingleton.disposeOf;
FSingleton := nil;
end;
*)
{ TMVCBrAggregatedFactory }
constructor TMVCBrAggregatedFactory.Create(const Controller: IInterface);
begin
// "unsafe" reference to controller - don't keep it alive
FController := Controller;
end;
destructor TMVCBrAggregatedFactory.Destroy;
begin
FController := nil;
inherited;
end;
function TMVCBrAggregatedFactory.GetDefault: IInterface;
begin
result := FController;
end;
function TMVCBrAggregatedFactory.QueryInterface(const IID: TGUID;
out Obj): HResult;
begin
result := FController.QueryInterface(IID, Obj);
end;
procedure TMVCBrAggregatedFactory.Release;
begin
if FDisabledRefCount then
begin
FDisabledRefCount := false;
end;
_Release;
end;
procedure TMVCBrAggregatedFactory.SetDisabledRefCount(const Value: boolean);
begin
FDisabledRefCount := Value;
end;
function TMVCBrAggregatedFactory._AddRef: Integer;
begin
if not FDisabledRefCount then
result := FController._AddRef
else
result := 1;
end;
function TMVCBrAggregatedFactory._Release: Integer;
begin
if not FDisabledRefCount then
result := FController._Release
else
result := 1;
end;
{ TMVCBrContainedFactory }
function TMVCBrContainedFactory.QueryInterface(const IID: TGUID;
out Obj): HResult;
begin
if GetInterface(IID, Obj) then
result := 0
else
result := E_NOINTERFACE;
end;
{ TMVCBrHelperFactory }
constructor TMVCBrHelperFactory<T>.Create(Instance: T);
begin
inherited Create;
FInstance := Instance;
end;
destructor TMVCBrHelperFactory<T>.Destroy;
begin
FInstance.free;
inherited;
end;
{ TMVCBrStaticFactory<T> }
constructor TMVCBrStaticFactory<T>.Create(AInstance: T);
begin
inherited Create;
FInstance := AInstance;
end;
function TMVCBrStaticFactory<T>.GetInstance: T;
begin
result := FInstance;
end;
initialization
finalization
//TMVCBrSingletonFactory<TObject>.Release;
end.