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. Activity关闭另一个Acitivity

    1.ActivityA跳转到ActivityB 2.ActivityB跳转到ActivityC 3.ActivityC结束掉自己的同时结束ActivityB 在ActivityB中声明一个自己的参数i ...

  2. Android源码下载

    Android源码下载 1.安装git 2.安装repo 从这里 https://dl-ssl.google.com/dl/googlesource/git-repo/repo 下载repo文件 3. ...

  3. IOS研究院之打开照相机与本地相册选择图片

    如下图所示 在本地相册中选择一张图片后,我们将他拷贝至沙盒当中,在客户端中将它的缩略图放在按钮旁边,这个结构其实和新浪微薄中选择图片后的效果一样.最终点击发送将按钮将图片2进制图片上传服务器. 下面我 ...

  4. Linux学习笔记2:如何快速的学习使用一个命令

    Linux 分层 内核 库: .so 共享对象,windows:dll 动态链接库 应用程序 Linux的基本原则: 1.由目的单一的小程序组成:组合小程序完成复杂任务: 2.一切皆文件: 3.尽量避 ...

  5. THP Transparent HugePages 相关知识与关闭

    近期遇到个LINUX系统内存比較大.未开 HugePages,业务有变化导致ORACLE连接数剧增至上千个,PageTables达到上百G.导致内存不足系统HANG住的案例. 因此须要开启 HugeP ...

  6. [UI]抽屉菜单DrawerLayout分析(三)

    在[UI]抽屉菜单DrawerLayout分析(一)和[UI]抽屉菜单DrawerLayout分析(二)中分别介绍了DrawerLayout得基本框架结构和ViewDragerHelper的作用以及手 ...

  7. UTF-8 BOM编码格式文件对SSI的影响

    最近在用SSI(Server Side Includes)加载子模块的时候发现一个奇怪的现象,加载完成后的网页老是CSS有问题,被加载模块渲染后老是有空白部分.下面给出简单的示例. 文件a.html的 ...

  8. bootstrap-js(4)标签页

    实例 标签页(Tab)在 Bootstrap 导航元素 一章中介绍过.通过结合一些 data 属性,您可以轻松地创建一个标签页界面. 通过这个插件您可以把内容放置在标签页或者是胶囊式标签页甚至是下拉菜 ...

  9. 使用sqlcmd执行连接的时候一直报有语法错误

    1.今天在使用sqlcmd进行执行连接操作的时候一直报有语法错误找了半天. 命令 sqlcmd -S 服务器名称 -U 帐户 -P 密码 示例 sqlcmd -S "LOCALHOST&qu ...

  10. 今天是迅驰ORM的诞生之日。

    欢迎各位朋友来造访,最主要还是那一点:版权所有,请尊重驰哥的著作版权,侵权必追究法律责任. 最后来一句刘德华的歌曲:独自去偷欢,我谢绝你监管!快乐心中常为我伴.