comp.lang.ada
 help / color / mirror / Atom feed
From: "Alex R. Mosteo" <alejandro@mosteo.invalid>
Subject: Re: Use of abstract tagged types without access types -- again and complete
Date: Sun, 12 Jun 2011 14:05:09 +0200
Date: 2011-06-12T14:05:09+02:00	[thread overview]
Message-ID: <it2a1m$4v6$1@dont-email.me> (raw)
In-Reply-To: 63c5c60a-ba75-4ab4-afb3-b78aead48815@l18g2000yql.googlegroups.com

Prof. Dr. Carl Weierstrass wrote:

> On 9 Jun., 23:06, "Randy Brukardt" <ra...@rrsoftware.com> wrote:
>> "Ludovic Brenta" <ludo...@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.
> 
> Thank you very much. I think that this could be the solution that I
> was looking for.
> In case of many children I considered using an
> Ada.Containers.Indefinite_Vectors type,
> but I was looking for a solution when the is only one child.
> Unfortunately I'am using
> GNAT GPL 2010 and can't find that container in the library.
> 
> Any idea, where I can find an implementation? I hope it will be in the
> next GNAT GPL 2011
> version.

I've been using this 'holder' approach for years. If you don't have the 
official, the simplest way is to make your own with any other of the 
standard containers, and a simplified spec.

Migration to the true Holder would later be a matter of seconds with a 
single search/replace if you take care to respect the subprogram names.

> 
> Regards




  reply	other threads:[~2011-06-12 12:05 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
2011-06-10 15:43     ` Prof. Dr. Carl Weierstrass
2011-06-12 12:05       ` Alex R. Mosteo [this message]
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