這不是很快,並且由於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)
您是否嘗試過使用標準的'包括()'和'排除()'功能,以及'in'操作?只要'T'和'ST'是兼容類型,我期望它可以工作,並且不能編譯爲不兼容的類型。 –
@Remy這不適用於泛型類型,因爲無法應用合適的約束 –
@Lars您的屬性聲明無效。你之前在Google+上做過這個。你能修復它嗎? –