-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathFFreeNotifications.pas
138 lines (115 loc) · 4.36 KB
/
FFreeNotifications.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
unit FFreeNotifications;
interface
uses
System.SysUtils,
System.Types,
System.UITypes,
System.Classes,
System.Variants,
System.Messaging,
FMX.Types,
FMX.Graphics,
FMX.Controls,
FMX.Forms,
FMX.Dialogs,
FMX.StdCtrls,
FMX.Controls.Presentation,
FMX.ScrollBox,
FMX.Memo,
FBase,
UBase,
USampleClasses;
type
TFrameFreeNotifications = class(TFrameBase)
ButtonCreateObjects: TButton;
ButtonFreeFirst: TButton;
ButtonFreeSecond: TButton;
procedure ButtonCreateObjectsClick(Sender: TObject);
procedure ButtonFreeFirstClick(Sender: TObject);
procedure ButtonFreeSecondClick(Sender: TObject);
private
{ Private declarations }
FObject1, FObject2: TFreeNotificationObject;
procedure FreeNotificationListener(const Sender: TObject; const M: TMessage);
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;
implementation
{$R *.fmx}
procedure TFrameFreeNotifications.ButtonCreateObjectsClick(Sender: TObject);
begin
ButtonCreateObjects.Enabled := False;
ButtonFreeFirst.Enabled := True;
ButtonFreeSecond.Enabled := True;
{ Create two objects. }
FObject1 := TFreeNotificationObject.Create(nil, 'Object1');
FObject2 := TFreeNotificationObject.Create(nil, 'Object2');
{ Enable free notifications for both objects.
Because we subscribed to the TFreeNotification message in the constructor,
we will get notified when these objects are destroyed. }
FObject1.EnableFreeNotification;
FObject2.EnableFreeNotification;
Log('----------------------------------------------------');
Log('Created two objects and enabled free notifications for both objects.');
Log(' * TFreeNotificationObject.InstanceCount = %d', [TFreeNotificationObject.InstanceCount]);
Log('');
end;
procedure TFrameFreeNotifications.ButtonFreeFirstClick(Sender: TObject);
begin
ButtonFreeFirst.Enabled := False;
Log('About to free the first object. This will result in a TFreeNotification message.');
{ Because we subscribed to the TFreeNotification message, freeing this object
will send a notification message. Note that we need to set the reference
to nil to really free the object on ARC platforms. }
FreeAndNil(FObject1);
Log('Completed freeing the first object.');
Log(' * TFreeNotificationObject.InstanceCount = %d', [TFreeNotificationObject.InstanceCount]);
Log('');
if (TFreeNotificationObject.InstanceCount = 0) then
ButtonCreateObjects.Enabled := True;
end;
procedure TFrameFreeNotifications.ButtonFreeSecondClick(Sender: TObject);
begin
ButtonFreeSecond.Enabled := False;
Log('About to free the second object. This will result in a TFreeNotification message.');
{ Because we subscribed to the TFreeNotification message, freeing this object
will send a notification message. Note that we need to set the reference
to nil to really free the object on ARC platforms. }
FreeAndNil(FObject2);
Log('Completed freeing the second object.');
Log(' * TFreeNotificationObject.InstanceCount = %d', [TFreeNotificationObject.InstanceCount]);
Log('');
if (TFreeNotificationObject.InstanceCount = 0) then
ButtonCreateObjects.Enabled := True;
end;
constructor TFrameFreeNotifications.Create(AOwner: TComponent);
begin
inherited;
{ Subscribe to the TFreeNotification message to get notified when objects
derived from TFreeNotificationBase are freed. }
TMessageManager.DefaultManager.SubscribeToMessage(TFreeNotificationMessage,
FreeNotificationListener);
end;
destructor TFrameFreeNotifications.Destroy;
begin
{ Unsubscribe from the TFreeNotification message. }
TMessageManager.DefaultManager.Unsubscribe(TFreeNotificationMessage,
FreeNotificationListener);
inherited;
end;
procedure TFrameFreeNotifications.FreeNotificationListener(
const Sender: TObject; const M: TMessage);
begin
{ This method is called when an object derived from TFreeNotificationBase
(whose EnableFreeNotification has been called) method is about to be freed.
The Sender parameter contains the object that is about to freed. We don't
care about the M parameter, since it is just used to describe the message
type (which should be TFreeNotification). }
Assert(M is TFreeNotificationMessage);
Assert(Sender is TFreeNotificationObject);
Log('TFreeNotificationMessage received for: '
+ TFreeNotificationObject(Sender).Name);
end;
end.