Access the list contents through a pointer, to get or set them efficiently.
Use this to directly access a list value, like this:
MyIntList.L[I] := 123;
MyIntList.L[I] := MyIntList.L[I] + 123;
MyRecordList.L[I].MyField := 123;
The above examples of L work only in FPC ObjFpc mode or in Delphi with pointermath "on" (see https://docwiki.embarcadero.com/RADStudio/Sydney/en/Pointer_Math_(Delphi) ). These are CGE default settings when being compiled with FPC or Delphi, so you can depend on them at least in CGE implementation. Note that Delphi applications just using L don't actually have pointermath "on", it is enough that this unit is compiled with pointermath "on" and then one can use math on PtrT.
This is in particular useful if you have a list of records and you would like to set their fields. E.g. this is incorrect (and should not even compile if MyField is really a simple field):
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.
In contrast, this will work OK:
MyList.L[I].MyField := 123;
Using L is preferred over this List, because it avoids "fake infinite list" type TTypeList, that in particular on Delphi may have too small size.
See also
- List
- Access the list contents through a pointer to an array of items.
|