-
Notifications
You must be signed in to change notification settings - Fork 1
/
sharedpointer.pas
92 lines (80 loc) · 2.36 KB
/
sharedpointer.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
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit sharedpointer;
interface
type
generic TSharedPointer <T: class> = record
private
type
SelfT = specialize TSharedPointer<T>;
var
FPointer: T;
FRefCount: PCardinal;
class procedure DecRef(var Self: SelfT); static;
public
class operator Initialize(var Self: SelfT);
class operator Finalize(var Self: SelfT);
class operator AddRef(var Self: SelfT);
class operator Copy(constref Source: SelfT; var Destination: SelfT);
class operator :=(const Source: T): SelfT;
procedure Free();
property Value: T read FPointer;
end;
implementation
uses
sysutils;
class operator TSharedPointer.Initialize(var Self: SelfT);
begin
Self.FPointer := nil;
Self.FRefCount := nil;
end;
class operator TSharedPointer.Finalize(var Self: SelfT);
begin
DecRef(Self);
end;
class operator TSharedPointer.AddRef(var Self: SelfT);
begin
Assert(Assigned(Self.FRefCount) = Assigned(Self.FPointer), 'Invariant violation: refcount and pointer are inconsistent in AddRef');
if (Assigned(Self.FRefCount)) then
InterlockedIncrement(Self.FRefCount^);
end;
class operator TSharedPointer.Copy(constref Source: SelfT; var Destination: SelfT);
begin
DecRef(Destination);
Assert(Assigned(Source.FRefCount) = Assigned(Source.FPointer), 'Invariant violation: Source refcount and pointer are inconsistent');
if (Assigned(Source.FRefCount)) then
InterlockedIncrement(Source.FRefCount^);
Destination.FRefCount := Source.FRefCount;
Destination.FPointer := Source.FPointer;
end;
class operator TSharedPointer.:=(const Source: T): SelfT;
begin
DecRef(Result); // {BOGUS Warning: Function result variable of a managed type does not seem to be initialized}
if (Assigned(Source)) then
begin
New(Result.FRefCount);
Result.FRefCount^ := 1;
Result.FPointer := Source;
end
else
begin
Result.FRefCount := nil;
Result.FPointer := nil;
end;
end;
class procedure TSharedPointer.DecRef(var Self: SelfT);
begin
if (Assigned(Self.FRefCount) and (InterlockedDecrement(Self.FRefCount^) = 0)) then
begin
FreeAndNil(Self.FPointer);
Dispose(Self.FRefCount);
Self.FRefCount := nil;
end;
end;
procedure TSharedPointer.Free();
begin
DecRef(Self);
FPointer := nil;
FRefCount := nil;
end;
end.