comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam@spam.com>
Subject: Re: Byte Swapping Constrained Data Types
Date: Tue, 21 Oct 2003 01:00:55 GMT
Date: 2003-10-21T01:00:55+00:00	[thread overview]
Message-ID: <bz%kb.4222$np1.2360@newsread3.news.pas.earthlink.net> (raw)
In-Reply-To: <e84604eb.0310200852.47a4a3e8@posting.google.com>

Brian Barker wrote:

>      type Terminal is ( Null_Terminal, Primary, Secondary, Console  );
>  
>      for Terminal use
>          ( Null_Terminal => 0,
>            Primary    => 1,
>            Secondary  => 2,
>            Console      => 3);

Get rid of this. It buys you nothing, and may cost you something. Some 
compilers generate extra code for enumeration types with a 
representation clause to deal with the possibility that the codes have 
holes and are used for indexing or for loop counters.

>      for Terminal'Size use 32;
>  
> I also have created a procedure based off of a generic byte-swap
> routine:
>  
>      procedure Swap is new Endian.Generic_Swap(Terminal);
>  
> I receive a record over TCP/IP and it is in Big Endian format, so I
> must byte swap it.  I have already copied the raw network data into my
> structure that contains the Terminal type.  When I try to byte swap it
> I receive a constraint error, because the big endian value of Terminal
> is out of range.

What you're receiving over the network is not of type Terminal. You need 
to receive it into a type that corresponds to all the possible values 
you can receive, and their byte-swapped equivalents (probably 
Unsigned_32), swap that, then unchecked convert it to your Terminal type.

Concerning the "efficiency" of Unchecked_Conversion, why are you 
worrying about a problem you have no evidence even exists? 
Unchecked_Conversion is the correct idiom to use. Use it. Measure the 
result. Only if that reveals efficiency problems directly related to the 
use of Unchecked_Conversion should you worry about.

Unchecked_Conversion usually does nothing. Given

type A is ...;
type B is ...;

pragma Assert (A'Size = B'Size);

function To_B is new Unchecked_Conversion (Target => B, Source => A);

X : A;
Y : B;
...
Y := To_B (X);

The assignment typically simply copies the bits of X into Y. The whole 
point of this being unchecked is that it AVOIDS the checks that the type 
system would usually require.

-- 
Jeff Carter
"Brave Sir Robin ran away."
Monty Python and the Holy Grail
59




  reply	other threads:[~2003-10-21  1:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-20 16:52 Byte Swapping Constrained Data Types Brian Barker
2003-10-21  1:00 ` Jeffrey Carter [this message]
  -- strict thread matches above, loose matches on Subject: below --
2003-10-20 18:25 Beard, Frank Randolph CIV
2003-10-20 19:21 ` Brian Barker
2003-10-20 21:01 Beard, Frank Randolph CIV
replies disabled

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