Debian Codegraph @

src/compatibility/delphi-only/rttiutils.pas

raw
1 {
2 Minimized for Castle Game Engine to support TPropInfoList,
3 to be able to use it in Delphi.
4 }
5 {
6 This file is part of the Free Pascal run time library.
7 Copyright (c) 2004 by the Free Pascal development team
8
9 Some RTTI utils, based on RX rtti utils.
10
11 See the file COPYING.FPC, included in this distribution,
12 for details about the copyright.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
18 **********************************************************************}
19 { **********************************************************************
20
21 Based on the rttiutils unit that comes with RXLib.
22 Adapted to work with FCL, free of VCL dependencies.
23 Fixed some errors along the way as well. MVC.
24
25 To make it work across the 'Root Component' (Form/Datamodule etc),
26 you MUST set the FindGlobalComponentCallBack event handler.
27
28 Original copyright:
29 Delphi VCL Extensions (RX)
30 Copyright (c) 1995, 1996 AO ROSNO
31 Copyright (c) 1997 Master-Bank
32 **********************************************************************}
33
34 {$I castleconf.inc}
35
36 {$ifdef FPC}
37 {$mode objfpc}
38 {$H+}
39 {$endif}
40 unit RttiUtils;
41
42 interface
43
44 uses
45 SysUtils, Classes, {Graphics, Controls, Forms,} TypInfo, StrUtils;
46
47 type
48
49 { TPropInfoList }
50
51 TPropInfoList = class(TObject)
52 private
53 FList: PPropList;
54 FCount: Integer;
55 FSize: Integer;
56 function Get(Index: Integer): PPropInfo;
57 public
58 constructor Create(AObject: TObject; Filter: TTypeKinds);
59 destructor Destroy; override;
60 function Contains(P: PPropInfo): Boolean;
61 function Find(const AName: string): PPropInfo;
62 procedure Delete(Index: Integer);
63 procedure Intersect(List: TPropInfoList);
64 property Count: Integer read FCount;
65 property Items[Index: Integer]: PPropInfo read Get; default;
66 end;
67
68 implementation
69
70 const
71 sCount = 'Count';
72 sItem = 'Item%d';
73 sNull = '(null)';
74
75 type
76 TCardinalSet = set of 0..SizeOf(Cardinal) * 8 - 1;
77
78 function GetPropType(PropInfo: PPropInfo): PTypeInfo;
79 begin
80 Result := PropInfo^.PropType {$ifndef FPC}^{$endif};
81 end;
82
83 { TPropInfoList }
84
85 constructor TPropInfoList.Create(AObject: TObject; Filter: TTypeKinds);
86 begin
87 if AObject <> nil then
88 begin
89 FCount := GetPropList(AObject.ClassInfo, Filter, nil);
90 FSize := FCount * SizeOf(Pointer);
91 GetMem(FList, FSize);
92 GetPropList(AObject.ClassInfo, Filter, FList);
93 end
94 else
95 begin
96 FCount := 0;
97 FList := nil;
98 end;
99 end;
100
101 destructor TPropInfoList.Destroy;
102 begin
103 if FList <> nil then FreeMem(FList, FSize);
104 end;
105
106 function TPropInfoList.Contains(P: PPropInfo): Boolean;
107 var
108 I: Integer;
109 begin
110 for I := 0 to FCount - 1 do
111 with FList^[I]^ do
112 if (PropType = P^.PropType) and (CompareText(Name, P^.Name) = 0) then
113 begin
114 Result := True;
115 Exit;
116 end;
117 Result := False;
118 end;
119
120 function TPropInfoList.Find(const AName: string): PPropInfo;
121 var
122 I: Integer;
123 begin
124 for I := 0 to FCount - 1 do
125 with FList^[I]^ do
126 if (CompareText(Name, AName) = 0) then
127 begin
128 Result := FList^[I];
129 Exit;
130 end;
131 Result := nil;
132 end;
133
134 procedure TPropInfoList.Delete(Index: Integer);
135 begin
136 Dec(FCount);
137 if Index < FCount then Move(FList^[Index + 1], FList^[Index],
138 (FCount - Index) * SizeOf(Pointer));
139 end;
140
141 function TPropInfoList.Get(Index: Integer): PPropInfo;
142 begin
143 Result := FList^[Index];
144 end;
145
146 procedure TPropInfoList.Intersect(List: TPropInfoList);
147 var
148 I: Integer;
149 begin
150 for I := FCount - 1 downto 0 do
151 if not List.Contains(FList^[I]) then Delete(I);
152 end;
153
154 { Utility routines }
155
156 function CreateStoredItem(const CompName, PropName: string): string;
157 begin
158 Result := '';
159 if (CompName <> '') and (PropName <> '') then
160 Result := CompName + '.' + PropName;
161 end;
162
163 function ParseStoredItem(const Item: string; var CompName, PropName: string): Boolean;
164 var
165 I: Integer;
166 begin
167 Result := False;
168 if Length(Item) = 0 then Exit;
169 I := Pos('.', Item);
170 if I > 0 then begin
171 CompName := Trim(Copy(Item, 1, I - 1));
172 PropName := Trim(Copy(Item, I + 1, MaxInt));
173 Result := (Length(CompName) > 0) and (Length(PropName) > 0);
174 end;
175 end;
176
177 function ReplaceComponentName(const Item, CompName: string): string;
178 var
179 ACompName, APropName: string;
180 begin
181 Result := '';
182 if ParseStoredItem(Item, ACompName, APropName) then
183 Result := CreateStoredItem(CompName, APropName);
184 end;
185
186 procedure UpdateStoredList(AComponent: TComponent; AStoredList: TStrings; FromForm: Boolean);
187
188 var
189 I: Integer;
190 Component: TComponent;
191 CompName, PropName: string;
192
193 begin
194 if (AStoredList = nil) or (AComponent = nil) then
195 Exit;
196 for I := AStoredList.Count - 1 downto 0 do
197 begin
198 if ParseStoredItem(AStoredList[I], CompName, PropName) then
199 begin
200 if FromForm then
201 begin
202 Component := AComponent.FindComponent(CompName);
203 if Component = nil then
204 AStoredList.Delete(I)
205 else
206 AStoredList.Objects[I]:=Component;
207 end
208 else
209 begin
210 Component := TComponent(AStoredList.Objects[I]);
211 if Component <> nil then
212 AStoredList[I] := ReplaceComponentName(AStoredList[I], Component.Name)
213 else
214 AStoredList.Delete(I);
215 end;
216 end
217 else
218 AStoredList.Delete(I);
219 end;
220 end;
221
222 end.