Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your TEntity instance should register itself with TMesh instance so that when TMesh instance is being freed, it will modify the TEntity instance about it.</p> <p>If your classes are derived from TComponent class, then this mechanism is already implemented for you; each TComponent instance has a method called FreeNotification and a method called Notification. Any TComponent instance can register itself with the other component calling its FreeNotification method and passing itself as the parameter. Whenever a component is being destroyed, it will check the list of components registered for its free notification, and will invoke Notification method of each registered component. This way, the register component will be notified whenever the other component is about to be destroyed.</p> <p>If one TComponent instance is the owner of the other (In your case, TEntity can be the owner of TMesh instance), then it will be notified automatically whenever TMesh instance is removed. All you need to do is to override its Notification method and do whatever you want to do inside that method.</p> <p>If you do not want to derive your classes from TComponent class or for any reason do not want to use Delphi's implementation, you can implement the same behavior in your own classes. You need an internal list in your TMesh class which holds a list of classes which should be notified. You also need a method to register a class with your TMesh class, and eventually you need a method in your TEntity class which should be called by TMesh whenever it is being freed.</p> <p>Here is a simple source code just for demonstrating the general idea. Take note that this sample code is not thread-safe, and might lack some other checks. I just wrote it fast to show you how to implement such an idea:</p> <pre><code>unit Unit1; interface uses Classes, Generics.Collections; type TBaseClass = class private FNotificationList : TList&lt;TBaseClass&gt;; protected procedure Notify(AClass: TBaseClass); virtual; public procedure RegisterForNotification(AClass: TBaseClass); procedure UnregisterNotification(AClass: TBaseClass); constructor Create; destructor Destroy; override; end; TMesh = class(TBaseClass) end; TEntity = class(TBaseClass) private FMesh : TMesh; FOnMeshRemoved : TNotifyEvent; procedure SetMesh(Value: TMesh); protected procedure Notify(AClass: TBaseClass); override; procedure DoMeshRemoved; virtual; public constructor Create; destructor Destroy; override; property Mesh : TMesh read FMesh write SetMesh; property OnMeshRemoved : TNotifyEvent read FOnMeshRemoved write FOnMeshRemoved; end; implementation { TBaseClass } constructor TBaseClass.Create; begin inherited; FNotificationList := TList&lt;TBaseClass&gt;.Create; end; destructor TBaseClass.Destroy; var AClass: TBaseClass; begin if Assigned(FNotificationList) then begin if (FNotificationList.Count &gt; 0) then for AClass in FNotificationList do AClass.Notify(Self); FNotificationList.Free; FNotificationList := nil; end; inherited; end; procedure TBaseClass.Notify(AClass: TBaseClass); begin end; procedure TBaseClass.RegisterForNotification(AClass: TBaseClass); begin if not Assigned(AClass) then Exit; if FNotificationList.IndexOf(AClass) &lt; 0 then FNotificationList.Add(AClass); end; procedure TBaseClass.UnregisterNotification(AClass: TBaseClass); begin if not Assigned(AClass) then Exit; if FNotificationList.IndexOf(AClass) &gt;= 0 then FNotificationList.Remove(AClass); end; { TEntity } constructor TEntity.Create; begin inherited; end; destructor TEntity.Destroy; begin if Assigned(FMesh) then FMesh.UnregisterNotification(Self); inherited; end; procedure TEntity.DoMeshRemoved; begin if Assigned(FOnMeshRemoved) then FOnMeshRemoved(Self); end; procedure TEntity.Notify(AClass: TBaseClass); begin inherited; FMesh := nil; DoMeshRemoved; end; procedure TEntity.SetMesh(Value: TMesh); begin if Assigned(FMesh) then begin FMesh.UnregisterNotification(Self); FMesh := nil; end; if Assigned(Value) then begin FMesh := Value; FMesh.RegisterForNotification(Self); end; end; end. </code></pre> <p>In this code, both TEntity and TMesh are derived from TBaseClass which provides notification mechanism. TEntity does not create any instance of TMesh initially, but you can assign a created TMesh instance to its Mesh property. Doing so will make TEntity to assign that value to its FMesh field, and call its RegisterForNotification class so that it can be notified if the mesh is being destroyed.</p> <p>When the mesh is being destroyed, it iterates over all the objects registered themselves with it, and invokes their Notify method. Here it would be Notify method of TEntity class. Inside Notify method of TEntity, it first makes its FMesh field nil, because that object is being destroyed and there is no need to keep a reference of a dead object. It then calls DoMeshRemove method which is an event-invoker for OnMeshRemove event.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload