comp.lang.ada
 help / color / mirror / Atom feed
From: news-feed-1.peachnet.edu!umn.edu!email.sp.paramax.com!not-for-mail@gatech .edu  (Robert Parkhill)
Subject: Re: Unchecked_Conversion question
Date: 3 Sep 93 21:55:21 GMT	[thread overview]
Message-ID: <268eg9$2j0@sun_tzu.sp.paramax.com> (raw)

adam@irvine.com (Adam Beneschan) writes:
> In article <2684k7$2hc@sun_tzu.sp.paramax.com> parkhill@sun_tzu.sp.paramax.co
m (Robert Parkhill) writes:
> 
> >   adam@irvine.com (Adam Beneschan) writes:
> >   > In article <1993Sep1.154715.10498@Rapnet.Sanders.Lockheed.Com> lvonrude
@Rapnet.Sanders.Lockheed.Com (Lowell S. VonRuden x5294) writes:
> >
> >   To it asking whether an enum was between the first legal value
> >   and the last legal value was like asking if an integer object
> >   had a value between integer'First and integer'Last.  The answer
> >   was always yes. 
> 
> Yes, that was my point exactly. Which is why I DIDN'T ask whether an
> enumeration was between the first legal value and the last legal value.  In
> my example above, Int is still an integer;  it hasn't been converted
> to an enumeration yet.  My point, and Tucker's also, was that the
> checking should be done on the *integer* before it's converted.
> 
>                                 -- Adam

Pardon Me. I obviously did not read the code carefully. :(


>    type Enum is (AAA, BBB, CCC, DDD);
>    for  Enum use (AAA=> 1,
>                  BBB=> 2,
>                  CCC=> 13,
>                  DDD=> 14);
>   for  Enum'Size use Integer'Size;

>    function Convert is new Unchecked_Conversion (Source => Integer,
                                                  Target => Enum);
>    function Convert_To_Integer is new 
>        Unchecked_Conversion (Source => Enum, Target => Integer);
>    Enum_Low  : constant Integer := Convert_To_Integer (Enum'first);
>   Enum_High : constant Integer := Convert_To_Integer (Enum'last);
> begin

>   if Int not in Enum_Low .. Enum_High then
>     raise Constraint_Error;   -- or whatever
>   end if;
>   E := Convert (Int);  

> end Sample;


I would not have used the "in" construct because there are holes
in the data (You or someone else may already have said this).
On many systems it is not enough to check whether it doesn't hit
the high or low bound.  You must check whether it it not one of
the four and only four values. So as you have stated I would have
checked the integer value to see that it was or was not 1,2,13,14.
This would be done differently depending upon the size of the field
holding the value (lookup table, if's, etc) and how easy it was to
access the field.  Meaning some old machines have weird allignment
for example:

bits 29 27 26 26 25 24 23 ... 0

          |              | five bit field to hold value.

We interface with hardware with only 30 bits with the top 2 on
our 32 bit system being noise..

             reply	other threads:[~1993-09-03 21:55 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-09-03 21:55 Robert Parkhill [this message]
  -- strict thread matches above, loose matches on Subject: below --
1993-09-04  0:13 Unchecked_Conversion question Robert Dewar
1993-09-03 20:14 Wes Groleau x1240 C73-8
1993-09-03 19:51 dog.ee.lbl.gov!agate!howland.reston.ans.net!spool.mu.edu!sdd.hp.com!netwo
1993-09-03 19:06 dog.ee.lbl.gov!agate!spool.mu.edu!umn.edu!email.sp.paramax.com!not-for-ma
1993-09-02 15:43 dog.ee.lbl.gov!overload.lbl.gov!agate!howland.reston.ans.net!noc.near.net
1993-09-02 13:38 Lowell S. VonRuden x5294
1993-09-02  3:00 Robert Dewar
1993-09-01 16:43 dog.ee.lbl.gov!network.ucsd.edu!news.cerf.net!shrike.irvine.com!adam
1993-09-01 15:47 Lowell S. VonRuden x5294
replies disabled

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