comp.lang.ada
 help / color / mirror / Atom feed
* [Q] : Unchecked conversion
@ 1996-11-09  0:00 Jonas Nygren
  1996-11-09  0:00 ` Robert Dewar
  1996-11-12  0:00 ` Stephen Leake
  0 siblings, 2 replies; 8+ messages in thread
From: Jonas Nygren @ 1996-11-09  0:00 UTC (permalink / raw)




I have defined an IO buffer type which I intend to use for my own IO
packages.
The buffer type is defined as follows:

   subtype Byte is Integer range 0..255;
   
   type Buffer is array (Positive range <>) of Byte;
   Pragma Pack(Buffer);
   for Buffer'Component_Size use 8;

I also want to be able to convert between Buffer and String types. I use
Gnat and
I know that the underlying data representation of Buffer and String are the
same
so I thought I could do an Unchecked_Conversion:

   function To_Buffer (S : String) return Buffer is
      function Convert is
         new Ada.Unchecked_Conversion(String, Buffer);
   begin
      return Convert(S);
   end To_Buffer;

but Gnat gives me an error:

   unconstrained type "Standard.String" not allowed in unchecked conversion

According to the RM95 Unchecked_Conversion should accept unconstrained
types
but implementations are allowed to impose limitations.

My questions are:

1)  Is this a Gnat(3.04a) imposed limitation on Unchecked_Conversion
     or is my code illegal irregardless of compiler?

2)  Is there a  way of doing this type conversion, save copying?

/jonas




^ permalink raw reply	[flat|nested] 8+ messages in thread
* Re: [Q]: Unchecked conversion
@ 1996-11-10  0:00 Jonas Nygren
  1996-11-10  0:00 ` Matthew Heaney
  1996-11-10  0:00 ` Robert Dewar
  0 siblings, 2 replies; 8+ messages in thread
From: Jonas Nygren @ 1996-11-10  0:00 UTC (permalink / raw)




I asked a question on how to or if possible to convert between
two array types using Unchecked_Conversion with the formal
arguments to the instantiation of the generic being two 
unconstrained array types. In an email from Frank Petranka I
got a solution that works with Gnat (3.04a at least). It doesn't
use Unchecked_Conversion but a rather more direct approach with
"for Tmp use at Arg'Address;", actually more convenient than
Unchecked_Conversion (almost like C ;-).

If you are intrested please see Franks mail below. I have
verified that it works with Gnat. Are there any problems with
this approach from a portability aspect?

/jonas


On 9 Nov 1996, Jonas Nygren wrote:

> 
> I have defined an IO buffer type which I intend to use for my own IO
> packages.
> The buffer type is defined as follows:
> 
>    subtype Byte is Integer range 0..255;
>    
>    type Buffer is array (Positive range <>) of Byte;
>    Pragma Pack(Buffer);
>    for Buffer'Component_Size use 8;
> 
> I also want to be able to convert between Buffer and String types. I use
> Gnat and
> I know that the underlying data representation of Buffer and String are
the
> same
> so I thought I could do an Unchecked_Conversion:
> 
>    function To_Buffer (S : String) return Buffer is
>       function Convert is
>          new Ada.Unchecked_Conversion(String, Buffer);
>    begin
>       return Convert(S);
>    end To_Buffer;
> 

I'm not sure if its portable, but I have used something similar to

    function To_Buffer (S : String) return Buffer is
       Temp_Buffer : Buffer(1..S'Length)
       for Temp_Buffer use at S'Address;
    begin
       return Temp_Buffer;
    end To_Buffer;

I don't have access to an Ada compiler right now so I'm not sure that is 
exactly correct (may need declare or S(S'First)'Address) but this should
put
you on the right track.

We also used unchecked conversion of pointers to these objects but I am
not sure these are portable.
 
> but Gnat gives me an error:
> 
>    unconstrained type "Standard.String" not allowed in unchecked
conversion
> 
> According to the RM95 Unchecked_Conversion should accept unconstrained
> types
> but implementations are allowed to impose limitations.
> 
> My questions are:
> 
> 1)  Is this a Gnat(3.04a) imposed limitation on Unchecked_Conversion
>      or is my code illegal irregardless of compiler?
> 
> 2)  Is there a  way of doing this type conversion, save copying?
> 
> /jonas
> 
> 

Disclaimer: The views or opinions expressed in this article are of the user

and do not, in any manner, reflect that of the Navy.

            Frank J. Petranka          Naval Surface Warfare Center
            (540)653-4849              Dahlgren, Va. 22448
                    fpetran@relay.nswc.navy.mil






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

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

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-11-09  0:00 [Q] : Unchecked conversion Jonas Nygren
1996-11-09  0:00 ` Robert Dewar
1996-11-12  0:00 ` Stephen Leake
  -- strict thread matches above, loose matches on Subject: below --
1996-11-10  0:00 [Q]: " Jonas Nygren
1996-11-10  0:00 ` Matthew Heaney
1996-11-10  0:00   ` Robert Dewar
1996-11-11  0:00     ` Matthew Heaney
1996-11-10  0:00 ` Robert Dewar

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