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 进阶基础技能说明

Delphi 对泛型TList的的改进(TSimpleList)的更多相关文章

  1. Delphi 2009 泛型容器单元(Generics.Collections)[1]: TList<T>

    Delphi 2009 新增了泛型容器单元: Generics.Collections, 同时还有一个 Generics.Defaults 单元做支持. Generics.Collections 包含 ...

  2. Delphi容器类之---Tlist,TStringlist,THashedStringlist的效率比较

    转载自:http://www.ylzx8.cn/windows/delphi/73200.html 本人在做一个测试,服务器是IOCP的,我假定最大链接数是50000个. 测试背景:如果每个链接之间的 ...

  3. Delphi容器类之---TList、TStringList、TObjectList,以及一个例程的代码分析

    转载自:http://blog.csdn.net/jqandjq/article/details/5429137 看了这里标题,大家可能以为我会谈TListBox控件,那就错了.我要谈的是Delphi ...

  4. Delphi容器类之---TList、TObjectList、TComponentList、TClassList

    转载自:http://blog.csdn.net/iseekcode/article/details/4922001 从Delphi5开始VCL中增加了新的Contnrs单元,单元中定义了8个新的类, ...

  5. delphi 线程教学第六节:TList与泛型

    第六节: TList 与泛型   TList 是一个重要的容器,用途广泛,配合泛型,更是如虎添翼. 我们先来改进一下带泛型的 TList 基类,以便以后使用. 本例源码下载(delphi XE8版本) ...

  6. Delphi泛型动态数组的扩展--转贴

    此文章转载于http://www.raysoftware.cn/?p=278&tdsourcetag=s_pcqq_aiomsg的博客 从Delphi支持泛型的第一天起就有了一种新的动态数组类 ...

  7. delphi一些小技巧 从别处看到

    开发环境--------    Delphi 7是一个很经典的版本,在Win2000/XP下推荐安装Delphi 7来开发软件,在Vista下推荐使用Delphi 2007开发软件.安装好Delphi ...

  8. (转载)Delphi开发经验谈

    Delphi开发经验谈 开发环境-------- Delphi 7是一个很经典的版本,在Win2000/XP下推荐安装Delphi 7来开发软件,在Vista下推荐使用Delphi 2007开发软件. ...

  9. Delphi 7以来的Delphi 2009测试版新语法特性

    我晕,Delphi 7 以后增加了这么多有用的语法,我都不知道.真是越学越觉得自己浅薄,自己所作的Delphi项目所用的知识还不够Delphi知识储备体系的十分之一,更别说Delphi还在继续发展. ...

随机推荐

  1. PLSQL Developer建表时注释(COMMENT)中文乱码的解决方案(Windows)

    简单的让你无法想象! 处理方法:在环境变量中新增系统变量 以下变量值对: 变量名:NLS_LANG变量值:AMERICAN_AMERICA.ZHS16GBK 好了

  2. struts2中的方法过滤拦截器

    方法过滤拦截器是只过滤指定的方法,如果使用针对action 的普通的过滤器则会过滤该action内部 所有方法.如果在一个action中同时有多个作为业务逻辑控制的方法存在 的话则会过滤所有的业务逻辑 ...

  3. nginx解决502错误

    添加一个域名绑定,突然服务器就挂了.由于是接受项目,没看过服务器配置.结果nginx和mysql 都跑不起来. 平滑启动nginx失败. 检查/usr/local/nginx/sbin/nginx - ...

  4. chroot

    用途:更改命令的根目录. 语法:chroot Directory Command 描述: 注意:如果新根目录中的特殊文件具有与实际根目录不同的主要和次要设备号,则可能会覆盖文件系统. 只有具有 roo ...

  5. 自定义清除重复uses-permission申明的AS插件

    分享一个我刚到天下布医工作时,写的一个android studio插件. 做安卓项目时,经常继承一些第三方sdk,这些sdk都会申请权限,导致AndroidManifest.xml中的uses-per ...

  6. 新手讲树:证明任意二叉树度为零的节点n0,永远比度为2的节点n2多1个

    证明:   设度为1的节点个数为n1,因为二叉树的所有节点的度都小于等于2, 所以n=n0+n1+n2; 又因为二叉树中,除了根节点所有的节点都有一个进入节点的分支,假设B为所有的分支,那么n=B+1 ...

  7. Linux查看网卡状态

    观看网卡传送.接收数据包的状态 $ netstat  -i Kernel Interface table Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK ...

  8. linux下安装配置svn独立服务器

    subversion(以下简称svn)是近年来崛起的版本管理工具,是cvs的接班人. svn服务器有2种运行方式:独立服务器和借助apache.2种方式各有利弊. file:/// 直接版本库访问(本 ...

  9. Nginx 介绍和安装

    Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev ...

  10. MFC网页

    写网页, 选择MFC,MFC应用程序,其他默认,单击确定 项目类型,选Offce,其他默认,单击下一步 默认,单击下一步 文件拓展名,输入html,其他默认,单击下一步 数据库支持,默认,单击下一步 ...