Anonymous editing of this Wiki page is disabled to hamper vandalism. To edit, you must first log in using the button in the left column.
|
|
Table of Contents
DIContainers WikiAnonymous editing of this Wiki page is disabled to hamper vandalism. To edit, you must first log in using the button in the left column. TList Equivalents?
Two DIContainers classes are available to replace Delphi's
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:
TDIPointerVector example
A small console application to demonstrate 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 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: 2009/10/28 19:28 (external edit)
|
| Copyright (c) 2000-2011 Ralf Junker – http://www.yunqa.de/delphi/ – Disclaimer – Haftungsausschluss – Impressum |