comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Arrays with discriminated task components
Date: Sat, 24 Dec 2022 20:05:19 +0200	[thread overview]
Message-ID: <k0otavF33okU1@mid.individual.net> (raw)
In-Reply-To: <9b729cf0-02c7-48e1-8cea-c0d177c4bf3bn@googlegroups.com>

On 2022-12-24 13:44, AdaMagica wrote:
> I've got a task type with a discriminant:
> 
> type Index is range 1 .. N;
> 
> task type T (D: Index);
> 
> Now I want an array of these tasks, where each task knows its 
> identity (the index) via the discriminant, an
> iterated_component_association: >
> Arr: array (Index) of T := (for I in Index => ???);
> 
> How can I do this?


One way is to give the discrimant a default value that is a function 
call that returns a new identifier on each call:

    Next_Index : Index := Index'First;
    -- The value returned by the next call of New_Index.

    function New_Index return Index
    -- Returns a unique Index value (up to N).
    is
       Result : constant Index := Next_Index;
    begin
       if Next_Index < Index'Last then
          Next_Index := Next_Index + 1;
       -- else report error?
       end if;
       return Result;
    end New_Index;

    task type T (D: Index := New_Index);

Then you can declare the array without any initial value:

    Arr: array (Index) of T;

and the initialization of each task in the array makes its own call to 
New_Index and gets its own identifier value.

A bit sneaky,  but has the advantage that it extends automatically to 
two arrays of tasks, or one array and some separate single declarations 
of tasks, etc.

  reply	other threads:[~2022-12-24 18:05 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-24 11:44 Arrays with discriminated task components AdaMagica
2022-12-24 18:05 ` Niklas Holsti [this message]
2022-12-24 22:41   ` Jeffrey R.Carter
2022-12-25 15:32     ` Niklas Holsti
2022-12-25 16:16 ` Niklas Holsti
2022-12-26 16:39   ` AdaMagica
replies disabled

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