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
timesnow:
GetMem(PB, SizeOf(TBar));
InitializeArray(PB, TypeInfo(TBar), 1); // FPC_INITIALIZE is executed
just once===== more examples =====
===== more details from commit message =====
