comp.lang.ada
 help / color / mirror / Atom feed
* 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

* Re: Need Value of enum type not position.
  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
  2 siblings, 1 reply; 8+ messages in thread
From: Robert I. Eachus @ 2003-11-07 19:26 UTC (permalink / raw)


Daniel Allex wrote:

> 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.

The ability to specify representations for enumeration types changes how 
the values are represented in hardware.  The function 'Pos returns the 
position number, not the representation.  If you want the 
representation, add also a 'Size clause, then do an Unchecked_Conversion 
to an appropriate integer type.

For this particular case you could just declare a type with eleven 
values, and derive from it:

type Hov_Base_Type is (HovO, Hov1, Hov2, Hov3, Hov4, Hov5, Hov6,
                        Invalid, Off, Initiate, Auto);
type Hov_Mode_Type is new Hov_Base_Type range Invalid..Auto;

-- 
                                           Robert I. Eachus

100% Ada, no bugs--the only way to create software.




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

* Re: Need Value of enum type not position.
  2003-11-07 19:26 ` Robert I. Eachus
@ 2003-11-08  0:16   ` Nick Roberts
  0 siblings, 0 replies; 8+ messages in thread
From: Nick Roberts @ 2003-11-08  0:16 UTC (permalink / raw)


A possible alternative approach, not necessarily any better than using 
Unchecked_Conversion, is to use a constant lookup array:

    type Hov_Mode_Type is
          (INVALID,
           OFF,
           INITIATE,
           AUTO);

    Hov_Mode_Code: constant array (Hov_Mode_Type) of Integer :=
          (INVALID  => 7,
           OFF      => 8,
           INITIATE => 9,
           AUTO     => 10);

Depending on other factors (you have not mentioned), it may or may not be 
necessary for you to also have the enumeration representation clause.

One advantage of this technique is that it may make the logic of a certain 
part of your code immune to changes in representation (encoding) which 
might perhaps be required during the maintenance or update of another part 
of your code (or hardware). Obviously, this separation could turn out to be 
a disadvantage, depending on the logic of your code.

A disadvantage is that the lookup array is likely (but not certain) to be 
implemented as an actual array in memory. If the speed of obtaining a code 
is critical, this might matter (but I think it's unlikely). This sort of 
array can have cache-clobbering properties, which can be unfortunate from a 
speed point of view (but not in any other way).

-- 
Nick Roberts




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

* Re: Need Value of enum type not position.
  2003-11-07 18:27 Daniel Allex
  2003-11-07 19:26 ` Robert I. Eachus
@ 2003-11-10 13:48 ` Mark Doherty
  2003-11-10 18:04 ` Marius Amado Alves
  2 siblings, 0 replies; 8+ messages in thread
From: Mark Doherty @ 2003-11-10 13:48 UTC (permalink / raw)


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.
 
with ada.text_io; use ada.text_io;
with ada.unchecked_conversion;

procedure test is

   Hov_Mode_Type_size : constant := 16;
   
   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 raw_hov_mode_type is range 7..10;
   
   for raw_hov_mode_type'size use Hov_Mode_Type_size;
   function unchecked is new ada.unchecked_conversion (
     source => hov_mode_type, 
     target => raw_hov_mode_type);
                                                       
   lookup : constant array ( Hov_Mode_Type) of raw_hov_mode_type := 
          (INVALID  => 7,
           OFF      => 8,
           INITIATE => 9,
           AUTO     => 10);

   hov_mode : hov_mode_type := off;
   
   unchecked_hov_mode : constant raw_hov_mode_type :=
unchecked(hov_mode);
   
   overlayed_hov_mode : raw_hov_mode_type;
   for overlayed_hov_mode'address use hov_mode'address;
   
   lookup_hov_mode : constant raw_hov_mode_type := lookup(hov_mode);
   
begin
   put_line("unchecked => " &
raw_hov_mode_type'image(unchecked_hov_mode));
   put_line("overlayed => " &
raw_hov_mode_type'image(overlayed_hov_mode));
   put_line("lookuped  => " &
raw_hov_mode_type'image(lookup_hov_mode));
end;



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

* 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-07 18:27 Daniel Allex
  2003-11-07 19:26 ` Robert I. Eachus
  2003-11-10 13:48 ` Mark Doherty
@ 2003-11-10 18:04 ` Marius Amado Alves
  2 siblings, 0 replies; 8+ messages in thread
From: Marius Amado Alves @ 2003-11-10 18:04 UTC (permalink / raw)
  To: comp.lang.ada

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

* 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

* Re: Need Value of enum type not position.
  2003-11-10 17:30 Need Value of enum type not position Beard, Frank Randolph CIV
@ 2003-11-18 21:07 ` Randy Brukardt
  0 siblings, 0 replies; 8+ messages in thread
From: Randy Brukardt @ 2003-11-18 21:07 UTC (permalink / raw)


"Beard, Frank Randolph CIV" <frank.beard@navy.mil> wrote in message
news:mailman.316.1068485520.25614.comp.lang.ada@ada-france.org...
> 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.

Note that the implementation of an enumeration representation clause is
generally a lookup array. Meaning that, unless you *really* need those
objects to always have the representation, you are better off using an
explicit array when you need it. Otherwise, every use of the enumeration
potentially is going through the compiler's lookup array (which is even more
expensive when going from representation to position number).

For instance, E'Succ generates code to find the position number, increment
it, and then find the representation. (As does the similar operations for
array indexing and for loops). That can be pretty expensive. Compilers do
optimize this code when they can, but the general case (as in the original
example) remains expensive.

               Randy.








^ 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