From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Date: 3 Sep 93 21:55:21 GMT From: news-feed-1.peachnet.edu!umn.edu!email.sp.paramax.com!not-for-mail@gatech .edu (Robert Parkhill) Subject: Re: Unchecked_Conversion question Message-ID: <268eg9$2j0@sun_tzu.sp.paramax.com> List-Id: 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..