comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Use of abstract tagged types without access types -- again and complete
Date: Thu, 9 Jun 2011 16:06:40 -0500
Date: 2011-06-09T16:06:40-05:00	[thread overview]
Message-ID: <isrcl4$p8m$1@munin.nbi.dk> (raw)
In-Reply-To: 87hb7yn4fs.fsf@ludovic-brenta.org

"Ludovic Brenta" <ludovic@ludovic-brenta.org> wrote in message 
news:87hb7yn4fs.fsf@ludovic-brenta.org...
> "Prof. Dr. Carl Weierstrass" writes on comp.lang.ada:
>>    type Parent_Type is new Ada.Finalization.Controlled with record
>>          Child : Child_Type'Class;
>>    end record;
>>
>> This doesn't work, because:
>> class-wide subtype with unknown discriminants in component declaration
>>
>> Of course I could use an access type to avoid the error but is there a
>> way to do something like this without access types.
>
> No.  The reason is that the size of Child is unknown, so you cannot
> encapsulate a Child into a Parent_Type, which has a fixed size.  In
> contrast, the size of an access value is fixed at comile time, so you
> can encapsulate an access value in the Parent_Type.

Not quite accurrate -- an access type is necessary somewhere, but it doesn't 
have to be visible.

Specifically, use the Holder container for this purpose:

     package Child_Holder is new Ada.Containers.Indefinite_Holders 
(Child_Type'Class);

     type Parent_Type is new Ada.Finalization.Controlled with record
           Child : Child_Holder.Holder;
     end record;

This is a slight complication when using the component (you have to use 
Element and Replace_Element to access the contents), but it is usually 
better than writing all of this storage management yourself.

The only downside is whether your Ada compiler supports this container (it 
was added after Ada 2005 was finished, early in the Ada 2012 process). It's 
used in the latest ASIS (that's why it was invented), so a compiler 
supporting ASIS for Ada 2005 probably supports the container.

If your compiler doesn't yet support this container, it's easy enough to 
create it yourself rather than using explicit access types.

                                             Randy.





  reply	other threads:[~2011-06-09 21:06 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-09 17:50 Use of abstract tagged types without access types -- again and complete Prof. Dr. Carl Weierstrass
2011-06-09 20:19 ` Ludovic Brenta
2011-06-09 21:06   ` Randy Brukardt [this message]
2011-06-10 15:43     ` Prof. Dr. Carl Weierstrass
2011-06-12 12:05       ` Alex R. Mosteo
2011-06-09 20:41 ` Georg Bauhaus
replies disabled

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