Ignore:
Timestamp:
Oct 30, 2010, 6:39:12 PM (14 years ago)
Author:
george
Message:
  • Added: Ability of TListObject to own items and be able to free them on deletion and list destruction.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Generics/TemplateGenerics/Specialized/ListObject.pas

    r71 r78  
    1515type
    1616
    17   { TObjectList }
     17  { TListObject }
    1818
    19   TObjectList = class(TGList)
    20     //OwnObjects: Boolean;
     19  TListObject = class(TGList)
     20  private
     21    procedure Put(Index: TListIndex; const AValue: TListItem); override;
     22  public
     23    OwnObjects: Boolean;
     24    procedure Delete(Index: TListIndex); override;
     25    procedure Clear; override;
     26    constructor Create;
    2127    destructor Destroy; override;
    2228  end;
     
    2935{ TObjectList }
    3036
    31 destructor TObjectList.Destroy;
     37procedure TListObject.Put(Index: TListIndex; const AValue: TListItem);
    3238begin
     39  if OwnObjects then FItems[Index].Free;
     40  inherited Put(Index, AValue);
     41end;
     42
     43procedure TListObject.Delete(Index: TListIndex);
     44begin
     45  if OwnObjects then FItems[Index].Free;
     46  inherited Delete(Index);
     47end;
     48
     49procedure TListObject.Clear;
     50var
     51  I: TListIndex;
     52begin
     53  if OwnObjects then begin
     54    I := 0;
     55    while I < Count do begin
     56      FItems[I].Free;
     57      I := I + 1;
     58    end;
     59  end;
     60  inherited Clear;
     61end;
     62
     63constructor TListObject.Create;
     64begin
     65  inherited;
     66  OwnObjects := True;
     67end;
     68
     69destructor TListObject.Destroy;
     70begin
     71  Clear;
    3372  inherited Destroy;
    3473end;
Note: See TracChangeset for help on using the changeset viewer.