dicas show delphi

DICAS

Visite a biblioteca de dicas da comunidade.

Saiba mais
sombra
Artigos Show Delphi

ARTIGOS

Abordagens detalhadas sobre assuntos diversos.

Saiba mais
sombra
iniciantes show delphi

INICIANTES

Aprenda a programar de um modo simples e fácil.

Saiba mais
sombra
downloads show delphi

DOWNLOADS

Acesse os materiais exclusivos aos membros.

Saiba mais
sombra
voltar

PARA QUEM GOSTA DE DELPHI

Classe para facilitar a manipulação de tipo enumerados.

Excelente classe para facilitar o trabalho com propriedades Enumeradas.

1
2
type
  TMyType = (mtFirst, mtSecond, mtThird);

Código fonte da classe:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
unit Enumerator;
 
interface
 
uses
System.TypInfo, System.Math;
 
type
TEnumeration<T> = class
strict private
class function TypeInfo: PTypeInfo; inline; static;
class function TypeData: PTypeData; inline; static;
public
class function IsEnumeration: Boolean; static;
class function ToOrdinal(Enum: T): Integer; inline; static;
class function FromOrdinal(Value: Integer): T; inline; static;
class function MinValue: Integer; inline; static;
class function MaxValue: Integer; inline; static;
class function InRange(Value: Integer): Boolean; inline; static;
class function EnsureRange(Value: Integer): Integer; inline; static;
end;
 
implementation
 
class function TEnumeration<T>.TypeInfo: PTypeInfo;
begin
Result := System.TypeInfo(T);
end;
 
class function TEnumeration<T>.TypeData: PTypeData;
begin
Result := System.TypInfo.GetTypeData(TypeInfo);
end;
 
class function TEnumeration<T>.IsEnumeration: Boolean;
begin
Result := TypeInfo.Kind = tkEnumeration;
end;
 
class function TEnumeration<T>.ToOrdinal(Enum: T): Integer;
begin
Assert(IsEnumeration);
Assert(SizeOf(Enum) <= SizeOf(Result));
Result := 0;
Move(Enum, Result, SizeOf(Enum));
Assert(InRange(Result));
end;
 
class function TEnumeration<T>.FromOrdinal(Value: Integer): T;
begin
Assert(IsEnumeration);
Assert(InRange(Value));
Assert(SizeOf(Result) <= SizeOf(Value));
Move(Value, Result, SizeOf(Result));
end;
 
class function TEnumeration<T>.MinValue: Integer;
begin
Assert(IsEnumeration);
Result := TypeData.MinValue;
end;
 
class function TEnumeration<T>.MaxValue: Integer;
begin
Assert(IsEnumeration);
Result := TypeData.MaxValue;
end;
 
class function TEnumeration<T>.InRange(Value: Integer): Boolean;
var
ptd: PTypeData;
begin
Assert(IsEnumeration);
ptd := TypeData;
Result := System.Math.InRange(Value, ptd.MinValue, ptd.MaxValue);
end;
 
class function TEnumeration<T>.EnsureRange(Value: Integer): Integer;
var
ptd: PTypeData;
begin
Assert(IsEnumeration);
ptd := TypeData;
Result := System.Math.EnsureRange(Value, ptd.MinValue, ptd.MaxValue);
end;
 
end.

Exemplo de uso

1
myTypeselection := TEnumeration<TMyType>.FromOrdinal(MyRadiogroup.ItemIndex);

Autor Original: David Heffernan
Fonte: http://stackoverflow.com/questions/21399427/convert-integer-value-to-enumeration-type?noredirect=1&lq=1

  • Giovani Da Cruz
  • 0 comentários
  • 14 de outubro de 2016

Deixe um comentário

Ir ao topo

© 2024 Infus Soluções em Tecnologia - Todos os Direitos Reservados