码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2016-04-10 12:52:20      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

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 := -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.

  

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

标签:

原文地址:http://www.cnblogs.com/lackey/p/5373653.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!