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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
{
This file is part of the Free Pascal Run Time Library (rtl)
Copyright (c) 2007 by Michael Van Canneyt,
member of the Free Pascal development team
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
type
// Quadruple representing an unresolved component property.
{ TUnresolvedReference }
TUnresolvedReference = class(TlinkedListItem)
Private
FRoot: TComponent; // Root component when streaming
FPropInfo: PPropInfo; // Property to set.
FGlobal, // Global component.
FRelative : string; // Path relative to global component.
Function Resolve(Instance : TPersistent) : Boolean; // Resolve this reference
Function RootMatches(ARoot : TComponent) : Boolean; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE} // True if Froot matches or ARoot is nil.
Function NextRef : TUnresolvedReference; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
end;
TLocalUnResolvedReference = class(TUnresolvedReference)
Finstance : TPersistent;
end;
// Linked list of TPersistent items that have unresolved properties.
{ TUnResolvedInstance }
TUnResolvedInstance = Class(TLinkedListItem)
Instance : TPersistent; // Instance we're handling unresolveds for
FUnresolved : TLinkedList; // The list
Destructor Destroy; override;
Function AddReference(ARoot : TComponent; APropInfo : PPropInfo; AGlobal,ARelative : String) : TUnresolvedReference;
Function RootUnresolved : TUnresolvedReference; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE} // Return root element in list.
Function ResolveReferences : Boolean; // Return true if all unresolveds were resolved.
end;
// Builds a list of TUnResolvedInstances, removes them from global list on free.
TBuildListVisitor = Class(TLinkedListVisitor)
List : TFPList;
Procedure Add(Item : TlinkedListItem); // Add TUnResolvedInstance item to list. Create list if needed
Destructor Destroy; override; // All elements in list (if any) are removed from the global list.
end;
// Visitor used to try and resolve instances in the global list
TResolveReferenceVisitor = Class(TBuildListVisitor)
Function Visit(Item : TLinkedListItem) : Boolean; override;
end;
// Visitor used to remove all references to a certain component.
TRemoveReferenceVisitor = Class(TBuildListVisitor)
FRef : String;
FRoot : TComponent;
Constructor Create(ARoot : TComponent;Const ARef : String);
Function Visit(Item : TLinkedListItem) : Boolean; override;
end;
// Visitor used to collect reference names.
TReferenceNamesVisitor = Class(TLinkedListVisitor)
FList : TStrings;
FRoot : TComponent;
Function Visit(Item : TLinkedListItem) : Boolean; override;
Constructor Create(ARoot : TComponent;AList : TStrings);
end;
// Visitor used to collect instance names.
TReferenceInstancesVisitor = Class(TLinkedListVisitor)
FList : TStrings;
FRef : String;
FRoot : TComponent;
Function Visit(Item : TLinkedListItem) : Boolean; override;
Constructor Create(ARoot : TComponent;Const ARef : String; AList : TStrings);
end;
// Visitor used to redirect links to another root component.
TRedirectReferenceVisitor = Class(TLinkedListVisitor)
FOld,
FNew : String;
FRoot : TComponent;
Function Visit(Item : TLinkedListItem) : Boolean; override;
Constructor Create(ARoot : TComponent;Const AOld,ANew : String);
end;
var
NeedResolving : TLinkedList;
ResolveSection : TRTLCriticalSection;
// Add an instance to the global list of instances which need resolving.
Function FindUnresolvedInstance(AInstance: TPersistent) : TUnResolvedInstance;
begin
Result:=Nil;
{$ifdef FPC_HAS_FEATURE_THREADING}
EnterCriticalSection(ResolveSection);
Try
{$endif}
If Assigned(NeedResolving) then
begin
Result:=TUnResolvedInstance(NeedResolving.Root);
While (Result<>Nil) and (Result.Instance<>AInstance) do
Result:=TUnResolvedInstance(Result.Next);
end;
{$ifdef FPC_HAS_FEATURE_THREADING}
finally
LeaveCriticalSection(ResolveSection);
end;
{$endif}
end;
Function AddtoResolveList(AInstance: TPersistent) : TUnResolvedInstance;
begin
Result:=FindUnresolvedInstance(AInstance);
If (Result=Nil) then
begin
{$ifdef FPC_HAS_FEATURE_THREADING}
EnterCriticalSection(ResolveSection);
Try
{$endif}
If not Assigned(NeedResolving) then
NeedResolving:=TLinkedList.Create(TUnResolvedInstance);
Result:=NeedResolving.Add as TUnResolvedInstance;
Result.Instance:=AInstance;
{$ifdef FPC_HAS_FEATURE_THREADING}
finally
LeaveCriticalSection(ResolveSection);
end;
{$endif}
end;
end;
// Walk through the global list of instances to be resolved.
Procedure VisitResolveList(V : TLinkedListVisitor);
begin
{$ifdef FPC_HAS_FEATURE_THREADING}
EnterCriticalSection(ResolveSection);
Try
{$endif}
try
NeedResolving.Foreach(V);
Finally
FreeAndNil(V);
end;
{$ifdef FPC_HAS_FEATURE_THREADING}
Finally
LeaveCriticalSection(ResolveSection);
end;
{$endif}
end;
procedure GlobalFixupReferences;
begin
If (NeedResolving=Nil) then
Exit;
{$ifdef FPC_HAS_FEATURE_THREADING}
GlobalNameSpace.BeginWrite;
try
{$endif}
VisitResolveList(TResolveReferenceVisitor.Create);
{$ifdef FPC_HAS_FEATURE_THREADING}
finally
GlobalNameSpace.EndWrite;
end;
{$endif}
end;
procedure GetFixupReferenceNames(Root: TComponent; Names: TStrings);
begin
If (NeedResolving=Nil) then
Exit;
VisitResolveList(TReferenceNamesVisitor.Create(Root,Names));
end;
procedure GetFixupInstanceNames(Root: TComponent; const ReferenceRootName: string; Names: TStrings);
begin
If (NeedResolving=Nil) then
Exit;
VisitResolveList(TReferenceInstancesVisitor.Create(Root,ReferenceRootName,Names));
end;
procedure RedirectFixupReferences(Root: TComponent; const OldRootName, NewRootName: string);
begin
If (NeedResolving=Nil) then
Exit;
VisitResolveList(TRedirectReferenceVisitor.Create(Root,OldRootName,NewRootName));
end;
procedure RemoveFixupReferences(Root: TComponent; const RootName: string);
begin
If (NeedResolving=Nil) then
Exit;
VisitResolveList(TRemoveReferenceVisitor.Create(Root,RootName));
end;
procedure RemoveFixups(Instance: TPersistent);
begin
// This needs work.
{
if not Assigned(GlobalFixupList) then
exit;
with GlobalFixupList.LockList do
try
for i := Count - 1 downto 0 do
begin
CurFixup := TPropFixup(Items[i]);
if (CurFixup.FInstance = Instance) then
begin
Delete(i);
CurFixup.Free;
end;
end;
finally
GlobalFixupList.UnlockList;
end;
}
end;
{ TUnresolvedReference }
Function TUnresolvedReference.Resolve(Instance : TPersistent) : Boolean;
Var
C : TComponent;
begin
C:=FindGlobalComponent(FGlobal);
Result:=(C<>Nil);
If Result then
begin
C:=FindNestedComponent(C,FRelative);
Result:=C<>Nil;
If Result then
SetObjectProp(Instance, FPropInfo,C);
end;
end;
Function TUnresolvedReference.RootMatches(ARoot : TComponent) : Boolean; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
begin
Result:=(ARoot=Nil) or (ARoot=FRoot);
end;
Function TUnResolvedReference.NextRef : TUnresolvedReference;
begin
Result:=TUnresolvedReference(Next);
end;
{ TUnResolvedInstance }
destructor TUnResolvedInstance.Destroy;
begin
FUnresolved.Free;
inherited Destroy;
end;
function TUnResolvedInstance.AddReference(ARoot: TComponent;
APropInfo: PPropInfo; AGlobal, ARelative: String): TUnresolvedReference;
begin
If (FUnResolved=Nil) then
FUnResolved:=TLinkedList.Create(TUnresolvedReference);
Result:=FUnResolved.Add as TUnresolvedReference;
Result.FGlobal:=AGLobal;
Result.FRelative:=ARelative;
Result.FPropInfo:=APropInfo;
Result.FRoot:=ARoot;
end;
Function TUnResolvedInstance.RootUnresolved : TUnresolvedReference;
begin
Result:=Nil;
If Assigned(FUnResolved) then
Result:=TUnresolvedReference(FUnResolved.Root);
end;
Function TUnResolvedInstance.ResolveReferences:Boolean;
Var
R,RN : TUnresolvedReference;
begin
R:=RootUnResolved;
While (R<>Nil) do
begin
RN:=R.NextRef;
If R.Resolve(Self.Instance) then
FUnresolved.RemoveItem(R,True);
R:=RN;
end;
Result:=RootUnResolved=Nil;
end;
{ TReferenceNamesVisitor }
Constructor TReferenceNamesVisitor.Create(ARoot : TComponent;AList : TStrings);
begin
FRoot:=ARoot;
FList:=AList;
end;
Function TReferenceNamesVisitor.Visit(Item : TLinkedListItem) : Boolean;
Var
R : TUnresolvedReference;
begin
R:=TUnResolvedInstance(Item).RootUnresolved;
While (R<>Nil) do
begin
If R.RootMatches(FRoot) then
If (FList.IndexOf(R.FGlobal)=-1) then
FList.Add(R.FGlobal);
R:=R.NextRef;
end;
Result:=True;
end;
{ TReferenceInstancesVisitor }
Constructor TReferenceInstancesVisitor.Create(ARoot : TComponent; Const ARef : String;AList : TStrings);
begin
FRoot:=ARoot;
FRef:=UpperCase(ARef);
FList:=AList;
end;
Function TReferenceInstancesVisitor.Visit(Item : TLinkedListItem) : Boolean;
Var
R : TUnresolvedReference;
begin
R:=TUnResolvedInstance(Item).RootUnresolved;
While (R<>Nil) do
begin
If (FRoot=R.FRoot) and (FRef=UpperCase(R.FGLobal)) Then
If Flist.IndexOf(R.FRelative)=-1 then
Flist.Add(R.FRelative);
R:=R.NextRef;
end;
Result:=True;
end;
{ TRedirectReferenceVisitor }
Constructor TRedirectReferenceVisitor.Create(ARoot : TComponent; Const AOld,ANew : String);
begin
FRoot:=ARoot;
FOld:=UpperCase(AOld);
FNew:=ANew;
end;
Function TRedirectReferenceVisitor.Visit(Item : TLinkedListItem) : Boolean;
Var
R : TUnresolvedReference;
begin
R:=TUnResolvedInstance(Item).RootUnresolved;
While (R<>Nil) do
begin
If R.RootMatches(FRoot) and (FOld=UpperCase(R.FGLobal)) Then
R.FGlobal:=FNew;
R:=R.NextRef;
end;
Result:=True;
end;
{ TRemoveReferenceVisitor }
Constructor TRemoveReferenceVisitor.Create(ARoot : TComponent; Const ARef : String);
begin
FRoot:=ARoot;
FRef:=UpperCase(ARef);
end;
Function TRemoveReferenceVisitor.Visit(Item : TLinkedListItem) : Boolean;
Var
I : Integer;
UI : TUnResolvedInstance;
R : TUnresolvedReference;
L : TFPList;
begin
UI:=TUnResolvedInstance(Item);
R:=UI.RootUnresolved;
L:=Nil;
Try
// Collect all matches.
While (R<>Nil) do
begin
If R.RootMatches(FRoot) and ((FRef = '') or (FRef=UpperCase(R.FGLobal))) Then
begin
If Not Assigned(L) then
L:=TFPList.Create;
L.Add(R);
end;
R:=R.NextRef;
end;
// Remove all matches.
IF Assigned(L) then
begin
For I:=0 to L.Count-1 do
UI.FUnresolved.RemoveItem(TLinkedListitem(L[i]),True);
end;
// If any references are left, leave them.
If UI.FUnResolved.Root=Nil then
begin
If List=Nil then
List:=TFPList.Create;
List.Add(UI);
end;
Finally
L.Free;
end;
Result:=True;
end;
{ TBuildListVisitor }
Procedure TBuildListVisitor.Add(Item : TlinkedListItem);
begin
If (List=Nil) then
List:=TFPList.Create;
List.Add(Item);
end;
Destructor TBuildListVisitor.Destroy;
Var
I : Integer;
begin
If Assigned(List) then
For I:=0 to List.Count-1 do
NeedResolving.RemoveItem(TLinkedListItem(List[I]),True);
FreeAndNil(List);
Inherited;
end;
{ TResolveReferenceVisitor }
Function TResolveReferenceVisitor.Visit(Item : TLinkedListItem) : Boolean;
begin
If TUnResolvedInstance(Item).ResolveReferences then
Add(Item);
Result:=True;
end;
|