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.3 required=5.0 tests=BAYES_00,INVALID_MSGID,XPRIO autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d17561d7e5eba62c,start X-Google-Attributes: gid103376,public From: Tom_Hargraves@Raytheon.com Subject: An alternative to Unchecked Conversion Date: 1999/12/16 Message-ID: #1/1 X-Deja-AN: 561661661 X-MIMETrack: Serialize by Router on RESMAIL70/SRV/Raytheon/CA(Release 5.0.1b|September 30, 1999) at 12/16/1999 12: 22:41 PM To: comp.lang.ada@ada.eu.org X-Priority: 3 (Normal) Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@enst.fr X-Trace: menuisier.enst.fr 945375812 18065 137.194.161.2 (16 Dec 1999 20:23:32 GMT) Organization: ENST, France X-BeenThere: comp.lang.ada@ada.eu.org Mime-Version: 1.0 Reply-To: comp.lang.ada@ada.eu.org NNTP-Posting-Date: 16 Dec 1999 20:23:32 GMT Newsgroups: comp.lang.ada Date: 1999-12-16T20:23:32+00:00 List-Id: Related to "Questions about Unchecked_Conversion". There have been some good responses to the related topic. I agree with the sentiment that Unchecked_Conversion should be rarely if ever used. However if you HAVE TO use it, it does come with an overhead. It is a routine which has to be called, and it may end up copying data around and cost you cpu time. So when you HAVE TO, e.g. to copy Ada structures onto byte streams being transmitted over a 'hardware' interface, there's another technique which has been around for a while. In the fortran era it was implemented using the common block and equivalence statements. The technique is simply to map the Ada structure directly onto the 'other' data structure. It has the following advantages: 1. It's fast, there is no data transfer 'cost'. 2. The data structures can be different sizes. It has the following disadvantages: 1. If your Ada structure contains constrained types, then a write to the 'other' data structure can subsequently cause a constraint error when you come to read the data. 2. Timing and synchronisation become a issue if the reader and writer are not in the same task/thread. If used with care, it is a good technique I've used for the simulation of memory mapped external interfaces. Here's a little one byte example. The output is as expected. Maybe there are other techniques to achieve the same result with the Ada95 extentions? (I am not too familiar with all the new techniques which Ada95 provides, but I'm enjoying discovering them!) Regards, Tom H. with Text_Io; procedure Test_Use_Address is type Unsigned_Byte is range 0 .. 2**8 - 1; for Unsigned_Byte'Size use 8; My_Char : Character := 'A'; My_Byte : Unsigned_Byte; for My_Byte'Address use My_Char'Address; begin Text_Io.Put_Line( "Before My_Byte assignment, My_Char = " & My_Char); My_Byte := Unsigned_Byte(Character'Pos('B')); Text_Io.Put_Line( "After My_Byte assignment, My_Char = " & My_Char); -- Expected Output: -- Before My_Byte assignment, My_Char = A -- After My_Byte assignment, My_Char = B end;