<div dir="ltr"><div>Hi,</div><div><br></div><div>I'm pleased to finally announce the additional record operators: Initialize, Finalize.</div><div><br></div><div>Download (r33200): <a href="http://svn.freepascal.org/svn/fpc/branches/maciej/smart_pointers">http://svn.freepascal.org/svn/fpc/branches/maciej/smart_pointers</a></div><div><br></div><div>"Record management operators aka record constructor/destructor"</div><div><br></div><div>They working like low level auto-executed constructor/destructor for records.</div><div><br></div><div>===== motivation =====</div><div><br></div><div>It can be used for:</div><div><br></div><div>-nullable types</div><div>-very fast RTTI.TValue implementation</div><div>-as replacement for manually called Init/Finit record methods for example like in mORMot for many types.</div><div>-auto init/finit for fields of pointer/object type</div><div><br></div><div>Additionally, it is base for my further work:</div><div><br></div><div>-smart pointers</div><div>-ARC objects</div><div>-Oxygene mode</div><div>-Delphi NextGen mode</div><div><br></div><div>===== example begin =====</div><div>{$MODE DELPHI}</div><div>uses SysUtils;</div><div><br></div><div>type</div><div>  TFoo = record</div><div>  private</div><div>    class operator Initialize(var aFoo: TFoo);</div><div>    class operator Finalize(var aFoo: TFoo);</div><div>  public</div><div>    F: Integer;</div><div>    S: string;</div><div>    P: Pointer;</div><div>    C: TObject</div><div>  end;</div><div><br></div><div>class operator TFoo.Initialize(var aFoo: TFoo);</div><div>begin</div><div>  F := 10;</div><div>  S := 'FPC rulezzz';</div><div>  P := nil;</div><div>  C := nil;</div><div>end;</div><div><br></div><div>class operator TFoo.Finalize(var aFoo: TFoo);</div><div>begin</div><div>  if S = 'Delphi rulezzz' then</div><div>    raise Exception.Create('Trololo');</div><div>  FreeMem(P);</div><div>  C.Free;</div><div>end;</div><div><br></div><div>procedure Foo;</div><div>var</div><div>  f: TFoo;</div><div>begin</div><div>  WriteLn(f.F); // output: 10</div><div>  WriteLn(f.S); // output: FPC rulezzz</div><div>  WriteLn(f.P = nil); // output: true</div><div>  WriteLn(f.C = nil); // output: true</div><div>end;</div><div><br></div><div>begin</div><div>  Foo;</div><div>end.</div><div>===== example end=====</div><div><br></div><div>===== more info =====</div><div><br></div><div>When Initialize of Finalize is used, record is treated as managed record (similar to record containg string, interface or dynamic array).</div><div><br></div><div>It works correctly in all possible ways, works for: </div><div><br></div><div>* New</div><div>* Dispose</div><div>* Initialize</div><div>* Finalize</div><div>* InitializeArray</div><div>* FinalizeArray</div><div>* global variables</div><div>* local variables</div><div>* for fields inside: records, objects, classes</div><div><br></div><div>===== additional gain =====</div><div><br></div><div>Speed of dynamicly created instances for records increased. For now is used always init table instead of rtti table:</div><div><br></div><div>type</div><div>  TBar = record</div><div>    f1,f2,f3,f4,f5,f6,f7,f8,f9: byte;</div><div>    s: string;</div><div>  end;</div><div><br></div><div>previously:</div><div><br></div><div>  GetMem(PB, SizeOf(TBar));</div><div>  InitializeArray(PB, TypeInfo(TBar), 1); // FPC_INITIALIZE was executed 10 times</div><div><br></div><div>now:</div><div><br></div><div>  GetMem(PB, SizeOf(TBar));</div><div>  InitializeArray(PB, TypeInfo(TBar), 1); // FPC_INITIALIZE is executed just once</div><div> </div><div><br></div><div>===== more examples =====</div><div><br></div><div><a href="http://svn.freepascal.org/cgi-bin/viewvc.cgi/branches/maciej/smart_pointers/tests/test/toperator90.pp?view=co&revision=33200&content-type=text%2Fplain&pathrev=33200">http://svn.freepascal.org/cgi-bin/viewvc.cgi/branches/maciej/smart_pointers/tests/test/toperator90.pp?view=co&revision=33200&content-type=text%2Fplain&pathrev=33200</a><br></div><div><br></div><div><a href="http://svn.freepascal.org/cgi-bin/viewvc.cgi/branches/maciej/smart_pointers/tests/test/toperator91.pp?view=co&revision=33200&content-type=text%2Fplain&pathrev=33200">http://svn.freepascal.org/cgi-bin/viewvc.cgi/branches/maciej/smart_pointers/tests/test/toperator91.pp?view=co&revision=33200&content-type=text%2Fplain&pathrev=33200</a><br></div><div><br></div><div><a href="http://svn.freepascal.org/cgi-bin/viewvc.cgi/branches/maciej/smart_pointers/tests/test/toperator92.pp?view=co&content-type=text%2Fplain">http://svn.freepascal.org/cgi-bin/viewvc.cgi/branches/maciej/smart_pointers/tests/test/toperator92.pp?view=co&content-type=text%2Fplain</a><br></div><div><br></div><div><a href="http://svn.freepascal.org/cgi-bin/viewvc.cgi/branches/maciej/smart_pointers/tests/test/toperator93.pp?view=co&content-type=text%2Fplain">http://svn.freepascal.org/cgi-bin/viewvc.cgi/branches/maciej/smart_pointers/tests/test/toperator93.pp?view=co&content-type=text%2Fplain</a><br></div><div><br></div><div>===== more details from commit message =====</div><div><br></div><div><a href="http://svn.freepascal.org/cgi-bin/viewvc.cgi/branches/maciej/smart_pointers/?view=log&sortby=date&pathrev=33200">http://svn.freepascal.org/cgi-bin/viewvc.cgi/branches/maciej/smart_pointers/?view=log&sortby=date&pathrev=33200</a><br></div><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div>Best regards,<br>Maciej Izak</div></div></div>
</div>