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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,cd9fd73b6a96d80d X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-10-20 18:00:56 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread3.news.pas.earthlink.net.POSTED!not-for-mail From: Jeffrey Carter Organization: jrcarter commercial-at acm [period | full stop] org User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.4) Gecko/20030624 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Byte Swapping Constrained Data Types References: In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Date: Tue, 21 Oct 2003 01:00:55 GMT NNTP-Posting-Host: 63.184.33.196 X-Complaints-To: abuse@earthlink.net X-Trace: newsread3.news.pas.earthlink.net 1066698055 63.184.33.196 (Mon, 20 Oct 2003 18:00:55 PDT) jrcarter010@earthlink.net NNTP-Posting-Date: Mon, 20 Oct 2003 18:00:55 PDT Xref: archiver1.google.com comp.lang.ada:1237 Date: 2003-10-21T01:00:55+00:00 List-Id: 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