TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy

TEAM-ADA@LISTSERV.ACM.ORG

Options: Use Classic View

Use Monospaced Font
Show Text Part by Default
Condense Mail Headers

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

Print Reply
Sender: "Team Ada: Ada Advocacy Issues (83 & 95)" <[log in to unmask]>
X-To: "Krishna, Buska (IE10)" <[log in to unmask]>
Date: Sun, 2 Sep 2001 17:28:31 -0400
Reply-To: Stephen Leake <[log in to unmask]>
From: Stephen Leake <[log in to unmask]>
In-Reply-To: <[log in to unmask]>
Content-Type: text/plain; charset=us-ascii
MIME-Version: 1.0
Parts/Attachments: 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
Unchecked_Conversion:

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

ATOM RSS1 RSS2