Class TGenericStructList

Unit

Declaration

type generic TGenericStructList<T> = class(TFPSList)

Description

Generic list of types that are compared by CompareByte.

This is equivalent to TFPGList, except it doesn't override IndexOf, so your type doesn't need to have a "=" operator built-in inside FPC. When calling IndexOf or Remove, it will simply compare values using CompareByte, this is what TFPSList.IndexOf uses. This way it works to create lists of records, vectors (constant size arrays), old-style TP objects, and also is suitable to create a list of methods (since for methods, the "=" is broken, for Delphi compatibility, see http://bugs.freepascal.org/view.php?id=9228).

We also add some trivial helper methods like Add and L.

Hierarchy

  • TFPSList
  • TGenericStructList

Overview

Internal Types

Published TCompareFunc = function(const Item1, Item2: T): Integer;

Fields

Protected var FOnCompare: TCompareFunc;

Methods

Protected procedure CopyItem(Src, Dest: Pointer); override;
Protected procedure Deref(Item: Pointer); override;
Protected function Get(Index: Integer): T;
Protected function GetList: PTypeList;
Protected function ItemPtrCompare(Item1, Item2: Pointer): Integer;
Protected procedure Put(Index: Integer; const Item: T);
Public constructor Create;
Public function Add(const Item: T): Integer;
Public function Extract(const Item: T): T;
Public function First: T;
Public function IndexOf(const Item: T): Integer;
Public procedure Insert(Index: Integer; const Item: T);
Public function Last: T;
Public procedure Assign(Source: TGenericStructList);
Public function Remove(const Item: T): Integer;
Public procedure Sort(Compare: TCompareFunc);
Public function L: PT;
Public function Add: PT;
Public function Ptr(I: Integer): PT;

Properties

Public property Items[Index:Integer]: T read Get write Put;
Public property List: PTypeList read GetList;

Description

Internal Types

Published TCompareFunc = function(const Item1, Item2: T): Integer;
 

Fields

Protected var FOnCompare: TCompareFunc;
 

Methods

Protected procedure CopyItem(Src, Dest: Pointer); override;
 
Protected procedure Deref(Item: Pointer); override;
 
Protected function Get(Index: Integer): T;
 
Protected function GetList: PTypeList;
 
Protected function ItemPtrCompare(Item1, Item2: Pointer): Integer;
 
Protected procedure Put(Index: Integer; const Item: T);
 
Public constructor Create;
 
Public function Add(const Item: T): Integer;
 
Public function Extract(const Item: T): T;
 
Public function First: T;
 
Public function IndexOf(const Item: T): Integer;
 
Public procedure Insert(Index: Integer; const Item: T);
 
Public function Last: T;
 
Public procedure Assign(Source: TGenericStructList);
 
Public function Remove(const Item: T): Integer;
 
Public procedure Sort(Compare: TCompareFunc);
 
Public function L: PT;

Access the list contents directly through a pointer to T structure.

This is exactly the same pointer as List, but the type is different: this points to a single item. This is useful if you have a list of records and you would like to set their fields. This allows to use L[I] instead of Listˆ[I] (only in FPC ObjFpc mode).

See the List description for a more detailed explanation and example.

See also
List
Access the list contents directly through a pointer.
Public function Add: PT;

Increase Count and return pointer to new item. Comfortable and efficient way to add a new item that you want to immediately initialize.

Public function Ptr(I: Integer): PT;

Pointer to ith item.

Properties

Public property Items[Index:Integer]: T read Get write Put;
 
Public property List: PTypeList read GetList;

Access the list contents directly through a pointer.

This is useful if you have a list of records and you would like to set their fields. This will not work correctly:

type
  TMyRecord = record MyField: Integer; end;
  TMyRecordList = specialize TGenericStructList<TMyRecord>;
var
  MyList: TMyRecordList;
begin
  // ...
  MyList[I].MyField := 123;

(It will not work OK because you would modify only a temporary record returned by the MyList[I] getter.) Instead, setting by

MyList.Listˆ[I].MyField := 123;

will work OK. Or you can use

MyList.L[I].MyField := 123;

See also
L
Access the list contents directly through a pointer to T structure.

Generated by PasDoc 0.15.0.