Advertisement
filhotecmail

System.Enumerates.Controller

Sep 29th, 2020 (edited)
1,721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.05 KB | None | 0 0
  1. (*Uma classe simples para poder abstrair uma forma de Iteração com um Tipo enumerado qualquer
  2.  passando um tipo qualquer. Foi adicionado um suporte a Interface para que você não
  3. se preocupe com o destructor do object
  4.  
  5. ex.:  type TDrivers = (Driver1,Driver2,Driver3,Driver4);
  6.  
  7.   private
  8.     { Private declarations }
  9.     FDrivers: IEnum<TDrivers,TArray<String>>;
  10.   public
  11.     { Public declarations }
  12.     .....
  13.  
  14.    implementation
  15. Um exemplo passando valores como um JsonString
  16. FDrivers := TEnum<TDrivers,TArray<String>>
  17.               .Create([TJsonObject.create.addPair('Driver','Driver de comunicação Firedac').ToString,
  18.                        TJsonObject.create.addPair('Driver','Driver de comunicação IBX').ToString,
  19.                        TJsonObject.create.addPair('Driver','Driver de comunicação ZEUS').ToString,
  20.                        TJsonObject.create.addPair('Driver','Driver de comunicação GENERICO').ToString]);
  21.  
  22. FDrivers.&Set(Driver4);
  23.  
  24. Pegando um Retorno
  25.   Memo1.Lines.Add( FDrivers.&To );
  26.  
  27. *)
  28. unit System.Enumerates.Controller;
  29.  
  30. interface
  31.   uses System.SysUtils;
  32.  
  33.  Type TGArray = TArray<Variant>;
  34.  type IEnum<T: Record;TGArray> = Interface
  35.    ['{C9038BE7-B224-43DE-8C5A-CD4E59184F9A}']
  36.     function &To:Variant;
  37.     function &Set(Arg: T):T;
  38.  End;
  39.  type TEnum<T: Record;TGArray> = class(TInterfacedObject,IEnum<T,TGArray>)
  40.  
  41.   strict private
  42.    FArray: TGArray;
  43.    FGenericType: T;
  44.    FIntegerType: Byte;
  45.  public
  46.   function &To:Variant;
  47.   function &Set(Arg: T):T;
  48.   constructor Create(Arg: TArray<Variant>);
  49.  
  50.  end;
  51.  
  52. implementation
  53. { TEnum<T, TArray> }
  54.  
  55. function TEnum<T, TGArray>.&Set(Arg: T): T;
  56. begin
  57.  FGenericType :=  Arg;
  58.  FintegerType:= PCardinal(@Arg)^;
  59.  Result := FGenericType;
  60. end;
  61.  
  62. constructor TEnum<T, TGArray>.Create( Arg: TArray<Variant>);
  63. begin
  64.  FArray := Arg;
  65. end;
  66.  
  67. function TEnum<T, TGArray>.&To: Variant;
  68. begin
  69.  if getTypeKind(FGenericType) = tkEnumeration then
  70.  begin
  71.    Result := FArray[FintegerType];
  72.  end else
  73.   raise Exception.Create('O Tipo não é um Enumerado');
  74. end;
  75.  
  76. end.
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement