comp.lang.ada
 help / color / mirror / Atom feed
From: edward@igate1.hac.com (Ed Bruce)
Subject: Optimizing and Constraint Checks
Date: Thu, 23 Mar 1995 10:04:27 -0600
Date: 1995-03-23T10:04:27-06:00	[thread overview]
Message-ID: <edward-2303951004270001@r-618macip27.hac.com> (raw)

I am currently attempting to implement a safe, portable
Unchecked_Conversion of an enumerated type to integer and from an integer
type back to the enumeration. The following code shows the current
solution, the question raised by one engineer is can we guarantee the the
return statement in the To_Enum function is portable. That is may a
compiler optimize away the ENUM'Pos and ENUM'Val and effectively eliminate
the constraint check.

with Unchecked_Conversion;

generic
  type ENUM is (<>);
package Convert is
  Illegal_Value : exception;

  function To_Integer( Value : in ENUM )    return INTEGER;
  function To_Enum   ( Value : in INTEGER ) return ENUM;
end Convert;

package Convert is
  function To_Integer is new Unchecked_Conversion(
    Source => ENUM,
    Target => INTEGER );

  function Convert_Enum is new Unchecked_Conversion(
    Source => INTEGER,
    Target => ENUM );

  function To_Enum( Value : in INTEGER ) return ENUM is
  begin
    return ENUM'Val( ENUM'Pos( Convert_Enum( Value )))
  exception
    when Constraint_Error =>
      raise Illegal_Value;
    when Others =>
     raise;
  end To_Enum;
end Convert;
Ed Bruce
edward@igate1.hac.com



             reply	other threads:[~1995-03-23 16:04 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-03-23 16:04 Ed Bruce [this message]
1995-03-24 13:34 ` Optimizing and Constraint Checks Theodore Dennison
1995-03-25 18:09   ` David Wheeler
1995-03-29  0:00   ` Ed Bruce
1995-03-24 20:15 ` Garlington KE
1995-03-26 12:01   ` Robert Dewar
1995-03-27 16:58     ` Garlington KE
1995-03-29  0:00     ` Ed Bruce
1995-03-29  0:00       ` Garlington KE
1995-03-30  0:00       ` Mike Meier
1995-04-04  0:00       ` Robert Dewar
1995-03-25 17:51 ` 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