我有基类
TWMapObject = class (TObject)
private
AFname: string;
FFileHandler: TWMapFileHandler;
function TryOpenFileHandler(const fName: string): TWMapFileHandler;
public
property FileHandler: TWMapFileHandler read FFileHandler;
constructor Create(const fName: string);
destructor Destroy; override;
end;
祖先
TBlpTexture = class (TWMapObject)
public
width, height: integer;
id: GLuint;
hdr: TBLP2Header;
constructor Create(const fname: string);
destructor Destroy; override;
procedure LoadBlp;
end;
和基于 T 的经理
TWMapObjectClass = class of TWMapObject;
TWMapObjectManager<T: TWMapObject, constructor> = class
type
PManagerRec = ^TManagerRec;
TManagerRec = record
obj: T;
ref: integer;
end;
private
ht: TDictionary<string, PManagerRec>;
function CreateNewT(const fName: string): T;
public
constructor Create;
destructor Destroy; override;
procedure Add(const fName: string);
procedure Remove(const fName: string);
procedure Clear;
function Get(const fName: string): T;
end;
我想使用TWMapObject
基于类作为通用参数T
,用于该辅助函数
function TWMapObjectManager<T>.CreateNewT(const fName: string): T;
var
obj: TWMapObject;
ct: TWMapObjectClass;
begin
ct := TWMapObjectClass(GetTypeData(TypeInfo(T)).ClassType);
obj := ct.Create(fName);
Move(obj, result, SizeOf(pointer));
end;
从调用
procedure TWMapObjectManager<T>.Add(const fName: string);
var
pr: PManagerRec;
begin
if ht.TryGetValue(fName, pr) then
inc(pr.ref)
else begin
GetMem(pr, sizeof(TManagerRec));
pr.obj := CreateNewT(fName);
pr.ref := 1;
ht.Add(fName, pr);
end
end;
和经理对象创建为TexManager: TWMapObjectManager<TBlpTexture>
. 该代码适用于TControl
诸如 etc 之类的基础对象TButton
,并在以前的项目中创建了精确TButton
的示例
class function TLuaClassTemplate<T>.CreateNewT(AOwner: TComponent): T;
var
Ctl: TControl;
begin
Ctl := TControlClass(GetTypeData(TypeInfo(T)).ClassType).Create(AOwner);
Move(Ctl, result, SizeOf(pointer));
end;
使用 TWMapObject 我将TBlpTexture
其视为 ct 的值,但仅调用构造函数TWMapObject
而不是TBlpTexture
. 难道我做错了什么?可以修复吗?