Delphi 对泛型TList的的改进(TSimpleList)

时间:2023-03-09 06:37:08
Delphi 对泛型TList的的改进(TSimpleList)

TList 有一个比较麻烦的问题是,到底由谁来释放List中的对象或指针。

本例将释放任务教给 TSimpleList ,方便使用。

如果 TList 为于管理对象,还可以实现 AddNewOne 功能。方便使用。

TSimpleList类应用源码

uSimpleList.pas 源码

 unit uSimpleList;

 interface

 uses
Generics.Collections; type TSimpleList<T> = class(TList<T>)
private
FCurIndexPos: integer;
function DoPopByIndex(Index: integer): T;
procedure FreeAllItems;
procedure SetCurIndexPos(const Value: integer);
protected
FNeedFreeItem: boolean;
procedure FreeItem(Item: T); virtual; //子类可以重截这个以确定该如何释放
public constructor Create;
destructor Destroy; override; procedure Lock; //新版的Lock功能值得学习
procedure Unlock; // function PopFirst: T; //不解释,下同
function PopLast: T;
function PopByIndex(Index: integer): T; procedure ClearAndFreeAllItems; //清空并释放所有的Item
property CurIndexPos: integer read FCurIndexPos write SetCurIndexPos; end; //加 Constructor 限制是要求 T 要有一个没带参数的Create函数,也就是构造器
TClassSimpleList<T: Class, Constructor> = class(TSimpleList<T>)
protected
procedure FreeItem(Item: T); override;
function AddNewOne: T;// T有了Create 才能写这个
end; implementation procedure TSimpleList<T>.ClearAndFreeAllItems;
begin
FreeAllItems;
clear;
end; constructor TSimpleList<T>.Create;
begin
inherited;
FNeedFreeItem := true;
FCurIndexPos := -;
end; destructor TSimpleList<T>.Destroy;
begin
FreeAllItems;
inherited;
end; function TSimpleList<T>.DoPopByIndex(Index: integer): T;
begin
if (index >= ) and (index <= count - ) then
begin
result := items[index];
Delete(index);
Exit;
end;
result := T(nil);
end; procedure TSimpleList<T>.FreeAllItems;
var
Item: T;
begin
if FNeedFreeItem then
begin
FCurIndexPos := -;
for Item in self do
FreeItem(Item);
end;
end; procedure TSimpleList<T>.FreeItem(Item: T);
begin
// 假设 T 是 PMyRec =^TMyRec TMyRec=record;
// 这个写法对吗?
// if GetTypeKind(T) = tkPointer then
// begin
// Dispose(Pointer(Pointer(@Item)^));
// end;
// 此写法未认真测试所以不使用。
// 如果 Item 是指针,我在继承类中的 FreeItem 中写 Dispose(Item);
end; procedure TSimpleList<T>.Lock;
begin
system.TMonitor.Enter(self);
end; procedure TSimpleList<T>.Unlock;
begin
system.TMonitor.Exit(self);
end; function TSimpleList<T>.PopByIndex(Index: integer): T;
begin
result := DoPopByIndex(index);
end; function TSimpleList<T>.PopFirst: T;
begin
result := DoPopByIndex();
end; function TSimpleList<T>.PopLast: T;
begin
result := DoPopByIndex(count - );
end; procedure TSimpleList<T>.SetCurIndexPos(const Value: integer);
begin
FCurIndexPos := Value;
end; { TThreadClassList<T> } function TClassSimpleList<T>.AddNewOne: T;
begin
result := T.Create();
Add(result);
end; procedure TClassSimpleList<T>.FreeItem(Item: T);
begin
Item.Free;
end; end.

uSimpleList.pas

附:delphi 进阶基础技能说明