comp.lang.ada
 help / color / mirror / Atom feed
From: "Beard, Frank Randolph CIV" <frank.beard@navy.mil>
To: <comp.lang.ada@ada-france.org>
Subject: RE: Need Value of enum type not position.
Date: Mon, 10 Nov 2003 12:30:42 -0500
Date: 2003-11-10T12:30:42-05:00	[thread overview]
Message-ID: <mailman.316.1068485520.25614.comp.lang.ada@ada-france.org> (raw)

I would still go with Unchecked_Conversion.  The problem with
a look-up table is it requires more maintenance.  With the
Unchecked_Conversion approach, you change or add to the type
in one place, instead of having to mirror the change in your
look-up table.  That way you just have:

   Hov_Mode_Type_size : constant := 8;  --+ or appropriate size
   
   type Hov_Mode_Type is
          (INVALID,
           OFF,
           INITIATE,
           AUTO);

   for Hov_Mode_Type'size use Hov_Mode_Type_size;

   for Hov_Mode_type use 
          (INVALID  => 7,
           OFF      => 8,
           INITIATE => 9,
           AUTO     => 10);

   type Number_Type is range 1..2**Hov_Mode_Type'size-1;
   for Number_Type'size use Hov_Mode_Type'size;

   function To_Number is new
      Ada.Unchecked_Conversion(Hov_Mode_Type, Number_Type);

   ...
   Ada.Text_IO.Put_Line(Number_Type'Image(To_Number(I)));


Just my $0.02 ;-)
 
Frank  

-----Original Message-----
From: Mark Doherty [mailto:mark_doherty@yahoo.co.uk]
Sent: Monday, November 10, 2003 8:48
To: comp.lang.ada@ada-france.org
Subject: Re: Need Value of enum type not position.


dallex@erols.com (Daniel Allex) wrote in message news:<686be06c.0311071027.79d6fa21@posting.google.com>...
> With the following declaration:
>  
>    type Hov_Mode_Type is 
>          (INVALID,  
>           OFF,      
>           INITIATE, 
>           AUTO); 
>    for Hov_Mode_Type use(
>       INVALID  => 7, 
>       OFF      => 8, 
>       INITIATE => 9, 
>       AUTO     => 10);
> 
> I can get the position 
>    Ada.Text_IO.Put_Line(Integer'Image(Hov_Mode_Type'Pos(I)));
> Which returns 0,1,2, or 3.  What I want is the value of 7,8,9,10.  I
> can't get 'val to work.  need some help.

You can use a lookup table, unchecked_conversion or an address
overlay.
I would use lookups where you are not mapping directly to hardware,
otherwise unchecked_conversion is safer (than address overlays) as is
can do compile and runtime checks. Use address overlays where
performance in important.
 
[snip]



             reply	other threads:[~2003-11-10 17:30 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-10 17:30 Beard, Frank Randolph CIV [this message]
2003-11-18 21:07 ` Need Value of enum type not position Randy Brukardt
  -- strict thread matches above, loose matches on Subject: below --
2003-11-10 18:25 Beard, Frank Randolph CIV
2003-11-07 18:27 Daniel Allex
2003-11-07 19:26 ` Robert I. Eachus
2003-11-08  0:16   ` Nick Roberts
2003-11-10 13:48 ` Mark Doherty
2003-11-10 18:04 ` Marius Amado Alves
replies disabled

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