comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <Stephen.Leake@gsfc.nasa.gov>
Subject: Re: NVRAM or how can I enforce a range check in Ada83.
Date: 1996/11/15
Date: 1996-11-15T00:00:00+00:00	[thread overview]
Message-ID: <328C6168.45B2@gsfc.nasa.gov> (raw)
In-Reply-To: 9611150709.AA09539@algol.ocag.ch


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 => <<byte array>>,
>                                                    Target => Elements);
>   begin
>      Item := Convert (<<Nvram_Byte_Array_Of_Element_Size>>);
>   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




  reply	other threads:[~1996-11-15  0:00 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-11-15  0:00 NVRAM or how can I enforce a range check in Ada83 Peter Vogelsanger
1996-11-15  0:00 ` Stephen Leake [this message]
1996-11-16  0:00 ` Ken Garlington
1996-11-17  0:00   ` Robert Dewar
1996-11-17  0:00     ` Robert A Duff
1996-11-18  0:00       ` Robert Dewar
1996-11-19  0:00         ` Robert A Duff
1996-11-22  0:00     ` Ken Garlington
1996-11-22  0:00       ` Robert Dewar
1996-11-25  0:00         ` Ken Garlington
1996-11-19  0:00   ` Laurent Guerby
replies disabled

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