comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <matthew_heaney@acm.org>
Subject: Re: Array of Variant Records Question...
Date: 1999/09/08
Date: 1999-09-08T00:00:00+00:00	[thread overview]
Message-ID: <37d6a45c@news1.prserv.net> (raw)
In-Reply-To: 7r5vh3$imu1@svlss.lmms.lmco.com

In article <7r5vh3$imu1@svlss.lmms.lmco.com> , "Bruce Detter" 
<bruce.detter@lmco.com> wrote:

> How do you declare an array of variant records where each variant record's
> descriminant is the array index?  Can it be done at all?  I would prefer not
> to use access pointers if it can be avoided.

I did something like this in all my dining philosopher examples, in the ACM
Ada95 design pattern archives.  Look for the "Dining Philosophers" example
in the Apr 99 link.

In that example, it's a task that has a discriminant, but that's equivalent
to your variant record.


   subtype Philosopher_Id is Positive range 1 .. 5;

   package Id_Management is

      function Get_Id return Philosopher_Id;

   end;
   use Id_Management;

   package body Id_Management is

      Id : Natural := 0;

      function Get_Id return Philosopher_Id is
      begin
         Id := Id + 1;
         return Id;
      end;

   end Id_Management;



   task type Philosopher_Task_Type
     (Id : Philosopher_Id := Get_Id) is

      entry Start;
   end;

   type Philosopher_Task_Array is
      array (Philosopher_Id) of Philosopher_Task_Type;

   Philosopher_Tasks : Philosopher_Task_Array;


At elaboration time, as each task in the array elaborates, it calls Get_Id,
which returns an Id for that task.

Note, however, that there is no necessary correlation between the order of
elaboration of array elements, and the index of the array.  In the example
I've shown above, the task Philosopher_Tasks (1) doesn't necessarily have an
Id with the value 1.

If you need that correspondence, you're going to have to use indirection.
Populate another array with access objects designating the task with that
array index, ie

   <as before>

   type Philosopher_Task_Array is
      array (Philosopher_Id) of aliased Philosopher_Task_Type;
   -- array components are now aliased

   Philosopher_Tasks : Philosopher_Task_Array;

   type Philosopher_Task_Access is
     access all Philosopher_Task_Type;

   type Philosopher_Task_Access_Array is
     array (Philosopher_Id) of Philosopher_Task_Access;

   Philosopher_Index : Philosopher_Task_Access_Array;
   ...
 begin  -- the begin part of the body, or where-ever

   for Id in Philosopher_Tasks'Range loop
     declare
       PT : Philosopher_Task_Type renames
          Philosopher_Task_Array (Id);
     begin
       Philosopher_Index (PT.Id) := PT'Access;
     end;
   end loop;

  end;


Now Philosopher_Index (i) points to the philosopher task whose Id = i.  So
you'll have to use a level of indirection:

  declare
    PT : Phil_Task_Type renames Philosopher_Index (I).all;
  begin
    -- PT.Id = I


Sorry it's a bit complicated; this is one of the glaring flaws in the
language.  But as you can see there is a work-around.

--
Matt

It is impossible to feel great confidence in a negative theory which has
always rested its main support on the weak points of its opponent.

Joseph Needham, "A Mechanistic Criticism of Vitalism"




  parent reply	other threads:[~1999-09-08  0:00 UTC|newest]

Thread overview: 69+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-09-08  0:00 Array of Variant Records Question Bruce Detter
1999-09-08  0:00 ` Ted Dennison
1999-09-08  0:00 ` Matthew Heaney [this message]
1999-09-08  0:00   ` Mike Silva
1999-09-08  0:00     ` Matthew Heaney
1999-09-09  0:00       ` Robert Dewar
1999-09-09  0:00         ` Matthew Heaney
1999-09-09  0:00           ` Matthew Heaney
1999-09-09  0:00             ` Mark Lundquist
1999-09-09  0:00             ` Robert Dewar
1999-09-09  0:00           ` Robert Dewar
1999-09-09  0:00             ` Matthew Heaney
1999-09-10  0:00               ` Mark Lundquist
1999-09-10  0:00                 ` Matthew Heaney
1999-09-11  0:00                 ` Robert Dewar
1999-09-10  0:00               ` Robert Dewar
1999-09-10  0:00                 ` Mark Lundquist
1999-09-10  0:00                   ` Matthew Heaney
1999-09-11  0:00                     ` Jean-Pierre Rosen
1999-09-14  0:00                     ` "cast away const" (was Re: Array of Variant Records Question...) Mark Lundquist
     [not found]                     ` <wccd7viiv59.fsf@world.std.com>
1999-09-22  0:00                       ` Array of Variant Records Question Robert I. Eachus
     [not found]                       ` <7rrmqd$l89@drn.newsguy.com>
     [not found]                         ` <wcciu59n2uf.fsf@world.std.com>
1999-09-22  0:00                           ` Robert I. Eachus
1999-09-23  0:00                             ` Robert Dewar
1999-09-23  0:00                               ` Robert I. Eachus
1999-09-11  0:00               ` Richard D Riehle
1999-09-13  0:00                 ` Hyman Rosen
1999-09-14  0:00                 ` Mark Lundquist
     [not found]                   ` <7roohh$s6r@dfw-ixnews7.ix.netcom.com>
     [not found]                     ` <37e01168@news1.prserv.net>
     [not found]                       ` <7rp86o$c6h@dfw-ixnews3.ix.netcom.com>
     [not found]                         ` <37E18CC6.C8D431B@rational.com>
     [not found]                           ` <7rs8bn$s6@dfw-ixnews4.ix.netcom.com>
     [not found]                             ` <37e2e58c@news1.prserv.net>
1999-09-22  0:00                               ` 'constant functions' and access constant params (was Re: Array of Variant Records Question...) Richard D Riehle
1999-09-22  0:00                                 ` Matthew Heaney
1999-09-22  0:00                                   ` Richard D Riehle
1999-09-22  0:00                                     ` Matthew Heaney
1999-09-22  0:00                                     ` Matthew Heaney
1999-09-23  0:00                                       ` Vincent Marciante
1999-09-23  0:00                                         ` Matthew Heaney
1999-09-24  0:00                                       ` Robert A Duff
1999-09-25  0:00                                         ` Matthew Heaney
1999-09-27  0:00                                       ` Richard D Riehle
1999-09-27  0:00                                         ` David Kristola
1999-09-27  0:00                                       ` Richard D Riehle
1999-09-23  0:00                                     ` Robert Dewar
1999-09-27  0:00                                       ` Richard D Riehle
1999-09-28  0:00                                         ` Robert Dewar
1999-09-28  0:00                                           ` Richard D Riehle
1999-09-29  0:00                                             ` Robert Dewar
1999-09-29  0:00                                             ` Robert A Duff
1999-09-28  0:00                                         ` Robert Dewar
1999-09-28  0:00                                           ` "Competence" (was: 'constant functions' and access constant params) Ted Dennison
1999-09-28  0:00                                             ` Robert Dewar
1999-09-22  0:00                                 ` 'constant functions' and access constant params (was Re: Array of Variant Records Question...) Mark Lundquist
1999-09-22  0:00                                   ` Mark Lundquist
     [not found]                             ` <wccemfxn15s.fsf@world.std.com>
1999-09-22  0:00                               ` Richard D Riehle
1999-09-09  0:00             ` Array of Variant Records Question Brian Rogoff
1999-09-13  0:00               ` Matthew Heaney
1999-09-13  0:00                 ` Robert A Duff
1999-09-13  0:00                   ` Matthew Heaney
1999-09-13  0:00                 ` Brian Rogoff
1999-09-14  0:00                   ` Robert Dewar
1999-09-14  0:00                     ` Brian Rogoff
1999-09-14  0:00                   ` Robert Dewar
1999-09-10  0:00             ` Proposed Ada features (was Re: Array of Variant Records Question...) Mark Lundquist
1999-09-10  0:00               ` Matthew Heaney
1999-09-10  0:00                 ` tmoran
1999-09-09  0:00     ` Array of Variant Records Question Nick Roberts
1999-09-09  0:00       ` Robert Dewar
1999-09-09  0:00       ` Tucker Taft
1999-09-10  0:00         ` Nick Roberts
1999-09-08  0:00 ` Martin C. Carlisle
1999-09-08  0:00 ` Thank you Bruce Detter
1999-09-08  0:00   ` Martin C. Carlisle
replies disabled

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