comp.lang.ada
 help / color / mirror / Atom feed
* Hierarchy destruction in Ada
@ 2004-12-12 15:18 Michael Mounteney
  2004-12-12 15:38 ` Dmitry A. Kazakov
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Michael Mounteney @ 2004-12-12 15:18 UTC (permalink / raw)


Ada 95 provides a mechanism with tagged and type'class to allow
dynamic dispatch. So if one has:

procedure something (O : basetype'class) is
begin
     enact (O);
end something;

then as we all know, with the appropriate redefinitions of enact ()
for the subtypes of basetype, we have dynamic dispatch.  But what
about deallocation ? I want to declare:

type handle is access all basetype;

procedure dispose (ptr : in out handle);

and can't see how to make this deallocate the correct object.
Obviously, one can dynamically dispatch on ptr.all but it seems
impossible to solve the complete problem without an unchecked
conversion. Anyone ?



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Hierarchy destruction in Ada
  2004-12-12 15:18 Hierarchy destruction in Ada Michael Mounteney
@ 2004-12-12 15:38 ` Dmitry A. Kazakov
  2004-12-12 16:52 ` Martin Krischik
  2004-12-13 13:41 ` Martin Krischik
  2 siblings, 0 replies; 4+ messages in thread
From: Dmitry A. Kazakov @ 2004-12-12 15:38 UTC (permalink / raw)


On 12 Dec 2004 07:18:32 -0800, Michael Mounteney wrote:

> Ada 95 provides a mechanism with tagged and type'class to allow
> dynamic dispatch. So if one has:
> 
> procedure something (O : basetype'class) is
> begin
>      enact (O);
> end something;
> 
> then as we all know, with the appropriate redefinitions of enact ()
> for the subtypes of basetype, we have dynamic dispatch.  But what
> about deallocation ? I want to declare:
> 
> type handle is access all basetype;

You should use class-wide pointers if you want dispatch on them:

type Handle is access all BaseType'Class;

> procedure dispose (ptr : in out handle);
> 
> and can't see how to make this deallocate the correct object.
> Obviously, one can dynamically dispatch on ptr.all but it seems
> impossible to solve the complete problem without an unchecked
> conversion. Anyone ?

procedure Dispose is
   new Ada.Unchecked_Deallocation (BaseType'Class, Handle);

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



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Hierarchy destruction in Ada
  2004-12-12 15:18 Hierarchy destruction in Ada Michael Mounteney
  2004-12-12 15:38 ` Dmitry A. Kazakov
@ 2004-12-12 16:52 ` Martin Krischik
  2004-12-13 13:41 ` Martin Krischik
  2 siblings, 0 replies; 4+ messages in thread
From: Martin Krischik @ 2004-12-12 16:52 UTC (permalink / raw)


Michael Mounteney wrote:

> Ada 95 provides a mechanism with tagged and type'class to allow
> dynamic dispatch. So if one has:
> 
> procedure something (O : basetype'class) is
> begin
>      enact (O);
> end something;
> 
> then as we all know, with the appropriate redefinitions of enact ()
> for the subtypes of basetype, we have dynamic dispatch.  But what
> about deallocation ? I want to declare:
> 
> type handle is access all basetype;

The trick is:

type handle is access basetype'Class;

> procedure dispose (ptr : in out handle);

If you want to write a "dispose" function you should not use "access all".
Reason: access all allows any kind of access but the "dispose" needs the
right type of access to work properly. i.E:

declare
   X : aliased basetype;
begin
   dispose (X'Access);
end;

You don't want that, don't you? If you need the "all" features for other
reasons then consider:

type pool_handle is access basetype'Class;
type any_handle is access all basetype'Class;

> and can't see how to make this deallocate the correct object.
> Obviously, one can dynamically dispatch on ptr.all but it seems
> impossible to solve the complete problem without an unchecked
> conversion. Anyone ?

When you deallocate an "access basetype'Class" then the propper finalisation
is done. 

With Regards

Martin

-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Hierarchy destruction in Ada
  2004-12-12 15:18 Hierarchy destruction in Ada Michael Mounteney
  2004-12-12 15:38 ` Dmitry A. Kazakov
  2004-12-12 16:52 ` Martin Krischik
@ 2004-12-13 13:41 ` Martin Krischik
  2 siblings, 0 replies; 4+ messages in thread
From: Martin Krischik @ 2004-12-13 13:41 UTC (permalink / raw)


Hello Michael,

Since questions like this come zwice a month i have begun a little article
aobut OO in Ada

http://en.wikibooks.org/wiki/Programming:Ada:OO

What do you think?

With Regards

Martin
-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-12-13 13:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-12 15:18 Hierarchy destruction in Ada Michael Mounteney
2004-12-12 15:38 ` Dmitry A. Kazakov
2004-12-12 16:52 ` Martin Krischik
2004-12-13 13:41 ` Martin Krischik

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