[fpc-announce] Feature announcement: Management Operators
Maciej Izak
hnb.code at gmail.com
Tue Feb 28 12:20:05 CET 2017
Hi,
I'm pleased to finally announce (again but now officially ;) ) the
additional record operators: Initialize, Finalize, AddRef and Copy.
Available in latest FPC trunk (r35485):
1. operator Initialize - is called after memory allocation for record (called
*after* FPC internal call recordrtti(data,typeinfo, at int_initialize);)
2. operator Finalize - is called when record goes out of scope (called
*before* FPC internal call recordrtti(data,typeinfo, at int_finalize);)
3. operator AddRef - is called after the contents of a record has been
duplicated by copying the contents byte by byte (called *after* FPC
internal call recordrtti(data,typeinfo, at int_addref); )
4. operator Copy - Copy operator (if exists) is called *instead of* default
copy behavior. Operator is responsible for copying everything that's needed
from the source to the target.
AddRef and Copy might be at beginning a little hard for understanding but
we have good explanation provided by Thorsten Engler in topic "Management
operators : Copy and Clone confusion..." :
=== quote begin ===
the reason for both to exist is that there are cases (e.g.
passing an interface parameter) where the callee is responsible for any
lifetime management implications (and the caller might not even be
implemented using fpc). The caller just passes the interface pointer on the
stack (or in a register), which, while it creates a copy of the bytes, does
not actually invoke the copy operator. In cases where reference counting is
required (parameter not marked as const), the callee may decide to simply
use the value that was passed on the stack as the backing for the parameter,
so it calls AddRef on that, without performing another copy.
Given this context, I find the terms AddRef and Copy to be perfectly
adequate, even when applied to types that aren't strictly speaking
performing reference counting; e.g. IIRC, AddRef for a BSTR (ole string type
for windows which does NOT have a reference count) actually involves making
a copy of the string contents and replacing the BSTR pointer.
AddRef here means "this is now an *additional* *reference* to whatever these
bytes might refer to".
=== quote end ===
my 2 cents for above: AddRef exist to speed up things.
New set of operators is unique and is called "management operators". That
is because:
A. each of record (even non managed or even empty) with management operator
becomes managed type.
B. is possible to implement new custom types (also thanks to other
operators) with own management of memory (new string types, fast TValue
implementations without hacks on RTL etc.).
C. "management operators" have no result.
D. For management operators is generated simple VMT (thanks to this is
possible to work with management operators together with all RTL functions
like InitializeArray/FinalizeArray).
=== example declaration begin ===
PFoo = ^TFoo;
TFoo = record
private
class operator Initialize(var a: TFoo);
class operator Finalize(var a: TFoo);
class operator AddRef(var a: TFoo);
class operator Copy(constref aSrc: TFoo; var aDst: TFoo);
end;
=== example declaration end ===
Management Operators feature can be used for many things:
* support for value types
* nullable types
* some custom ARC variations
* speed up existing types
* very fast RTTI.TValue implementation
* as replacement for manually called Init/Done record methods like in
mORMot for many types (for example SynCommons.TSynLocker).
* auto init/finit for pointers/classes/simple types or whatever we have in
Pascal
It works correctly in all possible ways with RTL:
* New (Initialize)
* Dispose (Finalize)
* Initialize (Initialize)
* Finalize (Finalize)
* InitializeArray (Initialize)
* FinalizeArray (Finalize)
* SetLength (Initialize/Finalize)
* Copy (AddRef)
* RTTI.IsManaged
Managements operators are often called implicitly in many cases. Few
examples:
* global variables (Initialize/Finalize)
* local variables (Initialize/Finalize)
* for fields inside: records, objects, classes (Initialize/Finalize)
* variable assignment (Copy)
* for parameters for routines (AddRef/Finalize/none - depends on modifiers
like var/constref/cons)
Own experiments recommended! :)
Complex example and test:
http://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/tests/tes
t/tmoperator8.pp?view=co&content-type=text%2Fplain
Note: Lazarus (trunk version) has support for all new operators syntax
(thanks for Mattias Gaertner).
Future directions and opened doors (where management operators can be used
but not directly):
+ "default" field for better handling smart pointers / nullable types
(maybe will be introduced new type called proxy record for handling
"default" modifier) (eg. https://github.com/maciej-izak/PascalSmartPointers
compilable with NewPascal)
+ New VMT entry "vmtManagedInit" for "Initialize operator" for record
fields inside classes (optimization)
+ Base for Nullable types syntax sugar
+ Probably base in some way for ARC classes (DELPHINEXTGEN compiler mode)
+ More Oxygene compatibility
+ Aspects
Note for myself: document the feature on the wiki in New Features Trunk and
the changes to the Rtti in User Changes Trunk :)
--
Best regards,
Maciej Izak
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-announce/attachments/20170228/ee6a6d70/attachment.html>
More information about the fpc-announce
mailing list