comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Smart Pointers and Tagged Type Hierarchies
Date: Thu, 27 Jul 2017 22:42:28 +0200
Date: 2017-07-27T22:42:28+02:00	[thread overview]
Message-ID: <oldj7i$1sct$1@gioia.aioe.org> (raw)
In-Reply-To: 2017072721305951845-contact@flyx.org

On 2017-07-27 21:30, Felix Krause wrote:
> On 2017-07-24 19:53:54 +0000, Dmitry A. Kazakov said:
> 
>> On 2017-07-24 17:41, Felix Krause wrote:
>>
>>> Now I am wondering what others think of these approaches. Are there 
>>> alternatives? Which one would be better from a user perspective?
>>
>> I am using generic pointer (Handle), which can be instantiated later 
>> with any derived type from the base reference counting type. See 
>> Simple Components:
>>
>>     http://www.dmitry-kazakov.de/ada/components.htm#Objects_etc
>>
>> This is your second approach but with object pointer type passed as a 
>> second generic parameter.
>>
>> generic
>>     type Object_Type (<>) is abstract new Entity with private;
>>     type Object_Type_Ptr is access Object_Type'Class;
>> package Object.Handle is
>>     type Handle is new Ada.Finalization.Controlled with private;
>>
>> Users of smart pointer need not to be generic. I don't know why you 
>> think it is necessary.
> 
> Using your types, let's say I have
> 
>     type Entity_Access is access Entity'Class;
>     package Entity_Handle is new Object.Handle (Entity, Entity_Access);
> 
>     type A_Type is new Entity with private;
>     type A_Access is access A_Type'Class;
> 
>     package A_Handle is new Object.Handle (A_Type, A_Access);
> 
> Now some subroutine wants to take an Entitiy as parameter, or more 
> precisely, a Handle to an Entity. If I specify this:
> 
>     procedure Do_Something (E : Entity_Handle.Handle);
> 
> I cannot call this procedure with an A_Handle.Handle. So I'd need to do:
> 
>     generic
>        with package Actual_Handle is new Object.Handle (<>);
>     procedure Do_Something (E : Actual_Handle.Handle);
> 
> I don't see how I can prevent this using your approach.

Assuming that you are not going to derive from Entity_Handle.Handle, you 
have two options:

1. Provide a conversion

    function "-" (A : A_Handle.Handle) return Entity_Handle.Handle'Class;

Then you can Do_Something (-A).

2. Provide a wrapper. Define Do_Something on A_Handle.Handle and call 
original the Do_Something from inside.

> Another question using generics: If I do use a generic package for the 
> smart pointers, is there a good way to put the instance of that package 
> inside the package that defines the derived type?

There is no good way, but it is not critical because if you are going 
smart pointers you also should hide objects they point to. Therefore 
they must go into separate packages and there will be no public cross 
operations in the interface packages, only factories.

P.S. I don't place object types in private packages, because most likely 
it will leak this way or another. So I just object type packages public 
but mark as an implementation.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


  reply	other threads:[~2017-07-27 20:42 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-24 15:41 Smart Pointers and Tagged Type Hierarchies Felix Krause
2017-07-24 19:53 ` Dmitry A. Kazakov
2017-07-27 19:30   ` Felix Krause
2017-07-27 20:42     ` Dmitry A. Kazakov [this message]
2017-07-24 21:24 ` Chris Moore
2017-07-27 19:38   ` Felix Krause
2017-08-01  4:07     ` Randy Brukardt
2017-07-26 17:53 ` Ivan Levashev
2017-07-28  9:21 ` AdaMagica
2017-07-30 19:45   ` briot.emmanuel
2017-08-01  3:43 ` Randy Brukardt
2017-08-01  7:36   ` Dmitry A. Kazakov
2017-08-01 22:41     ` Randy Brukardt
2017-08-02  6:28       ` Dmitry A. Kazakov
2017-08-02 19:26         ` Randy Brukardt
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox