Turning a read-only view of an object into a read-write view


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.

Contributed by: Matthew Heaney
Contributed on: November 25, 1998
License: Public Domain
Back