comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthewjheaney@earthlink.net>
Subject: Re: Deallocating list of polymorphic objects?
Date: Fri, 01 Dec 2006 04:11:34 GMT
Date: 2006-12-01T04:11:34+00:00	[thread overview]
Message-ID: <uu00gcl1o.fsf@earthlink.net> (raw)
In-Reply-To: 1164930027.758923.119740@h54g2000cwb.googlegroups.com

"Michael Rohan" <mrohan@ACM.ORG> writes:

> I would like to construct a list of polymorphic objects that,
> as part of the list's finalization, deallocates the objects on
> the list.  Basically, I have a vector of pointers to Object'Class.
> The objects are added to the list via procedures defined for
> the list, e.g., append an integer, append a floating point.
> These append procedures allocate objects derived from the
> base Object type for the type being appended, e.g.,
> Integer_Object, which is private to the list package.


Here's one way to do it:

--STX
with Ada.Containers.Indefinite_Vectors;
pragma Elaborate_All (Ada.Containers.Indefinite_Vectors);

package Lists is

   type List_Type is tagged limited private;

   procedure Append (L : in out List_Type; I : Integer);
   procedure Append (L : in out List_Type; F : Float);

private

   type Object is interface;
   procedure Print (O : Object) is abstract;

   package List_Vectors is
      new Ada.Containers.Indefinite_Vectors (Natural, Object'Class);

   type List_Type is tagged limited record
      V : List_Vectors.Vector;
   end record;


   type Integer_Object is new Object with record
      I : Integer;
   end record;

   procedure Print (O : Integer_Object);

   type Float_Object is new Object with record
      F : Float;
   end record;

   procedure Print (O : Float_Object);

end Lists;


package body Lists is

   procedure Append (L : in out List_Type; I : Integer) is
   begin
      L.V.Append (Integer_Object'(I => I));
   end;

   procedure Append (L : in out List_Type; F : Float) is
   begin
      L.V.Append (Float_Object'(F => F));
   end;

   procedure Print (O : Integer_Object) is
   begin
      null;
   end;

   procedure Print (O : Float_Object) is
   begin
      null;
   end;

end Lists;



  parent reply	other threads:[~2006-12-01  4:11 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-11-30 23:40 Deallocating list of polymorphic objects? Michael Rohan
2006-12-01  0:05 ` Robert A Duff
2006-12-01  6:41   ` Simon Wright
2006-12-01  1:24 ` Randy Brukardt
2006-12-01  8:57   ` Maciej Sobczak
2006-12-01 12:33     ` Matthew Heaney
2006-12-01 13:05       ` Maciej Sobczak
2006-12-01 14:56         ` Matthew Heaney
2006-12-01 19:03           ` Georg Bauhaus
2006-12-01  3:52 ` Matthew Heaney
2006-12-01  4:11 ` Matthew Heaney [this message]
2006-12-01  6:12   ` Michael Rohan
2006-12-01 12:40     ` Matthew Heaney
replies disabled

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