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 := -1; end; destructor TSimpleList<T>.Destroy; begin FreeAllItems; inherited; end; function TSimpleList<T>.DoPopByIndex(Index: integer): T; begin if (index >= 0) and (index <= count - 1) 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 := -1; 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(0); end; function TSimpleList<T>.PopLast: T; begin result := DoPopByIndex(count - 1); 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.
时间: 2024-11-14 23:13:10