Entwickler-Ecke

Delphi Language (Object-Pascal) / CLX - Generische "set of enum"


acadam71 - Sa 02.02.13 13:13
Titel: Generische "set of enum"
Ich arbeite viel mit verschiedenen Enums und Sets, z.B.
TOrderType = (otOffer, otOrderConfirmation, otDeliveryNote, otInvoice, otCancellation, otCreditNote, otOrder);
TOrderTypes = set of TOrderType;

Um Sets in einer DB zu speichern und dazu nicht für jeden Set-Typ eine Methode schreiben zu müssen, wandle ich Set-Typen (wie TOrderType, s.o.) in den allgemeinen Type "TIntegerSet" um.
ebenfalls gibt es eine Methode zum Umwandeln von TIntegerSet-Typen zu anderen Set-Typen wie folgt:


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
class procedure TEnumTool.IntegerSetToSet<TEnum>(const AIntegerSet: TIntegerSet; var ASet);
var
  LTypeInfo: PTypeInfo;
begin
  LTypeInfo := PTypeInfo(TypeInfo(TEnum));

  if LTypeInfo^.Kind <> tkEnumeration then
    raise Exception.Create('Invalid type cast.');

  case GetTypeData(LTypeInfo)^.OrdType of
    otUByte, otSByte:
      Byte(ASet) := Integer(AIntegerSet);
    otUWord, otSWord:
      Word(ASet) := Integer(AIntegerSet);
    otULong, otSLong:
      Integer(ASet) := Integer(AIntegerSet);
  else
    raise Exception.Create('Invalid type cast.');
  end;
end;

Nun meine Fragen:
1. Kann man das auch ganz einfach per

Delphi-Quelltext
1:
Integer(ASet) := Integer(AIntegerSet);                    

machen oder kann das zu Problemen führen, da ASet ja auch als Byte oder Word im Speicher dargestellt sein könnte?

2. Ist die oben angegebene Methode sicher (Var-Parameter "ASet" hat keinen Typ)?
Aufruf:

Delphi-Quelltext
1:
TEnumTool.IntegerSetToSet<TOrderType>(AIntegerSet, AOrderTypeSet);