comp.lang.ada
 help / color / mirror / Atom feed
* RE: Need Value of enum type not position.
@ 2003-11-10 17:30 Beard, Frank Randolph CIV
  2003-11-18 21:07 ` Randy Brukardt
  0 siblings, 1 reply; 8+ messages in thread
From: Beard, Frank Randolph CIV @ 2003-11-10 17:30 UTC (permalink / raw)
  To: comp.lang.ada

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]



^ permalink raw reply	[flat|nested] 8+ messages in thread
* RE: Need Value of enum type not position.
@ 2003-11-10 18:25 Beard, Frank Randolph CIV
  0 siblings, 0 replies; 8+ messages in thread
From: Beard, Frank Randolph CIV @ 2003-11-10 18:25 UTC (permalink / raw)
  To: comp.lang.ada

I like that one even better, because it's even shorter
and clearer.  Define the range of the numbers you want,
let the compiler figure out what it thinks is the best
number of bits to use, and then make the enumerated
type match it.

Frank

-----Original Message-----
From: Marius Amado Alves [mailto:amado.alves@netcabo.pt]

For what it's worth, this exact problem was discussed and solved in the
thread "accesing internal codes used for an enumeration" by myself on 7
May 2001.

http://groups.google.pt/groups?hl=pt-PT&lr=&ie=UTF-8&oe=UTF-8&frame=right&th=d1367eec61426d33&seekm=mailman.989261706.30667.comp.lang.ada%40ada.eu.org#link7




^ permalink raw reply	[flat|nested] 8+ messages in thread
* Need Value of enum type not position.
@ 2003-11-07 18:27 Daniel Allex
  2003-11-07 19:26 ` Robert I. Eachus
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Daniel Allex @ 2003-11-07 18:27 UTC (permalink / raw)


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.



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

end of thread, other threads:[~2003-11-18 21:07 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-10 17:30 Need Value of enum type not position Beard, Frank Randolph CIV
2003-11-18 21:07 ` 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

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