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-09  0:00 [Q] : Unchecked conversion Jonas Nygren
@ 1996-11-09  0:00 ` Robert Dewar
  1996-11-12  0:00 ` Stephen Leake
  1 sibling, 0 replies; 8+ messages in thread
From: Robert Dewar @ 1996-11-09  0:00 UTC (permalink / raw)



iJonas Nugren asks

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

asking about a case of unchecked conversion between uncosntrained array
types.

In the current version of GNAT, we have relaxed this restrictoin somewhat
and allow the source to be an unconstrained array, but not the target.
Letting the target be an unconstrained array is pretty bogus, where would
the bounds come from in the general case, yes, I guess you could allow
it in limited cases but it seems dubious (note that the DEC Ada 83
compiler has exactly the same set of restrictions -- that was the
inspiration for relaxing the GNAt rules).

You can easily do what you want however, by instantiating UC locally
for a constrained subtype with appropriate bounds. That's really a 
preferable way of doing things in any case.

Or you can do UC between pointers to such unconstrained types (that's
probably all the UC between the arrays does anyway, and that way you
avoid making a copy of the array, converting pointers just gives you
a different view of the data, and is usually preferable).





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

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



Matthew wrote

"The preferred approach is as Bob Dewar suggested:"

I prefer to be Robert Dewar (this also reduces overloading :-) :-)





^ 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

* Re: [Q]: Unchecked conversion
  1996-11-10  0:00 [Q]: " Jonas Nygren
@ 1996-11-10  0:00 ` Matthew Heaney
  1996-11-10  0:00   ` Robert Dewar
  1996-11-10  0:00 ` Robert Dewar
  1 sibling, 1 reply; 8+ messages in thread
From: Matthew Heaney @ 1996-11-10  0:00 UTC (permalink / raw)



In article <01bbcf21$4b32a020$829d6482@joy.ericsson.se>, "Jonas Nygren"
<ehsjony@ehs.ericsson.se> wrote:


>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;

In general, you should avoid overlay-style conversions when
Unchecked_Conversion will do.  Overlays are inherently dangerous because
you can unwittingly over-write memory that doesn't belong to you,
especially when the overlay-object has default initialization (for example,
an access object).

The preferred approach is as Bob Dewar suggested:

function To_Buffer (S : String) return Buffer is

   subtype Constrained_String is String (S'Range);
   subtype Constrained_Buffer is Buffer (S'Range);

   function To_Constrained_Buffer is 
      new Unchecked_Conversion (Constrained_String, Constrained_Buffer);
begin
   return To_Constrained_Buffer (S);
end;

This should be portable across all Ada compilers, because the source and
target types are both constrained.

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
mheaney@ni.net
(818) 985-1271




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

* Re: [Q]: Unchecked conversion
  1996-11-10  0:00 [Q]: " Jonas Nygren
  1996-11-10  0:00 ` Matthew Heaney
@ 1996-11-10  0:00 ` Robert Dewar
  1 sibling, 0 replies; 8+ messages in thread
From: Robert Dewar @ 1996-11-10  0:00 UTC (permalink / raw)



Jonas asks about conversion of unconstrained array types.

First, the solution with address clauses is of course for CONSTRAINED
array types (since all objects are constrained), and you could perfectly
well use unchecked conversion, or, as I suggested unchecked conversion
of pointers (since there is no need to copy the data) and if all array
types are constrained, then that should be portable.

Note that the solution with address clauses is definitely wrong for
Ada 83 (i.e. erroneous). The effect is implementation dependent in
Ada 95. In practice it will probably work, but I still think that
unchecked conversions of poitners to constrained array subtypes is
a much cleaner approach.

The use of address clauses in this way is really an *ab*use!





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

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



In article <dewar.847685861@merv>, dewar@merv.cs.nyu.edu (Robert Dewar) wrote:

>"The preferred approach is as Bob Dewar suggested:"
>
>I prefer to be Robert Dewar (this also reduces overloading :-) :-)

Are you sure you don't mean ROBERT DEWAR?

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
mheaney@ni.net
(818) 985-1271




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

* Re: [Q] : Unchecked conversion
  1996-11-09  0:00 [Q] : Unchecked conversion Jonas Nygren
  1996-11-09  0:00 ` Robert Dewar
@ 1996-11-12  0:00 ` Stephen Leake
  1 sibling, 0 replies; 8+ messages in thread
From: Stephen Leake @ 1996-11-12  0:00 UTC (permalink / raw)



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;
> 
> [snip]
>
>    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
> 
> [snip]

As the error message implies, you need to constrain the types before
instantiating Unchecked_Conversion:

   function To_Buffer (S : String) return Buffer is
      subtype Fixed_String is String (1 .. S'Length);
      subtype Fixed_Buffer is Buffer (1 .. S'Length);
      function Convert is
         new Unchecked_Conversion(Fixed_String, Fixed_Buffer);
   begin
      return Convert(S);
   end To_Buffer;

I just tried this with Gnat, and it works.

-- 
- Stephe




^ 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