Lars Fosdal
Lars Fosdal

Reputation: 1174

How do I check or change which set elements are present, using RTTI?

I want to be able to check, add and remove T:TElements from ST:TElementSet.

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

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

Generics doesn't enable me to tell the compiler that T is an enumerated type and that ST is a set of T.

RTTI enables me to identify the types as tkEnumeration and tkSet - but I am unsure if I can make a strict connection between the two using RTTI. That doesn't really matter as I only need to twiddle the set bits by ordinal value.

The question is: Can I do this safely, using Generics and RTTI, and if so - how?

Examples and/or references to prior art would be appreciated.

Upvotes: 5

Views: 1441

Answers (2)

Stefan Glienke
Stefan Glienke

Reputation: 21713

Assuming that we only handle enums that are contiguous (because others don't have proper typeinfo and could not be handled so easily) we can do this simply without typeInfo/RTTI.

An enum set it just a bit mask for the elements in an enum.

So for example the set [elA, elC] equals 00000101 (right-to-left) which equals 5.

The index of the bit to set equals the ordinal value of the enum + 1 (because the first enum value has ordinal 0 but it's the 1st bit).

Since we cannot set individual bits in Delphi but only Bytes we need to calculate the correct value which leads to this code for include:

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

Since sets cannot contain more than 256 elements we are also save to assume that the enum value is always the size of a Byte. Handling enums that don't start at 0 would require a bit more code and reading typeinfo for their min and max value

Here some test code - I tricksed a bit with using absolute but you can also use 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.

I leave implementing other methods and error checking as an exercise for the reader.

Upvotes: 5

user3392332
user3392332

Reputation:

This ain't fast, and you won't get any compiler-time safety thanks to Delphi having generics and not templates, but I think this should cover all the bases at runtime.

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.

This outputs the following for me, using 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)

Upvotes: 1

Related Questions