Management operators and classes? lazarus / 62-General ---------------------------------------------------------------------- soerensen3 · 2018-05-28 16:46 Hi, is something like this also possible for classes? http://wiki.freepascal.org/management_operators I hope it to simplify my managed pointer type which works but is way to complicated to use. I only know of interfaces but I never understood the way reference counting works with them. It seems to be quite complicated: https://www.freepascal.org/docs-html/ref/refse92.html ---------------------------------------------------------------------- Thaddy · 2018-05-28 20:06 In trunk this is fully supported and both Maciej (the author, a.k.a HNB) and me already gave some powerful examples on this forum. It still needs an extra pseudo reference, though.. ---------------------------------------------------------------------- hnb · 2018-05-28 20:19 you can try this library : https://github.com/maciej- izak/PascalSmartPointers especially this example should be interesting for you : https://github.com/maciej- izak/PascalSmartPointers/blob/master/examples/SmartObj01.pas note: code with "default field" will work only with http://newpascal.org/ not with FPC stable nor FPC trunk. ---------------------------------------------------------------------- soerensen3 · 2018-05-28 22:24 Thank you for your answers. @Thaddy: I couldn't find it somehow, all I could find was the code with the records. I was wondering if these operators also work on classes directly but probably not. This way I could get rid of the pseudo reference. With pseudo reference you mean the record, right? @hnb: That's the repo that I was looking for. Too bad with the default field but I think it's still useful, because assigning should still work. For now I would like to keep my code compatible but I will definitely give NewPascal a try. ---------------------------------------------------------------------- hnb · 2018-05-28 22:37 you can't use management operators in classes, this is possible to implement but may be not worth (I am still considering few scenarios). maybe someday close cooperation with FPC core team will be possible again (I have hope so), but for now too many bad emotions around, so rather I wouldn't expect any of my new feature (nor any activity) in FPC trunk in near future. ---------------------------------------------------------------------- Thaddy · 2018-05-29 07:03 Quote from: hnb on May 29, 2018, 12:37:51 amyou can't use management operators in classes, this is possible to implement but may be not worth (I am still considering few scenarios). maybe someday close cooperation with FPC core team will be possible again (I have hope so), but for now too many bad emotions around, so rather I wouldn't expect any of my new feature (nor any activity) in FPC trunk in near future. You can use management operators in classes provided the class has a locally typed and declared record! Because they also work for local records defined inside classes(including the type).  Is that by accident? You implemented it.... ---------------------------------------------------------------------- hnb · 2018-05-29 07:17 Quote from: Thaddy on May 29, 2018, 09:03:27 amYou can use management operators in classes provided the class has a locally typed and declared record! Because they also work for local records defined inside classes(including the type).  Is that by accident? You implemented it.... I mean here direct usage like : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TThaddy = class  private    class operator Initialize(var a: TThaddy);    ...  end;  it has not much sense for now ---------------------------------------------------------------------- Thaddy · 2018-05-29 07:29 Naah, that won't work (probably:yet) indeed, but my scenario happens to work and you can do funny things with it. Great to hear you seem optimistic about the future.... ---------------------------------------------------------------------- soerensen3 · 2018-05-29 08:58 Quote from: Thaddy on May 29, 2018, 09:03:27 amQuote from: hnb on May 29, 2018, 12:37:51 amyou can't use management operators in classes, this is possible to implement but may be not worth (I am still considering few scenarios). maybe someday close cooperation with FPC core team will be possible again (I have hope so), but for now too many bad emotions around, so rather I wouldn't expect any of my new feature (nor any activity) in FPC trunk in near future. You can use management operators in classes provided the class has a locally typed and declared record! Because they also work for local records defined inside classes(including the type).  Is that by accident? You implemented it.... Thanks! That's pretty smart I will try that when I have time later. I understand the locally declared part, but why locally typed? Because of the generic? I only have one class (and descendants) where I want this behaviour so I don't really need a generic for the record. ---------------------------------------------------------------------- Thaddy · 2018-05-29 09:22 Quote from: soerensen3 on May 29, 2018, 10:58:20 am but why locally typed? Because of the generic? I only have one class (and descendants) where I want this behaviour so I don't really need a generic for the record. It will probably work with a global type too, but my play garden used a class local type. Note that Maciej wrote it is not implemented for classes. I merely tested a work-around. ---------------------------------------------------------------------- soerensen3 · 2018-05-29 11:20 Ah damn thinking about it again though it works using a nested record it doesn't solve my problem!  If I copy the class pointer it does not make a new instance of the record. modified SmartObj.pas Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {    This file is part of the Free Pascal run time library.    Copyright (c) 1999-2014 by Maciej Izak aka hnb (NewPascal project)    See the file COPYING.FPC, included in this distribution,    for details about the copyright.    This program is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *********** ***********************************************************}  unit SmartObj; {$MODE DELPHI} interface uses  SysUtils, Classes; type   { TSmartObj }   TSmartObj = class    private type TContainer = record    Instance: TSmartObj;      RefCount: PLongint;       procedure SmartFinalize();       class operator Initialize(var aRec: TContainer);      class operator Finalize(var aRec: TContainer);      class operator AddRef(var aRec: TContainer);      class operator Copy(constref aSource: TContainer; var aDest: TContainer);       class operator Implicit(aObj: TSmartObj): TContainer;      procedure Assign(const aValue: TSmartObj);    end;     private      Container: TContainer;     public      constructor Create;      destructor Destroy; override;  end; implementation { TSmartObj } constructor TSmartObj.Create;begin  inherited Create;  Container.Instance:= Self;end; destructor TSmartObj.Destroy;begin  Container.Instance:= nil;  inherited Destroy;end; procedure TSmartObj.TContainer.SmartFinalize();begin  WriteLn( 'SmartFinalize' );  if RefCount <> nil then    if InterLockedDecrement(RefCount^)=0 then    begin      FreeMem(RefCount);      Instance.Free;    end;end; class operator TSmartObj.TContainer.Initialize(var aRec: TContainer);begin  aRec.RefCount := nil;  WriteLn( 'Initialize' );end; class operator TSmartObj.TContainer.Finalize(var aRec: TContainer);begin  aRec.SmartFinalize();end; class operator TSmartObj.TContainer.AddRef(var aRec: TContainer);begin  WriteLn( 'AddRef' );  if aRec.RefCount <> nil then    InterLockedIncrement(aRec.RefCount^);end; class operator TSmartObj.TContainer.Copy(constref aSource: TContainer;  var aDest: TContainer);begin  WriteLn( 'Copy' );  if aDest.RefCount <> nil then  aDest.SmartFinalize();  if aSource.RefCount <> nil then    InterLockedIncrement(aSource.RefCount^);  aDest.RefCount := aSource.RefCount;  aDest.Instance := aSource.Instance;end; class operator TSmartObj.TContainer.Implicit(aObj: TSmartObj): TContainer;begin  Result.Assign(aObj);end; procedure TSmartObj.TContainer.Assign(const aValue: TSmartObj);begin  WriteLn( 'Assign' );  if RefCount <> nil then    SmartFinalize();   GetMem(RefCount, SizeOf(LongInt));  RefCount^ := 0;   InterLockedIncrement(RefCount^);  Instance := aValue;end; end.Testproject: project1.lpr Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1; uses sysutils, SmartObj; var  Test: TSmartObj;  Test2: TSmartObj; begin  Test:= TSmartObj.Create;  Test2:= Test; // copy/addref is not called because only the pointer of TSmartObj is copied  //Test:= nil; // no smart finalize here  FreeAndNil( Test );  WriteLn( HexStr( @Test2 )); // Should be nil if it was working but it's notend.  So it's only working with the default field if you do not want the user to be dealing with the record. I hope this feature moves to fpc. ---------------------------------------------------------------------- ASerge · 2018-05-29 18:10 Quote from: soerensen3 on May 28, 2018, 06:46:06 pmI only know of interfaces but I never understood the way reference counting works with them. It seems to be quite complicated: https://www.freepascal.org/docs-html/ref/refse92.html In ref example, the call y.DoTest(8) in the Test3 procedure is a very unpredictable thing. In a real program you must never do this! It would be nice if the example also mentioned about this.