comp.lang.ada
 help / color / mirror / Atom feed
From: tmoran@acm.org
Subject: Re: Run-Time Type Assignment
Date: Wed, 28 Aug 2002 22:44:26 GMT
Date: 2002-08-28T22:44:26+00:00	[thread overview]
Message-ID: <encb9.215416$me6.28374@sccrnsc01> (raw)
In-Reply-To: mailman.1030551002.7393.comp.lang.ada@ada.eu.org

 >   type Level_One is new Base_Type with record
 >...
 >   type Level_Two is new Level_One with record
 >...
 >   type Level_Three is new Level_Two with record
   Seems the most straightforward to me.

 But in <mailman.1030517642.16877.comp.lang.ada@ada.eu.org> Bob Leif said:
 > I do not know how to select a subtype of a class at run-time.
     function Read_Whatever return Base_Type'class is
     begin
       -- read and find out what kind of record you have, then return it
       if figured_out_its_Level_One then
         declare
           Result : Level_One;
         begin
           -- fill Result with info
           return Result;
         end;
       elsif figured_out_its_Level_Two then
         declare
           Result : Level_Two;
         begin
           -- fill Result with info
           return Result;
         end;
       ...
     end Read_Whatever;

     One way to handle the different subtypes is

     procedure Process is
       Data : Base_Type'class := Read_Whatever;
     begin
       -- process the Level_One info that's always present
       if Data in Level_One then return;end if; -- that's all in this case
       -- now process the Level_Two stuff that's present in this case
       if Data in Level_Two then return;end if; -- that's all
       -- now process the Level_Three stuff that must be present in this case
       ...

     or, more in OO style:

     procedure Process(Data : in Level_One) is ...
     procedure Process(Data : in Level_Two) is ...
     procedure Process(Data : in Level_Three) is ...

     and then

     declare
       Data : Base_Type'class := Read_Whatever;
     begin
       Process(Base_Type'class(Data));  -- call the appropriate Process routine
     end;

     Since these things are different sizes, you can't put them in an
 array, all of whose elements must be the same size.  But you can put
 in access values.
     type Base_Type_Access is access Base_Type'class;
     Everything : array(1 .. 1_000_000) of Base_Type_Access;
     Count : Natural := 0;

     while not EOF loop
       Count := Count+1;
       Everything(Count) := new Base_Type'class'(Read_Whatever);
     end loop;
     -- Now Everything(1 .. Count) contains pointers to the records.
     -- The records are stored on the heap, each taking only as
     -- much room as it needs.
     for i in 1 .. Count loop                       -- for each record,
       Process(Base_Type'class(Everything(i).all)); -- do appropriate Process
     end loop;
 ---
 Code not tested!



  reply	other threads:[~2002-08-28 22:44 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <005101c24ea9$0de9c960$789a0844@robertqgx6k4x9>
2002-08-28 16:06 ` Run-Time Type Assignment sk
2002-08-28 22:44   ` tmoran [this message]
2002-08-29  0:37   ` tmoran
     [not found] <002a01c24e5f$9ee347b0$789a0844@robertqgx6k4x9>
2002-08-28 11:33 ` sk
     [not found] ` <3D6CB4F5.F4E05D76@myob.com>
2002-08-28 11:37   ` sk
2002-08-28 15:39   ` Robert C. Leif
2002-08-28 18:53     ` Jeffrey Carter
2002-08-28 20:54   ` Robert C. Leif
2002-08-28 22:55     ` Robert A Duff
2002-08-29  3:18       ` Robert C. Leif
2002-08-28  6:53 Robert C. Leif
2002-08-28 11:04 ` Robert Dewar
2002-08-28 13:35   ` Robert A Duff
2002-08-28 14:56     ` Larry Kilgallen
2002-08-28 14:31       ` Robert A Duff
2002-08-28 14:59         ` Lutz Donnerhacke
2002-08-28 22:32           ` Robert A Duff
2002-08-29 22:55           ` Dmitry A.Kazakov
2002-08-28 18:03         ` Frank J. Lhota
2002-08-28 18:37           ` Pat Rogers
2002-08-28 22:47           ` Robert A Duff
2002-08-29 13:32             ` Ben Brosgol
2002-08-29 13:52               ` SIMON Claude
2002-08-29 14:30                 ` Robert A Duff
2002-08-29 18:27                   ` Randy Brukardt
2002-08-29 14:56               ` Robert A Duff
2002-08-30  3:04                 ` Ben Brosgol
2002-08-30 22:54                   ` Robert A Duff
2002-08-29 15:09               ` Larry Kilgallen
2002-08-29 14:29                 ` Marin D. Condic
2002-08-28 13:41 ` Robert A Duff
2002-08-28 17:15 ` Hyman Rosen
2002-08-28 20:27 ` Björn Lundin
replies disabled

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