Menu

[r2591]: / trunk / Src / UBaseObjects.pas  Maximize  Restore  History

Download this file

449 lines (406 with data), 15.0 kB

  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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2005-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Contains various common classes for use as base classes to other classes.
* Classes are:
*
* 1) TNoConstructObject:
* Object that fails if its constructor is called. Provided as a base class
* for classes that contains only class methods and should never be
* constructed.
*
* 2) TNoPublicConstructObject:
* Object that provides a protected constructor but fails if the public
* constructor is called. For use as a base class for objects that are
* constructed via class methods but should not be constructed directly.
*
* 3) TNoPublicConstructIntfObject:
* Same as 2) except class descends from TInterfacedObject instead of
* TObject.
*
* 4) TNonRefCountedObject implements a non reference counted implementation
* of IInterface.
*
* 5) TAggregatedOrLoneObject is a base class for objects that can either
* exist as aggregated objects or as stand-alone reference counted objects.
* This implementation is based on code suggested by Hallvard VossBotn, as
* presented in Eric Harmon's book "Delphi COM programming".
*
* 6) TConditionalFreeObject:
* An abstract base class for objects that cannot be destroyed unless some
* condition is met.
*
* Also provides an interface - INoPublicConstruct - that can be supported by
* objects that don't allow public construction but cannot inherited from
* TNoPublicConstructObject, for example forms.
}
unit UBaseObjects;
interface
type
{
TNoConstructObject:
Class that fails if its constructor is called. Provided as a base class for
classes that contain only class methods and should never be constructed.
}
TNoConstructObject = class(TObject)
public
constructor Create;
{Class constructor. Causes an assertion failure if called. The object is
never constructed.
}
end;
{
TNoPublicConstructObject:
Class that provides a protected constructor but fails if the public
constructor is called. For use as a base class for objects that are
constructed via class methods but should not be constructed directly.
}
TNoPublicConstructObject = class(TObject)
strict protected
constructor InternalCreate;
{Protected class constructor. Does nothing but call inherited constructor.
Should be called by class methods of derived classes instead of inherited
Create.
}
public
constructor Create;
{Class constructor. Causes an assertion failure if called. The object is
never constructed.
}
end;
{
TNoPublicConstructIntfObject:
Class that provides a protected constructor but fails if the public
constructor is called. For use as a base class for reference counted
interfaced objects that are constructed via class methods but should not be
constructed directly.
}
TNoPublicConstructIntfObject = class(TInterfacedObject)
strict protected
constructor InternalCreate;
{Protected class constructor. Does nothing but call inherited constructor.
Should be called by class methods of derived classes instead of inherited
Create.
}
public
constructor Create;
{Class constructor. Causes an assertion failure if called. The object is
never constructed.
}
end;
{
INoPublicConstruct:
Interface that can be supported by objects that do not permit public
construction but that cannot descend from TNoPublicConstructObject. Such
objects must handle their own detection of attempts to call public
constructor. An example of use is in forms that may not be publically
constructed. It is also useful to flag some members of a class heirachy as
no public construct while leaving others able to be constructed.
The interface provides no additional methods - it exists purely for
detection purposes.
To flag an object as no-public-construct, add INoPublicConstruct to its type
definition then test for it in the public constructor using
Supports(Self, INoPublicConstruct).
}
INoPublicConstruct = interface(IInterface)
['{1170DC2C-5B79-453A-94AB-2C35B583F4BD}']
end;
{
TNonRefCountedObject:
Implements a non reference counted implementation of IInterface. It derives
directly from TObject rather than TInterfacedObject since most of the
code of TInterfacedObject is redundant when reference counting not used.
}
TNonRefCountedObject = class(TObject, IInterface)
protected
{ IInterface methods }
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
{Checks the specified interface is supported by this object. If so
reference to interface is passed out.
@param IID [in] Specifies interface being queried.
@param Obj [out] Reference to interface implementation or nil if not
supported.
@result S_OK if interface supported or E_NOINTERFACE if not supported.
}
function _AddRef: Integer; stdcall;
{Called by Delphi when interface is referenced. Reference count is not
updated.
@return -1.
}
function _Release: Integer; stdcall;
{Called by Delphi when interface reference goes out of scope. Reference
count is not updated and instance is never freed.
@return -1.
}
end;
{
TAggregatedOrLoneObject:
Base class for objects that can either exist as aggregated objects
(as specified by the "implements" directive) or as stand-alone reference
counted objects. If the IInterface reference of the containing (controller)
object is passed to the constructor this object behaves as an aggregated
object and defers to the container object for reference counting and
interface queries. When nil is passed to the constructor or the
parameterless constructor is called the object bahaves as a stand alone
implementation and handles its own reference counting.
}
TAggregatedOrLoneObject = class(TInterfacedObject, IInterface)
strict private
fController: Pointer;
{Weak reference to controlling object if aggregated or nil if stand-alone}
function GetController: IInterface;
{Returns IInterface of controlling object.
@return Required IInterface reference. This is the container object if
aggregated or Self if stand-alone.
}
protected
{ IInterface redefinitions }
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
{Checks whether the specified interface is supported. If so reference to
interface is passed out. If aggregated then the controller object is
queried otherwise this object determines if interface is supported.
@param IID [in] Specifies interface being queried.
@param Obj [out] Reference to interface implementation or nil if not
supported.
@result S_OK if interface supported or E_NOINTERFACE if not supported.
}
function _AddRef: Integer; stdcall;
{Called by Delphi when interface is referenced. If aggregated call is
passed off to controller, which may or may not perform reference counting.
Otherwise reference count is incremented.
@return Updated reference count.
}
function _Release: Integer; stdcall;
{Called by Delphi when interface reference goes out of scope. If
aggregated call is passed off to controller, which may or may not perform
reference counting. Otherwise reference count is decremented and object is
freed if count reaches zero.
@return Updated reference count.
}
public
constructor Create(const Controller: IInterface); overload; virtual;
{Class constructor. Creates either an aggregated or stand-alone object.
@param Controller [in] IInterface reference to containing object if
aggregated or nil if not aggregated.
}
constructor Create; overload; virtual;
{Class constructor. Creates a stand-alone object. Equivalent to calling
Create(nil).
}
property Controller: IInterface read GetController;
{Reference to controlling object's IInterface. Non-nil if aggregated or
nil if stand-alone}
end;
{
TConditionalFreeObject:
Abstract base class for objects that cannot be destroyed unless some
condition is met. Descendants must override CanDestroy which must return
True if the object can be freed. Attempts to free the object when CanDestroy
returns False fail and the object remains in existance.
}
TConditionalFreeObject = class abstract(TObject)
strict protected
procedure Finalize; virtual;
{Tidies up object. Descendants should override this method instead of
destructor.
}
function CanDestroy: Boolean; virtual; abstract;
{Determines if the object can be destroyed.
@return True if object can be destroyed.
}
public
destructor Destroy; override;
{Class destructor tidies up and tears down object only if object can be
freed.
}
procedure FreeInstance; override;
{Frees instance data only if object can be destroyed.
}
end;
implementation
uses
// Delphi
SysUtils;
{ TNoConstructObject }
constructor TNoConstructObject.Create;
{Class constructor. Causes an assertion failure if called. The object is never
constructed.
}
begin
raise ENoConstructException.Create(
ClassName + '.Create: Constructor can''t be called'
);
end;
{ TNoPublicConstructObject }
constructor TNoPublicConstructObject.Create;
{Class constructor. Causes an assertion failure if called. The object is
never constructed.
}
begin
raise ENoConstructException.Create(
ClassName + '.Create: Public constructor can''t be called'
);
end;
constructor TNoPublicConstructObject.InternalCreate;
{Protected class constructor. Does nothing but call inherited constructor.
Should be called by class methods of derived classes instead of inherited
Create.
}
begin
inherited Create;
end;
{ TNoPublicConstructIntfObject }
constructor TNoPublicConstructIntfObject.Create;
{Class constructor. Causes an assertion failure if called. The object is never
constructed.
}
begin
raise ENoConstructException.Create(
ClassName + '.Create: Public constructor can''t be called'
);
end;
constructor TNoPublicConstructIntfObject.InternalCreate;
{Protected class constructor. Does nothing but call inherited constructor.
Should be called by class methods of derived classes instead of inherited
Create.
}
begin
inherited Create;
end;
{ TNonRefCountedObject }
function TNonRefCountedObject.QueryInterface(const IID: TGUID;
out Obj): HResult;
{Checks the specified interface is supported by this object. If so reference
to interface is passed out.
@param IID [in] Specifies interface being queried.
@param Obj [out] Reference to interface implementation or nil if not
supported.
@result S_OK if interface supported or E_NOINTERFACE if not supported.
}
begin
if GetInterface(IID, Obj) then
Result := S_OK
else
Result := E_NOINTERFACE;
end;
function TNonRefCountedObject._AddRef: Integer;
{Called by Delphi when interface is referenced. Reference count is not
updated.
@return -1.
}
begin
Result := -1;
end;
function TNonRefCountedObject._Release: Integer;
{Called by Delphi when interface reference goes out of scope. Reference count
is not updated and instance is never freed.
@return -1.
}
begin
Result := -1;
end;
{ TAggregatedOrLoneObject }
constructor TAggregatedOrLoneObject.Create(const Controller: IInterface);
{Class constructor. Creates either an aggregated or stand-alone object.
@param Controller [in] IInterface reference to containing object if
aggregated or nil if not aggregated.
}
begin
inherited Create;
fController := Pointer(Controller);
end;
constructor TAggregatedOrLoneObject.Create;
{Class constructor. Creates a stand-alone object. Equivalent to calling
Create(nil).
}
begin
Create(nil);
end;
function TAggregatedOrLoneObject.GetController: IInterface;
{Returns IInterface of controlling object.
@return Required IInterface reference. This is the container object if
aggregated or Self if stand-alone.
}
begin
if Assigned(fController) then
Result := IInterface(fController)
else
Result := Self;
end;
function TAggregatedOrLoneObject.QueryInterface(const IID: TGUID;
out Obj): HResult;
{Checks whether the specified interface is supported. If so reference to
interface is passed out. If aggregated then the controller object is queried
otherwise this object determines if interface is supported.
@param IID [in] Specifies interface being queried.
@param Obj [out] Reference to interface implementation or nil if not
supported.
@result S_OK if interface supported or E_NOINTERFACE if not supported.
}
begin
if Assigned(fController) then
Result := IInterface(fController).QueryInterface(IID, Obj)
else
Result := inherited QueryInterface(IID, Obj);
end;
function TAggregatedOrLoneObject._AddRef: Integer;
{Called by Delphi when interface is referenced. If aggregated call is passed
off to controller, which may or may not perform reference counting. Otherwise
reference count is incremented.
@return Updated reference count.
}
begin
if Assigned(fController) then
Result := IInterface(fController)._AddRef
else
Result := inherited _AddRef;
end;
function TAggregatedOrLoneObject._Release: Integer;
{Called by Delphi when interface reference goes out of scope. If aggregated
call is passed off to controller, which may or may not perform reference
counting. Otherwise reference count is decremented and object is freed if
count reaches zero.
@return Updated reference count.
}
begin
if Assigned(fController) then
Result := IInterface(fController)._Release
else
Result := inherited _Release;
end;
{ TConditionalFreeObject }
destructor TConditionalFreeObject.Destroy;
{Class destructor tidies up and tears down object only if object can be freed.
}
begin
// Do not override to tidy up unless you first check CanDestroy and only tidy
// up if it returns true. Override Finalize instead.
if CanDestroy then
begin
Finalize;
inherited;
end;
end;
procedure TConditionalFreeObject.Finalize;
{Tidies up object. Descendants should override this method instead of
destructor.
}
begin
// Override this to tidy up the object instead of overriding the destructor
end;
procedure TConditionalFreeObject.FreeInstance;
{Frees instance data only if object can be destroyed.
}
begin
// Check if object can be destroyed
if CanDestroy then
inherited;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.