如何使用RTTI检查或更改存在哪些设置元素?

时间:2015-10-16 14:54:22

标签: delphi generics set rtti

我希望能够从ST:TElementSet检查,添加和删除T:TElements。

type
  TElements = (elA, elB, elC);
  TElementSet = set of TElements;

  TMyClass<T, ST> = class
    property SetValue:ST;
  end;

泛型不允许我告诉编译器T是枚举类型而ST是一组T。

RTTI使我能够将类型识别为tkEnumeration和tkSet - 但我不确定是否可以使用RTTI在两者之间建立严格的连接。这并不重要,因为我只需要通过序数值来旋转设置位。

问题是:我可以安全地使用Generics和RTTI,如果是这样 - 如何做到这一点?

将赞赏对现有技术的示例和/或参考。

2 个答案:

答案 0 :(得分:5)

假设我们只处理连续的枚举(因为其他人没有正确的typeinfo且无法轻易处理),我们可以在没有typeInfo / RTTI的情况下完成此操作。

枚举为枚举中的元素设置了一个掩码。

例如,集合[elA,elC]等于00000101(从右到左)等于5。

要设置的位的索引等于enum + 1的序数值(因为第一个枚举值的序数为0,但它是第1位)。

由于我们不能在Delphi中设置单个位而只能在字节中设置,我们需要计算正确的值,从而导致包含以下代码:

set [enum div 8]:= set [enum div 8]或(1 shl(enum mod 8))

由于集合不能包含超过256个元素,因此我们也可以假设枚举值始终是字节的大小。处理不从0开始的枚举将需要更多的代码并读取typeinfo的最小值和最大值

这里有一些测试代码 - 我使用绝对技巧,但你也可以使用hardcast:

program GenericEnumSet;

{$APPTYPE CONSOLE}

type
  TMyEnum = (elA, elB, elC);
  TMySet = set of TMyEnum;

  TEnumSet<TEnum,TSet> = record
    value: TSet;
    procedure Include(const value: TEnum); inline;
    procedure Exclude(const value: TEnum); inline;
  end;

procedure _Include(var setValue; const enumValue);
var
  localEnum: Byte absolute enumValue;
  localSet: array[0..31] of Byte absolute setValue;
begin
  localSet[localEnum div 8] := localSet[localEnum div 8] or (1 shl (localEnum mod 8));
end;

procedure _Exclude(var setValue; const enumValue);
var
  localEnum: Byte absolute enumValue;
  localSet: array[0..31] of Byte absolute setValue;
begin
  localSet[localEnum div 8] := localSet[localEnum div 8] and not (1 shl (localEnum mod 8));
end;

procedure TEnumSet<TEnum, TSet>.Include(const value: TEnum);
begin
  _Include(Self.value, value);
end;

procedure TEnumSet<TEnum, TSet>.Exclude(const value: TEnum);
begin
  _Exclude(Self.value, value);
end;

var
  mySet: TEnumSet<TMyEnum,TMySet>;
  myEnum: TMyEnum;
begin
  mySet.value := [];
  for myEnum := Low(TMyEnum) to High(TMyEnum) do
  begin
    mySet.Include(myEnum);
    Assert(mySet.value = [Low(TMyEnum)..myEnum]);
  end;
  for myEnum := Low(TMyEnum) to High(TMyEnum) do
  begin
    mySet.Exclude(myEnum);
    if myEnum < High(TMyEnum) then
      Assert(mySet.value = [Succ(myEnum)..High(TMyEnum)])
    else
      Assert(mySet.value = []);
  end;
  Readln;
end.

我将实施其他方法和错误检查作为读者的练习。

答案 1 :(得分:1)

这很快,由于Delphi具有泛型而非模板,您不会获得任何编译时安全性,但我认为这应该涵盖运行时的所有基础。

program GenericSetInclusion;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  System.TypInfo,
  System.Rtti;

type
  TElm = (elFoo, elBar, elXyz);
  TElms = set of TElm;

  TOrd = 7..150;
  TOrds = set of TOrd;

type
  SafeSet = record
    class procedure Include<ST, T>(var s: ST; const e: T); static;
  end;

{ SafeSet }

class procedure SafeSet.Include<ST, T>(var s: ST; const e: T);
var
  ctx: TRttiContext;
  typ1: TRttiType;
  typ2: TRttiType;
  styp: TRttiSetType;
  etyp: TRttiOrdinalType;
  ttyp: TRttiOrdinalType;
  tmp: set of 0..255;
  o: 0..255;
  i: integer;
begin
  ctx := TRttiContext.Create();
  typ1 := ctx.GetType(TypeInfo(ST));

  if (typ1 = nil) then
    raise EArgumentException.Create('SafeSet<ST, T>.Include: ST has no type info');

  typ2 := ctx.GetType(TypeInfo(T));
  if (typ2 = nil) then
    raise EArgumentException.CreateFmt('SafeSet<ST=%s, T>.Include: T has no type info (most likely due to explicit ordinality)', [typ1.Name]);

  if (not (typ1 is TRttiSetType)) then
    raise EArgumentException.CreateFmt('SafeSet<ST=%s, T=%s>.Include: ST is not a set type', [typ1.Name, typ2.Name]);

  styp := TRttiSetType(typ1);

  if (SizeOf(ST) > SizeOf(tmp)) then
    raise EInvalidOpException.CreateFmt('SafeSet<ST=%s, T=%s>.Include: SizeOf(ST) > 8', [styp.Name, typ2.Name]);

  etyp := styp.ElementType as TRttiOrdinalType;

  if (not (typ2 is TRttiOrdinalType)) then
    raise EArgumentException.CreateFmt('SafeSet<ST=%s, T=%s>.Include: T is not an ordinal type', [styp.Name, typ2.Name]);

  ttyp := TRttiOrdinalType(typ2);

  case ttyp.OrdType of
    otSByte: i := PShortInt(@e)^;
    otUByte: i := PByte(@e)^;
  else
    raise EInvalidOpException.CreateFmt('SafeSet<ST=%s, T=%s>.Include: SizeOf(T) > 1', [styp.Name, ttyp.Name]);
  end;

  if (ttyp.Handle <> styp.ElementType.Handle) then
  begin
    if (((etyp is TRttiEnumerationType) and (not (ttyp is TRttiEnumerationType)))) or
       ((not (etyp is TRttiEnumerationType)) and (ttyp is TRttiEnumerationType)) then
      raise EArgumentException.CreateFmt('SafeSet<ST=%s, T=%s>.Include: ST is not a set of T (ST is set of %s)', [styp.Name, ttyp.Name, etyp.Name]);

    // ST is a set of integers rather than a set of enum
    // so do bounds checking
    if ((i < etyp.MinValue) or (i > etyp.MaxValue)) then
      raise EArgumentException.CreateFmt('SafeSet<ST=%s, T=%s>.Include: %d is not a valid element for ST (ST is set of %s = %d..%d)', [styp.Name, ttyp.Name, i, etyp.Name, etyp.MinValue, etyp.MaxValue]);
  end;

  o := i;

  FillChar(tmp, SizeOf(tmp), 0);
  Move(s, tmp, SizeOf(ST));

  System.Include(tmp, o);

  Move(tmp, s, SizeOf(ST));
end;

procedure Test(const p: TProc);
begin
  try
    p();
    WriteLn('Success');
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end;

var
  s: TElms;
  o: TOrds;
begin
  Test(
    procedure
    begin
      SafeSet.Include(s, elFoo);
      Assert(elFoo in s, 'elFoo not in s');
      Assert((s - [elFoo]) = [], 's contains elements it should not');

      SafeSet.Include(s, elBar);
      Assert(elFoo in s, 'elFoo not in s');
      Assert(elBar in s, 'elBar not in s');
      Assert((s - [elFoo, elBar]) = [], 's contains elements it should not');

      SafeSet.Include(s, elXyz);
      Assert(elFoo in s, 'elFoo not in s');
      Assert(elBar in s, 'elBar not in s');
      Assert(elXyz in s, 'elXyz not in s');
      Assert((s - [elFoo, elBar, elXyz]) = [], 's contains elements it should not');
    end
  );

  Test(
    procedure
    begin
      SafeSet.Include(o, 7);
      Assert(7 in o, '7 not in o');
      Assert((o - [7]) = [], 'o contains elements it should not');
    end
  );

  Test(
    procedure
    begin
      SafeSet.Include(s, 7);
      Assert(False, '7 should not be in s');
    end
  );

  Test(
    procedure
    begin
      SafeSet.Include(o, elFoo);
      Assert(False, 'elFoo should not be in o');
    end
  );

  Test(
    procedure
    begin
      SafeSet.Include(o, 1);
      Assert(False, '1 should not be in o');
    end
  );

  ReadLn;
end.

这为我输出以下内容,使用D10:

Success
Success
EArgumentException: SafeSet<ST=TElms, T=ShortInt>.Include: ST is not a set of T (ST is set of TElm)
EArgumentException: SafeSet<ST=TOrds, T=TElm>.Include: ST is not a set of T (ST is set of TOrd)
EArgumentException: SafeSet<ST=TOrds, T=ShortInt>.Include: 1 is not a valid element for ST (ST is set of TOrd = 7..150)