comp.lang.ada
 help / color / mirror / Atom feed
From: Chris Moore <zmower@ntlworld.com>
Subject: Re: Smart Pointers and Tagged Type Hierarchies
Date: Mon, 24 Jul 2017 22:24:58 +0100
Date: 2017-07-24T22:24:58+01:00	[thread overview]
Message-ID: <JAtdB.405165$fp7.161716@fx39.am4> (raw)
In-Reply-To: <2017072417413775878-contact@flyx.org>

On 24/07/2017 16:41, Felix Krause wrote:
> With Ada's controlled types, it is possible to implement smart pointers 
> which manage heap objects with reference-counting. There is more than 
> one tutorial showing how that works.
> 
> A problem I encounter is how this can be used with type hierarchies i.e. 
> I have a smart pointer managing a tagged type, and I want to be able to 
> derive from that tagged type and still be able to use my smart pointer 
> with that new type. Let me give an example: Assume I want to implement 
> an abstract type Stream that represents a stream of events (has nothing 
> to do with Ada.Streams). I will use a slightly modified Rosen '95 name 
> scheme here for clarity: Reference is the smart pointer, Instance is the 
> actual object. Let this be the base type:
> 
<snip>
> 
> Some observations:
> 
> * Unless all implementations are child classes of Stream, it is 
> necessary to make the Instance type public.

Obv.

> * A derived type, if it wants to provide additional operations (like 
> Current_Position), must not only derive from Instance, but also from 
> Reference, to be able to provide an type-safe interface to those 
> operations.

Why?  All ops on Instance-derived types (including constructing 
subprograms) should be in terms of that type.  References are for access 
only (ho ho).

> * As types derived from Stream possibly need to derive Stream.Reference, 
> a consumer of a Stream object needs to take a Stream.Reference'Class as 
> input. This type cannot be used for a record field, so I need to 
> allocate it in heap memory and store a pointer if I want to memorize a 
> Stream.Reference value anywhere.

No.  This way lies madness.  A parallel hierarchy of References gains 
you very little and takes a lot of maintenance.

> * The implementation of Current_Position is cumbersome as I need the 
> Implementation_Access function and convert the result to 
> File_Stream.Instance, which creates a needless downcast check.

The downcast has to go *somewhere*.

<snip generic version>

I had to do this kind of thing a great deal in the Ada version of the 
mal lisp interpreter.  See for example:

https://github.com/zmower/mal/blob/master/ada/smart_pointers.ads
https://github.com/zmower/mal/blob/master/ada/types.ads

But my advice is to avoid tagged types if you can.  They only make sense 
if your problem is wildly dynamic or you want to be lazy when thinking 
about memory allocation.  mal qualified on both counts.


  parent reply	other threads:[~2017-07-24 21:24 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
2017-07-24 21:24 ` Chris Moore [this message]
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