-
Notifications
You must be signed in to change notification settings - Fork 35
/
DelphiUtils.AutoObjects.pas
660 lines (534 loc) · 16.1 KB
/
DelphiUtils.AutoObjects.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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
unit DelphiUtils.AutoObjects;
{
This module provides the core facilities for automatic lifetime management
for resources that require cleanup. When interacting with such resources
through interfaces, Delphi automatically emits code that counts outstanding
references and immediately releases the underlying resource when this value
drops to zero. Here you can find the definitions for the interfaces, as
well as their default implementations.
The module defines the following hierarchy of interfaces:
+---> IHandle
|
IAutoReleasable ---+---> IAutoObject<T>
|
+---> IAutoPointer<P> ---> IMemory<P>
}
interface
type
// A wrapper for resources that implement automatic cleanup.
IAutoReleasable = interface (IInterface)
['{46841558-AE45-4161-AF45-ED93AACC2868}']
function GetAutoRelease: Boolean;
procedure SetAutoRelease(Value: Boolean);
function GetReferenceCount: Integer;
property AutoRelease: Boolean read GetAutoRelease write SetAutoRelease;
property ReferenceCount: Integer read GetReferenceCount;
end;
// An automatically releaseable resource defined by a THandle value.
IHandle = interface (IAutoReleasable)
['{DFCAFCC6-4921-4CDF-A72A-C0211C45D1BF}']
function GetHandle: THandle;
property Handle: THandle read GetHandle;
end;
// An wrapper that automatically releases a Delphi class.
// You can safely cast between IAutoObject<TClassA> and IAutoObject<TClassB>
// whenever TClassA and TClassB form a compatible hierarchy.
IAutoObject<T: class> = interface (IAutoReleasable)
['{D9B743C7-A7E4-4EF4-9056-851FC66F14C6}']
function GetSelf: T;
property Self: T read GetSelf;
end;
// An untyped wrapper automatically releasing Delphi classes.
IAutoObject = IAutoObject<TObject>;
// A wrapper that automatically releases a record pointer. You can safely
// cast between IAutoPointer<P1> and IAutoPointer<P2> when necessary.
IAutoPointer<P> = interface (IAutoReleasable) // P must be a Pointer type
['{70B707BE-5B84-4EC7-856F-DF7F70DF81F6}']
function GetData: P;
property Data: P read GetData;
end;
// A automatic wrapper for an untyped pointer.
IAutoPointer = IAutoPointer<Pointer>;
TMemory = record
Address: Pointer;
Size: NativeUInt;
function Offset(Bytes: NativeUInt): Pointer;
class function From(Address: Pointer; Size: NativeUInt): TMemory; static;
class function Reference<T>(const [ref] Buffer: T): TMemory; static;
end;
// A wrapper that automatically releases a memory region.
// You can safely cast between IMemory<P1> and IMemory<P2> when necessary.
IMemory<P> = interface(IAutoPointer<P>) // P must be a Pointer type
['{7AE23663-B557-4398-A003-405CD4846BE8}']
property Data: P read GetData;
function GetSize: NativeUInt;
property Size: NativeUInt read GetSize;
function GetRegion: TMemory;
property Region: TMemory read GetRegion;
function Offset(Bytes: NativeUInt): Pointer;
end;
// An untyped automatic memory region
IMemory = IMemory<Pointer>;
// A record type for storing a weak reference to an interface
Weak<I: IInterface> = record
private
[Weak] FWeakRef: I;
public
class operator Implicit(const StrongRef: I): Weak<I>;
function Upgrade(out StrongRef: I): Boolean;
end;
// An interface type for storing a weak reference to an interface
IWeak<I: IInterface> = interface (IAutoReleasable)
['{BD834CC2-C269-4D4B-8D07-8D4A9E7754F0}']
procedure Assign(const StrongRef: I);
function Upgrade(out StrongRef: I): Boolean;
end;
// A prototype for a delayed operation
TOperation = reference to procedure;
// A prototype for anonymous for-in iterators
TEnumeratorPrepare = reference to function: Boolean;
TEnumeratorProvider<T> = reference to function (out Next: T): Boolean;
Auto = class abstract
// Automatically destroy an object when the last reference goes out of scope
class function From<T: class>(&Object: T): IAutoObject<T>; static;
// Create an automatically freeing dynamic memory allocation
class function AllocateDynamic(Size: NativeUInt): IMemory; static;
class function CopyDynamic(Buffer: Pointer; Size: NativeUInt): IMemory; static;
// Create a boxed Delphi record with automatic memory management. Note that
// it can include managed fields like long strings and dynamic arrays that
// will be released automatically. Less commonly, T can also be any other
// managed type (i.e., an interface, a string, a dynamic array, or an
// anonymous function).
class function Allocate<T>: IMemory; static;
class function Copy<T>(const Buffer: T): IMemory; static;
// Create a non-owning reference to a memory region
class function Address<T>(const [ref] Buffer: T): IMemory; static;
class function AddressRange(Start: Pointer; Size: NativeUInt): IMemory; static;
// Helper functions for getting the underlying memory address or nil
class function RefOrNil(const Memory: IAutoPointer): Pointer; overload; static;
class function RefOrNil<P>(const Memory: IAutoPointer<P>): P; overload; static;
class function SizeOrZero(const Memory: IMemory): NativeUInt; static;
// Create a non-owning reference to a handle
class function RefHandle(Handle: THandle): IHandle; static;
// Create a non-owning weak reference to an interface
class function RefWeak<I: IInterface>(const StrongRef: I): IWeak<I>;
// Perform an operation defined by the callback when the last reference to
// the object goes out of scope.
class function Delay(Operation: TOperation): IAutoReleasable; static;
// Use an anonymous function as a for-in iterator
class function Iterate<T>(Provider: TEnumeratorProvider<T>): IEnumerable<T>; static;
class function IterateEx<T>(Prepare: TEnumeratorPrepare; Provider: TEnumeratorProvider<T>): IEnumerable<T>; static;
end;
{ Base classes (for custom implementations) }
TCustomAutoReleasable = class abstract (TInterfacedObject)
protected
FAutoRelease: Boolean;
procedure Release; virtual; abstract;
function GetAutoRelease: Boolean; virtual;
procedure SetAutoRelease(Value: Boolean); virtual;
function GetReferenceCount: Integer; virtual;
public
procedure AfterConstruction; override;
destructor Destroy; override;
end;
TCustomAutoHandle = class abstract (TCustomAutoReleasable)
protected
FHandle: THandle;
constructor Capture(hObject: THandle);
function GetHandle: THandle; virtual;
end;
TCustomAutoPointer = class abstract (TCustomAutoReleasable)
protected
FData: Pointer;
constructor Capture(Address: Pointer);
function GetData: Pointer; virtual;
end;
TCustomAutoMemory = class abstract (TCustomAutoPointer)
protected
FSize: NativeUInt;
constructor Capture(Address: Pointer; Size: NativeUInt);
function GetSize: NativeUInt; virtual;
function GetRegion: TMemory; virtual;
function Offset(Bytes: NativeUInt): Pointer; virtual;
end;
{ Default implementations }
// Encapsulate a weak reference to an interface
TWeakReference<I: IInterface> = class (TCustomAutoReleasable, IWeak<I>)
protected
[Weak] FWeakRef: I;
procedure Assign(const StrongRef: I); virtual;
function Upgrade(out StrongRef: I): Boolean; virtual;
procedure Release; override;
constructor Create(const StrongRef: I);
end;
// Reference a handle value without taking ownership
THandleReference = class (TCustomAutoHandle, IHandle)
protected
procedure Release; override;
end;
// Maintains ownership over an instance of a Delphi class derived from TObject
TAutoObject = class (TCustomAutoReleasable, IAutoObject, IAutoReleasable)
protected
FObject: TObject;
procedure Release; override;
constructor Capture(&Object: TObject);
function GetSelf: TObject; virtual;
end;
// References a memory region without taking ownership
TMemoryReference = class (TCustomAutoMemory, IMemory, IAutoPointer,
IAutoReleasable)
protected
procedure Release; override;
end;
// Maintains ownership over a pointer to memory managed via AllocMem/FreeMem.
TAutoMemory = class (TCustomAutoMemory, IMemory, IAutoPointer, IAutoReleasable)
protected
procedure Release; override;
constructor Allocate(Size: NativeUInt);
constructor Copy(Source: Pointer; Size: NativeUInt);
end;
// A wrapper that automatically releases a boxed managed Delphi type. It is
// designed primarily for records, but can also hold other managed types
// (both directly and as part of managed records). These include interfaces,
// strings, dynamic arrays, and anonymous functions. This wrapper is similar
// to TAutoMemory, but adds type-specific calls to Initialize/Finalize.
TAutoManagedType<T> = class (TAutoMemory, IMemory, IAutoPointer, IAutoReleasable)
protected
procedure Release; override;
constructor Create;
constructor Copy(const Value: T);
end;
// Automatically performs an operation on destruction.
TDelayedOperation = class (TCustomAutoReleasable, IAutoReleasable)
protected
FOperation: TOperation;
procedure Release; override;
constructor Create(Operation: TOperation);
end;
// A wrapper for using anonymous functions as for-in loop providers
TAnonymousEnumerator<T> = class (TInterfacedObject, IEnumerator<T>,
IEnumerable<T>)
protected
FCurrent: T;
FIsPrepared: Boolean;
FPrepare: TEnumeratorPrepare;
FProvider: TEnumeratorProvider<T>;
private
function GetCurrent: TObject; // legacy (untyped)
function GetEnumerator: IEnumerator; // legacy (untyped)
public
constructor Create(
const Prepare: TEnumeratorPrepare;
const Provider: TEnumeratorProvider<T>
);
procedure Reset;
function MoveNext: Boolean;
function GetCurrentT: T;
function GetEnumeratorT: IEnumerator<T>;
function IEnumerator<T>.GetCurrent = GetCurrentT;
function IEnumerable<T>.GetEnumerator = GetEnumeratorT;
end;
implementation
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
{ TMemory }
class function TMemory.From;
begin
Result.Address := Address;
Result.Size := Size;
{$IFOPT Q+}
// Emit overflow checking
if UIntPtr(Address) + Size < UIntPtr(Address) then ;
{$ENDIF}
end;
function TMemory.Offset;
begin
Result := PByte(Address) + Bytes;
end;
class function TMemory.Reference<T>;
begin
Result.Address := @Buffer;
Result.Size := SizeOf(Buffer);
end;
{ Weak<T> }
class operator Weak<I>.Implicit(const StrongRef: I): Weak<I>;
begin
Result.FWeakRef := StrongRef;
end;
function Weak<I>.Upgrade;
begin
StrongRef := FWeakRef;
Result := Assigned(StrongRef);
end;
{ TCustomAutoReleasable }
procedure TCustomAutoReleasable.AfterConstruction;
begin
FAutoRelease := True;
inherited;
end;
destructor TCustomAutoReleasable.Destroy;
begin
if FAutoRelease then
Release;
inherited;
end;
function TCustomAutoReleasable.GetAutoRelease;
begin
Result := FAutoRelease;
end;
function TCustomAutoReleasable.GetReferenceCount;
begin
Result := FRefCount;
end;
procedure TCustomAutoReleasable.SetAutoRelease;
begin
FAutoRelease := Value;
end;
{ TCustomAutoHandle }
constructor TCustomAutoHandle.Capture;
begin
inherited Create;
FHandle := hObject;
end;
function TCustomAutoHandle.GetHandle;
begin
Result := FHandle;
end;
{ TCustomAutoPointer }
constructor TCustomAutoPointer.Capture;
begin
inherited Create;
FData := Address;
end;
function TCustomAutoPointer.GetData;
begin
Result := FData;
end;
{ TCustomAutoMemory }
constructor TCustomAutoMemory.Capture;
begin
inherited Capture(Address);
FSize := Size;
{$IFOPT Q+}
// Emit overflow checking
if UIntPtr(Address) + Size < UIntPtr(Address) then ;
{$ENDIF}
end;
function TCustomAutoMemory.GetRegion;
begin
Result.Address := FData;
Result.Size := FSize;
end;
function TCustomAutoMemory.GetSize;
begin
Result := FSize;
end;
function TCustomAutoMemory.Offset;
begin
Result := PByte(FData) + Bytes;
end;
{ TWeakReference<I> }
procedure TWeakReference<I>.Assign;
begin
FWeakRef := StrongRef;
end;
constructor TWeakReference<I>.Create;
begin
inherited Create;
FWeakRef := StrongRef;
end;
procedure TWeakReference<I>.Release;
begin
; // Nothing as we ony store a weak reference
end;
function TWeakReference<I>.Upgrade;
begin
StrongRef := FWeakRef;
Result := Assigned(StrongRef);
end;
{ THandleReference }
procedure THandleReference.Release;
begin
; // Nothing as we don't own the handle
end;
{ TAutoObject }
constructor TAutoObject.Capture;
begin
inherited Create;
FObject := &Object;
end;
function TAutoObject.GetSelf;
begin
Result := FObject;
end;
procedure TAutoObject.Release;
begin
if Assigned(FObject) then
FObject.Free;
FObject := nil;
inherited;
end;
{ TMemoryReference }
procedure TMemoryReference.Release;
begin
; // Nothing as we don't own the memory region
end;
{ TAutoMemory }
constructor TAutoMemory.Allocate;
begin
inherited Capture(AllocMem(Size), Size)
end;
constructor TAutoMemory.Copy;
begin
Allocate(Size);
Move(Source^, FData^, Size);
end;
procedure TAutoMemory.Release;
begin
if Assigned(FData) then
FreeMem(FData);
FData := nil;
inherited;
end;
{ TAutoManagedType<T> }
constructor TAutoManagedType<T>.Copy;
begin
Create;
T(FData^) := Value;
end;
constructor TAutoManagedType<T>.Create;
begin
inherited Allocate(SizeOf(T));
Initialize(T(FData^));
end;
procedure TAutoManagedType<T>.Release;
begin
if Assigned(FData) then
Finalize(T(FData^));
inherited;
end;
{ TDelayedOperation}
constructor TDelayedOperation.Create;
begin
inherited Create;
FOperation := Operation;
end;
procedure TDelayedOperation.Release;
begin
if Assigned(FOperation) then
FOperation;
FOperation := nil;
inherited;
end;
{ TAnonymousEnumerator<T> }
constructor TAnonymousEnumerator<T>.Create;
begin
FPrepare := Prepare;
FProvider := Provider;
end;
function TAnonymousEnumerator<T>.GetCurrent;
begin
Assert(False, 'Legacy (untyped) IEnumerator.GetCurrent not supported');
Result := nil;
end;
function TAnonymousEnumerator<T>.GetCurrentT;
begin
Result := FCurrent;
end;
function TAnonymousEnumerator<T>.GetEnumerator;
begin
Assert(False, 'Legacy (untyped) IEnumerable.GetEnumerator not supported');
Result := nil;
end;
function TAnonymousEnumerator<T>.GetEnumeratorT;
begin
Result := Self;
end;
function TAnonymousEnumerator<T>.MoveNext;
begin
// Run one-time preparation
if Assigned(FPrepare) and not FIsPrepared then
begin
Result := FPrepare;
if not Result then
Exit;
FIsPrepared := True;
end;
Result := FProvider(FCurrent);
end;
procedure TAnonymousEnumerator<T>.Reset;
begin
; // not supported
end;
{ Auto }
class function Auto.Address<T>;
begin
Result := TMemoryReference.Capture(@Buffer, SizeOf(Buffer));
end;
class function Auto.AddressRange;
begin
Result := TMemoryReference.Capture(Start, Size);
end;
class function Auto.Allocate<T>;
begin
Result := TAutoManagedType<T>.Create;
end;
class function Auto.AllocateDynamic;
begin
Result := TAutoMemory.Allocate(Size);
end;
class function Auto.Copy<T>;
begin
Result := TAutoManagedType<T>.Copy(Buffer);
end;
class function Auto.CopyDynamic;
begin
Result := TAutoMemory.Copy(Buffer, Size);
end;
class function Auto.Delay;
begin
Result := TDelayedOperation.Create(Operation);
end;
class function Auto.From<T>;
begin
IAutoObject(Result) := TAutoObject.Capture(&Object);
end;
class function Auto.Iterate<T>;
begin
Result := TAnonymousEnumerator<T>.Create(nil, Provider);
end;
class function Auto.IterateEx<T>;
begin
Result := TAnonymousEnumerator<T>.Create(Prepare, Provider);
end;
class function Auto.RefHandle;
begin
Result := THandleReference.Capture(Handle);
end;
class function Auto.RefOrNil(const Memory: IAutoPointer): Pointer;
begin
if Assigned(Memory) then
Result := Memory.Data
else
Result := nil;
end;
class function Auto.RefOrNil<P>(const Memory: IAutoPointer<P>): P;
begin
if Assigned(Memory) then
Result := Memory.Data
else
Result := Default(P); // nil
end;
class function Auto.RefWeak<I>;
begin
Result := TWeakReference<I>.Create(StrongRef);
end;
class function Auto.SizeOrZero;
begin
if Assigned(Memory) then
Result := Memory.Size
else
Result := 0;
end;
end.