From: Matthew Heaney <matthew_heaney@acm.org>
Subject: Re: generic package System.Address_To_Access_Conversion
Date: 1998/11/24
Date: 1998-11-24T00:00:00+00:00 [thread overview]
Message-ID: <m3sof9a3rl.fsf@mheaney.ni.net> (raw)
In-Reply-To: 3659CDAE.AFE2EC5B@wec.com
R Heltzel <HeltzeRF@wec.com> writes:
> Could someone please send me an example piece of code that illustrates
> the method of using this generic package using the GNAT compiler?
I've been using this to turn a read-only view of an object into a
read-write view. For example, if you have an access-to-constant type:
type Stack_Access is access constant Stack_Type;
then modifying the stack designated by such an access object is
illegal. But, you can get around this feature by using
Addr_To_Acc_Conv:
procedure Op (S : in Stack_Access) is
package Conversions is
new System.Address_To_Access_Conversions (Stack_Type);
use Conversions;
Read_Only_Stack : Stack_Type renames S.all;
Read_Write_Stack : Stack_Type renames
To_Pointer (Read_Only_Stack'Address).all;
begin
Push (19, On => Read_Write_Stack);
end Op;
Of course, if you're using gnat, then you can use the attribute
T'Unrestricted_Access, which is more type-safe but less portable:
procedure Op (S : in Stack_Access) is
type Read_Write_Stack_Access is access all Stack_Type;
for Read_Write_Stack_Access'Storage_Size use 0;
SA : constant Read_Write_Stack_Access :=
S.all'Unrestricted_Access;
Read_Write_Stack : Stack_Type renames SA.all;
begin
Push (19, On => Read_Write_Stack);
end Op;
Perhaps more clear is to just make the parameter mode "in Stack_Type"
(this is how you can implement an operation that has side effect, even
for a function). First, let's use our little friend
Sys.Addr_To_Acc_Conv:
procedure Op (S : in Stack_Type) is
package Conversions is
new System.Address_To_Access_Conversions (Stack_Type);
use Conversions;
Read_Write_Stack : Stack_Type renames
To_Pointer (S'Address).all;
begin
Push (19, On => Read_Write_Stack);
end Op;
(Note the mode of param S.) Now, using T'Unrestricted_Access:
procedure Op (S : in Stack_Type) is
type Read_Write_Stack_Access is access all Stack_Type;
for Read_Write_Stack_Access'Storage_Size use 0;
SA : constant Read_Write_Stack_Access :=
S'Unrestricted_Access;
Read_Write_Stack : Stack_Type renames SA.all;
begin
Push (19, On => Read_Write_Stack);
end Op;
Where possible, it's nice to take T'Address of an object whose type is
pass-by-reference. That is, a tagged type, or a limited type whose
full-view is also limited. The implementation advice in the RM
recommends that taking T'Address of a by-reference type should return "a
useful result," which will improve your chances for portability.
prev parent reply other threads:[~1998-11-24 0:00 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
1998-11-23 0:00 generic package System.Address_To_Access_Conversion R Heltzel
1998-11-23 0:00 ` Tom Moran
1998-11-24 0:00 ` Matthew Heaney [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox