-
Notifications
You must be signed in to change notification settings - Fork 2
/
RTBind.Mapping.pas
99 lines (76 loc) · 2.31 KB
/
RTBind.Mapping.pas
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
88
89
90
91
92
93
94
95
96
97
98
99
unit RTBind.Mapping;
interface
Uses
System.Classes, System.Generics.Collections, Controls, RTBind.Mapping.Custom;
type
TRTBindMapping = class
strict private
class var FClassList: TDictionary<TClassName, TPropertyName>;
private
class procedure Initialize;
class procedure ReleaseInstances;
public
class procedure Register<I>; overload;
class procedure Register(const AControlClass: string; const APropName: string = 'Text'); overload;
class procedure UnRegister<I>; overload;
class procedure UnRegister(const AControlClass: string); overload;
class procedure UnRegisterAll;
class function Prop(const Sender: TControl): string;
end;
implementation
Uses
System.TypInfo, System.Rtti, System.SysUtils;
{ TRTBindMapping }
class procedure TRTBindMapping.Initialize;
begin
FClassList := TDictionary<TClassName, TPropertyName>.Create;
end;
class procedure TRTBindMapping.ReleaseInstances;
begin
FClassList.Free;
end;
class function TRTBindMapping.Prop(const Sender: TControl): string;
begin
if FClassList.ContainsKey(Sender.ClassName) then
Exit(FClassList.Items[Sender.ClassName]);
if FClassList.ContainsKey(Sender.Parent.ClassName) then
Exit(FClassList.Items[Sender.Parent.ClassName]);
raise Exception.Create('Control class not found! Verify your Mappings...');
end;
class procedure TRTBindMapping.Register(const AControlClass, APropName: string);
begin
FClassList.Add(AControlClass, APropName);
end;
class procedure TRTBindMapping.Register<I>;
var
Context: TRttiContext;
AttrList: TArray<TCustomAttribute>;
Attr: TCustomAttribute;
begin
AttrList := Context.GetType(TypeInfo(I)).GetAttributes;
for Attr in AttrList do
FClassList.Add(Control(Attr).ControlClass, Control(Attr).PropName)
end;
class procedure TRTBindMapping.UnRegister(const AControlClass: string);
begin
FClassList.Remove(AControlClass);
end;
class procedure TRTBindMapping.UnRegister<I>;
var
Context: TRttiContext;
AttrList: TArray<TCustomAttribute>;
Attr: TCustomAttribute;
begin
AttrList := Context.GetType(TypeInfo(I)).GetAttributes;
for Attr in AttrList do
FClassList.Remove(Control(Attr).ControlClass)
end;
class procedure TRTBindMapping.UnRegisterAll;
begin
FClassList.Clear;
end;
initialization
TRTBindMapping.Initialize;
finalization
TRTBindMapping.ReleaseInstances;
end.