comp.lang.ada
 help / color / mirror / Atom feed
* Optimizing and Constraint Checks
@ 1995-03-23 16:04 Ed Bruce
  1995-03-24 13:34 ` Theodore Dennison
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Ed Bruce @ 1995-03-23 16:04 UTC (permalink / 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



^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~1995-04-04  0:00 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1995-03-23 16:04 Optimizing and Constraint Checks Ed Bruce
1995-03-24 13:34 ` 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

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