#1 2016-07-26 17:59:17

hnb
Member
Registered: 2015-06-15
Posts: 246

"Default" discussion for SmartPointers etc

already posted in fpc-devel:

Finally I have a working implementation (not published yet) of Smart Pointers/ARC objects and Nullable (Nilable?) types. I think is worth to discuss a little about new "default modifier" (which is strictly related to mentioned structures). If needed I can correct details.

As you know (or not) all is based on new modifier - "default" for fields. Overriding operators like "." or "^" is complicated and not obvious in Pascal world.

The "default" modifier is inspired by "default" modifier for indexed properties. All of my work is (I hope so) natural for Pascal language. All FPC tests pass fine (phew!). The idea is quiet simple:

"If all fails - try to use default field".

Example implementation of SmartPointers and SmartObjects available at:

https://github.com/maciej-izak/PascalSmartPointers (see OUTPUT on the end of each of file)

Tests (very good way to see how it works. NOTE: I need to add few other tests for functions with var/const/out, "for in do" loop and for arrays and indexed properties - help with additional tests is welcome):

https://github.com/maciej-izak/PascalSm … ster/tests

The way how to obtain pointer can be a little confusing for most of Pascal programmers. Anyway nothing new for Pascal language. In Pascal we have little known @@ operator to get pointer to variable which handle pointer to procedure/function. For records with "default field" @ means "get pointer to default field" and @@ means "get pointer to record".

Any questions and suggestions are welcome!

Side note: as side effect of new feature we can implement very compatible ARC objects compatible with Delphi without breaking existing code base and without ugly additions for TObject like:

    function __ObjAddRef: Integer; virtual;
    function __ObjRelease: Integer; virtual;
    property RefCount: Integer read FRefCount;
    procedure DisposeOf; // that can become also part of TObject

All can be delegated into record (record with default field works like proxy, note: each of method/field/property/operator of record has higher priority than "default field object"). We can omit easily TComponent problem for ARC compiler like mentioned here (maybe we can add some hidden field for TObject similar to hfMonitorOffset from Delphi to point proxy record for destroy notification - maybe as part of TMonitor):

https://plus.google.com/+DalijaPrasnika … CUnrZam6zp (see comments)

more info about DisposeOf:

https://plus.google.com/+HoracioJoseCav … hp7wbHX2hM (see comments)


best regards,
Maciej Izak

Offline

#2 2016-07-26 21:42:07

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 9,268
Website

Re: "Default" discussion for SmartPointers etc

Why use a pointer and slow memory allocations for the refcount?

Offline

#3 2016-07-27 06:09:59

hnb
Member
Registered: 2015-06-15
Posts: 246

Re: "Default" discussion for SmartPointers etc

The main point is to show how "default" is working so SmartPtr implementation is not tuned (yet). Any help is welcome.


best regards,
Maciej Izak

Offline

#4 2016-07-27 09:56:36

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 9,268
Website

Re: "Default" discussion for SmartPointers etc

Where does this @@ feature comes from?
I never heard of this before.
And I was not able to find any documentation about this as part of the "standard" pascal, nor its support in TurboPascal/Delphi/FPC.

Offline

#5 2016-07-27 10:50:42

hnb
Member
Registered: 2015-06-15
Posts: 246

Re: "Default" discussion for SmartPointers etc

It probably exist since TurboPascal...
@@ is mentioned in Delphi documentation:

http://docwiki.embarcadero.com/RADStudi … ural_Types

It is even mentioned in book "Delphi in a Nutshell. A Desktop Quick Reference".


best regards,
Maciej Izak

Offline

#6 2016-07-27 10:57:46

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 9,268
Website

Re: "Default" discussion for SmartPointers etc

Here @@ is used in a totally diverse context.
And it is still a reference to another reference, so could make sense in the context of procedural types.
AFAIR FPC uses a diverse approach of implementing references to procedural types...
So IMHO this is not something standardized.

What you propose for @@ is something new, and the use of it to access the "default property" or the "record instance" is a bit difficult to understand, in pascalish words.

Offline

#7 2016-07-27 12:03:25

hnb
Member
Registered: 2015-06-15
Posts: 246

Re: "Default" discussion for SmartPointers etc

I think the context is not totally diverse, but you are right, FPC uses a diverse approach. If you have better idea please share here or on fpc-devel mailing list. The topic of SmartObj/SmartPtr is complex. @@ seems to be best option to put all together without damages for language and other parts (like Implicit/Explicit operators). For example "typecasts" approach proposed by Sven will not work at all, "typecasts" has serious consequences for Implicit/Explicit operators for "default property".


best regards,
Maciej Izak

Offline

#8 2016-07-28 06:17:08

hnb
Member
Registered: 2015-06-15
Posts: 246

Re: "Default" discussion for SmartPointers etc

To get rid of @@/@@@ we can use typecast + new type kind. With "proxy" type, Sven proposition has more sense:

{$MODE DELPHI}

type
  TRawSomeSmart<T> = record
  private
    Instance: T;
    ... // normal record
  end;

  TSomeSmart<T> = proxy to TRawSomeSmart<T>.Instance;

var
  ptr: pointer;
  // PTypeInfo(TypeInfo(ni)).Kind = tkProxy
  ni: TSomeSmart<Integer>;
  np: TSomeSmart<TProcedure>;
begin
  ptr := @ni; // pointer to ni.Instance
  ptr := @TSomeSmart<Integer>(ni); // pointer to ni

  ptr := @@np; // pointer to np.Instance
  ptr := @np   // pointer to procedure
  ptr := @TSomeSmart<TProcedure>(np); // pointer to np

end;

best regards,
Maciej Izak

Offline

Board footer

Powered by FluxBB