基于泛型类的具体类型的条件行为

时间:2009-04-30 08:32:03

标签: delphi generics types delphi-2009

由于yesterday的问题可能并不完全清楚,而且我没有得到我想要的答案,我会尝试以更一般的方式制定答案:

有没有办法基于实例化泛型类型的实际类型实现特殊行为,使用明确的条件语句或使用某种特化?伪代码:

TGenericType <T> = class
  function Func : Integer;
end;
...
function TGenericType <T>.Func : Integer;
begin
  if (T = String) then Exit (0);
  if (T is class) then Exit (1);
end;
...
function TGenericType <T : class>.Func : Integer;
begin
Result := 1;
end;
function TGenericType <String>.Func : Integer;
begin
Result := 0;
end;

4 个答案:

答案 0 :(得分:22)

您可以使用TypeInfo(T) = TypeInfo(string)回退到RTTI。要测试某些内容是否属于某个类,您可以使用类似PTypeInfo(TypeInfo(T))^.Kind = tkClass的内容。

PTypeInfo类型和tkClass枚举成员在TypInfo单元中定义。

答案 1 :(得分:3)

如果有人感兴趣我是如何实施我的“最坏情况下的大小字符串特殊处理”

class function RTTIUtils.GetDeepSize <T> (Variable : T) : Integer;
var
  StringLength          : Integer;
  Ptr                   : PInteger;
begin
if (TypeInfo (T) = TypeInfo (String)) then
  begin
  Ptr := @Variable;
  Ptr := PInteger (Ptr^);
  Dec (Ptr);
  StringLength := Ptr^;
  Result := StringLength * SizeOf (Char) + 12;
  end
else
  Result := 0;
end;

对我而言,这完成了手头的工作。感谢所有贡献者!

答案 2 :(得分:1)

在C#中,您可以执行typeof(T),这样您就可以执行类似

的操作
(T = String)

(T is class)

我还没有看到你的另一个问题(你没有链接到它),但你真的正在寻找什么?一般来说,通过像你正在做的事情或切换来做类型或类型代码的条件通常最好转换为在某个地方通过上下文定制的接口或抽象函数。

答案 3 :(得分:1)

TypeInfo(T)是正确的方法。此外,您可以使用TypInfo单元中的所有内容(如TTypeData记录)来确定您使用的类型的某些特定属性,而不是泛型。当您确定使用的当前类型而不是T时,您可以使用指针技巧来获取变量的值。

这是一个示例代码,它接受任何枚举类型为通用。请注意,它仅适用于常规枚举(没有固定值,如

TEnumWontWork =(first = 1,second,third)

)并且不能在过程中将枚举声明为本地类型。在这些情况下,编译器不会为枚举生成TypeInfo。

type
  // Sample generic class that accepts any enumeration type as T
  TEnumArr<T> = class
  strict private
    fArr: array of Byte;
    fIdxType: TOrdType;
    function IdxToInt(idx: T): Int64;
    procedure Put(idx: T; Val: Byte);
    function Get(idx: T): Byte;
  public
    constructor Create;
    property Items[Index: T]: Byte read Get write Put; default;
  end;

constructor TEnumArr<T>.Create;
var
  pti: PTypeInfo;
  ptd: PTypeData;
begin
  pti := TypeInfo(T);
  if pti = nil then
    Error('no type info');
  // Perform run-time type check
  if pti^.Kind <> tkEnumeration then
    Error('not an enum');
  // Reach for TTypeData record that goes right after TTypeInfo record
  // Note that SizeOf(pti.Name) won't work here
  ptd := PTypeData(PByte(pti) + SizeOf(pti.Kind) + (Length(pti.Name)+1)*SizeOf(AnsiChar));
  // Init internal array with the max value of enumeration
  SetLength(fArr, ptd.MaxValue);
  // Save ordinal type of the enum
  fIdxType := ptd.OrdType;
end;

// Converts index given as enumeration item to integer.
// We can't just typecast here like Int64(idx) because of compiler restrictions so
//  use pointer tricks. We also check for the ordinal type of idx as it may vary
//  depending on compiler options and number of items in enumeration.
function TEnumArr<T>.IdxToInt(idx: T): Int64;
var
  p: Pointer;
begin
  p := @idx;

  case fIdxType of
    otSByte: Result := PShortInt(p)^;
    otUByte: Result := PByte(p)^;
    otSWord: Result := PSmallInt(p)^;
    otUWord: Result := PWord(p)^;
    otSLong: Result := PLongInt(p)^;
    otULong: Result := PLongWord(p)^;
  end;
end;

function TEnumArr<T>.Get(idx: T): Byte;
begin
  Result := fArr[IdxToInt(idx)];
end;

procedure TEnumArr<T>.Put(idx: T; Val: Byte);
begin
  fArr[IdxToInt(idx)] := Val;
end;

使用样本:

type
  TEnum  = (enOne, enTwo, enThree);
var
  tst: TEnumArr<TEnum>;
begin
  tst := TEnumArr<TEnum>.Create;
  tst[enTwo] := $FF;
  Log(tst[enTwo]);

作为简历,我在这里使用了三个技巧:

1)使用T

的常规道具获取T的TypeInfo

2)使用T

的详细道具获取T的TypeData

3)使用指针魔法获取T类型给出的参数值。

希望得到这个帮助。

相关问题