惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
GbyAI
GbyAI
aimingoo的专栏
aimingoo的专栏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
腾讯CDC
博客园 - Franky
Engineering at Meta
Engineering at Meta
C
Check Point Blog
T
The Blog of Author Tim Ferriss
有赞技术团队
有赞技术团队
Microsoft Azure Blog
Microsoft Azure Blog
MyScale Blog
MyScale Blog
I
InfoQ
Blog — PlanetScale
Blog — PlanetScale
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
N
Netflix TechBlog - Medium
Last Week in AI
Last Week in AI
S
SegmentFault 最新的问题
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
WordPress大学
WordPress大学

博客园 - Sephil

[小技巧] 倍数的向上取整和向下取整 WTL汉化版 Delphi 与 VC 共享接口和对象 百度音乐下载工具 (最后更新: 2012-7-20) 为Delphi应用增加脚本支持 插件框架Extensible Framework for Delphi DirectUI for Delphi VC CListCtrl 第一列列宽自适应 WriteFile写磁盘扇区是87错误的原因 批量更改文件名的批处理文件 替代Windows运行功能的工具FastRun 关于 API 中返回字串的一些问题 BCB/Delphi2007 隐藏任务栏图标 OGA & WGA Crack 所有小工具 迅雷/快车/旋风地址转换器 oracle ora-01033和ora-00600错误 数独游戏 SudokuPuzzle 将文件夹映射为驱动器的工具
DelphiXE泛型不能用类类型做为约束的另类解决方案
Sephil · 2012-09-06 · via 博客园 - Sephil

前天想写个泛型类,用来保存类类型(如TClass、TComponentClass之类)

但是DelphiXE的泛型约束不支持TClass

TClassList<T: TClass> 将会得到编译错误

因此我只能去掉约束条件 TClassList<T>

但是这样又有个问题,即 T 类型不明确,无法用 Assigned(T) 或 T <> nil 来判断值是否为空

后来想了个办法绕过了这个问题,详见代码

procedure TClassList<T>.Register(const ADisplayName: string; AClass: T);
var
  P: Pointer;
begin
  P := PPointer(@AClass)^;
  Assert(Assigned(P), 'Target class cannot be nil.');

  FClassList.AddObject(ADisplayName, P);
end;

function TClassList<T>.Find(const ADisplayName: stringout Value: T): Boolean;
var
  Index: Integer;
begin
  Assert(ADisplayName <> '''Display name cannot be blank.');

  Index := FClassList.IndexOf(ADisplayName);
  Result := Index >= 0;
  if Result then PPointer(@Value)^ := Pointer(FClassList.Objects[Index]);
end;

记录一下,免得忘记