[fpc-devel] Pascal Smart Pointers Idea + ARC implementation

Sven Barth pascaldragon at googlemail.com
Sat Oct 10 09:59:47 CEST 2015

Am 10.10.2015 02:41 schrieb "Maciej Izak" <hnb.code at gmail.com>:
> First step is almost done -> new record operators:
> === begin code ===
> type // Record with automatic constructor and destructor
>   TValue = record
>     class operator Initialize(var aRec: TValue);
>     class operator Finalize(var aRec: TValue);
>     class operator Copy(constref aSource: TValue; var aDest: TValue);
>   end;
> === end code ===
were created initially for fast implementation of TValue in RTTI.pas, but
they inspired me for smart pointers.

This part I definitely like, because I wanted to implement them as well. ;)
You don't need to hide these behind a modeswitch however as long as they
are optional for a record as they don't conflict with existing operators.

> The second step is some concept of low level structures for smart
pointers (in this conception we have two kinds of smart pointers - smart
pointers for objects and smart pointers for pointers):

Would you please explain why you'd need this raw structure besides for
printing the RecCounts? (honest question, maybe I'm still too tired ;) )

> For smart pointers we need to implement in some way ".", standard
inplicit opertor, "^", "@" and "@@" operator. I thought about it for a long
time. IMO minimal invasive method is "default" keyword, used as below:

This is where we reach the dangerous territory ;)
The problem with "default" is how to differentiate from a normal field
usage of the record? Keep in mind that a feature might be used in different
contexts than one first imagined. So how to ensure that one can access
field X of the record and not field X of the default?
Also for classes: is Instance a ^T as well or merely a T?

> class operator TSmartPtr<T>.Copy(constref aSource: TSmartPtr<T>; var
aDest: TSmartPtr<T>);
> begin
>   if aDest.RefCount <> nil then
>     Finalize(aDest);
>   if aSource.RefCount <> nil then
>     InterLockedIncrement(aSource.RefCount^);
>   aDest.RefCount := aSource.RefCount;
>   aDest.Instance := aSource.Instance;
> end;

I suppose Finalize here calls TSmartPtr<>.Finalize and not System.Finalize?
We don't usually have directly callable operators, so keep that in mind.

> next step is syntax sugar (oxygene compatibility):

Only behind a modeswitch or maybe even a mode Oxygene please. And take care
with prefixes, they are a PITA to handle as I've already told you ;)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20151010/1e4c4054/attachment.html>

More information about the fpc-devel mailing list