Board index » delphi » TList that deletes elements?

TList that deletes elements?

Is there any list class that automatically deletes (frees) elements
when they are removed, when the list is cleared, and when the list is
destroyed?  This seems like a fairly common thing to do, but it doesn't
seem available standard.

Ideally this behavior would either be available in TList (i.e. property
TList.AutoDeleteObjects : Boolean) or be available in a TList-derived
class (TAutoList = class(TList).)  Unfortunately, TList doesn't do the
former and the later cannot be done (without modifying the source code)
because the important methods in TList are non-virtual.

-Steve
sclin...@mdisafenet.com

 

Re:TList that deletes elements?


Quote
In article <VA.000000b7.0020a9de@stevent>, sclin...@mdisafenet.com wrote:
>Is there any list class that automatically deletes (frees) elements
>when they are removed, when the list is cleared, and when the list is
>destroyed?  This seems like a fairly common thing to do, but it doesn't
>seem available standard.

It *is* a fairly common thing to do :)

Quote
>Ideally this behavior would either be available in TList (i.e. property
>TList.AutoDeleteObjects : Boolean) or be available in a TList-derived
>class (TAutoList = class(TList).)  Unfortunately, TList doesn't do the
>former and the later cannot be done (without modifying the source code)
>because the important methods in TList are non-virtual.

I usually make a class that *contains* a TList; I can abstract from there.  
For example:

  EWrongTypeForList = class(Exception);
  TObjectList = class
  private
    FObjectType : TClass;
    FList : TList;
  protected
    function GetCount: Integer; virtual;
    function GetObject(Index: Integer): TObject; virtual;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Add(AObject: TObject); virtual;
    function AddNew: TObject; virtual;
    procedure Clear;
    procedure Insert(APos: Integer; AObject: TObject); virtual;
    procedure Delete(Index: Integer); virtual;
    procedure Remove(AObject: TObject); virtual;
    property Count: Integer read GetCount;
    property ObjectItem[Index: Integer]: TObject read GetObject;
    property ObjectType: TClass read FObjectType write FObjectType;
  end;

function TObjectList.GetCount: Integer;
begin
  if Assigned(FList) then
    Result := FList.Count
  else
    Result := 0;
end;

function TObjectList.GetObject(Index: Integer): TObject;
begin
  Result := TObject(FList[Index]);
end;

constructor TObjectList.Create;
begin
  inherited;
  FList := TList.Create;
end;

destructor TObjectList.Destroy;
begin
  if Assigned(FList) then
    Clear;
  inherited;
end;

procedure TObjectList.Add(AObject: TObject);
begin
  if not (AObject is ObjectType) then
    raise EWrongTypeForList.Create('Wrong object type in Add');
  FList.Add(AObject);
end;

function TObjectList.AddNew: TObject;
begin
  Result := ObjectType.Create;
  Add(Result);
end;

procedure TObjectList.Clear;
begin
  while Count>0 do
    Delete(Count-1);
end;

procedure TObjectList.Insert(APos: Integer; AObject: TObject);
begin
  if not (AObject is ObjectType) then
    raise EWrongTypeForList.Create('Wrong object type in Insert');
  FList.Insert(APos,AObject);
end;

procedure TObjectList.Delete(Index: Integer);
begin
  ObjectItem[Index].Free;
  FList.Delete(Index);
end;

procedure TObjectList.Remove(AObject: TObject);
begin
  FList.Remove(AObject);
  AObject.Free;  
end;

You might want to do another one for anything based on TComponent (replacing
TClass with TComponentClass) because the object constructor would need an
Owner for the objects to work properly.

Note - AddNew probably won't work terribly well in this demo class as
TObject's constructor isn't virtual.  Hope it gives a good starting base,
though...

  -- Ritchie Annand

(Anti-spam measure - e-mail address contains neither hyphens nor the
words 'at' or 'dot' :)

Re:TList that deletes elements?


The Spider Container classes have terrific TList replacements that
destroy all of the elements they hold when they are freed. I've used
them for over a year without a hitch. To find out more, contact
http://www.cam.org/~mibra/spider.

Re:TList that deletes elements?


Check out the type-safe lists paper at my site.
And, download the free TList derivative generator.

--
Grace + Peace | Peter N Roth | Engineering Objects Int'l
  Author: Round Robin, a Delphi scheduling component
Visit our website at http://www.inconresearch.com/eoi
"Random numbers are too important to be left to chance." - anon

Other Threads