comp.lang.ada
 help / color / mirror / Atom feed
* Record type as generic parameter
@ 2009-08-13  9:52 Gautier write-only
  2009-08-13 10:23 ` Jean-Pierre Rosen
  2009-08-13 10:30 ` Jacob Sparre Andersen
  0 siblings, 2 replies; 4+ messages in thread
From: Gautier write-only @ 2009-08-13  9:52 UTC (permalink / raw)


Hello,
Let's say I have an enumerated type called Scenario, and I want to
fill some data structure with the following information: for each
scenario, I have a certain number of items (of a defined type), which
is varying according to the scenario. That information is stored in an
array with pairs (scenario, item). An item might by appearing in
several scenarios, it is why I don't have simply an array(Item) of
Scenario. Everything fine up to now. Now, I would like to make it
generic, since I have several sets of scenario - i.e. different
enumerated types like Scenario. Then, my favourite Ada compiler
doesn't want a record as generic parameter:

      generic
        type Scenario is range <>;
        type Association is record
          scen: Scenario;
          it  : Item;
        end record;
        scenario_definition: array(Positive range <>) of Association;
      procedure Fill_Scenarios;

Any nice workaround ?
A not-so-nice one is to have two arrays, one array of Scenario, one
array of Item.
Ideally I would keep the type like Association above.
  XY_scenario_definition:
    constant array(Positive range <>) of XY_Association:=
    (
      (Scen_abc, Item_56),
      (Scen_abc, Item_345),
      (Scen_uvw, Item_12),
     ..
The data are easier to pump from a database directly as Ada sources,
they are then easier to read, and there is no chance of messing
positions as with the two arrays solution.
TIA
_________________________________________________________
Gautier's Ada programming -- http://sf.net/users/gdemont/
NB: For a direct answer, e-mail address on the Web site!




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Record type as generic parameter
  2009-08-13  9:52 Record type as generic parameter Gautier write-only
@ 2009-08-13 10:23 ` Jean-Pierre Rosen
  2009-08-13 13:34   ` Gautier write-only
  2009-08-13 10:30 ` Jacob Sparre Andersen
  1 sibling, 1 reply; 4+ messages in thread
From: Jean-Pierre Rosen @ 2009-08-13 10:23 UTC (permalink / raw)


Gautier write-only a �crit :
> [...] Then, my favourite Ada compiler
> doesn't want a record as generic parameter:
> 
>       generic
>         type Scenario is range <>;
>         type Association is record
>           scen: Scenario;
>           it  : Item;
>         end record;
>         scenario_definition: array(Positive range <>) of Association;
>       procedure Fill_Scenarios;
> 
> Any nice workaround ?

Any reason why you cannot make a generic package that would *export*
Association and Scenario_Definition (and procedure Fill_Scenarios),
rather than importing them?

-- 
---------------------------------------------------------
           J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Record type as generic parameter
  2009-08-13  9:52 Record type as generic parameter Gautier write-only
  2009-08-13 10:23 ` Jean-Pierre Rosen
@ 2009-08-13 10:30 ` Jacob Sparre Andersen
  1 sibling, 0 replies; 4+ messages in thread
From: Jacob Sparre Andersen @ 2009-08-13 10:30 UTC (permalink / raw)


Gautier wrote:

> [...] Then, my favourite Ada compiler doesn't want a record as
> generic parameter:
> 
>       generic
>         type Scenario is range <>;
>         type Association is record
>           scen: Scenario;
>           it  : Item;
>         end record;
>         scenario_definition: array(Positive range <>) of Association;
>       procedure Fill_Scenarios;
> 
> Any nice workaround ?

A private type.  With functions for accessing the fields, if necessary.

Greetings,

Jacob
-- 
<URL: small-talk://work/hallway-meeting/...>



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Record type as generic parameter
  2009-08-13 10:23 ` Jean-Pierre Rosen
@ 2009-08-13 13:34   ` Gautier write-only
  0 siblings, 0 replies; 4+ messages in thread
From: Gautier write-only @ 2009-08-13 13:34 UTC (permalink / raw)


On 13 Aug., 12:23, Jean-Pierre Rosen <ro...@adalog.fr> wrote:

> Any reason why you cannot make a generic package that would *export*
> Association and Scenario_Definition
No!

> (and procedure Fill_Scenarios),
Yes!

> rather than importing them?

So first of all, thanks for the very nice idea!
The "Yes!" is because the exporting generic package is in (or withed
by) a very low-level, satellite package, DB_Data.Geo, that basically
consists in data pumped (once for all) from a database in form of big
enumerated types and constant arrays. So it cannot "see" the
definitions needed for Fill_Scenarios.
But the "No!" is valid as well, considering the types used for
defining these scenarios.
So the solution is:

1) In the low-level package DB_Data.Geo.Links:

  generic
    type Scenario is (<>);
  package Scenario_framework is
    -- pairs:
    type Association is record
      scen: Scenario;
      it  : Item;
    end record;
    -- list of pairs:
    type Scenario_definition is array(Positive range <>) of
Association;
  end Scenario_framework;

  package Dept_Xyz is new Scenario_framework
(DB_Data.Geo.Dept_Xyz_Scenario);

  dept_xyz_scenario_definition: constant
Dept_Xyz.Scenario_definition:=
    (
      (Scen_abc, Item_56),
      (Scen_abc, Item_345),
      (Scen_uvw, Item_12),

2) In the high-level package:

      generic
        type Scenario is (<>);
        with package Framework is new Scenario_framework(Scenario);
        def: Framework.Scenario_definition;
      procedure Fill_Scenarios;

One instanciation is then:

       procedure Fill_Dept_Xyz_Scenarios is
        new Fill_Scenarios(
          Dept_Xyz_Scenario,
          Dept_Xyz,
          dept_xyz_scenario_definition
        );

That's it!
Thanks again
_________________________________________________________
Gautier's Ada programming -- http://sf.net/users/gdemont/
NB: For a direct answer, e-mail address on the Web site!



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-08-13 13:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-08-13  9:52 Record type as generic parameter Gautier write-only
2009-08-13 10:23 ` Jean-Pierre Rosen
2009-08-13 13:34   ` Gautier write-only
2009-08-13 10:30 ` Jacob Sparre Andersen

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