Class TFreeNotificationObserver

Unit

Declaration

type TFreeNotificationObserver = class(TComponent)

Description

Observe when something is freed, and call an event then. You need to set Observed of this component to make it notified about freeing of something. When the Observed is freed, this component will make OnFreeNotification event.

An example code using it:

type
  TChild = class(TComponent)
  end;

  TContainer = class(TComponent)
  private
    FChild: TChild;
    FChildObserver: TFreeNotificationObserver;
    procedure SetChild(const Value: TChild);
    procedure ChildFreeNotification(const Sender: TFreeNotificationObserver);
  public
    constructor Create(AOwner: TComponent);
    property Child: TChild read FChild write SetChild;
  end;

implementation

constructor TContainer.Create(AOwner: TComponent);
begin
  inherited;
  FChildObserver := TFreeNotificationObserver.Create(Self);
  FChildObserver.OnFreeNotification := @ChildFreeNotification;
end;

procedure TContainer.SetChild(const Value: TChild);
begin
  if FChild <> Value then
  begin
    FChild := Value;
    FChildObserver.Observed := Value;
  end;
end;

procedure TContainer.ChildFreeNotification(const Sender: TFreeNotificationObserver);
begin
  FChild := nil;
end;

Using this is an alternative to observing freeing using standard TComponent.FreeNotification / TComponent.RemoveFreeNotification mechanism https://castle-engine.io/modern_pascal_introduction.html#_free_notification . Using TFreeNotificationObserver is:

Hierarchy

Overview

Methods

Protected procedure Notification(AComponent: TComponent; Operation: TOperation); override;
Public destructor Destroy; override;

Properties

Public property OnFreeNotification: TFreeNotificationEvent read FOnFreeNotification write FOnFreeNotification;
Public property Observed: TComponent read FObserved write SetObserved;

Description

Methods

Protected procedure Notification(AComponent: TComponent; Operation: TOperation); override;
 
Public destructor Destroy; override;
 

Properties

Public property OnFreeNotification: TFreeNotificationEvent read FOnFreeNotification write FOnFreeNotification;

Called when we receive notification that something was freed.

Public property Observed: TComponent read FObserved write SetObserved;

Setting this property makes the given component observed, freeing it will make OnFreeNotification. When setting, the previous value of this property stops being observed.

Note that this property will be automatically changed to Nil after OnFreeNotification, if the OnFreeNotification will not change it. This is necessary (we need to detach our "free notification" from a component that will be freed) and makes it safer (we will not expose dangling pointer).


Generated by PasDoc 0.16.0.