comp.lang.ada
 help / color / mirror / Atom feed
From: "Michael Rohan" <mrohan@ACM.ORG>
Subject: Re: Deallocating list of polymorphic objects?
Date: 30 Nov 2006 22:12:57 -0800
Date: 2006-11-30T22:12:57-08:00	[thread overview]
Message-ID: <1164953577.897306.271030@79g2000cws.googlegroups.com> (raw)
In-Reply-To: uu00gcl1o.fsf@earthlink.net

Matthew Heaney wrote:
> "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;

Hi,

This looks really tidy and clean.

Thank you,
Michael.




  reply	other threads:[~2006-12-01  6:12 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
2006-12-01  6:12   ` Michael Rohan [this message]
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