comp.lang.ada
 help / color / mirror / Atom feed
* An alternative to Unchecked Conversion
@ 1999-12-16  0:00 Tom_Hargraves
  1999-12-16  0:00 ` Matthew Heaney
                   ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: Tom_Hargraves @ 1999-12-16  0:00 UTC (permalink / raw)
  To: comp.lang.ada

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;









^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~1999-12-23  0:00 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-12-16  0:00 An alternative to Unchecked Conversion Tom_Hargraves
1999-12-16  0:00 ` Matthew Heaney
1999-12-16  0:00   ` Richard D Riehle
1999-12-17  0:00     ` Ted Dennison
1999-12-17  0:00       ` Richard D Riehle
1999-12-16  0:00   ` Tom Hargraves
1999-12-16  0:00     ` DuckE
1999-12-16  0:00     ` Matthew Heaney
1999-12-17  0:00       ` Lutz Donnerhacke
1999-12-17  0:00         ` Matthew Heaney
     [not found]     ` <mhrj5s4qckk7rt1j3pu0s3r438e281pu37@4ax.com>
1999-12-17  0:00       ` Simon Wright
     [not found]         ` <g9rr5s84eeov4vvp2v9v70jo9b5uporm0v@4ax.com>
1999-12-20  0:00           ` Simon Wright
1999-12-23  0:00             ` Robert Dewar
1999-12-18  0:00       ` Rex Reges
1999-12-18  0:00         ` Matthew Heaney
1999-12-19  0:00         ` Robert Dewar
1999-12-20  0:00     ` Charles H. Sampson
1999-12-16  0:00 ` Mark A Biggar
1999-12-16  0:00 ` Jeff Creem

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