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 12:25:09 PST X-Abuse-Report: abuse@teranews.com Message-ID: Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.vmunix.org!peer02.cox.net!cox.net!news3.optonline.net!newsfeed-east.nntpserver.com!nntpserver.com!news.teranews.com!not-for-mail Date: Mon, 20 Oct 2003 19:21:33 GMT From: "Brian Barker" Newsgroups: comp.lang.ada References: Subject: Re: Byte Swapping Constrained Data Types X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Xref: archiver1.google.com comp.lang.ada:1226 Date: 2003-10-20T19:21:33+00:00 List-Id: Since I couldn't use the generic function to swap my original type (Terminal). I created some other swap functions called swap_2, swap_4, swap_8. They each take in a system.address as the only argument. So now I have, Swap_4(My_Record.Sending_Terminal'ADDRESS); -- <- Sending_Terminal is of type Terminal This works, but I don't like it because I could accidently call Swap_4 on a 16 bit value and cause problems. I've tried to avoid Unchecked_Conversion in the past, but it looks to provide a safer solution to the problem. Is Unchecked_Conversion an expensive operation? I will be converting a good bit of data over a periodic rate. Also, will the compiler generate an error on the following line if the size of Terminal changes (say from 32 bits down to 16), but not Intermediate_Type? function To_Terminal is Ada.Unchecked_Conversion(Intermediate_Type, Terminal); Brian -- ---------------------------------------------------- This mailbox protected from junk email by Matador from MailFrontier, Inc. http://info.mailfrontier.com "Beard, Frank Randolph CIV" wrote in message news:mailman.123.1066674379.25614.comp.lang.ada@ada-france.org... As soon as you reference the Terminal variable, range checking occurs. You are going to have to swap the bytes before you reference it. For instance: procedure Some_Routine is type Intermediate_Type is mod 2**32; procedure Swap is new Endian.Generic_Swap(Intermediate_Type); function To_Terminal is Ada.Unchecked_Conversion(Intermediate_Type, Terminal); begin --+ Copying your raw network data into the intermediate. --+ Swap. Swap(My_Intermediate); --+ Convert it. My_Terminal := To_Terminal(My_Intermediate); We had a case similar to yours but we used a rep clause instead. Ours about 72 bits of data into a record structure. We used the rep clause to lay it out from the data stream. The Swap is more flexible, because we had different rep clauses depending on what system we were running. Frank -----Original Message----- From: Brian Barker [mailto:bbarker@ball.com] Sent: Monday, October 20, 2003 12:52 To: comp.lang.ada@ada-france.org Subject: Byte Swapping Constrained Data Types ObjectAda for Windows 7.2.2 Native This is more of an Ada specific question if someone could help me. I have declared an enumerated type like so: type Terminal is ( Null_Terminal, Primary, Secondary, Console ); for Terminal use ( Null_Terminal => 0, Primary => 1, Secondary => 2, Console => 3); 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. Swap(My_Record.Sending_Terminal); -- <- Sending_Terminal is of type Terminal I want to keep the generic swap, because I have so many abstract data types. I just want to get around this constraint range check when I try to pass the value into the swap routine. Does anyone have a better suggestion? _______________________________________________ comp.lang.ada mailing list comp.lang.ada@ada-france.org http://www.ada-france.org/mailman/listinfo/comp.lang.ada