Eri*_*c G 5 delphi generics enums
我正在尝试创建一个泛型类,我可以使用一组枚举来启动内部的值.例如:
constructor TManager<TEnum>.Create;
var
enum: TEnum;
enumObj: TMyObject;
begin
fMyObjectList:= TObjectDictionary<TEnum,TMyObject>.Create([doOwnsValues],10);
for enum:= Low(TEnum) to High(TEnum) do
begin
enumObj:= TMyObject.Create();
fMyObjectList.Add(enum, enumObj);
end;
end;
Run Code Online (Sandbox Code Playgroud)
此外,以后的方法将通过枚举值获取对象,例如:
function TManager<TEnum>.Fetch(enum: TEnum): TMyObject;
begin
fMyObjectList.TryGetValue(enum, Result);
end;
Run Code Online (Sandbox Code Playgroud)
但是,作为通用参数传递,delphi不知道TEnum将是一个枚举.我能以某种方式强制执行吗?
正如David提到的那样,你可以做的最好的事情就是在运行时使用RTTI.
type
TRttiHelp = record
class procedure EnumIter<TEnum {:enum}>; static;
end;
class procedure TRttiHelp.EnumIter<TEnum {:enum}>;
var
typeInf: PTypeInfo;
typeData: PTypeData;
iterValue: Integer;
begin
typeInf := PTypeInfo(TypeInfo(TEnum));
if typeInf^.Kind <> tkEnumeration then
raise EInvalidCast.CreateRes(@SInvalidCast);
typeData := GetTypeData(typeInf);
for iterValue := typeData.MinValue to typeData.MaxValue do
WhateverYouWish;
end;
Run Code Online (Sandbox Code Playgroud)
虽然我不知道当你的枚举定义了诸如的值时代码的行为
(a=9, b=19, c=25)
Run Code Online (Sandbox Code Playgroud)
编辑:
如果你想返回iterValue
枚举,可以使用以下函数,取自Jim Ferguson的枚举助手类
class function TRttiHelp.EnumValue<TEnum {:enum}>(const aValue: Integer): TEnum;
var
typeInf: PTypeInfo;
begin
typeInf := PTypeInfo(TypeInfo(TEnum));
if typeInf^.Kind <> tkEnumeration then
raise EInvalidCast.CreateRes(@SInvalidCast);
case GetTypeData(typeInf)^.OrdType of
otUByte, otSByte:
PByte(@Result)^ := aValue;
otUWord, otSWord:
PWord(@Result)^ := aValue;
otULong, otSLong:
PInteger(@Result)^ := aValue;
else
raise EInvalidCast.CreateRes(@SInvalidCast);
end;
end;
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用通常提供的作为构造函数中字典的索引.
归档时间: |
|
查看次数: |
3679 次 |
最近记录: |