Recent

Author Topic: TFPGObjectlist allocator added?  (Read 366 times)

jamie

  • Hero Member
  • *****
  • Posts: 7707
TFPGObjectlist allocator added?
« on: April 23, 2026, 01:07:03 pm »
As it states, wouid be nice to set an optional default constructor in the Create constructor.

This wsy using the COUNT property it would call the defsult constructor if set.
As it is now you can specify if it should free the objects and that works if you reduce the count.
Jamie

The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 19165
  • Glad to be alive.
Re: TFPGObjectlist allocator added?
« Reply #1 on: April 23, 2026, 01:25:35 pm »
Not via the constructor, but you can set the initial capacity, which is the same?
Inherits from TFpsList and can speed up your code a lot.

https://www.freepascal.org/docs-html/rtl/fgl/tfpslist.capacity.html
« Last Edit: April 23, 2026, 01:37:21 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

jamie

  • Hero Member
  • *****
  • Posts: 7707
Re: TFPGObjectlist allocator added?
« Reply #2 on: April 24, 2026, 12:55:27 am »
Something like this is what I was referring to.

Of course one could fully expand this to a true Allocator Template.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, fgl;
  9.  
  10. type
  11.  
  12.   { TFPGObjectList }
  13.  
  14.   { TFPGObjectListAlloc }
  15.  
  16.   Generic TFPGObjectListAlloc<T: TObject> = class(specialize TFPGObjectList<T>)
  17.   private
  18.     FAllocateObjects: boolean;
  19.     procedure SetCount(NewCount: integer);
  20.   public
  21.     constructor Create(aFreeObjects: boolean = True; AllocateObjects: boolean = False); virtual;
  22.     property Count: integer read fCount write SetCount;
  23.   end;
  24.  
  25. type
  26.  
  27.   { TTestClass }
  28.  
  29.   TTestClass = class
  30.     F: TDateTime;
  31.     constructor Create; virtual;
  32.   end;
  33.  
  34.   { TForm1 }
  35.  
  36.   TForm1 = class(TForm)
  37.     Button1: TButton;
  38.     procedure Button1Click(Sender: TObject);
  39.   private
  40.  
  41.   public
  42.  
  43.   end;
  44.  
  45. var
  46.   Form1: TForm1;
  47.  
  48. implementation
  49.  
  50. {$R *.lfm}
  51.  
  52. { TFPGObjectList }
  53.  
  54. procedure TFPGObjectListAlloc.SetCount(NewCount: integer);
  55. var
  56.   OldCount, I: integer;
  57. begin
  58.   OldCount := inherited Count;
  59.   inherited Count := NewCount;
  60.   if fAllocateObjects then
  61.     for I := OldCount to inherited Count - 1 do
  62.       Items[I] := T.Create;
  63. end;
  64.  
  65. constructor TFPGObjectListAlloc.Create(aFreeObjects: boolean = True;
  66.   AllocateObjects: boolean = False);
  67. begin
  68.   inherited Create(aFreeObjects);
  69.   FAllocateObjects := AllocateObjects;
  70. end;
  71.  
  72. { TTestClass }
  73.  
  74. constructor TTestClass.Create;
  75. begin
  76.   inherited Create;
  77.   F := Now;
  78. end;
  79.  
  80. { TForm1 }
  81.  
  82. procedure TForm1.Button1Click(Sender: TObject);
  83. type
  84.   TMyAutoObjects = specialize TFPGObjectListAlloc<TTestClass>;
  85. var
  86.   Test: TmyAutoObjects;
  87. begin
  88.   Test := TMyAutoObjects.Create(True, True); {Set Owner For free and AutoCreate, Etc}
  89.   Test.Count := 10; {Creates 10 items}
  90.   Test.Count := 5;  {Removes 5 items and frees them}
  91.   Test.Count := 20; {Creates another 15 ontop of whats there}
  92.   Caption := DateTimeTostr(Test.Items[19].F);
  93.   Test.Free;  {Check HeapTrc , should be no leaks.}
  94. end;
  95.  
  96. end.
  97.  

I suppose this could be implemented for List and Maps too.

Jamie
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 19165
  • Glad to be alive.
Re: TFPGObjectlist allocator added?
« Reply #3 on: April 24, 2026, 08:18:43 am »
Like this?
Code: Pascal  [Select][+][-]
  1. // overloaded constructor
  2. {$ifdef fpc}{$mode delphi}{$endif}
  3. uses fgl;
  4.  
  5. type
  6.    TMyObjectList<T:class> = class(TFPGObjectList<T>)
  7.    public
  8.      Constructor Create(Caps:Integer;FreeObj:Boolean = true;CreateThem:Boolean = True);
  9.    end;
  10.    
  11.   Constructor TMyObjectList<T>.Create(Caps:Integer;
  12.                                        FreeObj:Boolean = true;
  13.                                        CreateThem:Boolean = True);
  14.    var i:integer;
  15.    begin
  16.      Inherited create(FreeObj);
  17.      self.capacity:= Caps;
  18.      if CreateThem and (caps > 0) then
  19.        For i := 0 to Pred(Capacity) do
  20.          Add(T.Create);
  21.    end;
  22.  
  23. var
  24.   Demo:TMyObjectList<TObject>;  
  25.   I:Integer;
  26. begin
  27.   // Object list with capacity 10, Auto Free'd and all object created
  28.   Demo := TMyObjectList<TObject>.Create(10);
  29.   for i := 0 to pred(demo.count) do writeln(Assigned(Demo.Items[i]));
  30.   Demo.Free;  
  31. end.
This example and yours have one disadvantage: this only works if the objects use only the parameterless constructor. (So no TFilestream for example)
But your example does the same.
« Last Edit: April 24, 2026, 09:06:47 am by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

 

TinyPortal © 2005-2018