From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,bd40601768eaf8fd X-Google-Attributes: gid103376,public From: "Matthew Heaney" Subject: Re: Array of Variant Records Question... Date: 1999/09/08 Message-ID: <37d6a45c@news1.prserv.net>#1/1 X-Deja-AN: 522617439 Content-transfer-encoding: 7bit References: <7r5vh3$imu1@svlss.lmms.lmco.com> Content-Type: text/plain; charset="US-ASCII" X-Complaints-To: abuse@prserv.net X-Trace: 8 Sep 1999 18:01:00 GMT, 129.37.213.244 Organization: Global Network Services - Remote Access Mail & News Services Mime-version: 1.0 Newsgroups: comp.lang.ada Date: 1999-09-08T00:00:00+00:00 List-Id: In article <7r5vh3$imu1@svlss.lmms.lmco.com> , "Bruce Detter" 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 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"