TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy


Options: Use Forum View

Use Monospaced Font
Show Text Part by Default
Condense Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
"Team Ada: Ada Advocacy Issues (83 & 95)" <[log in to unmask]>
"Krishna, Buska (IE10)" <[log in to unmask]>
Sun, 2 Sep 2001 17:28:31 -0400
Stephen Leake <[log in to unmask]>
Stephen Leake <[log in to unmask]>
text/plain; charset=us-ascii
text/plain (76 lines)
"Krishna, Buska (IE10)" <[log in to unmask]> writes:

> Hi all,
>         I have written a piece of code, in which I am using an enumeartion
> type. I am assigning some values to the enumeration items by using for
> statement. I want to refer the assigned vales using the enumeration items.
> That is as shown in the code I wanna refer the values 16#10# using the enum
> item "one", 16#20# using "two" etc. Is there any ways to do this? My aim is
> to assign some values to the enumeration type and use those values in the
> place of the enum items just like in C.

Your code is entirely correct, and does exactly what you describe;
every use of the identifier "One" uses a value of 16#10#.

However, perhaps you wanted the Put statement to print "16#10#",
rather than "1" or "One" (you did not actually say this, nor does the
C code call a Put routine). To do this, you need to use

function To_Raw is new Unchecked_Conversion
   (Source => Enum,
    Target => Integer);

Put (To_Raw (One), Base => 16);

The name "Unchecked" is a warning that you are violating an
abstraction; you are accessing the implementation value of the
identifier "One", rather than the logical value. This is sometimes
appropriate; for example, to verify that the compiler listened to your
representation clause.

> Ada code:
> with Ada.Text_Io;
> use Ada.Text_Io;
> with Ada.Integer_Text_IO;
> use Ada.Integer_Text_IO;
> procedure Test is
>    type Enum is
>          (One,
>           Two,
>           Three);
>    for Enum use
>       (
>       One   => 16#10#,
>       Two   => 16#20#,
>       Three => 16#30#);
> begin
>    Put( Enum'Pos (Three));
>    New_Line;
>    Put_Line (Enum'Image (Three));
> end Test;
> in C:
> enum {
> one = Ox10,
> two =Ox20,
> three =0x30
> } ;
> later I can use "one" to refer its value and son on.
> Could any body clarify me, if there is any way to do this.
> Thanx in advance.
> Krishna

-- Stephe