TCollection: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: TCollection
| Old | New | Line | |
|---|---|---|---|
| 1 | + | A **TCollection** is a base class for (unordered) collections of [[TCollectionItem|TCollectionItems]]. | |
| 2 | + | ||
| 3 | + | To use a TCollection, a derived TCollectionItem has to be created | |
| 4 | + | ||
| 5 | + | ## Inheritance | |
| 6 | + | ||
| 7 | + | - [[TObject]] - Base class of all classes.; [[IFPObserved]] - Interface implemented by an object that can be observed. | |
| 8 | + | - [[TPersistent]] - Base class for streaming system and persistent properties. | |
| 9 | + | - **TCollection** - Base class to manage collections of named objects. | |
| 10 | + | ||
| 11 | + | ## Simple example | |
| 12 | + | ||
| 13 | + | ``` pascal | |
| 14 | + | unit uhair; | |
| 15 | + | ||
| 16 | + | {$mode objfpc}{$H+} | |
| 17 | + | ||
| 18 | + | interface | |
| 19 | + | ||
| 20 | + | uses | |
| 21 | + | Classes, SysUtils, Dialogs; | |
| 22 | + | ||
| 23 | + | type | |
| 24 | + | ||
| 25 | + | { THairItem } | |
| 26 | + | ||
| 27 | + | THairItem = class(TCollectionItem) | |
| 28 | + | private | |
| 29 | + | FLength: integer; | |
| 30 | + | public | |
| 31 | + | constructor Create(ACollection: TCollection); override; | |
| 32 | + | published | |
| 33 | + | property Length: integer read FLength write FLength; | |
| 34 | + | end; | |
| 35 | + | ||
| 36 | + | { THairList } | |
| 37 | + | ||
| 38 | + | THairList = class(TCollection) | |
| 39 | + | private | |
| 40 | + | function GetItems(Index: integer): THairItem; | |
| 41 | + | procedure SetItems(Index: integer; AValue: THairItem); | |
| 42 | + | public | |
| 43 | + | constructor Create; | |
| 44 | + | public | |
| 45 | + | function Add: THairItem; | |
| 46 | + | function AddEx(length: integer): THairItem; | |
| 47 | + | property Items[Index: integer]: THairItem read GetItems write SetItems; default; | |
| 48 | + | end; | |
| 49 | + | ||
| 50 | + | var | |
| 51 | + | hairs: THairList; | |
| 52 | + | ||
| 53 | + | implementation | |
| 54 | + | ||
| 55 | + | { THairItem } | |
| 56 | + | ||
| 57 | + | constructor THairItem.Create(ACollection: TCollection); | |
| 58 | + | begin | |
| 59 | + | if Assigned(ACollection) {and (ACollection is THairList)} then | |
| 60 | + | inherited Create(ACollection); | |
| 61 | + | end; | |
| 62 | + | ||
| 63 | + | { THairList } | |
| 64 | + | ||
| 65 | + | function THairList.GetItems(Index: integer): THairItem; | |
| 66 | + | begin | |
| 67 | + | Result := THairItem(inherited Items[Index]); | |
| 68 | + | end; | |
| 69 | + | ||
| 70 | + | procedure THairList.SetItems(Index: integer; AValue: THairItem); | |
| 71 | + | begin | |
| 72 | + | Items[Index].Assign(AValue); | |
| 73 | + | end; | |
| 74 | + | ||
| 75 | + | constructor THairList.Create; | |
| 76 | + | begin | |
| 77 | + | inherited Create(THairItem); | |
| 78 | + | end; | |
| 79 | + | ||
| 80 | + | function THairList.Add: THairItem; | |
| 81 | + | begin | |
| 82 | + | Result := inherited Add as THairItem; | |
| 83 | + | end; | |
| 84 | + | ||
| 85 | + | function THairList.AddEx(length: integer): THairItem; | |
| 86 | + | begin | |
| 87 | + | Result := inherited Add as THairItem; | |
| 88 | + | Result.Length := length; | |
| 89 | + | end; | |
| 90 | + | ``` | |
| 91 | + | ||
| 92 | + | Example of use: | |
| 93 | + | ||
| 94 | + | ``` pascal | |
| 95 | + | initialization | |
| 96 | + | hairs := THairList.Create; | |
| 97 | + | hairs.AddEx(10); | |
| 98 | + | hairs.Add.Length := 100; | |
| 99 | + | hairs.Delete(0); | |
| 100 | + | ||
| 101 | + | ShowMessage(IntToStr(hairs.Count)); | |
| 102 | + | ||
| 103 | + | finalization | |
| 104 | + | hairs.Free; | |
| 105 | + | ||
| 106 | + | end. | |
| 107 | + | ``` | |
| 108 | + | ||
| 109 | + | ## Streaming | |
| 110 | + | ||
| 111 | + | This adds a TComponent class that can stream the list loading and saving to text files with LResources. See [[Streaming components]]. | |
| 112 | + | ||
| 113 | + | ``` pascal | |
| 114 | + | unit uhair; | |
| 115 | + | ||
| 116 | + | {$mode objfpc}{$H+} | |
| 117 | + | ||
| 118 | + | interface | |
| 119 | + | ||
| 120 | + | uses | |
| 121 | + | Classes, Graphics, SysUtils, LResources, Dialogs; | |
| 122 | + | ||
| 123 | + | type | |
| 124 | + | ||
| 125 | + | { THairItem } | |
| 126 | + | ||
| 127 | + | THairItem = class(TCollectionItem) | |
| 128 | + | private | |
| 129 | + | FLength: integer; | |
| 130 | + | FColor: TColor; | |
| 131 | + | public | |
| 132 | + | constructor Create(ACollection: TCollection); override; | |
| 133 | + | published | |
| 134 | + | property Length: integer read FLength write FLength; | |
| 135 | + | property Color: TColor read FColor write FColor; | |
| 136 | + | end; | |
| 137 | + | ||
| 138 | + | { THairList } | |
| 139 | + | ||
| 140 | + | THairList = class(TCollection) | |
| 141 | + | private | |
| 142 | + | function GetItems(Index: integer): THairItem; | |
| 143 | + | procedure SetItems(Index: integer; AValue: THairItem); | |
| 144 | + | public | |
| 145 | + | constructor Create; | |
| 146 | + | public | |
| 147 | + | function Add: THairItem; | |
| 148 | + | function AddEx(length: integer): THairItem; | |
| 149 | + | property Items[Index: integer]: THairItem read GetItems write SetItems; default; | |
| 150 | + | end; | |
| 151 | + | ||
| 152 | + | { THairComponent } | |
| 153 | + | ||
| 154 | + | THairComponent = class(TComponent) | |
| 155 | + | private | |
| 156 | + | FHairList: THairList; | |
| 157 | + | public | |
| 158 | + | constructor Create(AOwner: TComponent); override; | |
| 159 | + | destructor Destroy; override; | |
| 160 | + | public | |
| 161 | + | procedure SaveToFile(AFileName: string); | |
| 162 | + | procedure LoadFromFile(AFileName: string); | |
| 163 | + | procedure OnFindClass(Reader: TReader; const AClassName: string; | |
| 164 | + | var ComponentClass: TComponentClass); | |
| 165 | + | published | |
| 166 | + | property HairList: THairList read FHairList write FHairList; | |
| 167 | + | end; | |
| 168 | + | ||
| 169 | + | var | |
| 170 | + | hairs: THairComponent; | |
| 171 | + | ||
| 172 | + | implementation | |
| 173 | + | ||
| 174 | + | { THairComponent } | |
| 175 | + | ||
| 176 | + | constructor THairComponent.Create(AOwner: TComponent); | |
| 177 | + | begin | |
| 178 | + | inherited Create(AOwner); | |
| 179 | + | Self.SetSubComponent(True); | |
| 180 | + | // add this line if you want to put this class inside other and also be streamed | |
| 181 | + | HairList := THairList.Create; | |
| 182 | + | end; | |
| 183 | + | ||
| 184 | + | destructor THairComponent.Destroy; | |
| 185 | + | begin | |
| 186 | + | HairList.Free; | |
| 187 | + | inherited Destroy; | |
| 188 | + | end; | |
| 189 | + | ||
| 190 | + | procedure THairComponent.SaveToFile(AFileName: string); | |
| 191 | + | var | |
| 192 | + | AStream: TMemoryStream; | |
| 193 | + | begin | |
| 194 | + | AStream := TMemoryStream.Create; | |
| 195 | + | try | |
| 196 | + | WriteComponentAsTextToStream(AStream, Self); | |
| 197 | + | AStream.SaveToFile(AFileName); | |
| 198 | + | finally | |
| 199 | + | AStream.Free; | |
| 200 | + | end; | |
| 201 | + | end; | |
| 202 | + | ||
| 203 | + | procedure THairComponent.LoadFromFile(AFileName: string); | |
| 204 | + | var | |
| 205 | + | AStream: TMemoryStream; | |
| 206 | + | begin | |
| 207 | + | AStream := TMemoryStream.Create; | |
| 208 | + | try | |
| 209 | + | AStream.LoadFromFile(AFileName); | |
| 210 | + | ReadComponentFromTextStream(AStream, TComponent(Self), @OnFindClass); | |
| 211 | + | finally | |
| 212 | + | AStream.Free; | |
| 213 | + | end; | |
| 214 | + | end; | |
| 215 | + | ||
| 216 | + | procedure THairComponent.OnFindClass(Reader: TReader; const AClassName: string; | |
| 217 | + | var ComponentClass: TComponentClass); | |
| 218 | + | begin | |
| 219 | + | if CompareText(AClassName, 'THairComponent') = 0 then | |
| 220 | + | ComponentClass := THairComponent; | |
| 221 | + | end; | |
| 222 | + | ||
| 223 | + | { THairItem } | |
| 224 | + | ||
| 225 | + | constructor THairItem.Create(ACollection: TCollection); | |
| 226 | + | begin | |
| 227 | + | if Assigned(ACollection) and (ACollection is THairList) then | |
| 228 | + | inherited Create(ACollection); | |
| 229 | + | end; | |
| 230 | + | ||
| 231 | + | { THairList } | |
| 232 | + | ||
| 233 | + | function THairList.GetItems(Index: integer): THairItem; | |
| 234 | + | begin | |
| 235 | + | Result := THairItem(inherited Items[Index]); | |
| 236 | + | end; | |
| 237 | + | ||
| 238 | + | procedure THairList.SetItems(Index: integer; AValue: THairItem); | |
| 239 | + | begin | |
| 240 | + | Items[Index].Assign(AValue); | |
| 241 | + | end; | |
| 242 | + | ||
| 243 | + | constructor THairList.Create; | |
| 244 | + | begin | |
| 245 | + | inherited Create(THairItem); | |
| 246 | + | end; | |
| 247 | + | ||
| 248 | + | function THairList.Add: THairItem; | |
| 249 | + | begin | |
| 250 | + | Result := inherited Add as THairItem; | |
| 251 | + | end; | |
| 252 | + | ||
| 253 | + | function THairList.AddEx(length: integer): THairItem; | |
| 254 | + | begin | |
| 255 | + | Result := inherited Add as THairItem; | |
| 256 | + | Result.Length := length; | |
| 257 | + | end; | |
| 258 | + | ``` | |
| 259 | + | ||
| 260 | + | Example of use: | |
| 261 | + | ||
| 262 | + | ``` pascal | |
| 263 | + | initialization | |
| 264 | + | hairs := THairComponent.Create(nil); | |
| 265 | + | ||
| 266 | + | hairs.HairList.AddEx(10); | |
| 267 | + | hairs.HairList.AddEx(20); | |
| 268 | + | hairs.HairList.AddEx(30); | |
| 269 | + | hairs.HairList[0].Color := clRed; | |
| 270 | + | ||
| 271 | + | hairs.SaveToFile('test.txt'); | |
| 272 | + | ||
| 273 | + | hairs.HairList.Delete(2); | |
| 274 | + | hairs.HairList.Delete(1); | |
| 275 | + | hairs.HairList.Delete(0); | |
| 276 | + | ||
| 277 | + | hairs.LoadFromFile('test.txt'); | |
| 278 | + | ||
| 279 | + | ShowMessage(IntToStr(hairs.HairList[0].Length)); | |
| 280 | + | ||
| 281 | + | finalization | |
| 282 | + | hairs.Free; | |
| 283 | + | ||
| 284 | + | end. | |
| 285 | + | ``` | |
| 286 | + | ||
| 287 | + | Output text file: | |
| 288 | + | ||
| 289 | + | ``` pascal | |
| 290 | + | object THairComponent | |
| 291 | + | HairList = < | |
| 292 | + | item | |
| 293 | + | Length = 10 | |
| 294 | + | Color = clRed | |
| 295 | + | end | |
| 296 | + | item | |
| 297 | + | Length = 20 | |
| 298 | + | Color = clBlack | |
| 299 | + | end | |
| 300 | + | item | |
| 301 | + | Length = 30 | |
| 302 | + | Color = clBlack | |
| 303 | + | end> | |
| 304 | + | end | |
| 305 | + | ``` | |
| 306 | + | ||
| 307 | + | ## Generics | |
| 308 | + | ||
| 309 | + | This is a ready-to-use collection [[Generics|generic class]]. It implements the most elemental structure. Save time overriding the same methods each time you want to create a collection quickly. It does it for you. | |
| 310 | + | ||
| 311 | + | ``` pascal | |
| 312 | + | unit ugenericcollection; | |
| 313 | + | ||
| 314 | + | {$mode objfpc}{$H+} | |
| 315 | + | ||
| 316 | + | interface | |
| 317 | + | ||
| 318 | + | uses | |
| 319 | + | Classes, SysUtils; | |
| 320 | + | ||
| 321 | + | type | |
| 322 | + | ||
| 323 | + | { TGenericCollection } | |
| 324 | + | ||
| 325 | + | generic TGenericCollection<T> = class(TCollection) | |
| 326 | + | private | |
| 327 | + | function GetItems(Index: integer): T; | |
| 328 | + | procedure SetItems(Index: integer; AValue: T); | |
| 329 | + | public | |
| 330 | + | constructor Create; | |
| 331 | + | public | |
| 332 | + | function Add: T; | |
| 333 | + | public | |
| 334 | + | property Items[Index: integer]: T read GetItems write SetItems; default; | |
| 335 | + | end; | |
| 336 | + | ||
| 337 | + | implementation | |
| 338 | + | ||
| 339 | + | { TGenericCollection } | |
| 340 | + | ||
| 341 | + | function TGenericCollection.GetItems(Index: integer): T; | |
| 342 | + | begin | |
| 343 | + | Result := T(inherited Items[Index]); | |
| 344 | + | end; | |
| 345 | + | ||
| 346 | + | procedure TGenericCollection.SetItems(Index: integer; AValue: T); | |
| 347 | + | begin | |
| 348 | + | Items[Index].Assign(AValue); | |
| 349 | + | end; | |
| 350 | + | ||
| 351 | + | constructor TGenericCollection.Create; | |
| 352 | + | begin | |
| 353 | + | inherited Create(T); | |
| 354 | + | end; | |
| 355 | + | ||
| 356 | + | function TGenericCollection.Add: T; | |
| 357 | + | begin | |
| 358 | + | Result := T(inherited Add); | |
| 359 | + | end; | |
| 360 | + | ||
| 361 | + | end. | |
| 362 | + | ``` | |
| 363 | + | ||
| 364 | + | Example of use: | |
| 365 | + | ||
| 366 | + | ``` pascal | |
| 367 | + | unit usaving; | |
| 368 | + | ||
| 369 | + | {$mode objfpc}{$H+} | |
| 370 | + | ||
| 371 | + | interface | |
| 372 | + | ||
| 373 | + | uses | |
| 374 | + | Classes, SysUtils, LResources, ugenericcollection; | |
| 375 | + | ||
| 376 | + | type | |
| 377 | + | ||
| 378 | + | { TBookItem } | |
| 379 | + | ||
| 380 | + | TBookItem = class(TCollectionItem) | |
| 381 | + | private | |
| 382 | + | FCode: string; | |
| 383 | + | FTitle: string; | |
| 384 | + | published | |
| 385 | + | property Code: string read FCode write FCode; | |
| 386 | + | property Title: string read FTitle write FTitle; | |
| 387 | + | end; | |
| 388 | + | ||
| 389 | + | { TPeopleItem } | |
| 390 | + | ||
| 391 | + | TPeopleItem = class(TCollectionItem) | |
| 392 | + | private | |
| 393 | + | FName: string; | |
| 394 | + | FChoice: string; | |
| 395 | + | published | |
| 396 | + | property Name: string read FName write FName; | |
| 397 | + | property Choice: string read FChoice write FChoice; | |
| 398 | + | end; | |
| 399 | + | ||
| 400 | + | { TBookCollection } | |
| 401 | + | ||
| 402 | + | TCustomBookCollection = specialize TGenericCollection<TBookItem>; | |
| 403 | + | ||
| 404 | + | TBookCollection = class(TCustomBookCollection) | |
| 405 | + | public | |
| 406 | + | function AddEx(aCode, aTitle: string): TBookItem; | |
| 407 | + | end; | |
| 408 | + | ||
| 409 | + | { TPeopleCollection } | |
| 410 | + | ||
| 411 | + | TCustomPeopleCollection = specialize TGenericCollection<TPeopleItem>; | |
| 412 | + | ||
| 413 | + | TPeopleCollection = class(TCustomPeopleCollection) | |
| 414 | + | public | |
| 415 | + | function AddEx(aName, aChoice: string): TPeopleItem; | |
| 416 | + | end; | |
| 417 | + | ||
| 418 | + | implementation | |
| 419 | + | ||
| 420 | + | { TBookCollection } | |
| 421 | + | ||
| 422 | + | function TBookCollection.AddEx(aCode, aTitle: string): TBookItem; | |
| 423 | + | begin | |
| 424 | + | Result := inherited Add as TBookItem; | |
| 425 | + | Result.Code := aCode; | |
| 426 | + | Result.Title := aTitle; | |
| 427 | + | end; | |
| 428 | + | ||
| 429 | + | { TPeopleCollection } | |
| 430 | + | ||
| 431 | + | function TPeopleCollection.AddEx(aName, aChoice: string): TPeopleItem; | |
| 432 | + | begin | |
| 433 | + | Result := inherited Add as TPeopleItem; | |
| 434 | + | Result.Name := aName; | |
| 435 | + | Result.Choice := aChoice; | |
| 436 | + | end; | |
| 437 | + | ||
| 438 | + | end. | |
| 439 | + | ``` | |
| 440 | + | ||
| 441 | + | ## See also | |
| 442 | + | ||
| 443 | + | - [[doc:rtl/classes/tcollection.html|TCollection doc]] | |
| 444 | + | ||
| 445 | + | --- | |
| 446 | + | *From the Free Pascal wiki, page "TCollection", as it stood on 2020-02-29 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
