Recent

Author Topic: how to add a ForEach call to TFPGList  (Read 402 times)

mas steindorff

  • Hero Member
  • *****
  • Posts: 601
how to add a ForEach call to TFPGList
« on: June 10, 2026, 02:03:45 am »
I see the TFPList has a .ForEachCall() procedure
is it hard to add this to a TFPGList?

I'm looking for a cleaner way to loop though each item I've defined as a class and call it's .ClearHand() procedure without a for loop.
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

cdbc

  • Hero Member
  • *****
  • Posts: 2850
    • http://www.cdbc.dk
Re: how to add a ForEach call to TFPGList
« Reply #1 on: June 10, 2026, 02:19:44 am »
Hi
  1)
Code: Pascal  [Select][+][-]
  1. TListCallback = procedure(data,arg:pointer) of object;  // TFPList
1.1)
Code: Pascal  [Select][+][-]
  1. TGListCallback = procedure(data,arg:<T>) of object;  // TFPGList
 

  2)
Code: Pascal  [Select][+][-]
  1. procedure ForEachCall(proc2call:TListCallback;arg:pointer);
2.1)
Code: Pascal  [Select][+][-]
  1. procedure ForEachCall(proc2call:TGListCallback;arg:<T>);
  3)
Code: Pascal  [Select][+][-]
  1. procedure TFPGList.ForEachCall(proc2call:TGListCallback;arg:<T>);
  2. var
  3.   i : integer;
  4.   p : T;
  5. begin
  6.   For I:=0 To Count-1 Do
  7.     begin
  8.       p:=FList^[i];
  9.       if assigned(pointer(p)) then
  10.         proc2call(p,arg);
  11.     end;
  12. end;
Maybe something like the above :: It is untested code, just my first stab at it... Perhaps you can implement it in a descendant class...
Regards Benny
« Last Edit: June 10, 2026, 02:22:04 am by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

jamie

  • Hero Member
  • *****
  • Posts: 7823
Re: how to add a ForEach call to TFPGList
« Reply #2 on: June 10, 2026, 12:42:04 pm »
There is already a enumerator in that generics, just encase it with a inline function.
Jamie
The only true wisdom is knowing you know nothing

mas steindorff

  • Hero Member
  • *****
  • Posts: 601
Re: how to add a ForEach call to TFPGList
« Reply #3 on: June 11, 2026, 07:12:07 pm »
Thank you Benny,
I'll give the code you posted a try

Jamie, I'm afraid your post is over my head so I've been looking around at your other post for more info. I also looked into the header of TFPGList but did not find any ForEach... calls to "inline"??
can you point me to a code example you are recommending I try?
 

Thank you both for your post. This project goal is for me to learn and both post are firing the brain cells.
Mas
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

cdbc

  • Hero Member
  • *****
  • Posts: 2850
    • http://www.cdbc.dk
Re: how to add a ForEach call to TFPGList
« Reply #4 on: June 11, 2026, 07:47:10 pm »
Hi Mas
I think jamie means something like this:
Code: Pascal  [Select][+][-]
  1. generic procedure FPGList_ForEachCall(aList: TFPGList;
  2.   proc2call:TGListCallback;
  3.   arg:<T>); inline;
  4. var
  5.   i : integer;
  6.   p : T;
  7. begin
  8.   For I:=0 To aList.Count-1 Do
  9.     begin
  10.       p:=aList^[i];
  11.       if assigned(pointer(p)) then
  12.         proc2call(p,arg);
  13.     end;
  14. end;
I could ofc. be mistaken...
Regards Benny
« Last Edit: June 11, 2026, 07:48:41 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

jamie

  • Hero Member
  • *****
  • Posts: 7823
Re: how to add a ForEach call to TFPGList
« Reply #5 on: June 11, 2026, 11:12:22 pm »
I'll try to make this simple for you ::)

Code: Pascal  [Select][+][-]
  1.  
  2. var
  3.  YourClass:TMyCalssThatIsStoredInMyTFPGLIST;
  4. Begin
  5. For yourClass In MyInstanceOfTheTFPLIST do
  6.   YourClass.ClearHand;
  7. End;
  8.  

 That will loop through all the classes you have stored in the TFPGLIST without using indexes etc.

You can incapsulate that within a Procedure to make it easier for you.

Code: Pascal  [Select][+][-]
  1. Procedure ClearAllHands;
  2. /// Encapsulate above code here;
  3. End;
  4.  

basically, the compiler will create a Enumerator class in the background and free it when the FOR LOOP is completed.

This Enumerator will return a class instance starting from the first one to the last one.



Jamie
The only true wisdom is knowing you know nothing

mas steindorff

  • Hero Member
  • *****
  • Posts: 601
Re: how to add a ForEach call to TFPGList
« Reply #6 on: June 11, 2026, 11:42:24 pm »
thank you, now I understand.
I would have called this a pascal "set" or use of "in" approach.  I just need to update my vocabulary.
Mas
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

 

TinyPortal © 2005-2018