[fpc-devel] Feature announcement: Record management operators

Maciej Izak hnb.code at gmail.com
Mon Mar 7 15:13:43 CET 2016


Hi,

I'm pleased to finally announce the additional record operators:
Initialize, Finalize.

Download (r33200):
http://svn.freepascal.org/svn/fpc/branches/maciej/smart_pointers

"Record management operators aka record constructor/destructor"

They working like low level auto-executed constructor/destructor for
records.

===== motivation =====

It can be used for:

-nullable types
-very fast RTTI.TValue implementation
-as replacement for manually called Init/Finit record methods for example
like in mORMot for many types.
-auto init/finit for fields of pointer/object type

Additionally, it is base for my further work:

-smart pointers
-ARC objects
-Oxygene mode
-Delphi NextGen mode

===== example begin =====
{$MODE DELPHI}
uses SysUtils;

type
  TFoo = record
  private
    class operator Initialize(var aFoo: TFoo);
    class operator Finalize(var aFoo: TFoo);
  public
    F: Integer;
    S: string;
    P: Pointer;
    C: TObject
  end;

class operator TFoo.Initialize(var aFoo: TFoo);
begin
  F := 10;
  S := 'FPC rulezzz';
  P := nil;
  C := nil;
end;

class operator TFoo.Finalize(var aFoo: TFoo);
begin
  if S = 'Delphi rulezzz' then
    raise Exception.Create('Trololo');
  FreeMem(P);
  C.Free;
end;

procedure Foo;
var
  f: TFoo;
begin
  WriteLn(f.F); // output: 10
  WriteLn(f.S); // output: FPC rulezzz
  WriteLn(f.P = nil); // output: true
  WriteLn(f.C = nil); // output: true
end;

begin
  Foo;
end.
===== example end=====

===== more info =====

When Initialize of Finalize is used, record is treated as managed record
(similar to record containg string, interface or dynamic array).

It works correctly in all possible ways, works for:

* New
* Dispose
* Initialize
* Finalize
* InitializeArray
* FinalizeArray
* global variables
* local variables
* for fields inside: records, objects, classes

===== additional gain =====

Speed of dynamicly created instances for records increased. For now is used
always init table instead of rtti table:

type
  TBar = record
    f1,f2,f3,f4,f5,f6,f7,f8,f9: byte;
    s: string;
  end;

previously:

  GetMem(PB, SizeOf(TBar));
  InitializeArray(PB, TypeInfo(TBar), 1); // FPC_INITIALIZE was executed 10
times

now:

  GetMem(PB, SizeOf(TBar));
  InitializeArray(PB, TypeInfo(TBar), 1); // FPC_INITIALIZE is executed
just once


===== more examples =====

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/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/toperator92.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

===== more details from commit message =====

http://svn.freepascal.org/cgi-bin/viewvc.cgi/branches/maciej/smart_pointers/?view=log&sortby=date&pathrev=33200

-- 
Best regards,
Maciej Izak
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20160307/7f5e5f03/attachment.html>


More information about the fpc-devel mailing list