如何使用 RTTI 检查或更改存在的集合元素?

How do I check or change which set elements are present, using 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 在两者之间建立严格的联系。这并不重要,因为我只需要按序数值调整设置位。

问题是:我可以使用泛型和 RTTI 安全地执行此操作吗?如果可以,怎么做?

示例 and/or 对现有技术的引用将不胜感激。

假设我们只处理连续的枚举(因为其他枚举没有正确的类型信息并且不能如此容易地处理)我们可以在没有 typeInfo/RTTI.

的情况下简单地做到这一点

枚举只是为枚举中的元素设置位掩码。

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

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

由于我们无法在 Delphi 中设置 div 等位,而只能设置字节,因此我们需要计算正确的值,这导致此代码包含:

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

由于集合不能包含超过 256 个元素,我们还假设枚举值始终是一个字节的大小。处理不从 0 开始的枚举需要更多代码并读取类型信息以获取它们的最小值和最大值

这里是一些测试代码——我使用绝对值做了一些小动作,但你也可以使用 hardcasts:

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.

我将实现其他方法和错误检查作为 reader 的练习。

这并不快,而且由于 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)