From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,4576669b9167cd1d,start X-Google-Attributes: gid103376,public From: Stephen Leake Subject: Re: NVRAM or how can I enforce a range check in Ada83. Date: 1996/11/15 Message-ID: <328C6168.45B2@gsfc.nasa.gov>#1/1 X-Deja-AN: 196648531 references: <9611150709.AA09539@algol.ocag.ch> content-type: text/plain; charset=us-ascii organization: NASA Goddard Space Flight Center -- Greenbelt, Maryland USA mime-version: 1.0 reply-to: Stephen.Leake@gsfc.nasa.gov newsgroups: comp.lang.ada x-mailer: Mozilla 3.0 (Win95; U) Date: 1996-11-15T00:00:00+00:00 List-Id: Peter Vogelsanger wrote: > > Hello Ada people (or fans ;-)) > > We are useing a Non Volatile RAM (EEPROM) in our project. The NVRAM driver > accesses the hardware by word operations. Now we've programmed a generic driver > which transform the generic type to a byte or word array. Because of the > possibility of an hardware error, we have to check the read values from the > NVRAM. We use an unchecked_conversion to transform from byte array to the > generic type. We have got no informations about the type inside this generic > procedure. > > Code: > > generic > type Elements is private; > procedure Read (Item : out Elements); > > procedure Read (Item : out Elements) is > function Convert is new Unchecked_Conversion (Source => <>, > Target => Elements); > begin > Item := Convert (<>); > end Read; > > The Unchecked_Conversion does not make any checks, therefore the name unchecked. > [snip] > Now is there a construct that enforce the compiler to make a range check? But > remember in this generic procedure we haven't got any type information. > Also important: we use Ada83. > Apparently you want to validate the data before converting returning it? One way is to add a Validate function to the generic interface: generic type Elements is private; with function Validate (Item : in Elements; Data : in Byte_Array) return Boolean; procedure Read (Item : out Elements); Where Byte_Array is the "raw" data. Validate should do whatever checks are appropriate to the type, and return True if it's ok. This makes Read much more powerful, but much less abstract. Another way is provide a Checked_Conversion for each scalar type that might be used in Elements, and let the user write a higher-level Checked_Conversion. Depends on what sort of checks you want to perform. > Peter > > -- - Stephe