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. Android NIO(Noblocking I/O非阻塞I/O)小结

    参考:http://www.cnblogs.com/cpcpc/archive/2011/06/27/2123009.html 对于Android的网络通讯性能的提高,我们可以使用Java上高性能的N ...

  2. Android 监听网络变化

    Android 监听网络变化

  3. FileProvider是个什么东西?

    FileProvider是个什么东西? 在<读取并监控文件的变化>中,我们通过三个简单的实例演示从编程的角度对文件系统做了初步的体验,接下来我们继续从设计的角度来继续认识它.这个抽象的文件 ...

  4. Linux--本地yum库

    Linux配置本地yum云: 第一步:把cd 挂载到目录树 mount /dev/cdrom /mnt/media 第二步:增加/etc/yum.repo.d/local.repo touch /et ...

  5. Visual Studio 2012 Update3 安装失败错误“正在关闭管道'

    问题描述: Visual Studio 2012 update3 安装失败错误“ 正在关闭管道' 环境: Windows 7 SP1(x86和x64) Windows 8(x86和x64) Windo ...

  6. relatedTarget, fromElement, toElement

    原文:http://www.quirksmode.org/js/events_mouse.html#relatedtarget W3C在mouseover和mouseout事件中添加了relatedT ...

  7. python手记(51)

    python通过声音将文件内容隐藏,实现原理是将文件的内容分别插入到声音文件的不同位置中做为当次采样的数据,目前是对英文文本文档加解密 #!/usr/bin/env python # -*- codi ...

  8. WISE安装程序增加注册控制

    我做安装程序,一直用的WISE 9.最近为一个用户提供安装程序时,公司要求对安装程序增加控制,避免用户到处安装,增加公司服务的压力.因此,我在WISE制作的安装程序中增加了注册码校验控制,不能给出正确 ...

  9. linux学习之(三)-文件操作命令

    创建一个空文件: touch  文件名 例:touch   tom 查看: 查看一个文件的内容命令cat 文件名 例:cat tom   注:cat命令并不能显示文件的所有信息,但屏幕显示的 行数是有 ...

  10. Android编程之LayoutInflater的inflate方法具体解释

    LayoutInflater的inflate方法,在fragment的onCreateView方法中经经常使用到: public View onCreateView(LayoutInflater in ...