Recent

Author Topic: [Resolved] Feature Request need lnDeleting in TList.Notify  (Read 457 times)

Zaher

  • Hero Member
  • *****
  • Posts: 678
    • parmaja.org
[Resolved] Feature Request need lnDeleting in TList.Notify
« on: October 22, 2022, 07:39:36 pm »
I inherited TObjectList, and used TList.Notify to work with my own hash list in the list, I need to recive deleted item before freed by Object List that own that items.
lnDeleted is call after this item really deleted and freed, I think  we need new one "lnDeleting" to call before deleting the item.

Code: [Select]
procedure TList.Delete(Index: Integer);
var P : pointer;
begin
  P:=FList.Get(Index);
  if assigned(p) then
    Notify(p, lnDeleting);
  FList.Delete(Index);
  if assigned(p) then
    Notify(p, lnDeleted);
end;

Thank in advance.
« Last Edit: October 24, 2022, 02:07:19 pm by Zaher »

ASerge

  • Hero Member
  • *****
  • Posts: 2049
Re: Feature Request need lnDeleting in TList.Notify
« Reply #1 on: October 23, 2022, 12:55:14 pm »
I inherited TObjectList, and used TList.Notify to work with my own hash list in the list, I need to recive deleted item before freed by Object List that own that items.
lnDeleted is call after this item really deleted and freed, I think  we need new one "lnDeleting" to call before deleting the item.
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyObjList = class(TObjectList)
  3.   protected
  4.     procedure Notify(Ptr: Pointer; Action: TListNotification); override;
  5.   end;
  6.  
  7. procedure TMyObjList.Notify(Ptr: Pointer; Action: TListNotification);
  8. begin
  9.   if Action = lnDeleted then
  10.     WorkWithItemBeforeFree(Ptr)
  11.   else
  12.     inherited;
  13. end;

Zaher

  • Hero Member
  • *****
  • Posts: 678
    • parmaja.org
Re: Feature Request need lnDeleting in TList.Notify
« Reply #2 on: October 24, 2022, 02:06:04 pm »
Works now thank you so much.

I used this in another place, but I have complicated list inheritance and I got crash on removing line, It seem FPC compiler mess with me, It not recompile my changed unit (idk why)
I will mark as solved

Thaddy

  • Hero Member
  • *****
  • Posts: 12933
Re: [Resolved] Feature Request need lnDeleting in TList.Notify
« Reply #3 on: October 24, 2022, 03:30:52 pm »
The proper way is to use observer/observed:
Code: Pascal  [Select][+][-]
  1. {$MODE OBJFPC}
  2. {$APPTYPE CONSOLE}
  3.  
  4. uses Classes;
  5.  
  6. type
  7.   TMyObject = class(TObject)
  8.   strict private
  9.     FName: string;
  10.   public
  11.     constructor Create(const AName: string);
  12.     destructor Destroy; override;
  13.   end;
  14.  
  15.   TFreeObjectObserver = class(TInterfacedObject, IFPObserver)
  16.     procedure FPOObservedChanged(ASender: TObject; Operation: TFPObservedOperation; Data: Pointer);
  17.   end;
  18.  
  19. constructor TMyObject.Create(const AName: string);
  20. begin
  21.   inherited Create;
  22.   FName := AName;
  23. end;
  24.  
  25. destructor TMyObject.Destroy;
  26. begin
  27.   Writeln(#9'Item ''', FName, ''' destroy');
  28.   inherited;
  29. end;
  30.  
  31. procedure TFreeObjectObserver.FPOObservedChanged(ASender: TObject; Operation: TFPObservedOperation; Data: Pointer);
  32. begin
  33.   if Operation = ooDeleteItem then
  34.     TObject(Data).Free;
  35. end;
  36.  
  37. procedure Test;
  38. var
  39.   L: TList;
  40.   Observer: TFreeObjectObserver;
  41. begin
  42.   Observer := TFreeObjectObserver.Create;
  43.   try
  44.     L := TList.Create;
  45.     try
  46.       L.FPOAttachObserver(Observer);
  47.       Writeln('Populate list');
  48.       L.Add(TMyObject.Create('Zero'));
  49.       L.Add(TMyObject.Create('One'));
  50.       L.Add(TMyObject.Create('Two'));
  51.       L.Add(TMyObject.Create('Three'));
  52.       Writeln('Set item 0 to nil');
  53.       L[0] := nil;
  54.       Writeln('Delete item 2');
  55.       L.Delete(2);
  56.       Writeln('End of using list');
  57.     finally
  58.       L.Free;
  59.     end;
  60.   finally
  61.     Observer.Free;
  62.   end;
  63. end;
  64.  
  65. begin
  66.   Test;
  67.   Readln;
  68. end.
This is all standard code and supported by the RTL. Only dependency is classes.
« Last Edit: October 24, 2022, 03:33:57 pm by Thaddy »
In memory of Gordon Moore  (January 3, 1929 – March 24, 2023) Just double the heaven every two years from now.

 

TinyPortal © 2005-2018