wiki:containers:index
Table of Contents
DIContainers: Wiki
Please register and / or log in to edit. Anonymous Wiki edits are disabled to protect against vandalism.
TList Equivalents?
Two DIContainers classes are available to replace Delphi's TList class:
TDIPointerVector– stores simple pointers very much likeTList.TDIObjectVector– stores instances ofTObject, quite likeTObjectList.
Both containers can be configured to “own” their data, so any stored memory or object is automatically freed when an item is deleted. This is achieved by using a different constructor function:
NewDIPointerVectorvs.NewDIPointerOwnerVectorNewDIObjectVectorvs.NewDIObjectOwnerVector
TDIPointerVector example
A small console application to demonstrate TDIPointerVector without and with owning its pointer data.
program PointerVector; {$APPTYPE Console} {$I DI.inc} uses DIPointerVector, DIPointerOwnerVector; type TDataRec = record Data: Integer; end; PDataRec = ^TDataRec; //------------------------------------------------------------------------------ procedure UsePointerVector(const APointerVector: TDIPointerVector); var i: Integer; p: PDataRec; begin { Add a few pointers to the vector. } for i := 1 to 10 do begin New(p); p^.Data := i; APointerVector.InsertPointerLast(p); end; { Retrieve all stored pointers and write their data. } for i := 0 to APointerVector.Count - 1 do begin p := APointerVector.PointerAt[i]; WriteLn(p.Data); end; end; //------------------------------------------------------------------------------ var i: Integer; pv: TDIPointerVector; begin { Create a TDIPointerVector container. This does not "own" the memory pointed to. We must free them manually (below). } pv := NewDIPointerVector; UsePointerVector(pv); { Free the stored Pointers and the container. } for i := 0 to pv.Count - 1 do FreeMem(pv.PointerAt[i]); pv.Free; //------------------------------------------------------------------------------ { Create a TDIPointerVector container which "owns" its memory. } pv := NewDIPointerOwnerVector; UsePointerVector(pv); { No need to free the stored Pointers. The container frees them automatically when it deletes items. } pv.Free; end.
TDIObjectVector example
A small console application to demonstrate TDIObjectVector without and with owning its objects.
program ObjectVector; {$APPTYPE Console} {$I DI.inc} uses DIObjectVector, DIObjectOwnerVector; type TDataObject = class private FData: Integer; public property Data: Integer read FData write FData; end; //------------------------------------------------------------------------------ procedure UseObjectVector(const AObjectVector: TDIObjectVector); var i: Integer; o: TDataObject; begin { Add a few objects to the vector. } for i := 1 to 10 do begin o := TDataObject.Create; o.Data := i; AObjectVector.InsertObjectLast(o); end; { Retrieve all stored objects and write their data. } for i := 0 to AObjectVector.Count - 1 do begin o := AObjectVector.ObjectAt[i] as TDataObject; WriteLn(o.Data); end; end; //------------------------------------------------------------------------------ var i: Integer; ov: TDIObjectVector; begin { Create a TDIObjectVector container. this does not "own" its objects. We must free them manually (below). } ov := NewDIObjectVector; UseObjectVector(ov); { Free the stored objects and the container. } for i := 0 to ov.Count - 1 do ov.ObjectAt[i].Free; ov.Free; //------------------------------------------------------------------------------ { Create a TDIObjectVector container which "owns" its objects. } ov := NewDIObjectOwnerVector; UseObjectVector(ov); { No need to free the stored objects. The container frees them automatically when it deletes items. } ov.Free; end.
wiki/containers/index.txt · Last modified: 2016/01/22 14:08 by 127.0.0.1

