comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@ni.net (Matthew Heaney)
Subject: Re: Ada 83 - avoiding unchecked conversions.
Date: 1996/12/10
Date: 1996-12-10T00:00:00+00:00	[thread overview]
Message-ID: <mheaney-ya023280001012960811440001@news.ni.net> (raw)
In-Reply-To: 32A311C1.41C67EA6@escmail.orl.lmco.com


>Ensco Vendor wrote:
>> 
>> we have two 16 bit integers which we need to assemble into a single 32
>> bit integer (one is high order, the other low order).  We wish to avoid
>> unchecked conversion if we can.  Is there a standard accepted way of
>> doing this?

Unchecked conversion is the accepted way of doing this:

function To_Integer_32 (Low, High : Integer_16)
   return Integer_32 is

   type Integer_32_Record is
      record
         High_Order : Integer_16;
         Low_Order : Integer_16;
      end record;

   for Integer_32_Record use
      record
         High_Order at 0 range 16 .. 32;
         Low_Order at 0 range 0 .. 15;
      end record;

   for Integer_32_Record'Size use 32;

   function To_Integer_32 is 
      new Unchecked_Conversion (
         Integer_32_Record, 
         Integer_32);

   The_Record : constant Integer_32_Record := (
      High_Order => High, 
      Low_Order => Low);
begin
   return To_Integer_32 (The_Record);
end;

Because the source and target types have the same size, this should be
completely portable.

Some shops have the rule "Thou shalt not use Unchecked_Conversion."  But
this is silly; use it when it makes sense.

The proper place to use Unchecked_Conversion is at the interface boundary
of the sytem.  That seems to be the case for you, because you're
manipulating types whose size is known explicitly (16 and 32 bit integers).

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
mheaney@ni.net
(818) 985-1271




  reply	other threads:[~1996-12-10  0:00 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-11-27  0:00 Ada 83 - avoiding unchecked conversions Ensco Vendor
1996-11-27  0:00 ` Robert I. Eachus
1996-11-29  0:00 ` Robert Dewar
1996-12-01  0:00   ` Darel Cullen
1996-11-30  0:00     ` Robert Dewar
1996-12-11  0:00     ` Richard Riehle
1996-12-02  0:00 ` Ted Dennison
1996-12-10  0:00   ` Matthew Heaney [this message]
     [not found] <md5:8B831999BCF200C6E70994BDF6CC529F>
1996-12-11  0:00 ` Chris Sparks (Mr. Ada)
1996-12-11  0:00   ` Dewi Daniels
1996-12-12  0:00     ` Richard Kenner
1996-12-17  0:00       ` Eric Miller
1996-12-18  0:00         ` Robert Dewar
1996-12-18  0:00           ` Robert A Duff
1996-12-19  0:00           ` Keith Thompson
1996-12-26  0:00             ` Robert Dewar
1996-12-11  0:00   ` Matthew Heaney
1996-12-12  0:00     ` Chris Brand
1996-12-13  0:00       ` Stephen Leake
1996-12-14  0:00         ` Robert A Duff
1996-12-14  0:00     ` BGaffney42
1996-12-19  0:00   ` Robert I. Eachus
replies disabled

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