Using an enum with generics

后端 未结 2 1050
太阳男子
太阳男子 2021-01-06 02:40

I\'m trying to create a generic class to which I can use a set of enums to initiate the values inside. For example:

constructor TManager.Create         


        
相关标签:
2条回答
  • 2021-01-06 02:56

    As David mentioned the best you can do is at runtime with RTTI.

        type  
          TRttiHelp = record
            class procedure EnumIter<TEnum {:enum}>; static;
          end;
    
        class procedure TRttiHelp.EnumIter<TEnum {:enum}>;
        var
          typeInf: PTypeInfo;
          typeData: PTypeData;
          iterValue: Integer;
        begin
          typeInf := PTypeInfo(TypeInfo(TEnum));
          if typeInf^.Kind <> tkEnumeration then
            raise EInvalidCast.CreateRes(@SInvalidCast);
    
          typeData := GetTypeData(typeInf);
          for iterValue := typeData.MinValue to typeData.MaxValue do
            WhateverYouWish;
        end;  
    

    Although I don't know how the code behaves when your enum has defined values such as

        (a=9, b=19, c=25)
    

    Edit:

    If you would like to return iterValue to the enum, you may use the following function, taken from a enum helper class by Jim Ferguson

    class function TRttiHelp.EnumValue<TEnum {:enum}>(const aValue: Integer): TEnum;
    var
      typeInf: PTypeInfo;
    begin
      typeInf := PTypeInfo(TypeInfo(TEnum));
      if typeInf^.Kind <> tkEnumeration then
        raise EInvalidCast.CreateRes(@SInvalidCast);
    
      case GetTypeData(typeInf)^.OrdType of
        otUByte, otSByte:
          PByte(@Result)^ := aValue;
        otUWord, otSWord:
          PWord(@Result)^ := aValue;
        otULong, otSLong:
          PInteger(@Result)^ := aValue;
      else
        raise EInvalidCast.CreateRes(@SInvalidCast);
      end;
    end;
    

    You may then use the generically provided as the index to the dictionary in your constructor.

    0 讨论(0)
  • 2021-01-06 02:56

    You cannot constrain a generic parameter such that low() and high() can be used in the generic class. The only constraints available are class or interface constraints.

    To the very best of my knowledge, the language offers no generic way to enumerate over a generic enumerated type. Probably the best you can do is to use RTTI, sacrificing compile time type safety (as illustrated by Tobias).


    Having read the comments to Tobias's answer, it seems likely that what you really want here is TObjectDictionary<TEnum,TMyObject>. That's because you want to be able to find a TMyObject instance given a TEnum key. And you want TObjectDictionary rather than TDictionary because the former takes over ownership of the TMyObject instances. You need somebody to free them all when you are done, and you may as well let TObjectDictionary do it for you.

    For an example of the ownership side of this, see @RRUZ's answer here: Example for using Generics.Collections.TObjectDictionary

    0 讨论(0)
提交回复
热议问题