comp.lang.ada
 help / color / mirror / Atom feed
From: "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com>
Subject: Re: Enumeration representation
Date: 1999/09/10
Date: 1999-09-10T00:00:00+00:00	[thread overview]
Message-ID: <7rava6$kht@hobbes.crc.com> (raw)
In-Reply-To: 37D8E3BC.175DB72C@newtech.it


Alex <busato@newtech.it> wrote in message
news:37D8E3BC.175DB72C@newtech.it...
> I've written the following example program for testing enumeration
> represetantion clause:
>
> with Ada.Text_IO; use Ada.Text_IO;
>
>
> procedure MainEnum is
>
>       type Status is (a,b,c,d,e);
>       pragma Discard_Names(Status);
>
>       for Status use
>        ( a => 0,
>          b => 20,
>          c => 400,
>          d => 800,
>          e => 1600);
>
>        S : Status;
> begin
>       S := b;
>       Put(" => " & S'img);
> end MainEnum;
>
> I've compiled it with Gnat (the latest version)
> the output of program is 1 but I was expecting another value : 20.
> Does  Gnat support this clause?
I'm not sure to which clause you refer.

> Did I use this clause badly?
>

About the discard_names pragma the LRM says C.5 (7):
If the pragma applies to an enumeration type, then the semantics
of the Wide_Image and Wide_Value attributes are implementation
defined for that type; the semantics of Image and Value are still
defined in terms of Wide_Image and Wide_Value. In addition, the
semantics of Text_IO.Enumeration_IO are implementation defined.
If the pragma applies to a tagged type, then the semantics of the
Tags.Expanded_Name function are implementation defined for that
type. If the pragma applies to an exception, then the semantics
of the Exceptions.Exception_Name function are implementation
defined for that exception.

The GNAT Reference Manual is not entirely correct in what it
states about this pragma, in that it says it is ignored.  What
really happens, is that the Image attribute returns instead
of the image of the name, the image of the 'pos attribute.

To do what you want to do, you need an instance of
Ada.Unchecked_Conversion, as demonstrated in this modification
of your program:

with Ada.Text_Io;
use Ada.Text_Io;
with Ada.Unchecked_Conversion;
procedure MainEnum is

   type Status is (a,b,c,d,e);
   pragma Discard_Names(Status);

   for Status use
      ( a => 0,
        b => 20,
        c => 400,
        d => 800,
        e => 1600);
   type Status_Rep is mod 2 ** Status'Size;

   function Rep is new Ada.Unchecked_Conversion
     (Source => Status,
      Target => Status_rep);

--   S : Status;
begin
   for S in Status'Range loop
      Put("pos => " & S'img);
      Put_Line ("; rep =>" & Rep(S)'Img);
   end loop;
end MainEnum;

Abitavo a Milano dal 6/1963 al 11/1965, e il mio
Italiano non e' quanto fluente come era allora ;)







  parent reply	other threads:[~1999-09-10  0:00 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-09-10  0:00 Enumeration representation Alex
1999-09-10  0:00 ` Ted Dennison
1999-09-10  0:00   ` Robert Dewar
1999-09-13  0:00     ` Ted Dennison
1999-09-10  0:00 ` Keith Thompson
1999-09-11  0:00   ` Robert Dewar
1999-09-11  0:00     ` Keith Thompson
1999-09-12  0:00       ` Bryce Bardin
1999-09-13  0:00         ` Robert Dewar
1999-09-13  0:00           ` Keith Thompson
1999-09-13  0:00       ` Robert Dewar
1999-09-12  0:00         ` Keith Thompson
1999-09-13  0:00           ` Robert Dewar
1999-09-13  0:00             ` Ted Dennison
1999-09-13  0:00             ` Keith Thompson
1999-09-14  0:00               ` Robert Dewar
1999-09-13  0:00         ` Robert A Duff
1999-09-13  0:00           ` Robert Dewar
1999-09-10  0:00 ` Matthew Heaney
1999-09-10  0:00   ` Robert Dewar
1999-09-10  0:00 ` David C. Hoos, Sr. [this message]
1999-09-10  0:00   ` Robert Dewar
1999-09-13  0:00 ` Alex
  -- strict thread matches above, loose matches on Subject: below --
2004-01-01 20:44 Luke A. Guest
2004-01-01 21:45 ` Stephen Leake
2004-01-01 22:01   ` Luke A. Guest
2004-01-02  1:17     ` tmoran
2004-01-02  1:29     ` Stephen Leake
2004-01-02  3:10       ` Luke A. Guest
2004-01-02  2:46 ` Robert A Duff
2004-01-02  3:12   ` Luke A. Guest
2004-01-02 13:58   ` Marin David Condic
2004-01-02 21:39     ` Pat Rogers
2004-01-03 13:42       ` Marin David Condic
2004-01-03  1:53     ` Robert A Duff
2004-01-02 20:52   ` Randy Brukardt
2004-01-02 21:05     ` Luke A. Guest
replies disabled

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