-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathWDDT.RTTI.pas
More file actions
161 lines (146 loc) · 4.79 KB
/
Copy pathWDDT.RTTI.pas
File metadata and controls
161 lines (146 loc) · 4.79 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
unit WDDT.RTTI;
interface
uses
System.SysUtils,
System.Classes,
System.TypInfo,
System.Rtti;
type
TPropertyAcceptProc = reference to procedure(Target: TObject; PropInfo: PPropInfo; out Accept: Boolean);
procedure CopyObject(ObjFrom, ObjTo: TObject; AcceptProc: TPropertyAcceptProc = nil);
function HasAllRequiredProperties(RClass: TRttiInstanceType; RequiredProperties: array of string;
out Properties: TArray<TRttiProperty>): Boolean;
implementation
procedure CopyObject(ObjFrom, ObjTo: TObject; AcceptProc: TPropertyAcceptProc);
var
PropInfos: PPropList;
PropInfo: PPropInfo;
Count, Loop: Integer;
OrdVal: Longint;
StrVal: String;
FloatVal: Extended;
MethodVal: TMethod;
PropertyAccepted: Boolean;
begin
Count := GetPropList(ObjFrom.ClassInfo, tkAny, nil);
GetMem(PropInfos, Count * SizeOf(PPropInfo));
try
GetPropList(ObjFrom.ClassInfo, tkAny, PropInfos);
for Loop := 0 to Count - 1 do
begin
PropInfo := GetPropInfo(ObjTo.ClassInfo, string(PropInfos^[Loop]^.Name));
PropertyAccepted := True;
if Assigned(AcceptProc) then
begin
AcceptProc(ObjTo, PropInfo, PropertyAccepted);
if not PropertyAccepted then
Continue;
end;
case PropInfos^[Loop]^.PropType^.Kind of
tkInteger, tkChar, tkEnumeration,
tkSet, tkClass, tkWChar:
begin
OrdVal := GetOrdProp(ObjFrom, PropInfos^[Loop]);
if Assigned(PropInfo) then
SetOrdProp(ObjTo, PropInfo, OrdVal);
end;
tkFloat:
begin
FloatVal := GetFloatProp(ObjFrom, PropInfos^[Loop]);
if Assigned(PropInfo) then
SetFloatProp(ObjTo, PropInfo, FloatVal);
end;
tkWString,
tkLString,
tkString,
tkUnicodeString:
begin
{ Avoid copying 'Name' - components must have unique names }
if UpperCase(string(PropInfos^[Loop]^.Name)) = 'NAME' then
Continue;
StrVal := GetStrProp(ObjFrom, PropInfos^[Loop]);
if Assigned(PropInfo) then
SetStrProp(ObjTo, PropInfo, StrVal);
end;
tkMethod:
begin
MethodVal := GetMethodProp(ObjFrom, PropInfos^[Loop]);
if Assigned(PropInfo) then
SetMethodProp(ObjTo, PropInfo, MethodVal);
end
end
end
finally
FreeMem(PropInfos, Count * SizeOf(PPropInfo));
end;
end;
// Überprüft, ob alle in RequiredProperties angegebenen Eigenschaften in RClass
// existieren. Falls dies der Fall ist (Result = True), dann liegen im Ausgabeparameter
// Properties an exakt den gleichen Positionen die TRttiProperty-Instanzen an, über welche
// wiederum auf die Zieleigenschaften einer Klasseninstanz zugegriffen werden kann.
//
// Wie man an den Parameter RClass ist im folgenden Beispiel ersichtlich. Bei Target handelt sich
// um die Instanz auf wessen Eigenschaften man zugreifen möchte.
// <code>
// RC := TRttiContext.Create;
// try
// RClass := RC.GetType(Target.ClassType) as TRttiInstanceType;
// finally
// RC.Free;
// end;
// </code>
//
// Wenn man die Properties schlussendlich hat, so wird wie folgt darauf zugegriffen:
// <code>
// if HasAllRequiredProperties(RClass, ['PropA', 'PropB'], Properties) then
// begin
// Properties[0].SetValue(Target, 'My new value for PropA');
// Properties[1].SetValue(Target, 'My new value for PropB');
// end;
// </code>
function HasAllRequiredProperties(RClass: TRttiInstanceType; RequiredProperties: array of string;
out Properties: TArray<TRttiProperty>): Boolean;
var
AllProperties: TArray<TRttiProperty>;
Status: TBits;
CurProperty: TRttiProperty;
CurPropertyName: string;
PropIndex, ReqPropCount, MatchCount: Integer;
function GetPropertyParameterIndex(const PropertyName: string): Integer;
var
cc: Integer;
begin
for cc := 0 to ReqPropCount - 1 do
if not Status[cc] and SameText(PropertyName, RequiredProperties[cc]) then
Exit(cc);
Result := -1;
end;
begin
Result := False;
AllProperties := RClass.GetProperties;
ReqPropCount := Length(RequiredProperties);
if Length(AllProperties) < ReqPropCount then
Exit;
Status := TBits.Create;
try
Status.Size := ReqPropCount;
SetLength(Properties, ReqPropCount);
MatchCount := 0;
for CurProperty in AllProperties do
begin
CurPropertyName := CurProperty.Name;
PropIndex := GetPropertyParameterIndex(CurPropertyName);
if PropIndex >= 0 then
begin
Properties[PropIndex] := CurProperty;
Status[PropIndex] := True;
Inc(MatchCount);
if MatchCount = ReqPropCount then
Exit(True);
end;
end;
finally
Status.Free;
end;
end;
end.