source: trunk/Demo/Packages/TemplateGenerics/Specialized/SpecializedList.pas

Last change on this file was 60, checked in by chronos, 12 years ago
File size: 8.7 KB
Line 
1unit SpecializedList;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils;
9
10type
11{$MACRO ON}
12
13// TListInteger<Integer, Integer>
14{$DEFINE TGListIndex := Integer}
15{$DEFINE TGListItem := Integer}
16{$DEFINE TGList := TListInteger}
17{$DEFINE INTERFACE}
18{$I 'GenericList.inc'}
19
20// TListBoolean<Integer, Boolean>
21{$DEFINE TGListIndex := Integer}
22{$DEFINE TGListItem := Boolean}
23{$DEFINE TGList := TListBoolean}
24{$DEFINE INTERFACE}
25{$I 'GenericList.inc'}
26
27// TListSmallInt<Integer, SmallInt>
28{$DEFINE TGListIndex := Integer}
29{$DEFINE TGListItem := SmallInt}
30{$DEFINE TGList := TListSmallInt}
31{$DEFINE INTERFACE}
32{$I 'GenericList.inc'}
33
34// TListDouble<Integer, Double>
35{$DEFINE TGListIndex := Integer}
36{$DEFINE TGListItem := Double}
37{$DEFINE TGList := TListDouble}
38{$DEFINE INTERFACE}
39{$INCLUDE '..\Generic\GenericList.inc'}
40
41// TListPointer<Integer, Pointer>
42{$DEFINE TGListIndex := Integer}
43{$DEFINE TGListItem := Pointer}
44{$DEFINE TGList := TListPointer}
45{$DEFINE INTERFACE}
46{$I 'GenericList.inc'}
47
48// TListString<Integer, string>
49{$DEFINE TGListStringIndex := Integer}
50{$DEFINE TGListStringItem := string}
51{$DEFINE TGListString := TListString}
52{$DEFINE INTERFACE}
53{$I 'GenericListString.inc'}
54
55// TListByte<Integer, Byte>
56{$DEFINE TGListIndex := Integer}
57{$DEFINE TGListItem := Byte}
58{$DEFINE TGList := TListByteBase}
59{$DEFINE INTERFACE}
60{$I 'GenericList.inc'}
61
62TListByte = class(TListByteBase)
63 procedure WriteToStream(Stream: TStream);
64 procedure WriteToStreamPart(Stream: TStream; ItemIndex, ItemCount: TGListIndex);
65 procedure ReplaceStream(Stream: TStream);
66 procedure ReplaceStreamPart(Stream: TStream; ItemIndex, ItemCount: TGListIndex);
67 procedure AddStream(Stream: TStream);
68 procedure AddStreamPart(Stream: TStream; ItemCount: TGListIndex);
69end;
70
71// TListChar<Integer, Char>
72{$DEFINE TGListIndex := Integer}
73{$DEFINE TGListItem := Char}
74{$DEFINE TGList := TListCharBase}
75{$DEFINE INTERFACE}
76{$I 'GenericList.inc'}
77
78// TListObject<Integer, TObject>
79{$DEFINE TGListObjectIndex := Integer}
80{$DEFINE TGListObjectItem := TObject}
81{$DEFINE TGListObjectList := TListObjectList}
82{$DEFINE TGListObject := TListObject}
83{$DEFINE INTERFACE}
84{$I 'GenericListObject.inc'}
85
86
87{ TListChar }
88
89// TListByte<Integer, Char>
90TListChar = class(TListCharBase)
91 procedure UpperCase;
92 procedure LowerCase;
93 procedure Trim;
94 procedure TrimLeft;
95 procedure TrimRight;
96end;
97
98// TListMethodBase<Integer, TMethod>
99{$DEFINE TGListIndex := Integer}
100{$DEFINE TGListItem := TMethod}
101{$DEFINE TGList := TListMethodBase}
102{$DEFINE INTERFACE}
103{$I 'GenericList.inc'}
104
105// TListMethod<Integer, TMethod>
106TListMethod = class(TListMethodBase)
107 procedure CallAll;
108end;
109
110// TListNotifyEventBase<Integer, TNotifyEvent>
111{$DEFINE TGListIndex := Integer}
112{$DEFINE TGListItem := TNotifyEvent}
113{$DEFINE TGList := TListNotifyEventBase}
114{$DEFINE INTERFACE}
115{$I 'GenericList.inc'}
116
117// TListNotifyEvent<Integer, TNotifyEvent>
118TListNotifyEvent = class(TListNotifyEventBase)
119 procedure CallAll(Sender: TObject);
120end;
121
122
123TBaseEvent = procedure of object;
124
125// TListSimpleEventBase<Integer, TBaseEvent>
126{$DEFINE TGListIndex := Integer}
127{$DEFINE TGListItem := TBaseEvent}
128{$DEFINE TGList := TListSimpleEventBase}
129{$DEFINE INTERFACE}
130{$I 'GenericList.inc'}
131
132// TListSimpleEvent<Integer, TSimpleEvent>
133TListSimpleEvent = class(TListSimpleEventBase)
134 procedure CallAll;
135end;
136
137
138// TFileListByte<Integer, Byte>
139{$DEFINE TGFileListIndex := Integer}
140{$DEFINE TGFileListItem := Byte}
141{$DEFINE TGFileListList := TFileListListByte}
142{$DEFINE TGFileList := TFileListByte}
143{$DEFINE INTERFACE}
144{$I 'GenericFileList.inc'}
145
146function StrToStr(Value: string): string;
147
148
149
150
151
152implementation
153
154{$DEFINE IMPLEMENTATION_USES}
155{$I 'GenericList.inc'}
156
157// TListInteger<Integer, Integer>
158{$DEFINE TGListIndex := Integer}
159{$DEFINE TGListItem := Integer}
160{$DEFINE TGList := TListInteger}
161{$DEFINE IMPLEMENTATION}
162{$I 'GenericList.inc'}
163
164// TListSmallInt<Integer, SmallInt>
165{$DEFINE TGListIndex := Integer}
166{$DEFINE TGListItem := SmallInt}
167{$DEFINE TGList := TListSmallInt}
168{$DEFINE IMPLEMENTATION}
169{$I 'GenericList.inc'}
170
171// TListBoolean<Integer, Boolean>
172{$DEFINE TGListIndex := Integer}
173{$DEFINE TGListItem := Boolean}
174{$DEFINE TGList := TListBoolean}
175{$DEFINE IMPLEMENTATION}
176{$I 'GenericList.inc'}
177
178// TListDouble<Integer, Double>
179{$DEFINE TGListIndex := Integer}
180{$DEFINE TGListItem := Double}
181{$DEFINE TGList := TListDouble}
182{$DEFINE IMPLEMENTATION}
183{$I 'GenericList.inc'}
184
185// TListPointer<Integer, Pointer>
186{$DEFINE TGListIndex := Integer}
187{$DEFINE TGListItem := Pointer}
188{$DEFINE TGList := TListPointer}
189{$DEFINE IMPLEMENTATION}
190{$I 'GenericList.inc'}
191
192// TListString<Integer, string>
193{$DEFINE TGListStringIndex := Integer}
194{$DEFINE TGListStringItem := string}
195{$DEFINE TGListString := TListString}
196{$DEFINE IMPLEMENTATION}
197{$I 'GenericListString.inc'}
198
199// TListByte<Integer, Byte>
200{$DEFINE TGListIndex := Integer}
201{$DEFINE TGListItem := Byte}
202{$DEFINE TGList := TListByteBase}
203{$DEFINE IMPLEMENTATION}
204{$I 'GenericList.inc'}
205
206// TListByte<Integer, Char>
207{$DEFINE TGListIndex := Integer}
208{$DEFINE TGListItem := Char}
209{$DEFINE TGList := TListCharBase}
210{$DEFINE IMPLEMENTATION}
211{$I 'GenericList.inc'}
212
213// TListObject<Integer, TObject>
214{$DEFINE TGListObjectIndex := Integer}
215{$DEFINE TGListObjectItem := TObject}
216{$DEFINE TGListObjectList := TListObjectList}
217{$DEFINE TGListObject := TListObject}
218{$DEFINE IMPLEMENTATION}
219{$I 'GenericListObject.inc'}
220
221// TListMethod<Integer, TMethod>
222{$DEFINE TGListIndex := Integer}
223{$DEFINE TGListItem := TMethod}
224{$DEFINE TGList := TListMethodBase}
225{$DEFINE IMPLEMENTATION}
226{$I 'GenericList.inc'}
227
228// TListNotifyEventBase<Integer, TNotifyEvent>
229{$DEFINE TGListIndex := Integer}
230{$DEFINE TGListItem := TNotifyEvent}
231{$DEFINE TGList := TListNotifyEventBase}
232{$DEFINE IMPLEMENTATION}
233{$I 'GenericList.inc'}
234
235// TListSimpleEventBase<Integer, TBaseEvent>
236{$DEFINE TGListIndex := Integer}
237{$DEFINE TGListItem := TBaseEvent}
238{$DEFINE TGList := TListSimpleEventBase}
239{$DEFINE IMPLEMENTATION}
240{$I 'GenericList.inc'}
241
242// TFileListByte<Integer, Byte>
243{$DEFINE TGFileListIndex := Integer}
244{$DEFINE TGFileListItem := Byte}
245{$DEFINE TGFileListList := TFileListListByte}
246{$DEFINE TGFileList := TFileListByte}
247{$DEFINE IMPLEMENTATION}
248{$I 'GenericFileList.inc'}
249
250
251function StrToStr(Value: string): string;
252begin
253 Result := Value;
254end;
255
256{ TListSimpleEvent }
257
258procedure TListSimpleEvent.CallAll;
259var
260 I: TGListIndex;
261begin
262 I := 0;
263 while (I < Count) do begin
264 TBaseEvent(Items[I])();
265 I := I + 1;
266 end;
267end;
268
269
270{ TListChar }
271
272procedure TListChar.UpperCase;
273var
274 I: TGListIndex;
275begin
276 for I := 0 to Count - 1 do
277 if (FItems[I] in ['a'..'z']) then
278 FItems[I] := Char(Byte(FItems[I]) - 32);
279end;
280
281procedure TListChar.LowerCase;
282var
283 I: TGListIndex;
284begin
285 for I := 0 to Count - 1 do
286 if (FItems[I] in ['A'..'Z']) then
287 FItems[I] := Char(Byte(FItems[I]) + 32);
288end;
289
290procedure TListChar.Trim;
291begin
292 TrimLeft;
293 TrimRight;
294end;
295
296procedure TListChar.TrimLeft;
297var
298 I: TGListIndex;
299begin
300 I := 0;
301 while (I < Count) and (FItems[I] = ' ') do
302 I := I + 1;
303 if I < Count then
304 DeleteItems(0, I);
305end;
306
307procedure TListChar.TrimRight;
308var
309 I: TGListIndex;
310begin
311 I := Count - 1;
312 while (I >= 0) and (FItems[I] = ' ') do
313 I := I - 1;
314 if I >= 0 then
315 DeleteItems(I + 1, Count - I - 1);
316end;
317
318procedure TListMethod.CallAll;
319var
320 I: TGListIndex;
321begin
322 I := 0;
323 while (I < Count) do begin
324 Items[I];
325 I := I + 1;
326 end;
327end;
328
329procedure TListNotifyEvent.CallAll(Sender: TObject);
330var
331 I: TGListIndex;
332begin
333 I := Count - 1;
334 while (I >= 0) do begin
335 TNotifyEvent(Items[I])(Sender);
336 I := I - 1;
337 end;
338end;
339
340{ TListByte }
341
342procedure TListByte.WriteToStream(Stream: TStream);
343var
344 I: Integer;
345begin
346 Stream.Position := 0;
347 I := 0;
348 while I < Count do begin
349 Stream.WriteByte(Items[I]);
350 I := I + 1;
351 end;
352end;
353
354procedure TListByte.WriteToStreamPart(Stream: TStream; ItemIndex, ItemCount: Integer);
355var
356 I: Integer;
357begin
358 I := ItemIndex;
359 while I < ItemCount do begin
360 Stream.WriteByte(Items[I]);
361 I := I + 1;
362 end;
363end;
364
365procedure TListByte.ReplaceStream(Stream: TStream);
366var
367 I: Integer;
368begin
369 Stream.Position := 0;
370 I := 0;
371 while I < Count do begin
372 Items[I] := Stream.ReadByte;
373 I := I + 1;
374 end;
375end;
376
377procedure TListByte.ReplaceStreamPart(Stream: TStream; ItemIndex,
378 ItemCount: Integer);
379var
380 I: Integer;
381begin
382 I := ItemIndex;
383 while I < ItemCount do begin
384 Items[I] := Stream.ReadByte;
385 I := I + 1;
386 end;
387end;
388
389procedure TListByte.AddStream(Stream: TStream);
390var
391 I: Integer;
392begin
393 Stream.Position := 0;
394 I := Count;
395 Count := Count + Stream.Size;
396 while I < Count do begin
397 Items[I] := Stream.ReadByte;
398 I := I + 1;
399 end;
400end;
401
402procedure TListByte.AddStreamPart(Stream: TStream; ItemCount: Integer);
403var
404 I: Integer;
405begin
406 I := Count;
407 Count := Count + ItemCount;
408 while I < Count do begin
409 Items[I] := Stream.ReadByte;
410 I := I + 1;
411 end;
412end;
413
414
415end.
Note: See TracBrowser for help on using the repository browser.