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
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Value.Path;
interface
uses
Boolean,
Collection.List,
Condition,
Event,
Integer._32,
Object_.ReferenceCounted,
Query,
Value;
type
TValuePath = class(TReferenceCountedObject)
protected
Values: TList<TValue>;
function GetLength(): TInteger32;
function GetNodes(Index: TInteger32): TValue;
function GetReversedNodes(Index: TInteger32): TValue;
function GetLast(): TValue;
function ExtractObject(Object_: TObject): TObject;
public
constructor Create(const Elements: array of TValue); reintroduce; virtual;
destructor Destroy(); override;
function HandleEvent(Event: TEvent): TBoolean; override;
function Extend(const Elements: array of TValue): TValuePath;
function GetAncestor(Count: TInteger32): TValuePath;
function Find(Condition: TCondition<TObject>): TObject;
function Contains(Object_: TObject): TBoolean;
function Query(Query: TQuery; FreeQuery: TBoolean = True): TObject;
function ExtractLastObject(): TObject;
property Length: TInteger32 read GetLength;
property Nodes[Index: TInteger32]: TValue read GetNodes;
property ReversedNodes[Index: TInteger32]: TValue read GetReversedNodes;
property Last: TValue read GetLast;
end;
implementation
uses
Object_.Base,
Object_.List,
Object_.Model,
Object_.Reference,
Object_.Reference.Counter;
{ TValuePath }
function TValuePath.Contains(Object_: TObject): TBoolean;
var
i: TInteger32;
begin
Result := False;
i := 0;
while not Result and (i < Length) do
begin
Result := ExtractObject(ReversedNodes[i]) = Object_;
Inc(i);
end;
end;
constructor TValuePath.Create(const Elements: array of TValue);
var
Element: TValue;
begin
inherited Create();
Values := TObjectList<TValue>.Create();
Values.ElementManager := TReferenceCounter<TValue>.Create();
for Element in Elements do
Values.Add(Element);
end;
function TValuePath.Extend(const Elements: array of TValue): TValuePath;
begin
Result := TValuePath.Create([]);
Result.Values.AddAll(Values);
Result.Values.AddAll(Elements);
end;
destructor TValuePath.Destroy();
begin
Values.Free();
inherited Destroy();
end;
function TValuePath.ExtractLastObject(): TObject;
begin
Result := ExtractObject(Last);
end;
function TValuePath.ExtractObject(Object_: TObject): TObject;
begin
if Object_ is TObjectReference then
Result := ExtractObject((Object_ as TObjectReference).Value)
else if Object_ is TObjectMember then
Result := ExtractObject((Object_ as TObjectMember).Value.Value)
else
Result := Object_;
end;
function TValuePath.Find(Condition: TCondition<TObject>): TObject;
var
i: TInteger32;
Object_: TObject;
begin
Condition.AddReference();
try
Result := nil;
i := 0;
while (Result = nil) and (i < Length) do
begin
Object_ := ExtractObject(ReversedNodes[i]);
if Condition.Matches(Object_) then
Result := Object_;
Inc(i);
end;
finally
Condition.ReleaseReference();
end;
end;
function TValuePath.GetLength(): TInteger32;
begin
Result := Values.Count;
end;
function TValuePath.GetAncestor(Count: TInteger32): TValuePath;
var
Index: TInteger32;
begin
Result := TValuePath.Create([]);
for Index := 0 to Values.Count - Count do
Result.Values.Add(Values[Index]);
end;
function TValuePath.GetLast(): TValue;
begin
Result := Values.Last;
end;
function TValuePath.GetNodes(Index: TInteger32): TValue;
begin
Result := Values[Index];
end;
function TValuePath.GetReversedNodes(Index: TInteger32): TValue;
begin
Result := Values[Length - 1 - Index];
end;
function TValuePath.HandleEvent(Event: TEvent): TBoolean;
var
i: TInteger32;
Object_: TObject;
begin
Result := False;
i := 0;
while (not Result) and (i < Length) do
begin
Object_ := ExtractObject(ReversedNodes[i]);
if Object_ is TBaseObject then
Result := (Object_ as TBaseObject).HandleEvent(Event);
Inc(i);
end;
end;
function TValuePath.Query(Query: TQuery; FreeQuery: TBoolean = True): TObject;
var
i: TInteger32;
Object_: TObject;
begin
Result := nil;
i := 0;
while (Result = nil) and (i < Length) do
begin
Object_ := ExtractObject(ReversedNodes[i]);
if Object_ is TBaseObject then
Result := (Object_ as TBaseObject).Query(Query);
Inc(i);
end;
if FreeQuery then
Query.Free();
end;
end.
|