source: Generics/NativeGenerics/Specialized/SpecializedList.pas

Last change on this file was 496, checked in by chronos, 7 years ago
  • Modified: New native generics classes working under FPC 3.0 transformed from TemplateGenerics package.
File size: 3.6 KB
Line 
1unit SpecializedList;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, GenericList;
9
10type
11 TListByte = class(TGList<Byte>)
12 procedure WriteToStream(Stream: TStream);
13 procedure WriteToStreamPart(Stream: TStream; ItemIndex, ItemCount: Integer);
14 procedure ReplaceStream(Stream: TStream);
15 procedure ReplaceStreamPart(Stream: TStream; ItemIndex, ItemCount: Integer);
16 procedure AddStream(Stream: TStream);
17 procedure AddStreamPart(Stream: TStream; ItemCount: Integer);
18 end;
19
20 TListChar = class(TGList<Char>)
21 procedure UpperCase;
22 procedure LowerCase;
23 procedure Trim;
24 procedure TrimLeft;
25 procedure TrimRight;
26 end;
27
28 TListMethod = class(TGList<TMethod>)
29 procedure CallAll;
30 end;
31
32 TListNotifyEvent = class(TGList<TNotifyEvent>)
33 procedure CallAll(Sender: TObject);
34 end;
35
36 TSimpleEvent = procedure of object;
37 TListSimpleEvent = class(TGList<TSimpleEvent>)
38 procedure CallAll;
39 end;
40
41 function StrToStr(Value: string): string;
42
43
44implementation
45
46function StrToStr(Value: string): string;
47begin
48 Result := Value;
49end;
50
51{ TListSimpleEvent }
52
53procedure TListSimpleEvent.CallAll;
54var
55 I: Integer;
56begin
57 I := 0;
58 while (I < Count) do begin
59 TSimpleEvent(Items[I])();
60 I := I + 1;
61 end;
62end;
63
64
65{ TListChar }
66
67procedure TListChar.UpperCase;
68var
69 I: Integer;
70begin
71 for I := 0 to Count - 1 do
72 if (FItems[I] in ['a'..'z']) then
73 FItems[I] := Char(Byte(FItems[I]) - 32);
74end;
75
76procedure TListChar.LowerCase;
77var
78 I: Integer;
79begin
80 for I := 0 to Count - 1 do
81 if (FItems[I] in ['A'..'Z']) then
82 FItems[I] := Char(Byte(FItems[I]) + 32);
83end;
84
85procedure TListChar.Trim;
86begin
87 TrimLeft;
88 TrimRight;
89end;
90
91procedure TListChar.TrimLeft;
92var
93 I: Integer;
94begin
95 I := 0;
96 while (I < Count) and (FItems[I] = ' ') do
97 I := I + 1;
98 if I < Count then
99 DeleteItems(0, I);
100end;
101
102procedure TListChar.TrimRight;
103var
104 I: Integer;
105begin
106 I := Count - 1;
107 while (I >= 0) and (FItems[I] = ' ') do
108 I := I - 1;
109 if I >= 0 then
110 DeleteItems(I + 1, Count - I - 1);
111end;
112
113procedure TListMethod.CallAll;
114var
115 I: Integer;
116begin
117 I := 0;
118 while (I < Count) do begin
119 Items[I];
120 I := I + 1;
121 end;
122end;
123
124procedure TListNotifyEvent.CallAll(Sender: TObject);
125var
126 I: Integer;
127begin
128 I := Count - 1;
129 while (I >= 0) do begin
130 TNotifyEvent(Items[I])(Sender);
131 I := I - 1;
132 end;
133end;
134
135{ TListByte }
136
137procedure TListByte.WriteToStream(Stream: TStream);
138var
139 I: Integer;
140begin
141 Stream.Position := 0;
142 I := 0;
143 while I < Count do begin
144 Stream.WriteByte(Items[I]);
145 I := I + 1;
146 end;
147end;
148
149procedure TListByte.WriteToStreamPart(Stream: TStream; ItemIndex, ItemCount: Integer);
150var
151 I: Integer;
152begin
153 I := ItemIndex;
154 while I < ItemCount do begin
155 Stream.WriteByte(Items[I]);
156 I := I + 1;
157 end;
158end;
159
160procedure TListByte.ReplaceStream(Stream: TStream);
161var
162 I: Integer;
163begin
164 Stream.Position := 0;
165 I := 0;
166 while I < Count do begin
167 Items[I] := Stream.ReadByte;
168 I := I + 1;
169 end;
170end;
171
172procedure TListByte.ReplaceStreamPart(Stream: TStream; ItemIndex,
173 ItemCount: Integer);
174var
175 I: Integer;
176begin
177 I := ItemIndex;
178 while I < ItemCount do begin
179 Items[I] := Stream.ReadByte;
180 I := I + 1;
181 end;
182end;
183
184procedure TListByte.AddStream(Stream: TStream);
185var
186 I: Integer;
187begin
188 Stream.Position := 0;
189 I := Count;
190 Count := Count + Stream.Size;
191 while I < Count do begin
192 Items[I] := Stream.ReadByte;
193 I := I + 1;
194 end;
195end;
196
197procedure TListByte.AddStreamPart(Stream: TStream; ItemCount: Integer);
198var
199 I: Integer;
200begin
201 I := Count;
202 Count := Count + ItemCount;
203 while I < Count do begin
204 Items[I] := Stream.ReadByte;
205 I := I + 1;
206 end;
207end;
208
209end.
Note: See TracBrowser for help on using the repository browser.