From: "Marin David Condic, 561.796.8997, M/S 731-96" <condicma@PWFL.COM>
Subject: Using the GNAT defined attribute: 'Enum_Rep
Date: 1997/08/27
Date: 1997-08-27T00:00:00+00:00 [thread overview]
Message-ID: <97082719523509@psavax.pwfl.com> (raw)
Here's a little problem which I could use some help handling:
I get external data for my program which is extracted from the
internal representation of an enumerated type. That is to say, I'm
getting integers which correspond to the representation clause of
the enumerated type, not the position. On input, they are read as
integers and I'd like to convert them to the enumerated type.
The ARM suggests using Unchecked_Conversion to get from here to
there, but now you've got to create an integer type that is the
same size as the enumerated type. Here's a code fragment:
--
type Temp_Integer is range Integer'First..Integer'Last ;
for Temp_Integer'Size use User_Types.CL_Evt_Status'Size ;
--^^^^^^^^^^^^^^^^^^^^^^ The enumerated type.
function
CVT_Evt_Status
is new
Ada.Unchecked_Conversion (
Source => Temp_Integer ;
Target => User_Types.CL_Evt_Status) ;
You can now use the CVT_Evt_Status function to get from the
external integer to the internal representation. However, the
declaration of Temp_Integer might not work because you don't know
that the 'Size of the enumerated type is big enough for the range.
You can easily build a custom one-off that will work if you know
enough about the type and it's representation, but it would be
nicer to have a general solution which will work for any
enumerated type & representation.
GNAT is nice enough to provide the attribute: 'Enum_Rep which
returns a Universal_Integer of the internal representation of an
enumerated object. (I'm surprised this isn't standard!) This is a
great answer when I'm writing the data out - all I've got to worry
about is that the enumeral doesn't create a constraint_error when
assigned to an integer object for output. What I'd like is a
corresponding "'Integer_Rep" (or some other more appropriate name)
attribute which converted from a Universal_Integer to the internal
representation of the enumerated type - risking only
Constraint_Error if the integer is not a member of the
representation set.
Has anyone got a suggestion as to how this can be done more
elegantly with some other attributes or Unchecked_Conversion? I
don't see any simple way to do this. (Please don't suggest 'Pos
and 'Val. These will work If and only if the representation is
identical to the position numbers - what you get if you don't
specify a representation. What I've got are representations with
gaps in them.) Thanks for any suggestions you have to offer.
MDC
Marin David Condic, Senior Computer Engineer ATT: 561.796.8997
Pratt & Whitney GESP, M/S 731-96, P.O.B. 109600 Fax: 561.796.4669
West Palm Beach, FL, 33410-9600 Internet: CONDICMA@PWFL.COM
===============================================================================
"I saw a bank that said "24 Hour Banking", but I don't have that much time."
-- Steven Wright
===============================================================================
next reply other threads:[~1997-08-27 0:00 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-08-27 0:00 Marin David Condic, 561.796.8997, M/S 731-96 [this message]
1997-09-03 0:00 ` Using the GNAT defined attribute: 'Enum_Rep W. Wesley Groleau x4923
1997-09-06 0:00 ` Robert Dewar
1997-09-08 0:00 ` Robert A Duff
1997-09-08 0:00 ` W. Wesley Groleau x4923
1997-09-08 0:00 ` Matthew Heaney
1997-09-08 0:00 ` Robert A Duff
1997-09-08 0:00 ` W. Wesley Groleau x4923
1997-09-08 0:00 ` Matthew Heaney
1997-09-09 0:00 ` Robert A Duff
1997-09-11 0:00 ` Robert Dewar
1997-09-08 0:00 ` Robert A Duff
-- strict thread matches above, loose matches on Subject: below --
1997-09-09 0:00 Marin David Condic, 561.796.8997, M/S 731-96
1997-09-09 0:00 ` Robert A Duff
1997-09-09 0:00 Marin David Condic, 561.796.8997, M/S 731-96
1997-09-09 0:00 ` W. Wesley Groleau x4923
1997-09-11 0:00 ` Robert Dewar
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox