comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthew_heaney@acm.org>
Subject: Re: Destructor question
Date: 1998/12/06
Date: 1998-12-06T00:00:00+00:00	[thread overview]
Message-ID: <m3k90559po.fsf@mheaney.ni.net> (raw)
In-Reply-To: EACHUS.98Dec4184342@spectre.mitre.org

eachus@spectre.mitre.org (Robert I. Eachus) writes:

>      Did you get my e-mail note on this?  Anyway what you have do to
> is to have an operation for the "root" type that takes the access type
> as a parameter:
> 
>     procedure Deallocate(The_Access : in out Object_Class);
> 
>     Next, you need, and this need not be visible, to have a two
> parameter version that is dispatching on Object:
> 
>     procedure Deallocate(The_Access : in out Object_Class;
>                          The_Object : in Object);


An alternative implementation uses a (private) subprogram that takes an
access parameter:

   procedure Do_Deallocate (The_Object : access Object);

This of course dispatches on the tag of The_Object.  It more-or-less
combines the two parameters of Bob's solution into one parameter.

> 
>     The bodies are fairly straightforward:
> 
> 
>     procedure Deallocate(The_Access : in out Object_Class) is
>     begin Deallocate(The_Access, The_Access.all); end Deallocate;
>     -- This dispatches on the second operand.
>
>     procedure Deallocate(The_Access : in out Object_Class;
>                          The_Object : in Object) is
>       function Free is new Unchecked_Deallocation(Object, Object_Class); 
>     begin
>       Deallocate(The_Access.all); -- may not need this if using Controlled
>       Free(The_Access);
>     end Deallocate;


The bodies of the alternative implementation are:

   procedure Deallocate (The_Access : in out Object_Class) is
   begin
      if The_Access /= null then
        Do_Deallocate (The_Access);
        The_Access := null;
      end if;
   end Deallocate;


   procedure Do_Deallocate (The_Object : access Object) is

      OA : Object_Access := Object_Access (The_Object);
   begin
      Free (OA);
   end;

where type Object_Access is an access type that designates the specific
type Object.  Analogous implementations would be required for each type
in the hierarchy.  (I prefer to instantiate Unchecked_Deallocation using
a specific type, rather than a class-wide one.)

A complete example using this technique may be found at the patterns
archive at the ACM:

<http://www.acm.org/archives/patterns.html>

Look under month Nov 1998, and the post labeled "No subject."  It
discusses an implementation of the Visitor pattern that uses the memory
management technique described above.

From the original post (not from Bob Eachus):

>  > On another note,  if i override a proceudre in a derived class, how can I
>  > "chain" it to its super class (i.e.,  call  within the procedure the super
>  > class procedure which it overrides)?  I could certainly see the use in
>  > chaining  the Initialize procedure of the Controlled class mentioned
>  > above.  Explicit casting doesn't seem to work, and casting is not a very
>  > desirable solution anyway.

Sometimes you need to cast.  Downcasts from a class-wide type to a
specific one are safe.






  reply	other threads:[~1998-12-06  0:00 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-12-02  0:00 Destructor question Rusnak
1998-12-03  0:00 ` Jeff Carter
1998-12-03  0:00   ` Rusnak
1998-12-04  0:00     ` Robert I. Eachus
1998-12-06  0:00       ` Matthew Heaney [this message]
1998-12-08  0:00         ` Robert I. Eachus
1998-12-06  0:00     ` Matthew Heaney
1998-12-07  0:00     ` Jeff Carter
1998-12-06  0:00 ` Matthew Heaney
1998-12-06  0:00 ` david.c.hoos.sr
1998-12-06  0:00   ` Matthew Heaney
1998-12-08  0:00   ` Robert I. Eachus
replies disabled

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