From: "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com>
Subject: Re: Representation clause
Date: Sat, 24 Feb 2001 07:26:35 -0600
Date: 2001-02-24T07:26:35-06:00 [thread overview]
Message-ID: <fEOl6.129219$Ch.23956265@newsrump.sjc.telocity.net> (raw)
In-Reply-To: 3a95c52f@post.usenet.com
Given Randy Brukardt's comment that adding an enumeration representation
attribute
"was discussed ad-nausum during the Ada 9x process and eventually dropped as
having
insufficient value," I submit the following code for a complete generic
package (along
with a simple test program for it), implementing conversions both ways:
-- Begin submitted source code --
generic
type Enum is (<>);
type Reps is range <>;
package Enum_Reps is
function Value (X : Reps;
Raise_Exception_On_Error : Boolean := False)
return Enum;
function Rep (X : Enum) return Reps;
end Enum_Reps;
with Ada.Exceptions;
with Ada.Unchecked_Conversion;
package body Enum_Reps is
-- The following three declarations are an artifice to obtain the
-- name of the instantiation, to use in exception messages.
ID : exception;
Id_Name : constant String :=
Ada.Exceptions.Exception_Name (ID'Identity);
Unit_Name : String renames ID_Name (1 .. ID_Name'Last - 3);
---------
-- Rep --
---------
function Rep (X : Enum) return Reps is
function To_Rep is new Ada.Unchecked_Conversion
(Source => Enum,
Target => Reps);
begin
return To_Rep (X);
end Rep;
-----------
-- Value --
-----------
function Value (X : Reps;
Raise_Exception_On_Error : Boolean := False)
return Enum is
function To_Enum is new Ada.Unchecked_Conversion
(Source => Reps,
Target => Enum);
Result : Enum := To_Enum (X);
begin
if Raise_Exception_On_Error and then not Result'Valid then
Ada.Exceptions.Raise_Exception
(E => Constraint_Error'Identity,
Message => "Invalid representation:" &
Reps'Image (X) &
"; raised in " & Unit_Name & ".VALUE.");
end if;
return Result;
end Value;
end Enum_Reps;
with Ada.Text_IO;
with Enum_Reps;
procedure Test_Enum_Reps is
type Colors is (Red, Yellow, Green);
for Colors use
(Red => 1,
Yellow => 2,
Green => 4);
-- The enumeration's 'Size attribute must be the same as that of
-- the representation type, else a compile error will occur when
-- instantiating the generic package.
for Colors'Size use Integer'Size;
package Color_Reps is new Enum_Reps
(Enum => Colors,
Reps => Integer);
Color : Colors;
begin
for C in Colors'Range loop
Ada.Text_Io.Put_Line
("Representation of " & Colors'Image (C) &
" is" & Integer'Image (Color_Reps.Rep (C)));
end loop;
Ada.Text_IO.New_Line;
-- Test 'Valid attribute
for I in 0 .. 5 loop
Color := Color_Reps.Value (I);
if Color'Valid then
Ada.Text_IO.Put_Line
("Color represented by" & Integer'Image (I) &
" is " & Colors'Image (Color));
else
Ada.Text_IO.Put_Line
(Integer'Image (I) &
" is an invalid Colors representation.");
end if;
end loop;
-- Raise exception for invalid representation
for I in 0 .. 5 loop
Color := Color_Reps.Value (I, True);
Ada.Text_IO.Put_Line
("Color represented by" & Integer'Image (I) &
" is " & Colors'Image (Color));
end loop;
end Test_Enum_Reps;
-- end submitted source code --
"Anatoly Chernyshev" <@http://www.adapower.net/~achernyshev> wrote in
message news:3a95c52f@post.usenet.com...
> **** Post for FREE via your newsreader at post.usenet.com ****
>
> Hello again,
>
> Now I wander about representation clause for enumerated types.
> For example,
> type ATOM is (H, C, N, O);
> for ATOM use (H=>1, C=>6, N=>7, O=>8);
>
> How can I access these 'internal' numbers?
> ATOM'POS yields 0, 1, 2, 3 respectively and I know no other relevant
> attributes.
>
> Thanks,
> Anatoly
>
>
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> *** Usenet.com - The #1 Usenet Newsgroup Service on The Planet! ***
> http://www.usenet.com
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
next prev parent reply other threads:[~2001-02-24 13:26 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-02-23 2:39 Representation clause Anatoly Chernyshev
2001-02-23 3:59 ` James Rogers
2001-02-23 8:46 ` Ada 0x: " Martin Dowie
2001-02-23 9:01 ` Lutz Donnerhacke
2001-02-23 10:22 ` David C. Hoos, Sr.
2001-02-23 13:56 ` Florian Weimer
2001-02-23 14:57 ` David C. Hoos, Sr.
2001-02-23 21:38 ` Florian Weimer
2001-02-23 21:12 ` Randy Brukardt
2001-02-24 5:44 ` James Rogers
2001-02-24 10:43 ` Florian Weimer
2001-02-24 17:47 ` James Rogers
2001-02-26 19:51 ` Randy Brukardt
2001-02-26 20:20 ` James Rogers
[not found] ` <WURm6.3437$7e6.1392211@homer.alpha.net>
2001-02-28 2:32 ` James Rogers
2001-02-23 13:25 ` Marc A. Criley
2001-02-23 14:08 ` Des Walker
2001-02-24 13:26 ` David C. Hoos, Sr. [this message]
2001-02-24 14:45 ` Ken Garlington
2001-02-25 20:22 ` David C. Hoos, Sr.
2001-02-26 0:53 ` Ken Garlington
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox